Repository navigation
Host core: one Space shortcut, roving choice and play button for every UI #113
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Keyboard behaviour every host UI shares: the Space shortcut for playback and roving choice among radios or tabs. | ||
| // DOM-typed and framework-free: a React, Vue or plain DOM keydown event works as it is. | ||
| import type {ProjectionController} from './projection/types.js' | ||
|
|
||
| /** | ||
| * Elements whose own keyboard behaviour uses Space, or that hold text: the playback shortcut leaves Space to them. | ||
| * A UI adds its design system's custom elements with `keeps` (the VS Code UI adds VSCode Elements'). | ||
| */ | ||
| export const spaceKeeping: readonly string[] = [ | ||
| 'input:not([type="range"])', | ||
| 'textarea', | ||
| 'select', | ||
| 'button', | ||
| 'a[href]', | ||
| 'summary', | ||
| 'iframe', | ||
| '[contenteditable]:not([contenteditable="false"])', | ||
| ...[ | ||
| 'button', | ||
| 'checkbox', | ||
| 'radio', | ||
| 'switch', | ||
| 'tab', | ||
| 'menu', | ||
| 'menuitem', | ||
| 'menuitemradio', | ||
| 'menuitemcheckbox', | ||
| 'listbox', | ||
| 'option', | ||
| 'combobox', | ||
| 'textbox', | ||
| 'searchbox', | ||
| 'treeitem', | ||
| 'gridcell', | ||
| 'link', | ||
| 'dialog', | ||
| ].map(role => `[role="${role}"]`), | ||
| ] | ||
|
|
||
| /** What the shortcuts read of a keydown event: a DOM `KeyboardEvent` or a framework's wrapper of one. */ | ||
| export interface KeyEventLike { | ||
| key: string | ||
| altKey: boolean | ||
| ctrlKey: boolean | ||
| metaKey: boolean | ||
| shiftKey: boolean | ||
| defaultPrevented: boolean | ||
| /** The focused element. */ | ||
| target: EventTarget | null | ||
| /** The element listening: a toolbar, or the stage's chrome. */ | ||
| currentTarget: EventTarget | null | ||
| } | ||
|
|
||
| const isNode = (value: EventTarget | null): value is Node => typeof Node !== 'undefined' && value instanceof Node | ||
|
|
||
| /** | ||
| * Whether a keydown is the playback shortcut: Space without modifiers, not yet handled, from the listening element or | ||
| * an element in it that doesn't use Space itself (`spaceKeeping`, and `keeps`). An element that uses Space but | ||
| * encloses the listener (say, a dialog the page puts the view in) doesn't count; a key from outside the listener (an | ||
| * overlay portaled elsewhere) is never the shortcut. | ||
| */ | ||
| export function isPlaybackShortcut(event: KeyEventLike, keeps: readonly string[] = []): boolean { | ||
| if (event.key !== ' ' || event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false | ||
| const {target, currentTarget} = event | ||
| if (!isNode(target) || !isNode(currentTarget) || !currentTarget.contains(target)) return false | ||
| const element = target instanceof Element ? target : target.parentElement | ||
| const keeper = element?.closest([...spaceKeeping, ...keeps].join(', ')) | ||
| return !keeper || keeper.contains(currentTarget) | ||
| } | ||
|
|
||
| /** | ||
| * Handles the playback shortcut (`isPlaybackShortcut`) in a keydown: prevents its default, then plays or pauses | ||
| * (`togglePlayback`), once per press, so a held Space doesn't flicker. Returns whether the key was the shortcut. | ||
| */ | ||
| export function handlePlaybackShortcut( | ||
| event: KeyEventLike & {repeat: boolean; preventDefault(): void}, | ||
| projection: Pick<ProjectionController, 'togglePlayback'>, | ||
| keeps?: readonly string[], | ||
| ): boolean { | ||
| if (!isPlaybackShortcut(event, keeps)) return false | ||
| event.preventDefault() | ||
| if (!event.repeat) projection.togglePlayback() | ||
| return true | ||
| } | ||
|
|
||
| /** | ||
| * Where an arrow, Home or End key moves the choice in a radio group or a tab list of `count` items, from `index`, | ||
| * wrapping at either end: Right and Down step forward, Left and Up back (`orientation` keeps to one axis), Home and End | ||
| * go to the first and last. Undefined for any other key, or when there is nothing to choose. | ||
| */ | ||
| export function rovingIndex( | ||
| key: string, | ||
| index: number, | ||
| count: number, | ||
| orientation: 'horizontal' | 'vertical' | 'both' = 'both', | ||
| ): number | undefined { | ||
| if (count < 1) return undefined | ||
| const forward = (orientation !== 'vertical' && key === 'ArrowRight') || (orientation !== 'horizontal' && key === 'ArrowDown') | ||
| const back = (orientation !== 'vertical' && key === 'ArrowLeft') || (orientation !== 'horizontal' && key === 'ArrowUp') | ||
| if (forward) return (index + 1 + count) % count | ||
| if (back) return (index - 1 + count) % count | ||
| if (key === 'Home') return 0 | ||
| if (key === 'End') return count - 1 | ||
| return undefined | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import {test} from 'node:test' | ||
| import assert from 'node:assert/strict' | ||
| import {handlePlaybackShortcut, isPlaybackShortcut, playButton, rovingIndex} from '../dist/index.js' | ||
|
|
||
| // Just enough DOM for the shortcut's containment rules: `keeps` marks an element that uses Space itself. | ||
| class FakeElement { | ||
| constructor(parent, keeps = false) { | ||
| this.parentElement = parent | ||
| this.keeps = keeps | ||
| } | ||
| contains(other) { | ||
| for (let node = other; node; node = node.parentElement) if (node === this) return true | ||
| return false | ||
| } | ||
| closest() { | ||
| if (this.keeps) return this | ||
| return this.parentElement?.closest() ?? null | ||
| } | ||
| } | ||
| globalThis.Node = FakeElement | ||
| globalThis.Element = FakeElement | ||
|
|
||
| const space = (target, currentTarget, extra = {}) => ({ | ||
| key: ' ', | ||
| altKey: false, | ||
| ctrlKey: false, | ||
| metaKey: false, | ||
| shiftKey: false, | ||
| defaultPrevented: false, | ||
| target, | ||
| currentTarget, | ||
| ...extra, | ||
| }) | ||
|
|
||
| test('Space on the listener or an element that leaves Space alone is the playback shortcut', () => { | ||
| const toolbar = new FakeElement(null) | ||
| const slider = new FakeElement(toolbar) | ||
| assert.equal(isPlaybackShortcut(space(toolbar, toolbar)), true) | ||
| assert.equal(isPlaybackShortcut(space(slider, toolbar)), true) | ||
| assert.equal(isPlaybackShortcut(space(slider, toolbar, {shiftKey: true})), false) | ||
| assert.equal(isPlaybackShortcut(space(slider, toolbar, {defaultPrevented: true})), false) | ||
| assert.equal(isPlaybackShortcut({...space(slider, toolbar), key: 'Enter'}), false) | ||
| }) | ||
|
|
||
| test('Space stays with fields and controls that use it, unless one encloses the listener', () => { | ||
| const dialog = new FakeElement(null, true) | ||
| const toolbar = new FakeElement(dialog) | ||
| const button = new FakeElement(toolbar, true) | ||
| assert.equal(isPlaybackShortcut(space(button, toolbar)), false) | ||
| assert.equal(isPlaybackShortcut(space(toolbar, toolbar)), true) | ||
| const portaled = new FakeElement(null) | ||
| assert.equal(isPlaybackShortcut(space(portaled, toolbar)), false) | ||
| }) | ||
|
|
||
| test('the shortcut toggles once per press, preventing the default of every Space it takes and of nothing else', () => { | ||
| const toolbar = new FakeElement(null) | ||
| const field = new FakeElement(toolbar, true) | ||
| let toggles = 0 | ||
| const projection = {togglePlayback: () => toggles++} | ||
| const press = (target, repeat) => { | ||
| let prevented = false | ||
| const event = {...space(target, toolbar), repeat, preventDefault: () => (prevented = true)} | ||
| return {handled: handlePlaybackShortcut(event, projection), prevented} | ||
| } | ||
| assert.deepEqual(press(toolbar, false), {handled: true, prevented: true}) | ||
| assert.deepEqual(press(toolbar, true), {handled: true, prevented: true}) | ||
| assert.deepEqual(press(toolbar, true), {handled: true, prevented: true}) | ||
| assert.equal(toggles, 1) | ||
| assert.deepEqual(press(field, false), {handled: false, prevented: false}) | ||
| assert.equal(toggles, 1) | ||
| }) | ||
|
|
||
| test('roving choice wraps, keeps to its axis, and ignores other keys', () => { | ||
| assert.equal(rovingIndex('ArrowRight', 2, 3), 0) | ||
| assert.equal(rovingIndex('ArrowUp', 0, 3), 2) | ||
| assert.equal(rovingIndex('ArrowDown', 0, 3, 'horizontal'), undefined) | ||
| assert.equal(rovingIndex('ArrowLeft', 1, 3, 'vertical'), undefined) | ||
| assert.equal(rovingIndex('End', 0, 3), 2) | ||
| assert.equal(rovingIndex('Home', 2, 3), 0) | ||
| assert.equal(rovingIndex('a', 1, 3), undefined) | ||
| assert.equal(rovingIndex('ArrowRight', 0, 0), undefined) | ||
| }) | ||
|
|
||
| test('the play button pauses while playing, plays in its direction, and shows an endpoint under reduced motion', () => { | ||
| const p = (playing, nextDirection, reducedMotion = false) => ({playing, nextDirection, reducedMotion}) | ||
| assert.deepEqual(playButton(p(true, 1), 'main', 'head'), {action: 'pause', direction: 1, label: 'Pause'}) | ||
| assert.equal(playButton(p(false, -1), 'main', 'head').label, 'Play backward') | ||
| assert.deepEqual(playButton(p(false, 1, true), 'main', 'head'), {action: 'show', direction: 1, label: 'Show head'}) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.