From 230e23484a3e81ef7ee99248e6e5bf004c869fb5 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:14:59 +0100 Subject: [PATCH] Properly disable hotkey scopes for open reader settings --- src/modules/hotkeys/Hotkeys.utils.ts | 18 ++++++++++++------ .../components/settings/ReaderSettings.tsx | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/modules/hotkeys/Hotkeys.utils.ts b/src/modules/hotkeys/Hotkeys.utils.ts index 5ab1aed6..8512d701 100644 --- a/src/modules/hotkeys/Hotkeys.utils.ts +++ b/src/modules/hotkeys/Hotkeys.utils.ts @@ -6,21 +6,27 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import { useEffect, useState } from 'react'; +import { useEffect, useRef } from 'react'; import { useHotkeysContext } from 'react-hotkeys-hook'; import { HotkeyScope } from '@/modules/hotkeys/Hotkeys.types.ts'; -export const useDisableAllHotkeysWhileMounted = () => { +export const useDisableAllHotkeysWhileMounted = (shouldDisable?: boolean) => { const { enabledScopes, enableScope, disableScope } = useHotkeysContext(); - const [previouslyEnabledScopes] = useState(enabledScopes); + const previouslyEnabledScopes = useRef([]); useEffect(() => { + if (!shouldDisable) { + return () => {}; + } + + previouslyEnabledScopes.current = [...enabledScopes]; + enableScope(HotkeyScope.NONE); - previouslyEnabledScopes.forEach(disableScope); + previouslyEnabledScopes.current.forEach(disableScope); return () => { disableScope(HotkeyScope.NONE); - previouslyEnabledScopes.forEach(enableScope); + previouslyEnabledScopes.current.forEach(enableScope); }; - }, []); + }, [shouldDisable]); }; diff --git a/src/modules/reader/components/settings/ReaderSettings.tsx b/src/modules/reader/components/settings/ReaderSettings.tsx index f9751612..3ca70131 100644 --- a/src/modules/reader/components/settings/ReaderSettings.tsx +++ b/src/modules/reader/components/settings/ReaderSettings.tsx @@ -20,7 +20,7 @@ export const ReaderSettings = ({ isOpen, close }: { isOpen: boolean; close: () = const { manga } = useReaderStateMangaContext(); const settings = ReaderService.useSettings(); - useDisableAllHotkeysWhileMounted(); + useDisableAllHotkeysWhileMounted(isOpen); const [activeTab, setActiveTab] = useState(0);