Skip to content

feat: Keyboard shortcut handler#9929

Open
snowystinger wants to merge 20 commits into
mainfrom
keyboard-shortcut-handler
Open

feat: Keyboard shortcut handler#9929
snowystinger wants to merge 20 commits into
mainfrom
keyboard-shortcut-handler

Conversation

@snowystinger
Copy link
Copy Markdown
Member

@snowystinger snowystinger commented Apr 15, 2026

Closes

Aiming to help us solve the issue of event leaks, this will allow us more fine grained control of what useKeyboard blocks/prevents default on in a more declarative way.

New behaviour is opt in through shortcuts in useKeyboard. This will preventDefault and stopPropagation if the associated function in the map returns true. Or it will let the event pass without altering it if false is returned. In addition, an object can be returned to alter that behaviour in whatever way necessary. Given that all key combinations are exclusive matches, this should make it more obvious what events are continuing or not.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

@github-actions github-actions Bot added the RAC label Apr 15, 2026
@snowystinger snowystinger changed the title Keyboard shortcut handler [WIP]: Keyboard shortcut handler Apr 15, 2026
@rspbot
Copy link
Copy Markdown

rspbot commented Apr 30, 2026

@github-actions github-actions Bot added the S2 label May 1, 2026
@rspbot
Copy link
Copy Markdown

rspbot commented May 1, 2026

@snowystinger snowystinger marked this pull request as ready for review May 1, 2026 06:37
@rspbot
Copy link
Copy Markdown

rspbot commented May 1, 2026

@rspbot
Copy link
Copy Markdown

rspbot commented May 1, 2026


const isSelected = selectedKey === key;

let onKeyDown = (event) => {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems just designed to skip arrow down and up, i think it'd be better to do this in the keyboard delegate or just allow it, i'm not sure why we wouldn't, there could be vertical steplists and it's a nice non-rtl dependent set of keys.

Just removing it for now as it'd make debugging difficult anyways

@rspbot
Copy link
Copy Markdown

rspbot commented May 4, 2026

@snowystinger snowystinger changed the title [WIP]: Keyboard shortcut handler feat: Keyboard shortcut handler May 4, 2026
# Conflicts:
#	packages/react-aria-components/test/Calendar.test.js
#	packages/react-aria-components/test/RangeCalendar.test.tsx
#	packages/react-aria/src/actiongroup/useActionGroup.ts
#	packages/react-aria/src/calendar/useCalendarGrid.ts
#	packages/react-aria/src/color/useColorArea.ts
#	packages/react-aria/src/combobox/useComboBox.ts
#	packages/react-aria/src/datepicker/useDateField.ts
#	packages/react-aria/src/datepicker/useDateSegment.ts
#	packages/react-aria/src/interactions/useKeyboard.ts
#	packages/react-aria/src/menu/useMenuItem.ts
#	packages/react-aria/src/menu/useMenuTrigger.ts
#	packages/react-aria/src/menu/useSubmenuTrigger.ts
#	packages/react-aria/src/numberfield/useNumberField.ts
#	packages/react-aria/src/overlays/useOverlay.ts
#	packages/react-aria/src/radio/useRadioGroup.ts
#	packages/react-aria/src/searchfield/useSearchField.ts
#	packages/react-aria/src/select/useSelect.ts
#	packages/react-aria/src/selection/useSelectableCollection.ts
#	packages/react-aria/src/spinbutton/useSpinButton.ts
#	packages/react-aria/src/steplist/useStepListItem.ts
#	packages/react-aria/src/table/useTableColumnResize.ts
#	packages/react-aria/test/combobox/useComboBox.test.js
@rspbot
Copy link
Copy Markdown

rspbot commented May 14, 2026

@rspbot
Copy link
Copy Markdown

rspbot commented May 14, 2026

## API Changes

@react-aria/interactions

/@react-aria/interactions:KeyboardProps

 KeyboardProps {
+  ignorePortalRef?: RefObject<Element | null> | null
   isDisabled?: boolean
   onKeyDown?: (KeyboardEvent) => void
   onKeyUp?: (KeyboardEvent) => void
+  shortcuts?: KeyboardShortcutBindings
 }

'control',
'meta',
'mod', // OS dependent - Cmd on Mac, Ctrl on Windows/Linux
'sel' // OS dependent - Alt on Mac, Ctrl on Windows/Linux
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use expansion instead, don't build in a shortcut

'ctrl',
'control',
'meta',
'mod', // OS dependent - Cmd on Mac, Ctrl on Windows/Linux
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different name? just CMD?

return;
}
shortcutHandler(e);
props.onKeyDown?.(e);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call first and allow users to cancel our behaviour?

let action = map.get(canonical);
let result = action?.(e);
if (typeof result === 'boolean') {
result = {shouldContinuePropagation: !result, shouldPreventDefault: result};
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow return type of void to behave like return true?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants