Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only subscribe shortcuts when modal is open #18425

Merged
merged 5 commits into from
May 4, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions src/components/KeyboardShortcutsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,69 @@ const defaultProps = {
class KeyboardShortcutsModal extends React.Component {
componentDidMount() {
const openShortcutModalConfig = CONST.KEYBOARD_SHORTCUTS.SHORTCUT_MODAL;
this.subscribedOpenModalShortcuts = [];
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
this.unsubscribeShortcutModal = KeyboardShortcut.subscribe(openShortcutModalConfig.shortcutKey, () => {
ModalActions.close();
KeyboardShortcutsActions.showKeyboardShortcutModal();
}, openShortcutModalConfig.descriptionKey, openShortcutModalConfig.modifiers, true);

if (this.props.isShortcutsModalOpen) {
// The modal started open, which can happen if you reload the page when the modal is open.
this.subscribeOpenModalKeyboardShortcuts();
}
}

componentDidUpdate(prevProps) {
if (!prevProps.isShortcutsModalOpen && this.props.isShortcutsModalOpen) {
this.subscribeOpenModalKeyboardShortcuts();
} else if (prevProps.isShortcutsModalOpen && !this.props.isShortcutsModalOpen) {
// Modal is closing, remove keyboard shortcuts
this.unsubscribeOpenModalShortcuts();
}
}

componentWillUnmount() {
if (this.unsubscribeShortcutModal) {
this.unsubscribeShortcutModal();
}
this.unsubscribeOpenModalShortcuts();
}

/*
* Subscribe shortcuts that only are used when the modal is open
*/
subscribeOpenModalKeyboardShortcuts() {
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
// Allow closing the modal with the both Enter and Escape keys
// Both callbacks have the lowest priority (0) to ensure that they are called before any other callbacks
// and configured so that event propagation is stopped after the callback is called (only when the modal is open)
const closeShortcutEscapeModalConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
this.unsubscribeCloseEscapeModal = KeyboardShortcut.subscribe(closeShortcutEscapeModalConfig.shortcutKey, () => {
this.subscribedOpenModalShortcuts.push(KeyboardShortcut.subscribe(closeShortcutEscapeModalConfig.shortcutKey, () => {
ModalActions.close();
KeyboardShortcutsActions.hideKeyboardShortcutModal();
}, closeShortcutEscapeModalConfig.descriptionKey, closeShortcutEscapeModalConfig.modifiers, true, true);
}, closeShortcutEscapeModalConfig.descriptionKey, closeShortcutEscapeModalConfig.modifiers, true, true));

const closeShortcutEnterModalConfig = CONST.KEYBOARD_SHORTCUTS.ENTER;
this.unsubscribeCloseEnterModal = KeyboardShortcut.subscribe(closeShortcutEnterModalConfig.shortcutKey, () => {
this.subscribedOpenModalShortcuts.push(KeyboardShortcut.subscribe(closeShortcutEnterModalConfig.shortcutKey, () => {
ModalActions.close();
KeyboardShortcutsActions.hideKeyboardShortcutModal();
}, closeShortcutEnterModalConfig.descriptionKey, closeShortcutEnterModalConfig.modifiers, true, () => !this.props.isShortcutsModalOpen);
}, closeShortcutEnterModalConfig.descriptionKey, closeShortcutEnterModalConfig.modifiers, true));

// Intercept arrow up and down keys to prevent scrolling ArrowKeyFocusManager while this modal is open
const arrowUpConfig = CONST.KEYBOARD_SHORTCUTS.ARROW_UP;
this.unsubscribeArrowUpKey = KeyboardShortcut.subscribe(arrowUpConfig.shortcutKey, () => {
}, arrowUpConfig.descriptionKey, arrowUpConfig.modifiers, true, () => !this.props.isShortcutsModalOpen);
this.subscribedOpenModalShortcuts.push(KeyboardShortcut.subscribe(arrowUpConfig.shortcutKey, () => {
}, arrowUpConfig.descriptionKey, arrowUpConfig.modifiers, true));

const arrowDownConfig = CONST.KEYBOARD_SHORTCUTS.ARROW_DOWN;
this.unsubscribeArrowDownKey = KeyboardShortcut.subscribe(arrowDownConfig.shortcutKey, () => {
}, arrowDownConfig.descriptionKey, arrowDownConfig.modifiers, true, () => !this.props.isShortcutsModalOpen);
this.subscribedOpenModalShortcuts.push(KeyboardShortcut.subscribe(arrowDownConfig.shortcutKey, () => {
}, arrowDownConfig.descriptionKey, arrowDownConfig.modifiers, true));
}

componentWillUnmount() {
if (this.unsubscribeShortcutModal) {
this.unsubscribeShortcutModal();
}
if (this.unsubscribeCloseEscapeModal) {
this.unsubscribeCloseEscapeModal();
}
if (this.unsubscribeCloseEnterModal) {
this.unsubscribeCloseEnterModal();
}
if (this.unsubscribeArrowUpKey) {
this.unsubscribeArrowUpKey();
}
if (this.unsubscribeArrowDownKey) {
this.unsubscribeArrowDownKey();
}
/*
* Unsubscribe all shortcuts that were subscribed when the modal opened
*/
unsubscribeOpenModalShortcuts() {
this.subscribedOpenModalShortcuts.forEach(unsubscribe => unsubscribe());
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
this.subscribedOpenModalShortcuts = [];
}

/**
Expand Down