keystra 0.0.0-stage → 0.1.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE +21 -0
  3. package/README.md +177 -2
  4. package/dist/chunk-2BLQUBG7.js +816 -0
  5. package/dist/chunk-77GGU3AX.js +135 -0
  6. package/dist/chunk-KDOJARJD.js +29 -0
  7. package/dist/chunk-LYYUOTBP.js +125 -0
  8. package/dist/chunk-S5YVXIMA.js +61 -0
  9. package/dist/chunk-SSIKGDD4.js +255 -0
  10. package/dist/chunk-U7YK6PPT.js +1729 -0
  11. package/dist/chunk-XFNYIPOW.js +253 -0
  12. package/dist/chunk-ZOJRXXQN.js +308 -0
  13. package/dist/core.cjs +2861 -0
  14. package/dist/core.d.cts +41 -0
  15. package/dist/core.d.ts +41 -0
  16. package/dist/core.js +7 -0
  17. package/dist/engine-COPfXXTp.d.ts +229 -0
  18. package/dist/engine-DcbkGRgz.d.cts +229 -0
  19. package/dist/focus.cjs +374 -0
  20. package/dist/focus.d.cts +74 -0
  21. package/dist/focus.d.ts +74 -0
  22. package/dist/focus.js +2 -0
  23. package/dist/index-IZkcWAXP.d.cts +395 -0
  24. package/dist/index-IZkcWAXP.d.ts +395 -0
  25. package/dist/index.cjs +3745 -0
  26. package/dist/index.d.cts +9 -0
  27. package/dist/index.d.ts +9 -0
  28. package/dist/index.global.js +1 -0
  29. package/dist/index.js +9 -0
  30. package/dist/layout-C2-n8aUI.d.ts +17 -0
  31. package/dist/layout-CBzD-87D.d.cts +17 -0
  32. package/dist/navigation.cjs +293 -0
  33. package/dist/navigation.d.cts +33 -0
  34. package/dist/navigation.d.ts +33 -0
  35. package/dist/navigation.js +2 -0
  36. package/dist/patterns.cjs +3505 -0
  37. package/dist/patterns.d.cts +12 -0
  38. package/dist/patterns.d.ts +12 -0
  39. package/dist/patterns.js +7 -0
  40. package/dist/roving-focus-CT7Dm6di.d.ts +63 -0
  41. package/dist/roving-focus-ZQ0v7hLe.d.cts +63 -0
  42. package/dist/selection.cjs +266 -0
  43. package/dist/selection.d.cts +43 -0
  44. package/dist/selection.d.ts +43 -0
  45. package/dist/selection.js +2 -0
  46. package/dist/utils-CkveVeIJ.d.cts +18 -0
  47. package/dist/utils-JmPZTlys.d.ts +18 -0
  48. package/dist/utils.cjs +195 -0
  49. package/dist/utils.d.cts +3 -0
  50. package/dist/utils.d.ts +3 -0
  51. package/dist/utils.js +2 -0
  52. package/package.json +147 -4
package/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ This project follows [Semantic Versioning](https://semver.org/).
4
+
5
+ ## [0.1.0] — 2026-11-02
6
+
7
+ First release.
8
+
9
+ - `createKeystra()` engine: key normalization and matching, modifier chords,
10
+ multi-key sequences, custom key maps and event delegation.
11
+ - Navigation modes: sequential, vertical, horizontal, grid, directional and
12
+ custom strategies, with looping and disabled and hidden item skipping.
13
+ - Focus management: roving tabindex, `aria-activedescendant`, focus restoration
14
+ and nested groups.
15
+ - Selection: single, multiple and range modes, independent of styling.
16
+ - Patterns: `createTabs`, `createMenu`, `createDropdown`, `createList`,
17
+ `createGrid`, `createTree`, `createCommandPalette` and `createRovingFocus`.
18
+ - `createKeyBinding()` for document-level shortcuts, and `useKeystra()` for
19
+ elements that do not exist yet.
20
+ - Dynamic DOM support through `refresh()`, explicit registration and optional
21
+ mutation observation.
22
+ - ESM, CommonJS and script-tag builds, TypeScript declarations and deep imports
23
+ such as `keystra/patterns`. No runtime dependencies.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zahid Hasan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,178 @@
1
- # Temporary Holding Version
1
+ # Keystra
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Framework-independent keyboard navigation, focus and selection for DOM interfaces. Keystra is an interaction engine, not a component library: you supply markup, styles, accessible names and application behavior. It has no runtime dependencies and ships ESM, CommonJS and TypeScript declarations.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install keystra
9
+ ```
10
+
11
+ ## Quick start
12
+
13
+ ```html
14
+ <div id="actions" aria-label="Actions">
15
+ <button data-keystra-item>Copy</button>
16
+ <button data-keystra-item>Move</button>
17
+ <button data-keystra-item disabled>Delete</button>
18
+ </div>
19
+ ```
20
+
21
+ ```ts
22
+ import { createKeystra } from 'keystra';
23
+
24
+ const nav = createKeystra({
25
+ container: '#actions',
26
+ mode: 'vertical',
27
+ loop: true,
28
+ onFocus: (element, index) => console.log('focus', index, element),
29
+ onSelect: (element) => console.log('activate', element),
30
+ });
31
+ nav.focusFirst();
32
+ // When the UI is removed:
33
+ nav.destroy();
34
+ ```
35
+
36
+ Items default to `[data-keystra-item]`; set `items` to a selector or resolver. The engine delegates events to its container, skips disabled and hidden items, and manages roving tabindex by default. Tab is not intercepted by default. It does not create UI or style selection.
37
+
38
+ Enter and Space are consumed only when something acts on them: `onSelect`, `onAction`, a selection manager or a pattern. Otherwise links and buttons keep their native activation. Escape is likewise left to the surrounding page, such as a dialog, unless `onEscape`, `onAction` or a pattern handles it. Subscribing with `nav.on(...)` does not count, so call `preventDefault()` on the event yourself if you need to.
39
+
40
+ ## Core API
41
+
42
+ `createKeystra(options)` returns an instance with `next()`, `previous()`, `focusFirst()`, `focusLast()`, `focusIndex(index)`, `focusElement(element)`, `move(direction)`, `perform(action)`, `refresh()`, `enable()`, `disable()`, `update(...)`, `restoreFocus()` and `destroy()`. Read `items`, `activeElement`, `activeIndex`, and `currentIndex`. `activeIndex` normally reflects DOM focus, while `currentIndex` retains the navigation cursor when focus leaves. With `activeDescendant`, focus remains on its owner and the cursor is exposed as the active index.
43
+
44
+ `selection: { enabled: true, mode: 'multiple' }` enables state independent of styling. Use `nav.selection` for `select`, `toggle`, `selectRange`, `selectAll`, `clear`, and `selected`; set `aria`, `attribute`, or `selectedClass` in selection options only if the widget needs them. Supported modes are `single`, `multiple`, `range`, and `none`.
45
+
46
+ Events can be subscribed with `nav.on('action', handler)` and removed with `nav.off(...)`. Options include `onFocus`, `onSelect`, `onAction`, `onEscape` and `onRefresh`. For dynamic items call `refresh()` after a change, or set `observe: true` to watch mutations; otherwise navigation revalidates the item collection lazily. `add`, `remove`, and `setItems` allow explicit registration. `update` supports `keys`, `loop`, `mode`, `columns`, `pageSize`, `enabled`, and `container`.
47
+
48
+ ## Navigation and keys
49
+
50
+ Modes include `vertical` (default), `horizontal`, `sequential`, `grid`, `directional`, and `custom`. Grid can use `columns` to avoid geometry reads; directional navigation and a grid without columns use element geometry. `loop` controls wraparound. Supply `strategy(context)` to replace the built-in strategy; return an item index or `null` to decline movement.
51
+
52
+ ```ts
53
+ const nav = createKeystra({
54
+ container: '#actions',
55
+ keys: { next: 'j', previous: 'k', first: 'Ctrl+Home' },
56
+ sequences: [{ binding: ['g', 'g'], action: 'first' }],
57
+ onAction: (action, event, element, index) => {
58
+ // Custom action names are forwarded here.
59
+ },
60
+ });
61
+ ```
62
+
63
+ Key maps override individual actions; `false` removes an action, and `useDefaultKeys: false` disables the whole default map. Modifiers, `KeyboardEvent.code`, sequences and custom actions are supported. Binding Tab or overriding keys in editable fields is opt-in. `createKeyBinding('Ctrl+K', handler)` provides a document-level shortcut with `disable`, `enable`, and `destroy`.
64
+
65
+ ## Patterns
66
+
67
+ `createTabs`, `createMenu`, `createDropdown`, `createList`, `createGrid`, `createTree`, `createCommandPalette`, and `createRovingFocus` provide pattern-specific defaults. For example:
68
+
69
+ ```ts
70
+ import { createTabs } from 'keystra/patterns';
71
+ const tabs = createTabs({ container: '#tabs', items: '[role="tab"]' });
72
+ // Later: tabs.activate(1); tabs.destroy();
73
+ ```
74
+
75
+ Menus and dropdowns remove a `hidden` attribute on open and restore it on close; pass `toggleHidden: false` to control visibility yourself. Pattern defaults can fill in missing ARIA roles and state; pass `aria: false` where supported to manage those yourself. Appropriate markup, roles, names, relationships and widget behavior remain the application's responsibility. Follow the [WAI-ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/) for the specific pattern and test with real browsers and assistive technology. A grid should have valid row/cell structure. For a combobox, `activeDescendant` keeps DOM focus on the input while moving its virtual cursor; ensure the active item has the proper ownership relationship. A command palette's search field should sit outside the listbox, so when it lives inside `container`, point `list` at the element that holds the results.
76
+
77
+ ## TypeScript and frameworks
78
+
79
+ ```ts
80
+ import { createKeystra, type KeystraOptions } from 'keystra';
81
+ const options: KeystraOptions<HTMLButtonElement> = {
82
+ container: '#actions', items: 'button', loop: true,
83
+ };
84
+ const nav = createKeystra<HTMLButtonElement>(options);
85
+ ```
86
+
87
+ Keystra only needs a DOM element, so every framework integration has the same shape: create an instance when the element is mounted and destroy it on unmount. These examples show that lifecycle; they have not been run inside each framework.
88
+
89
+ ```tsx
90
+ // React
91
+ function Actions() {
92
+ const ref = useRef<HTMLDivElement>(null);
93
+ useEffect(() => {
94
+ const nav = createKeystra({ container: ref.current, items: 'button' });
95
+ return () => nav.destroy();
96
+ }, []);
97
+ return <div ref={ref}>...</div>;
98
+ }
99
+ ```
100
+
101
+ ```ts
102
+ // Vue, in <script setup>
103
+ const root = ref<HTMLElement>();
104
+ let nav: KeystraInstance;
105
+ onMounted(() => { nav = createKeystra({ container: root.value, items: 'button' }); });
106
+ onBeforeUnmount(() => nav.destroy());
107
+ ```
108
+
109
+ ```ts
110
+ // Svelte
111
+ let root: HTMLElement;
112
+ onMount(() => {
113
+ const nav = createKeystra({ container: root, items: 'button' });
114
+ return () => nav.destroy();
115
+ });
116
+ ```
117
+
118
+ ```ts
119
+ // Angular
120
+ ngAfterViewInit() { this.nav = createKeystra({ container: this.root.nativeElement, items: 'button' }); }
121
+ ngOnDestroy() { this.nav?.destroy(); }
122
+ ```
123
+
124
+ ## Cleanup and lifecycle
125
+
126
+ `destroy()` removes every listener and observer and puts `tabindex` and `aria-activedescendant` back as they were. Call it once, when the UI goes away; the instance cannot be reused. `disable()` and `enable()` pause an instance and resume it.
127
+
128
+ The `useKeystra` helper suits an element that does not exist yet: `attach(element)` creates the instance on first use, `detach()` stops listening and allows a later `attach`, and `destroy()` is permanent. Despite the name it is not a React hook and is safe to call anywhere. When reusing a handle in development, account for framework lifecycle replay.
129
+
130
+ Groups can be nested. Items inside an inner group's container belong to that group, even when the outer selector matches them, and keys the inner group does not bind bubble to the outer one.
131
+
132
+ ## Accessibility and browser support
133
+
134
+ Keystra does not make arbitrary markup accessible automatically. Supply valid native elements or appropriate ARIA, accessible labels and state; preserve visible focus and check keyboard behavior with screen readers. Native behavior is preserved for unmatched keys; configure bindings carefully, particularly for text inputs. A chord does not claim a longer one: `Ctrl+S` ignores `Ctrl+Shift+S`. Hidden items are detected with `checkVisibility()`, so stylesheet changes are seen on the next key press without `refresh()`. Keystra's focus behavior is tested in Chrome only, and it has not been tested with screen readers; validate both in your target environments.
135
+
136
+ Targets modern browsers with ES2020, DOM and standard event APIs; optional mutation observation needs `MutationObserver`. Importing modules in Node does not require DOM globals, but interaction requires a DOM. Working on Keystra itself needs Node 22.12+, because of Vitest 5.
137
+
138
+ ## Size and benchmarks
139
+
140
+ `npm run size` measured these consumer bundles. Unused patterns are tree-shaken, but `createKeystra` always includes every navigation mode, selection and typeahead.
141
+
142
+ | Import | Minified | Gzip |
143
+ | --- | --- | --- |
144
+ | `createKeyBinding` | 10.22 kB | 3.79 kB |
145
+ | `createKeystra` | 45.21 kB | 13.27 kB |
146
+ | `createRovingFocus` | 45.48 kB | 13.38 kB |
147
+ | Everything | 66.29 kB | 18.55 kB |
148
+
149
+ `npm run bench` compares roving focus and shortcuts in real Chrome. These are median milliseconds per key press, measured on one Windows machine:
150
+
151
+ | Items | Native baseline | roving-ux | Keystra | Keystra with `observe: true` |
152
+ | --- | --- | --- | --- | --- |
153
+ | 100 | 0.1 | 0.1 | 0.4 | 0.2 |
154
+ | 1,000 | 0.1 | 0.1 | 3.0 | 0.2 |
155
+ | 10,000 | 0.1 | 0.1 | 21.4 | 0.2 |
156
+
157
+ The comparison shows the cost of features, not a winner. The baseline and roving-ux (0.69 kB gzip) never re-read the DOM, so they miss added, disabled or hidden items, while Keystra checks them on every press. With `observe: true` a `MutationObserver` does that work instead, so use it for lists of about 1,000 items or more. Startup is also slower: 8.7 ms for 1,000 items against 0.8 ms for roving-ux, and a 10,000-item group retains about 420 kB.
158
+
159
+ For 20 shortcuts, a press took 68 µs with `createKeyBinding`, 34 µs with tinykeys (0.93 kB gzip) and 37 µs with hotkeys-js (3.6 kB gzip). If shortcuts are all you need, those libraries are smaller and faster. Keystra's case is one engine for navigation, focus and selection, not the smallest size for any single job.
160
+
161
+ ## Development
162
+
163
+ ```sh
164
+ npm install
165
+ npm run typecheck
166
+ npm test
167
+ npm run build
168
+ npm run test:browser
169
+ npm run size
170
+ npm run bench
171
+ npm run verify
172
+ ```
173
+
174
+ `npm test` runs in happy-dom. `npm run test:browser` and `npm run bench` drive the Chrome installed on your machine, which covers native activation, Tab order and stylesheet visibility.
175
+
176
+ ## License
177
+
178
+ MIT — see [LICENSE](LICENSE).