Properly disable hotkey scopes for open reader settings

This commit is contained in:
schroda
2024-12-09 17:14:59 +01:00
parent 044aa23ad4
commit 230e23484a
2 changed files with 13 additions and 7 deletions

View File

@@ -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<typeof enabledScopes>([]);
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]);
};

View File

@@ -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);