@sdxc/ui 2026.10.8 → 2026.10.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -1
- package/dist/behaviors/announcer.d.ts +18 -0
- package/dist/behaviors/announcer.js +31 -3
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/shortcut-list.d.ts +49 -0
- package/dist/components/shortcut-list.js +39 -0
- package/dist/mixins/hotkey.d.ts +2 -1
- package/dist/mixins/hotkey.js +8 -54
- package/dist/mixins/index.d.ts +1 -0
- package/dist/mixins/index.js +1 -0
- package/dist/mixins/keymap.d.ts +61 -0
- package/dist/mixins/keymap.js +88 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/key-combo.d.ts +69 -0
- package/dist/utils/key-combo.js +142 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -303,6 +303,8 @@ positioning, or a custom `--ui-*` command answered by a mixin.
|
|
|
303
303
|
- `Text`, `Header`, `Keyboard`, `Typeset` — muted body copy, an uppercase section label in a
|
|
304
304
|
`<header>`, a shortcut hint in a `<kbd>`, and a typography layer for already-rendered
|
|
305
305
|
markup with `docs`, `chat` and `reading` presets.
|
|
306
|
+
- `ShortcutList` — a two-column `<dl>` pairing each action with the key caps that run it,
|
|
307
|
+
for a keyboard-shortcuts panel.
|
|
306
308
|
- `Highlight` — text split into `{ text, match }` segments, each match a tinted `<mark>`
|
|
307
309
|
that keeps the surrounding color and weight, for search results and filtered lists.
|
|
308
310
|
- `HeadingScope`, `Heading` — an ambient heading-depth scope and the heading that reads it.
|
|
@@ -341,6 +343,7 @@ script never runs, so the cost of every behavior stays visible before you apply
|
|
|
341
343
|
| `gridListKeys(model)` | `GridList` | The ARIA grid keyboard pattern against a `SelectionModel`. |
|
|
342
344
|
| `headingLevelFallback(options)` | an island's heading root | Recovers the ambient heading depth from `data-heading-level` at a hydration boundary. |
|
|
343
345
|
| `hotkey(combo)` | any `<dialog>`/`[popover]` | A document-level shortcut opening or closing its host regardless of focus. |
|
|
346
|
+
| `keymap(bindings)` | any host | Page-wide shortcuts that stand down in fields, IME composition, and dialogs outside the host. |
|
|
344
347
|
| `imageFallback()` | `Avatar` / `Logo` images | Flags a failed image load, including retroactively for cached images. |
|
|
345
348
|
| `listboxKeys()` | `ListBox` | Arrow/Home/End/typeahead through `@remix-run/ui/listbox`. |
|
|
346
349
|
| `longPress(options?)` | any element | Fires once a pointer holds still past a duration and movement tolerance. |
|
|
@@ -365,7 +368,8 @@ script never runs, so the cost of every behavior stays visible before you apply
|
|
|
365
368
|
Headless, DOM-free state models. Each is unit-testable on its own, and an island subscribes
|
|
366
369
|
to it and re-renders.
|
|
367
370
|
|
|
368
|
-
- `Announcer` — a priority-ordered queue of `aria-live` announcements
|
|
371
|
+
- `Announcer` — a priority-ordered queue of `aria-live` announcements, optionally advancing
|
|
372
|
+
on its own once each message has been current for a `hold`.
|
|
369
373
|
- `CalendarModel` — the keyboard-focused day, the visible month, and an in-progress range.
|
|
370
374
|
- `DragSession` — the dragged item, the drop candidate under the pointer, and the computed
|
|
371
375
|
drop position.
|
|
@@ -436,6 +440,11 @@ Plain TypeScript with no rendering dependency, importable on its own.
|
|
|
436
440
|
- **DOM** — `asCommandEvent`, `dispatchChange`, `isNewPrimaryPress`, `isPrintableKey`,
|
|
437
441
|
`focusItem`, `queryItems`, `setRovingTabindex`, `labelFor`, `hasAccessibleText`,
|
|
438
442
|
`mergeStyle`, `prefersReducedMotion`, `writeCookie`, `DISABLED_SELECTOR`.
|
|
443
|
+
- **Keys** — `parseKeyCombo`, `matchesKeyCombo`, `keyComboGlyphs`, `isTypingTarget`,
|
|
444
|
+
`TYPING_TARGET_SELECTOR`, and the `KeyCombo` interface. A combo is written once
|
|
445
|
+
(`"mod+k"`, `"escape"`, `"?"`) and both matched and printed from that spelling: `mod` is
|
|
446
|
+
Command on Apple keyboards and Control elsewhere, and a bare character names the character
|
|
447
|
+
typed, so `"J"` and `"?"` match whatever Shift produced them.
|
|
439
448
|
- **Types** — `SemanticColor` (the five-tone union every `color` prop resolves to),
|
|
440
449
|
`AnchorPlacement`, `AriaInvalid`, `CSSStyles`, `StyleProp`, `FieldColor`.
|
|
441
450
|
|
|
@@ -479,6 +488,60 @@ export const AppToaster = clientEntry(
|
|
|
479
488
|
Any descendant reaches the queue through context:
|
|
480
489
|
`handle.context.get(AppToaster).toaster.add({ title: t("toasts.saved") })`.
|
|
481
490
|
|
|
491
|
+
## Pattern: A Keyboard Shortcuts Panel
|
|
492
|
+
|
|
493
|
+
The bindings, the panel that lists them, and the live region that says what a key did are
|
|
494
|
+
three independent pieces. `keymap()` binds the keys on the island's host, so a panel rendered
|
|
495
|
+
inside that host keeps answering them while any other dialog keeps its own keys; the panel
|
|
496
|
+
is a native `<dialog>` the `?` key opens through the same button a pointer presses; and an
|
|
497
|
+
`Announcer` with a `hold` paces what the live region reads.
|
|
498
|
+
|
|
499
|
+
```tsx
|
|
500
|
+
import type { Handle } from "remix/component";
|
|
501
|
+
|
|
502
|
+
import { visuallyHidden } from "@sdxc/u/a11y";
|
|
503
|
+
import { Button, Modal, ShortcutList } from "@sdxc/ui";
|
|
504
|
+
import { Announcer } from "@sdxc/ui/behaviors";
|
|
505
|
+
import { keymap } from "@sdxc/ui/mixins";
|
|
506
|
+
import { keyComboGlyphs } from "@sdxc/ui/utils";
|
|
507
|
+
import { clientEntry } from "remix/component";
|
|
508
|
+
|
|
509
|
+
type Props = { apple: boolean };
|
|
510
|
+
|
|
511
|
+
export default clientEntry(
|
|
512
|
+
"/app/components/shortcuts.tsx#default",
|
|
513
|
+
function Shortcuts(handle: Handle<Props>) {
|
|
514
|
+
let announcer = new Announcer({ hold: 1200 });
|
|
515
|
+
announcer.addEventListener("change", () => handle.update(), { signal: handle.signal });
|
|
516
|
+
|
|
517
|
+
let openHelp = () =>
|
|
518
|
+
document.querySelector<HTMLButtonElement>('[commandfor="shortcuts"]')?.click();
|
|
519
|
+
|
|
520
|
+
return () => (
|
|
521
|
+
<div mix={[keymap({ "mod+s": save, j: next, "?": openHelp })]}>
|
|
522
|
+
<Button type="button" commandfor="shortcuts" command="show-modal">
|
|
523
|
+
{t("shortcuts.open")}
|
|
524
|
+
</Button>
|
|
525
|
+
<Modal id="shortcuts" aria-label={t("shortcuts.open")}>
|
|
526
|
+
<ShortcutList>
|
|
527
|
+
<ShortcutList.Item keys={keyComboGlyphs("mod+s", handle.props.apple)}>
|
|
528
|
+
{t("shortcuts.save")}
|
|
529
|
+
</ShortcutList.Item>
|
|
530
|
+
<ShortcutList.Item keys={["j"]}>{t("shortcuts.next")}</ShortcutList.Item>
|
|
531
|
+
</ShortcutList>
|
|
532
|
+
</Modal>
|
|
533
|
+
<div role="status" aria-live="polite" mix={[visuallyHidden()]}>
|
|
534
|
+
{announcer.current?.text ?? ""}
|
|
535
|
+
</div>
|
|
536
|
+
</div>
|
|
537
|
+
);
|
|
538
|
+
},
|
|
539
|
+
);
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
Where the bindings are installed from a `ref` callback instead of a mixin, `bindKeymap(document,
|
|
543
|
+
bindings, { signal, scope })` takes the same map and the same stand-down rules.
|
|
544
|
+
|
|
482
545
|
## Pattern: The Custom Command Trigger Contract
|
|
483
546
|
|
|
484
547
|
[Invoker Commands](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API)
|
|
@@ -28,6 +28,17 @@ export declare namespace Announcer {
|
|
|
28
28
|
readonly text: string;
|
|
29
29
|
readonly priority: Priority;
|
|
30
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Options for constructing an {@link Announcer}.
|
|
33
|
+
*/
|
|
34
|
+
interface Options {
|
|
35
|
+
/**
|
|
36
|
+
* Milliseconds each message stays current before the queue advances on its own: long
|
|
37
|
+
* enough for a screen reader to reach it, short enough that a run of quick changes is
|
|
38
|
+
* heard one by one. Omitted, the live region advances the queue with `next()`.
|
|
39
|
+
*/
|
|
40
|
+
hold?: number;
|
|
41
|
+
}
|
|
31
42
|
/**
|
|
32
43
|
* Events dispatched by {@link Announcer}.
|
|
33
44
|
*/
|
|
@@ -49,6 +60,13 @@ export declare namespace Announcer {
|
|
|
49
60
|
*/
|
|
50
61
|
export declare class Announcer extends TypedEventTarget<Announcer.Events> {
|
|
51
62
|
#private;
|
|
63
|
+
/**
|
|
64
|
+
* Without `hold`, the queue moves only when a caller advances or dismisses it.
|
|
65
|
+
*
|
|
66
|
+
* @param options How long each message holds before the queue advances on its own.
|
|
67
|
+
* @example new Announcer({ hold: 1200 })
|
|
68
|
+
*/
|
|
69
|
+
constructor(options?: Announcer.Options);
|
|
52
70
|
/**
|
|
53
71
|
* The message a live region should currently render, or `undefined` when
|
|
54
72
|
* the queue is empty.
|
|
@@ -19,6 +19,18 @@ const DEFAULT_PRIORITY = "polite";
|
|
|
19
19
|
*/
|
|
20
20
|
export class Announcer extends TypedEventTarget {
|
|
21
21
|
#queue = [];
|
|
22
|
+
#hold;
|
|
23
|
+
#held;
|
|
24
|
+
/**
|
|
25
|
+
* Without `hold`, the queue moves only when a caller advances or dismisses it.
|
|
26
|
+
*
|
|
27
|
+
* @param options How long each message holds before the queue advances on its own.
|
|
28
|
+
* @example new Announcer({ hold: 1200 })
|
|
29
|
+
*/
|
|
30
|
+
constructor(options = {}) {
|
|
31
|
+
super();
|
|
32
|
+
this.#hold = options.hold;
|
|
33
|
+
}
|
|
22
34
|
/**
|
|
23
35
|
* The message a live region should currently render, or `undefined` when
|
|
24
36
|
* the queue is empty.
|
|
@@ -54,7 +66,7 @@ export class Announcer extends TypedEventTarget {
|
|
|
54
66
|
}
|
|
55
67
|
else
|
|
56
68
|
this.#queue.push(message);
|
|
57
|
-
this
|
|
69
|
+
this.#changed();
|
|
58
70
|
return message.id;
|
|
59
71
|
}
|
|
60
72
|
/**
|
|
@@ -68,7 +80,7 @@ export class Announcer extends TypedEventTarget {
|
|
|
68
80
|
if (index === -1)
|
|
69
81
|
return;
|
|
70
82
|
this.#queue.splice(index, 1);
|
|
71
|
-
this
|
|
83
|
+
this.#changed();
|
|
72
84
|
}
|
|
73
85
|
/**
|
|
74
86
|
* Advances past the current message so the next queued one becomes
|
|
@@ -79,7 +91,7 @@ export class Announcer extends TypedEventTarget {
|
|
|
79
91
|
if (this.#queue.length === 0)
|
|
80
92
|
return;
|
|
81
93
|
this.#queue.shift();
|
|
82
|
-
this
|
|
94
|
+
this.#changed();
|
|
83
95
|
}
|
|
84
96
|
/**
|
|
85
97
|
* Empties the queue, dispatching `"change"` only when it held messages.
|
|
@@ -88,6 +100,22 @@ export class Announcer extends TypedEventTarget {
|
|
|
88
100
|
if (this.#queue.length === 0)
|
|
89
101
|
return;
|
|
90
102
|
this.#queue = [];
|
|
103
|
+
this.#changed();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Restarts the hold whenever a different message becomes current, so each message is
|
|
107
|
+
* held for the full duration once it reaches the front, then dispatches `"change"`.
|
|
108
|
+
*/
|
|
109
|
+
#changed() {
|
|
110
|
+
let current = this.current;
|
|
111
|
+
if (this.#hold !== undefined && this.#held?.id !== current?.id) {
|
|
112
|
+
if (this.#held)
|
|
113
|
+
clearTimeout(this.#held.timer);
|
|
114
|
+
this.#held =
|
|
115
|
+
current === undefined
|
|
116
|
+
? undefined
|
|
117
|
+
: { id: current.id, timer: setTimeout(() => this.next(), this.#hold) };
|
|
118
|
+
}
|
|
91
119
|
this.dispatchEvent(new Event("change"));
|
|
92
120
|
}
|
|
93
121
|
}
|
|
@@ -90,6 +90,7 @@ export * from "./sentinel-row.js";
|
|
|
90
90
|
export * from "./separator.js";
|
|
91
91
|
export * from "./shared-element.js";
|
|
92
92
|
export * from "./sheet.js";
|
|
93
|
+
export * from "./shortcut-list.js";
|
|
93
94
|
export * from "./sidebar.js";
|
|
94
95
|
export * from "./skeleton.js";
|
|
95
96
|
export * from "./slider.js";
|
package/dist/components/index.js
CHANGED
|
@@ -90,6 +90,7 @@ export * from "./sentinel-row.js";
|
|
|
90
90
|
export * from "./separator.js";
|
|
91
91
|
export * from "./shared-element.js";
|
|
92
92
|
export * from "./sheet.js";
|
|
93
|
+
export * from "./shortcut-list.js";
|
|
93
94
|
export * from "./sidebar.js";
|
|
94
95
|
export * from "./skeleton.js";
|
|
95
96
|
export * from "./slider.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The list a keyboard-shortcuts panel shows: a native `<dl>` pairing each action with the
|
|
3
|
+
* key caps that run it, laid out in two columns so every key lands on one vertical line
|
|
4
|
+
* and the list reads down that line instead of across ragged hints.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Handle, Props as TagProps, RemixNode } from "remix/component";
|
|
10
|
+
/**
|
|
11
|
+
* Prop types for {@link ShortcutList} and its compound parts.
|
|
12
|
+
*/
|
|
13
|
+
export declare namespace ShortcutList {
|
|
14
|
+
/**
|
|
15
|
+
* Props accepted by {@link ShortcutList}.
|
|
16
|
+
*/
|
|
17
|
+
interface Props extends TagProps<"dl"> {
|
|
18
|
+
/** One {@link ShortcutList.Item} per binding, in the order the panel lists them. */
|
|
19
|
+
children: RemixNode;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Props accepted by {@link ShortcutList.Item}.
|
|
23
|
+
*/
|
|
24
|
+
interface ItemProps extends TagProps<"div"> {
|
|
25
|
+
/**
|
|
26
|
+
* The caps to press, in press order, each already the glyph to print — a combo
|
|
27
|
+
* formatted for the reader's keyboard by `keyComboGlyphs()`, or a bare character.
|
|
28
|
+
*/
|
|
29
|
+
keys: string[];
|
|
30
|
+
/** What pressing the keys does. */
|
|
31
|
+
children: RemixNode;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Renders the list's host `<dl>`, a two-column grid whose first column takes the actions
|
|
36
|
+
* and whose second sizes itself to the widest run of caps.
|
|
37
|
+
*
|
|
38
|
+
* @param handle Runtime handle carrying the host `<dl>`'s props.
|
|
39
|
+
* @returns The render function producing the list's markup.
|
|
40
|
+
* @example
|
|
41
|
+
* <ShortcutList>
|
|
42
|
+
* <ShortcutList.Item keys={keyComboGlyphs("mod+k", apple)}>{t("openSearch")}</ShortcutList.Item>
|
|
43
|
+
* <ShortcutList.Item keys={["?"]}>{t("showShortcuts")}</ShortcutList.Item>
|
|
44
|
+
* </ShortcutList>
|
|
45
|
+
*/
|
|
46
|
+
export declare function ShortcutList(handle: Handle<ShortcutList.Props>): () => import("remix/component").RemixElement;
|
|
47
|
+
export declare namespace ShortcutList {
|
|
48
|
+
var Item: (handle: Handle<ShortcutList.ItemProps>) => () => import("remix/component").RemixElement;
|
|
49
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "remix/component/jsx-runtime";
|
|
2
|
+
import { fg } from "@sdxc/u/color";
|
|
3
|
+
import { contents, flex, gap, grid, gridTemplate, items, justify } from "@sdxc/u/layout";
|
|
4
|
+
import { m } from "@sdxc/u/size";
|
|
5
|
+
import { text, weight } from "@sdxc/u/typography";
|
|
6
|
+
import { Keyboard } from "./keyboard.js";
|
|
7
|
+
/**
|
|
8
|
+
* Renders the list's host `<dl>`, a two-column grid whose first column takes the actions
|
|
9
|
+
* and whose second sizes itself to the widest run of caps.
|
|
10
|
+
*
|
|
11
|
+
* @param handle Runtime handle carrying the host `<dl>`'s props.
|
|
12
|
+
* @returns The render function producing the list's markup.
|
|
13
|
+
* @example
|
|
14
|
+
* <ShortcutList>
|
|
15
|
+
* <ShortcutList.Item keys={keyComboGlyphs("mod+k", apple)}>{t("openSearch")}</ShortcutList.Item>
|
|
16
|
+
* <ShortcutList.Item keys={["?"]}>{t("showShortcuts")}</ShortcutList.Item>
|
|
17
|
+
* </ShortcutList>
|
|
18
|
+
*/
|
|
19
|
+
export function ShortcutList(handle) {
|
|
20
|
+
return () => {
|
|
21
|
+
let { children, mix, ...rest } = handle.props;
|
|
22
|
+
return (_jsx("dl", { ...rest, "data-slot": "shortcut-list", mix: [m(0), grid(), gap(2, 4), items("center"), gridTemplate({ columns: "1fr auto" }), mix], children: children }));
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Renders one binding as a `<dt>` naming the action and a `<dd>` holding a cap per key.
|
|
27
|
+
* The wrapping `<div>` lays out as its children, so both cells join the list's own grid.
|
|
28
|
+
*
|
|
29
|
+
* @param handle Runtime handle carrying the wrapping `<div>`'s props.
|
|
30
|
+
* @returns The render function producing the binding's markup.
|
|
31
|
+
* @example
|
|
32
|
+
* <ShortcutList.Item keys={["⌘", "K"]}>{t("openSearch")}</ShortcutList.Item>
|
|
33
|
+
*/
|
|
34
|
+
ShortcutList.Item = function ShortcutListItem(handle) {
|
|
35
|
+
return () => {
|
|
36
|
+
let { children, keys, mix, ...rest } = handle.props;
|
|
37
|
+
return (_jsxs("div", { ...rest, "data-slot": "shortcut", mix: [contents(), mix], children: [_jsx("dt", { mix: [text("sm"), fg("neutral")], children: children }), _jsx("dd", { mix: [m(0), flex(), gap(1), items("center"), justify("end")], children: keys.map((key, index) => (_jsx(Keyboard, { mix: [m(0), weight("medium")], children: key }, `${index}-${key}`))) })] }));
|
|
38
|
+
};
|
|
39
|
+
};
|
package/dist/mixins/hotkey.d.ts
CHANGED
|
@@ -13,7 +13,8 @@ import type { MixinFactory } from "remix/component";
|
|
|
13
13
|
* closed, and closes it again on the same combo while it's already open,
|
|
14
14
|
* driving the host's own native `<dialog>` or `[popover]` open state.
|
|
15
15
|
*
|
|
16
|
-
* @param combo Key combination that opens or closes the host, e.g. `"mod+k"
|
|
16
|
+
* @param combo Key combination that opens or closes the host, e.g. `"mod+k"`, where `mod`
|
|
17
|
+
* is Command on Apple keyboards and Control elsewhere.
|
|
17
18
|
* @example
|
|
18
19
|
* <dialog id="command-palette" mix={[hotkey("mod+k")]}>
|
|
19
20
|
* <Command>...</Command>
|
package/dist/mixins/hotkey.js
CHANGED
|
@@ -8,58 +8,8 @@
|
|
|
8
8
|
* @copyright Sergio Xalambrí 2026
|
|
9
9
|
*/
|
|
10
10
|
import { createMixin } from "remix/component";
|
|
11
|
+
import { matchesKeyCombo, parseKeyCombo } from "../utils/key-combo.js";
|
|
11
12
|
import { trackHostNode } from "./track-host-node.js";
|
|
12
|
-
/** Modifier token spellings {@link parseCombo} resolves to the platform's primary modifier, checked as `event.ctrlKey || event.metaKey`. */
|
|
13
|
-
const MOD_ALIASES = new Set(["mod", "ctrl", "control", "cmd", "command", "meta"]);
|
|
14
|
-
/** Modifier token spellings {@link parseCombo} resolves to `event.altKey`. */
|
|
15
|
-
const ALT_ALIASES = new Set(["alt", "option"]);
|
|
16
|
-
/** Modifier token spelling {@link parseCombo} resolves to `event.shiftKey`. */
|
|
17
|
-
const SHIFT_ALIAS = "shift";
|
|
18
|
-
/**
|
|
19
|
-
* Parses a `+`-joined combo string (`"mod+k"`, `"mod+shift+p"`) into the
|
|
20
|
-
* modifier flags and literal trigger key {@link matchesCombo} compares a
|
|
21
|
-
* `keydown` event against, matching tokens case-insensitively.
|
|
22
|
-
*
|
|
23
|
-
* @param combo Combo string as passed to {@link hotkey}.
|
|
24
|
-
* @returns The parsed modifier flags and trigger key.
|
|
25
|
-
*/
|
|
26
|
-
function parseCombo(combo) {
|
|
27
|
-
let tokens = combo
|
|
28
|
-
.split("+")
|
|
29
|
-
.map((token) => token.trim().toLowerCase())
|
|
30
|
-
.filter((token) => token.length > 0);
|
|
31
|
-
let key = tokens.at(-1) ?? "";
|
|
32
|
-
let modifiers = tokens.slice(0, -1);
|
|
33
|
-
if (key === "" && import.meta.env.DEV) {
|
|
34
|
-
console.warn(`hotkey(): "${combo}" has no trigger key and will never match.`);
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
key,
|
|
38
|
-
mod: modifiers.some((token) => MOD_ALIASES.has(token)),
|
|
39
|
-
alt: modifiers.some((token) => ALT_ALIASES.has(token)),
|
|
40
|
-
shift: modifiers.includes(SHIFT_ALIAS),
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Reports whether `event` strikes exactly the key and modifier combination
|
|
45
|
-
* `combo` describes, requiring every modifier `combo` lists and none it
|
|
46
|
-
* doesn't, so a combo never fires on a superset chord.
|
|
47
|
-
*
|
|
48
|
-
* @param event `keydown` event observed on the document.
|
|
49
|
-
* @param combo Parsed combo to compare `event` against.
|
|
50
|
-
* @returns Whether `event` is an exact match for `combo`.
|
|
51
|
-
*/
|
|
52
|
-
function matchesCombo(event, combo) {
|
|
53
|
-
if (event.key.toLowerCase() !== combo.key)
|
|
54
|
-
return false;
|
|
55
|
-
if ((event.ctrlKey || event.metaKey) !== combo.mod)
|
|
56
|
-
return false;
|
|
57
|
-
if (event.altKey !== combo.alt)
|
|
58
|
-
return false;
|
|
59
|
-
if (event.shiftKey !== combo.shift)
|
|
60
|
-
return false;
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
13
|
/**
|
|
64
14
|
* Reads `host`'s current native open state: `HTMLDialogElement.open` for a
|
|
65
15
|
* `<dialog>`, or the `:popover-open` pseudo-class for anything else.
|
|
@@ -104,7 +54,8 @@ function setOpen(host, open) {
|
|
|
104
54
|
* closed, and closes it again on the same combo while it's already open,
|
|
105
55
|
* driving the host's own native `<dialog>` or `[popover]` open state.
|
|
106
56
|
*
|
|
107
|
-
* @param combo Key combination that opens or closes the host, e.g. `"mod+k"
|
|
57
|
+
* @param combo Key combination that opens or closes the host, e.g. `"mod+k"`, where `mod`
|
|
58
|
+
* is Command on Apple keyboards and Control elsewhere.
|
|
108
59
|
* @example
|
|
109
60
|
* <dialog id="command-palette" mix={[hotkey("mod+k")]}>
|
|
110
61
|
* <Command>...</Command>
|
|
@@ -127,13 +78,16 @@ export const hotkey = createMixin((handle) => {
|
|
|
127
78
|
let hostNode = getHostNode();
|
|
128
79
|
if (hostNode === undefined || parsed === undefined)
|
|
129
80
|
return;
|
|
130
|
-
if (keyEvent.repeat || !
|
|
81
|
+
if (keyEvent.repeat || !matchesKeyCombo(keyEvent, parsed))
|
|
131
82
|
return;
|
|
132
83
|
keyEvent.preventDefault();
|
|
133
84
|
setOpen(hostNode, !isOpen(hostNode));
|
|
134
85
|
}, { signal: handle.signal });
|
|
135
86
|
});
|
|
136
87
|
return (combo) => {
|
|
137
|
-
parsed =
|
|
88
|
+
parsed = parseKeyCombo(combo);
|
|
89
|
+
if (parsed.key === "" && import.meta.env.DEV) {
|
|
90
|
+
console.warn(`hotkey(): "${combo}" has no trigger key and will never match.`);
|
|
91
|
+
}
|
|
138
92
|
};
|
|
139
93
|
});
|
package/dist/mixins/index.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export * from "./grid-list-keys.js";
|
|
|
29
29
|
export * from "./heading-level-fallback.js";
|
|
30
30
|
export * from "./hotkey.js";
|
|
31
31
|
export * from "./image-fallback.js";
|
|
32
|
+
export * from "./keymap.js";
|
|
32
33
|
export * from "./listbox-keys.js";
|
|
33
34
|
export * from "./long-press.js";
|
|
34
35
|
export * from "./menu-keys.js";
|
package/dist/mixins/index.js
CHANGED
|
@@ -29,6 +29,7 @@ export * from "./grid-list-keys.js";
|
|
|
29
29
|
export * from "./heading-level-fallback.js";
|
|
30
30
|
export * from "./hotkey.js";
|
|
31
31
|
export * from "./image-fallback.js";
|
|
32
|
+
export * from "./keymap.js";
|
|
32
33
|
export * from "./listbox-keys.js";
|
|
33
34
|
export * from "./long-press.js";
|
|
34
35
|
export * from "./menu-keys.js";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a page-wide shortcut answers a key pressed anywhere in the document, and HTML
|
|
3
|
+
* has no declarative wiring from a keystroke to an action.
|
|
4
|
+
* No-JS baseline: every action stays reachable through the controls it presses, by `Tab`
|
|
5
|
+
* and the pointer.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import type { MixinFactory } from "remix/component";
|
|
11
|
+
/**
|
|
12
|
+
* Types for {@link keymap} and {@link bindKeymap}.
|
|
13
|
+
*/
|
|
14
|
+
export declare namespace Keymap {
|
|
15
|
+
/**
|
|
16
|
+
* Each combo, as {@link parseKeyCombo} reads it, mapped to what pressing it runs. The
|
|
17
|
+
* first entry a keystroke matches runs, and the keystroke is then default-prevented.
|
|
18
|
+
*/
|
|
19
|
+
interface Bindings {
|
|
20
|
+
[combo: string]: (event: KeyboardEvent) => void;
|
|
21
|
+
}
|
|
22
|
+
/** Options for {@link bindKeymap}. */
|
|
23
|
+
interface Options {
|
|
24
|
+
/** Takes the bindings off the document. */
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
/**
|
|
27
|
+
* The element the bindings belong to. A keystroke inside a `<dialog>` stands the
|
|
28
|
+
* bindings down unless that dialog sits inside this element, so a modal layered over
|
|
29
|
+
* the page keeps its keys while the keymap's own help panel still answers them.
|
|
30
|
+
*/
|
|
31
|
+
scope?: Element | null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Whether a keystroke belongs to someone else: handled nearer its target already, part
|
|
36
|
+
* of an IME composition, typed into a field, or pressed inside a dialog outside `scope`.
|
|
37
|
+
*
|
|
38
|
+
* @param event The keystroke as it reached the document.
|
|
39
|
+
* @param scope The element whose own dialogs still answer the bindings.
|
|
40
|
+
* @returns Whether the bindings stand down for this keystroke.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isClaimedKeystroke(event: KeyboardEvent, scope?: Element | null): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Listens for `bindings` on `document` in the bubble phase, so a handler nearer the
|
|
45
|
+
* target that claims the keystroke first keeps it.
|
|
46
|
+
*
|
|
47
|
+
* @param document The document to listen on.
|
|
48
|
+
* @param bindings Combos mapped to what each runs.
|
|
49
|
+
* @param options The signal that removes the listener, and the bindings' own scope.
|
|
50
|
+
* @example bindKeymap(document, { j: next, k: previous, "?": openHelp }, { signal });
|
|
51
|
+
*/
|
|
52
|
+
export declare function bindKeymap(document: Document, bindings: Keymap.Bindings, options: Keymap.Options): void;
|
|
53
|
+
/**
|
|
54
|
+
* Binds a map of page-wide shortcuts for as long as the host stays mounted, with the host
|
|
55
|
+
* as the bindings' scope: a dialog inside it answers them, any other dialog and any field
|
|
56
|
+
* being typed in keeps its keys. The latest map passed is the one a keystroke runs.
|
|
57
|
+
*
|
|
58
|
+
* @param bindings Combos mapped to what each runs.
|
|
59
|
+
* @example <div mix={[keymap({ j: next, k: previous, "?": openHelp })]}>…</div>
|
|
60
|
+
*/
|
|
61
|
+
export declare const keymap: MixinFactory<HTMLElement, [bindings: Keymap.Bindings]>;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why JS: a page-wide shortcut answers a key pressed anywhere in the document, and HTML
|
|
3
|
+
* has no declarative wiring from a keystroke to an action.
|
|
4
|
+
* No-JS baseline: every action stays reachable through the controls it presses, by `Tab`
|
|
5
|
+
* and the pointer.
|
|
6
|
+
*
|
|
7
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
8
|
+
* @copyright Sergio Xalambrí 2026
|
|
9
|
+
*/
|
|
10
|
+
import { createMixin } from "remix/component";
|
|
11
|
+
import { isTypingTarget, matchesKeyCombo, parseKeyCombo } from "../utils/key-combo.js";
|
|
12
|
+
/**
|
|
13
|
+
* Whether a keystroke belongs to someone else: handled nearer its target already, part
|
|
14
|
+
* of an IME composition, typed into a field, or pressed inside a dialog outside `scope`.
|
|
15
|
+
*
|
|
16
|
+
* @param event The keystroke as it reached the document.
|
|
17
|
+
* @param scope The element whose own dialogs still answer the bindings.
|
|
18
|
+
* @returns Whether the bindings stand down for this keystroke.
|
|
19
|
+
*/
|
|
20
|
+
export function isClaimedKeystroke(event, scope) {
|
|
21
|
+
if (event.defaultPrevented || event.isComposing)
|
|
22
|
+
return true;
|
|
23
|
+
if (isTypingTarget(event.target))
|
|
24
|
+
return true;
|
|
25
|
+
let target = event.target;
|
|
26
|
+
if (typeof Element === "undefined" || !(target instanceof Element))
|
|
27
|
+
return false;
|
|
28
|
+
let dialog = target.closest("dialog");
|
|
29
|
+
if (dialog === null)
|
|
30
|
+
return false;
|
|
31
|
+
return !(scope?.contains(dialog) ?? false);
|
|
32
|
+
}
|
|
33
|
+
/** Parses every combo once, keeping the binding order a keystroke is matched in. */
|
|
34
|
+
function compile(bindings) {
|
|
35
|
+
return Object.entries(bindings).map(([combo, run]) => [parseKeyCombo(combo), run]);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Runs the first binding a keystroke matches, reading the bindings at keystroke time so
|
|
39
|
+
* a caller that swaps its map keeps one listener.
|
|
40
|
+
*/
|
|
41
|
+
function listen(document, bindings, options) {
|
|
42
|
+
document.addEventListener("keydown", (event) => {
|
|
43
|
+
if (isClaimedKeystroke(event, options.scope))
|
|
44
|
+
return;
|
|
45
|
+
let match = bindings().find(([combo]) => matchesKeyCombo(event, combo));
|
|
46
|
+
if (match === undefined)
|
|
47
|
+
return;
|
|
48
|
+
match[1](event);
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
}, { signal: options.signal });
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Listens for `bindings` on `document` in the bubble phase, so a handler nearer the
|
|
54
|
+
* target that claims the keystroke first keeps it.
|
|
55
|
+
*
|
|
56
|
+
* @param document The document to listen on.
|
|
57
|
+
* @param bindings Combos mapped to what each runs.
|
|
58
|
+
* @param options The signal that removes the listener, and the bindings' own scope.
|
|
59
|
+
* @example bindKeymap(document, { j: next, k: previous, "?": openHelp }, { signal });
|
|
60
|
+
*/
|
|
61
|
+
export function bindKeymap(document, bindings, options) {
|
|
62
|
+
let compiled = compile(bindings);
|
|
63
|
+
listen(document, () => compiled, options);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Binds a map of page-wide shortcuts for as long as the host stays mounted, with the host
|
|
67
|
+
* as the bindings' scope: a dialog inside it answers them, any other dialog and any field
|
|
68
|
+
* being typed in keeps its keys. The latest map passed is the one a keystroke runs.
|
|
69
|
+
*
|
|
70
|
+
* @param bindings Combos mapped to what each runs.
|
|
71
|
+
* @example <div mix={[keymap({ j: next, k: previous, "?": openHelp })]}>…</div>
|
|
72
|
+
*/
|
|
73
|
+
export const keymap = createMixin((handle) => {
|
|
74
|
+
let compiled = [];
|
|
75
|
+
/**
|
|
76
|
+
* The listener goes on the document the host was inserted into, which leaves the mixin
|
|
77
|
+
* inert wherever the host renders without one, such as on a server.
|
|
78
|
+
*/
|
|
79
|
+
handle.addEventListener("insert", (event) => {
|
|
80
|
+
listen(event.node.ownerDocument, () => compiled, {
|
|
81
|
+
signal: handle.signal,
|
|
82
|
+
scope: event.node,
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return (bindings) => {
|
|
86
|
+
compiled = compile(bindings);
|
|
87
|
+
};
|
|
88
|
+
});
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from "./geometry.js";
|
|
|
21
21
|
export * from "./has-accessible-text.js";
|
|
22
22
|
export * from "./hue-spectrum.js";
|
|
23
23
|
export * from "./is-new-primary-press.js";
|
|
24
|
+
export * from "./key-combo.js";
|
|
24
25
|
export * from "./keyboard-nav.js";
|
|
25
26
|
export * from "./merge-style.js";
|
|
26
27
|
export * from "./paired-range-inputs.js";
|
package/dist/utils/index.js
CHANGED
|
@@ -21,6 +21,7 @@ export * from "./geometry.js";
|
|
|
21
21
|
export * from "./has-accessible-text.js";
|
|
22
22
|
export * from "./hue-spectrum.js";
|
|
23
23
|
export * from "./is-new-primary-press.js";
|
|
24
|
+
export * from "./key-combo.js";
|
|
24
25
|
export * from "./keyboard-nav.js";
|
|
25
26
|
export * from "./merge-style.js";
|
|
26
27
|
export * from "./paired-range-inputs.js";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard shortcut combos as written once and read twice: matched against a `keydown`
|
|
3
|
+
* and printed as the key caps a hint shows. One spelling (`"mod+k"`, `"?"`, `"J"`) serves
|
|
4
|
+
* both, so a binding and the hint advertising it never disagree about the platform.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Where a reader is typing, and so where a bare letter is a letter rather than a command.
|
|
11
|
+
*/
|
|
12
|
+
export declare const TYPING_TARGET_SELECTOR = "input, textarea, select, [contenteditable]:not([contenteditable=\"false\"]), [role=\"textbox\"]";
|
|
13
|
+
/**
|
|
14
|
+
* A combo split into its trigger and the modifiers held with it. A bare single character
|
|
15
|
+
* is a `character` combo: it names the character typed, so its case is significant and
|
|
16
|
+
* Shift is whatever produced it.
|
|
17
|
+
*/
|
|
18
|
+
export interface KeyCombo {
|
|
19
|
+
/** `KeyboardEvent.key` the combo fires on; lowercased unless the combo is a bare character. */
|
|
20
|
+
key: string;
|
|
21
|
+
/** Whether the combo is one typed character with no modifier listed. */
|
|
22
|
+
character: boolean;
|
|
23
|
+
/** Whether the platform's primary modifier, Command or Control, is held. */
|
|
24
|
+
mod: boolean;
|
|
25
|
+
ctrl: boolean;
|
|
26
|
+
meta: boolean;
|
|
27
|
+
alt: boolean;
|
|
28
|
+
shift: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Splits a `+`-joined combo into its trigger key and modifiers, matching modifier names
|
|
32
|
+
* case-insensitively. Unknown tokens before the trigger are ignored.
|
|
33
|
+
*
|
|
34
|
+
* @param combo The combo as written, e.g. `"mod+shift+k"`, `"escape"`, `"?"`.
|
|
35
|
+
* @returns The trigger key and the modifier flags {@link matchesKeyCombo} compares.
|
|
36
|
+
* @example parseKeyCombo("J") // { key: "J", character: true, … }
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseKeyCombo(combo: string): KeyCombo;
|
|
39
|
+
/**
|
|
40
|
+
* Whether a keystroke strikes exactly `combo`. A character combo matches the character
|
|
41
|
+
* typed with Control, Alt and Meta all up; any other combo matches its key in any case
|
|
42
|
+
* with exactly the listed modifiers held, so it never fires on a larger chord.
|
|
43
|
+
*
|
|
44
|
+
* @param event The keystroke, or any object carrying its key and modifier flags.
|
|
45
|
+
* @param combo The combo, as written or already parsed.
|
|
46
|
+
* @returns Whether the keystroke is this combo.
|
|
47
|
+
* @example matchesKeyCombo(event, "mod+k") // ⌘K on a Mac, Ctrl+K elsewhere
|
|
48
|
+
*/
|
|
49
|
+
export declare function matchesKeyCombo(event: Pick<KeyboardEvent, "key" | "ctrlKey" | "metaKey" | "altKey" | "shiftKey">, combo: KeyCombo | string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* The key caps a hint prints for `combo`, one per key in press order, in the glyphs of
|
|
52
|
+
* the keyboard in front of the reader. A bare character prints as written, since its case
|
|
53
|
+
* is the binding; a key inside a chord prints upper-cased, the way a key cap shows it.
|
|
54
|
+
*
|
|
55
|
+
* @param combo The combo as written, e.g. `"mod+shift+z"`.
|
|
56
|
+
* @param apple Whether the reader's keyboard carries the Apple modifier glyphs.
|
|
57
|
+
* @returns The caps to render, each already the glyph to print.
|
|
58
|
+
* @example keyComboGlyphs("mod+k", true) // ["⌘", "K"]
|
|
59
|
+
* @example keyComboGlyphs("mod+k", false) // ["Ctrl", "K"]
|
|
60
|
+
*/
|
|
61
|
+
export declare function keyComboGlyphs(combo: string, apple: boolean): string[];
|
|
62
|
+
/**
|
|
63
|
+
* Whether a keystroke was aimed at a field the reader is typing in, where a bare letter
|
|
64
|
+
* shortcut would swallow the letter.
|
|
65
|
+
*
|
|
66
|
+
* @param target The keystroke's target.
|
|
67
|
+
* @returns Whether the target is, or sits inside, an editable control.
|
|
68
|
+
*/
|
|
69
|
+
export declare function isTypingTarget(target: EventTarget | null): boolean;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard shortcut combos as written once and read twice: matched against a `keydown`
|
|
3
|
+
* and printed as the key caps a hint shows. One spelling (`"mod+k"`, `"?"`, `"J"`) serves
|
|
4
|
+
* both, so a binding and the hint advertising it never disagree about the platform.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Where a reader is typing, and so where a bare letter is a letter rather than a command.
|
|
11
|
+
*/
|
|
12
|
+
export const TYPING_TARGET_SELECTOR = 'input, textarea, select, [contenteditable]:not([contenteditable="false"]), [role="textbox"]';
|
|
13
|
+
/** Modifier spellings meaning the platform's primary modifier: Command on Apple, Control elsewhere. */
|
|
14
|
+
const MOD_TOKENS = new Set(["mod"]);
|
|
15
|
+
/** Modifier spellings meaning the Control key itself, on every platform. */
|
|
16
|
+
const CTRL_TOKENS = new Set(["ctrl", "control"]);
|
|
17
|
+
/** Modifier spellings meaning the Command (Meta) key itself. */
|
|
18
|
+
const META_TOKENS = new Set(["cmd", "command", "meta"]);
|
|
19
|
+
/** Modifier spellings meaning Alt, which Apple keyboards print as Option. */
|
|
20
|
+
const ALT_TOKENS = new Set(["alt", "option"]);
|
|
21
|
+
/** Short spellings of a named key, mapped to the `KeyboardEvent.key` value they stand for. */
|
|
22
|
+
const KEY_ALIASES = { esc: "escape" };
|
|
23
|
+
/** The glyph an Apple keyboard prints on each modifier and named key. */
|
|
24
|
+
const APPLE_GLYPHS = {
|
|
25
|
+
mod: "⌘",
|
|
26
|
+
cmd: "⌘",
|
|
27
|
+
command: "⌘",
|
|
28
|
+
meta: "⌘",
|
|
29
|
+
shift: "⇧",
|
|
30
|
+
alt: "⌥",
|
|
31
|
+
option: "⌥",
|
|
32
|
+
ctrl: "⌃",
|
|
33
|
+
control: "⌃",
|
|
34
|
+
enter: "↵",
|
|
35
|
+
esc: "esc",
|
|
36
|
+
escape: "esc",
|
|
37
|
+
};
|
|
38
|
+
/** The spelled-out name every other keyboard prints on each modifier and named key. */
|
|
39
|
+
const OTHER_GLYPHS = {
|
|
40
|
+
mod: "Ctrl",
|
|
41
|
+
cmd: "Meta",
|
|
42
|
+
command: "Meta",
|
|
43
|
+
meta: "Meta",
|
|
44
|
+
shift: "Shift",
|
|
45
|
+
alt: "Alt",
|
|
46
|
+
option: "Alt",
|
|
47
|
+
ctrl: "Ctrl",
|
|
48
|
+
control: "Ctrl",
|
|
49
|
+
enter: "Enter",
|
|
50
|
+
esc: "Esc",
|
|
51
|
+
escape: "Esc",
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Splits a `+`-joined combo into its trigger key and modifiers, matching modifier names
|
|
55
|
+
* case-insensitively. Unknown tokens before the trigger are ignored.
|
|
56
|
+
*
|
|
57
|
+
* @param combo The combo as written, e.g. `"mod+shift+k"`, `"escape"`, `"?"`.
|
|
58
|
+
* @returns The trigger key and the modifier flags {@link matchesKeyCombo} compares.
|
|
59
|
+
* @example parseKeyCombo("J") // { key: "J", character: true, … }
|
|
60
|
+
*/
|
|
61
|
+
export function parseKeyCombo(combo) {
|
|
62
|
+
let tokens = combo
|
|
63
|
+
.split("+")
|
|
64
|
+
.map((token) => token.trim())
|
|
65
|
+
.filter((token) => token.length > 0);
|
|
66
|
+
let trigger = tokens.at(-1) ?? "";
|
|
67
|
+
let modifiers = tokens.slice(0, -1).map((token) => token.toLowerCase());
|
|
68
|
+
let character = modifiers.length === 0 && trigger.length === 1;
|
|
69
|
+
let named = trigger.toLowerCase();
|
|
70
|
+
return {
|
|
71
|
+
key: character ? trigger : (KEY_ALIASES[named] ?? named),
|
|
72
|
+
character,
|
|
73
|
+
mod: modifiers.some((token) => MOD_TOKENS.has(token)),
|
|
74
|
+
ctrl: modifiers.some((token) => CTRL_TOKENS.has(token)),
|
|
75
|
+
meta: modifiers.some((token) => META_TOKENS.has(token)),
|
|
76
|
+
alt: modifiers.some((token) => ALT_TOKENS.has(token)),
|
|
77
|
+
shift: modifiers.includes("shift"),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Whether a keystroke strikes exactly `combo`. A character combo matches the character
|
|
82
|
+
* typed with Control, Alt and Meta all up; any other combo matches its key in any case
|
|
83
|
+
* with exactly the listed modifiers held, so it never fires on a larger chord.
|
|
84
|
+
*
|
|
85
|
+
* @param event The keystroke, or any object carrying its key and modifier flags.
|
|
86
|
+
* @param combo The combo, as written or already parsed.
|
|
87
|
+
* @returns Whether the keystroke is this combo.
|
|
88
|
+
* @example matchesKeyCombo(event, "mod+k") // ⌘K on a Mac, Ctrl+K elsewhere
|
|
89
|
+
*/
|
|
90
|
+
export function matchesKeyCombo(event, combo) {
|
|
91
|
+
let parsed = typeof combo === "string" ? parseKeyCombo(combo) : combo;
|
|
92
|
+
if (parsed.key === "")
|
|
93
|
+
return false;
|
|
94
|
+
if (parsed.character) {
|
|
95
|
+
return event.key === parsed.key && !event.ctrlKey && !event.altKey && !event.metaKey;
|
|
96
|
+
}
|
|
97
|
+
if (event.key.toLowerCase() !== parsed.key)
|
|
98
|
+
return false;
|
|
99
|
+
if (event.altKey !== parsed.alt || event.shiftKey !== parsed.shift)
|
|
100
|
+
return false;
|
|
101
|
+
if (parsed.mod) {
|
|
102
|
+
return (event.ctrlKey || event.metaKey) && (!parsed.ctrl || event.ctrlKey);
|
|
103
|
+
}
|
|
104
|
+
return event.ctrlKey === parsed.ctrl && event.metaKey === parsed.meta;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The key caps a hint prints for `combo`, one per key in press order, in the glyphs of
|
|
108
|
+
* the keyboard in front of the reader. A bare character prints as written, since its case
|
|
109
|
+
* is the binding; a key inside a chord prints upper-cased, the way a key cap shows it.
|
|
110
|
+
*
|
|
111
|
+
* @param combo The combo as written, e.g. `"mod+shift+z"`.
|
|
112
|
+
* @param apple Whether the reader's keyboard carries the Apple modifier glyphs.
|
|
113
|
+
* @returns The caps to render, each already the glyph to print.
|
|
114
|
+
* @example keyComboGlyphs("mod+k", true) // ["⌘", "K"]
|
|
115
|
+
* @example keyComboGlyphs("mod+k", false) // ["Ctrl", "K"]
|
|
116
|
+
*/
|
|
117
|
+
export function keyComboGlyphs(combo, apple) {
|
|
118
|
+
let glyphs = apple ? APPLE_GLYPHS : OTHER_GLYPHS;
|
|
119
|
+
let tokens = combo
|
|
120
|
+
.split("+")
|
|
121
|
+
.map((token) => token.trim())
|
|
122
|
+
.filter((token) => token.length > 0);
|
|
123
|
+
if (tokens.length === 1 && tokens[0]?.length === 1)
|
|
124
|
+
return tokens;
|
|
125
|
+
return tokens.map((token) => {
|
|
126
|
+
let named = glyphs[token.toLowerCase()];
|
|
127
|
+
return named ?? (token.length === 1 ? token.toUpperCase() : token);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Whether a keystroke was aimed at a field the reader is typing in, where a bare letter
|
|
132
|
+
* shortcut would swallow the letter.
|
|
133
|
+
*
|
|
134
|
+
* @param target The keystroke's target.
|
|
135
|
+
* @returns Whether the target is, or sits inside, an editable control.
|
|
136
|
+
*/
|
|
137
|
+
export function isTypingTarget(target) {
|
|
138
|
+
if (target === null || typeof Element === "undefined" || !(target instanceof Element)) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return target.closest(TYPING_TARGET_SELECTOR) !== null;
|
|
142
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdxc/ui",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.9",
|
|
4
4
|
"description": "Styled, accessible remix/component components rendered as server HTML",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@sdxc/u": "2026.10.2",
|
|
22
22
|
"remix": "3.0.0"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "64fa732ed0a8ca8693a35aeef6345ae5be2534ef",
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|