eba997be98
# Replace hotkey scopes by focus stack (Part 6 - Remove Hotkey scopes) This PR is the last part of a refactoring aiming to deprecate the hotkey scopes api in favor of the new focus stack api which is more robust. Part 1: https://github.com/twentyhq/twenty/pull/12673 Part 2: https://github.com/twentyhq/twenty/pull/12798 Part 3: https://github.com/twentyhq/twenty/pull/12910 Part 4: https://github.com/twentyhq/twenty/pull/12933 Part 5: https://github.com/twentyhq/twenty/pull/13106 In this part, we completely remove the hotkey scopes.
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { DIALOG_CLICK_OUTSIDE_ID } from '@/ui/feedback/dialog-manager/constants/DialogClickOutsideId';
|
|
import { MODAL_CLICK_OUTSIDE_LISTENER_EXCLUDED_ID } from '@/ui/layout/modal/constants/ModalClickOutsideListenerExcludedClassName';
|
|
import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';
|
|
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
|
|
import { Key } from 'ts-key-enum';
|
|
|
|
type ModalHotkeysAndClickOutsideEffectProps = {
|
|
modalRef: React.RefObject<HTMLDivElement>;
|
|
onEnter?: () => void;
|
|
isClosable?: boolean;
|
|
onClose?: () => void;
|
|
modalId: string;
|
|
};
|
|
|
|
export const ModalHotkeysAndClickOutsideEffect = ({
|
|
modalRef,
|
|
onEnter,
|
|
isClosable = false,
|
|
onClose,
|
|
modalId,
|
|
}: ModalHotkeysAndClickOutsideEffectProps) => {
|
|
useHotkeysOnFocusedElement({
|
|
keys: [Key.Enter],
|
|
callback: () => {
|
|
onEnter?.();
|
|
},
|
|
focusId: modalId,
|
|
dependencies: [onEnter],
|
|
});
|
|
|
|
useHotkeysOnFocusedElement({
|
|
keys: [Key.Escape],
|
|
callback: () => {
|
|
if (isClosable && onClose !== undefined) {
|
|
onClose();
|
|
}
|
|
},
|
|
focusId: modalId,
|
|
dependencies: [isClosable, onClose],
|
|
});
|
|
|
|
useListenClickOutside({
|
|
refs: [modalRef],
|
|
excludedClickOutsideIds: [
|
|
MODAL_CLICK_OUTSIDE_LISTENER_EXCLUDED_ID,
|
|
DIALOG_CLICK_OUTSIDE_ID,
|
|
],
|
|
listenerId: `MODAL_CLICK_OUTSIDE_LISTENER_ID_${modalId}`,
|
|
callback: () => {
|
|
if (isClosable && onClose !== undefined) {
|
|
onClose();
|
|
}
|
|
},
|
|
});
|
|
|
|
return null;
|
|
};
|