feat(devtools): Handle keyboard shortcuts with TanStack Hotkeys #582
Pushedcd7a133
Pushedf4d5dee
- @@ -67,3 +67,4 @@6767 "@neodrag/core": "3.0.0-next.11",6868 "@tanstack/devtools-client": "workspace:*",6969 "@tanstack/devtools-event-bus": "workspace:*",70+ "@tanstack/hotkeys": "^0.39.0",@@ -30,9 +29,15 @@3029 })3130 })323133- const downList = getKeyDownList()32+ const heldKeys = createHeldKeys()34333534 const [disabledAfterClick, setDisabledAfterClick] = createSignal(false)363536+ // Exactly the inspect keys: holding another key as well turns it off.3737 const isHighlightingKeysHeld = createMemo(() => {38- return isHotkeyCombinationPressed(downList(), settings().inspectHotkey)38+ const keys = new Set<string>(39+ settings().inspectHotkey.map((key) => resolveModifier(key)),40+ )41+ const held = heldKeys()42+ return (43+ keys.size > 0 &&@@ -720,14 +734,9 @@720734 )721735722- createEffect(isFloating, (floating) => {723- if (!floating) return724- const onKeyDown = (event: KeyboardEvent) => {725- if (event.key === 'Escape' && cancelGesture()) event.stopPropagation()726- if (event.altKey && event.key.toLowerCase() === 'm') {727- setMagneticMode((v) => !v)728- event.stopPropagation()729- }730- }731- window.addEventListener('keydown', onKeyDown)732- return () => window.removeEventListener('keydown', onKeyDown)733- })736+ createHotkeys(() => [737+ // Shift may still be held from magnetizing the drag.738+ ...(['Escape', 'Shift+Escape'] as const).map((hotkey) => ({739+ hotkey,740+ callback: () => cancelGesture(),741+ options: {742+ enabled: gestureActive(),@@ -122,0 +136,21 @@136+ return MODIFIER_ALIASES[key] ?? MODIFIER_ALIASES[key.toLowerCase()]137+}138+139+/**140+ * The open hotkey, from a hotkey string or from the array of keys older141+ * versions used, such as `['Control', '~']`. An array converts only when it142+ * has one key that isn't a modifier.143+ */144+const toOpenHotkey = (value: unknown): Hotkey | undefined => {145+ let hotkey = value146+ if (Array.isArray(value)) {147+ const modifiers = new Set<ResolvableModifier>()148+ const keys: Array<string> = []149+ for (const key of value) {150+ const modifier = toModifier(key)151+ if (modifier) modifiers.add(modifier)152+ else if (typeof key === 'string') keys.push(key)153+ else return undefined154+ }155+ if (keys.length !== 1) return undefined156+ hotkey = [...modifiers, normalizeKeyName(keys[0]!)].join('+')@@ -186,0 +193,21 @@193+ callback: toggleOpen,194+ options: {195+ enabled: !hiddenInAutomation(),196+ // Never while typing in a field, whatever the hotkey.197+ ignoreInputs: true,198+ requireReset: true,199+ meta: {200+ name: OPEN_HOTKEY_NAME,201+ description: 'Opens and closes TanStack Devtools',202+ },203+ },204+ },205+ {206+ hotkey: 'Escape',207+ callback: toggleOpen,208+ options: {209+ enabled: isOpen() && !hiddenInAutomation(),210+ // An Escape for an open dialog or popover of the app goes to it, not211+ // here. In the modal host the devtools are the topmost dialog, and only212+ // a hotkey inside it keeps Escape.213+ target: modalHost() ?? document,
Sign in to join this conversation.
coderabbitai[bot](GitHub profile, opens in a new tab) reviewed
Actionable comments posted: 5
e6f29aba-d378-430b-b10c-15eb337bc361Reviewing files that changed from the base of the PR and between 1ff67341c1c28af3ce2ae3522e44064f08fea785 and f4d5deed491d89aa38cffe8f3467758a0c70a5a0.
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml.changeset/devtools-tanstack-hotkeys.mddocs/configuration.mddocs/source-inspector.mddocs/vite-plugin.mde2e/apps/react-vite/tests/hotkey.spec.tse2e/apps/react-vite/tests/open-as-modal.spec.tse2e/apps/react-vite/tests/pip-hotkeys.spec.tspackage.jsonpackages/devtools-vite/skills/devtools-vite-plugin/SKILL.mdpackages/devtools/package.jsonpackages/devtools/skills/devtools-app-setup/SKILL.mdpackages/devtools/src/components/content-panel.tsxpackages/devtools/src/components/plugin-workspace.tsxpackages/devtools/src/components/source-inspector.tsxpackages/devtools/src/components/trigger.test.tsxpackages/devtools/src/components/trigger.tsxpackages/devtools/src/context/devtools-context.test.tspackages/devtools/src/context/devtools-context.tsxpackages/devtools/src/context/devtools-store.tspackages/devtools/src/devtools.tsxpackages/devtools/src/hooks/use-modal-host.tspackages/devtools/src/tabs/hotkey-config.tsxpackages/devtools/src/tabs/marketplace/plugin-section.tsxpackages/devtools/src/tabs/settings-tab.test.tsxpackages/devtools/src/tabs/settings-tab.tsxpackages/devtools/src/utils/constants.tspackages/devtools/src/utils/hotkey.test.tspackages/devtools/src/utils/hotkey.tspackages/devtools/src/utils/sanitize.test.tspackages/devtools/src/utils/sanitize.tspackages/devtools/tests/index.test.tspackages/devtools/tests/workbench.test.tsxpackages/devtools/tsup.config.tsIncluded review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.