fix: ignore star map playback hotkeys inside context menu

The node context menu now uses Radix, whose menu items are focusable
`div[role=menuitem]` elements. The window-level Space handler in
star-map.tsx only skipped INPUT/TEXTAREA/BUTTON/contentEditable, so
pressing Space on a focused menu item both activated the item and toggled
playback.

Extract the guard into `shouldIgnorePlaybackHotkey`, which additionally
bails when the event was already `defaultPrevented` or when the target or
active element sits inside a `[role=menu]`, and cover the menuitem case
with a small vitest.
This commit is contained in:
teknium1
2026-09-13 13:41:15 -07:00
committed by Teknium
parent 99979c3be2
commit fedaad0cc4
3 changed files with 66 additions and 9 deletions
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest'
import { shouldIgnorePlaybackHotkey } from './playback-hotkey'
function spaceOn(target: Element | null, defaultPrevented = false) {
return { code: 'Space', defaultPrevented, key: ' ', target } as unknown as KeyboardEvent
}
describe('shouldIgnorePlaybackHotkey', () => {
it('toggles on Space when nothing focusable owns the key', () => {
expect(shouldIgnorePlaybackHotkey(spaceOn(document.body), document.body)).toBe(false)
})
it('ignores Space on a focused Radix menu item (div[role=menuitem] inside role=menu)', () => {
const menu = document.createElement('div')
menu.setAttribute('role', 'menu')
const item = document.createElement('div')
item.setAttribute('role', 'menuitem')
item.tabIndex = -1
menu.append(item)
document.body.append(menu)
try {
expect(shouldIgnorePlaybackHotkey(spaceOn(item), item)).toBe(true)
expect(shouldIgnorePlaybackHotkey(spaceOn(document.body, true), document.body)).toBe(true)
} finally {
menu.remove()
}
})
})
@@ -0,0 +1,32 @@
/**
* Decide whether a window-level Space keydown should be ignored by the Star
* Map playback toggle. Ignored when the key is not Space, when something
* already handled it (`defaultPrevented`), when the user is typing or the
* focused element handles Space natively (inputs, buttons), or when focus is
* inside a menu — Radix menu items are `div[role=menuitem]`, so the tag check
* alone would let Space both activate the item and toggle playback.
*/
export function shouldIgnorePlaybackHotkey(e: Pick<KeyboardEvent, 'code' | 'defaultPrevented' | 'key' | 'target'>, activeElement: Element | null): boolean {
if (e.code !== 'Space' && e.key !== ' ') {
return true
}
if (e.defaultPrevented) {
return true
}
const el = activeElement as HTMLElement | null
const tag = el?.tagName
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'BUTTON' || el?.isContentEditable) {
return true
}
const target = e.target as HTMLElement | null
if (target?.closest?.('[role=menu]') || el?.closest?.('[role=menu]')) {
return true
}
return false
}
+4 -9
View File
@@ -11,6 +11,7 @@ import { computePalette, memoryInkFor, resolveRgb, rgba } from './color'
import { RING_OUTER, TILT, ZOOM_MAX, ZOOM_MIN } from './constants'
import { clamp, distToSegmentSq, fitScale, fitViewport, nodeRadius } from './geometry'
import { NodeContextMenu, type NodeMenuTarget } from './node-context-menu'
import { shouldIgnorePlaybackHotkey } from './playback-hotkey'
import { drawScene, drawScramble } from './render'
import { decodeShareCode, encodeShareCode, ShareCodeError } from './share-code'
import { ShareControls } from './share-controls'
@@ -445,17 +446,11 @@ export function StarMap({
)
// Spacebar toggles playback (unless typing, or the play button itself is
// focused — that already handles Space natively, so skip to avoid a double).
// focused — that already handles Space natively, so skip to avoid a double;
// same for focused context-menu items, which Radix renders as divs).
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.code !== 'Space' && e.key !== ' ') {
return
}
const el = document.activeElement
const tag = el?.tagName
if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'BUTTON' || (el as HTMLElement | null)?.isContentEditable) {
if (shouldIgnorePlaybackHotkey(e, document.activeElement)) {
return
}