@pithyjs/core 0.1.0-beta.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.
- package/LICENSE +21 -0
- package/README.md +513 -0
- package/dist/a11y/focus-trap.d.ts +109 -0
- package/dist/a11y/focus-trap.d.ts.map +1 -0
- package/dist/a11y/focus-trap.js +246 -0
- package/dist/a11y/focus-trap.js.map +1 -0
- package/dist/a11y/index.d.ts +12 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/index.js +16 -0
- package/dist/a11y/index.js.map +1 -0
- package/dist/a11y/keyboard-utils.d.ts +165 -0
- package/dist/a11y/keyboard-utils.d.ts.map +1 -0
- package/dist/a11y/keyboard-utils.js +321 -0
- package/dist/a11y/keyboard-utils.js.map +1 -0
- package/dist/a11y/roving-tabindex.d.ts +118 -0
- package/dist/a11y/roving-tabindex.d.ts.map +1 -0
- package/dist/a11y/roving-tabindex.js +249 -0
- package/dist/a11y/roving-tabindex.js.map +1 -0
- package/dist/a11y/skip-link.d.ts +55 -0
- package/dist/a11y/skip-link.d.ts.map +1 -0
- package/dist/a11y/skip-link.js +164 -0
- package/dist/a11y/skip-link.js.map +1 -0
- package/dist/compile/bindSwitchCases.d.ts +16 -0
- package/dist/compile/bindSwitchCases.d.ts.map +1 -0
- package/dist/compile/bindSwitchCases.js +323 -0
- package/dist/compile/bindSwitchCases.js.map +1 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts +14 -0
- package/dist/compile/binds/bindBoundAttributes.d.ts.map +1 -0
- package/dist/compile/binds/bindBoundAttributes.js +57 -0
- package/dist/compile/binds/bindBoundAttributes.js.map +1 -0
- package/dist/compile/binds/bindClassDirectives.d.ts +20 -0
- package/dist/compile/binds/bindClassDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindClassDirectives.js +94 -0
- package/dist/compile/binds/bindClassDirectives.js.map +1 -0
- package/dist/compile/binds/bindComponentsTags.d.ts +17 -0
- package/dist/compile/binds/bindComponentsTags.d.ts.map +1 -0
- package/dist/compile/binds/bindComponentsTags.js +355 -0
- package/dist/compile/binds/bindComponentsTags.js.map +1 -0
- package/dist/compile/binds/bindConditionals.d.ts +16 -0
- package/dist/compile/binds/bindConditionals.d.ts.map +1 -0
- package/dist/compile/binds/bindConditionals.js +102 -0
- package/dist/compile/binds/bindConditionals.js.map +1 -0
- package/dist/compile/binds/bindDefaultTwoWay.d.ts +35 -0
- package/dist/compile/binds/bindDefaultTwoWay.d.ts.map +1 -0
- package/dist/compile/binds/bindDefaultTwoWay.js +128 -0
- package/dist/compile/binds/bindDefaultTwoWay.js.map +1 -0
- package/dist/compile/binds/bindDeferDirectives.d.ts +21 -0
- package/dist/compile/binds/bindDeferDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindDeferDirectives.js +465 -0
- package/dist/compile/binds/bindDeferDirectives.js.map +1 -0
- package/dist/compile/binds/bindErrorBoundary.d.ts +71 -0
- package/dist/compile/binds/bindErrorBoundary.d.ts.map +1 -0
- package/dist/compile/binds/bindErrorBoundary.js +389 -0
- package/dist/compile/binds/bindErrorBoundary.js.map +1 -0
- package/dist/compile/binds/bindEventHandlers.d.ts +17 -0
- package/dist/compile/binds/bindEventHandlers.d.ts.map +1 -0
- package/dist/compile/binds/bindEventHandlers.js +174 -0
- package/dist/compile/binds/bindEventHandlers.js.map +1 -0
- package/dist/compile/binds/bindRefDirectives.d.ts +14 -0
- package/dist/compile/binds/bindRefDirectives.d.ts.map +1 -0
- package/dist/compile/binds/bindRefDirectives.js +35 -0
- package/dist/compile/binds/bindRefDirectives.js.map +1 -0
- package/dist/compile/binds/bindTextInterpolations.d.ts +16 -0
- package/dist/compile/binds/bindTextInterpolations.d.ts.map +1 -0
- package/dist/compile/binds/bindTextInterpolations.js +408 -0
- package/dist/compile/binds/bindTextInterpolations.js.map +1 -0
- package/dist/compile/binds/directiveRegistry.d.ts +46 -0
- package/dist/compile/binds/directiveRegistry.d.ts.map +1 -0
- package/dist/compile/binds/directiveRegistry.js +50 -0
- package/dist/compile/binds/directiveRegistry.js.map +1 -0
- package/dist/compile/compileExpression.d.ts +10 -0
- package/dist/compile/compileExpression.d.ts.map +1 -0
- package/dist/compile/compileExpression.js +26 -0
- package/dist/compile/compileExpression.js.map +1 -0
- package/dist/compile/compileNodes.d.ts +32 -0
- package/dist/compile/compileNodes.d.ts.map +1 -0
- package/dist/compile/compileNodes.js +104 -0
- package/dist/compile/compileNodes.js.map +1 -0
- package/dist/compile/compileTemplate.d.ts +64 -0
- package/dist/compile/compileTemplate.d.ts.map +1 -0
- package/dist/compile/compileTemplate.js +157 -0
- package/dist/compile/compileTemplate.js.map +1 -0
- package/dist/compile/contentProjection.d.ts +29 -0
- package/dist/compile/contentProjection.d.ts.map +1 -0
- package/dist/compile/contentProjection.js +210 -0
- package/dist/compile/contentProjection.js.map +1 -0
- package/dist/compile/forLoops/bindForLoops.d.ts +21 -0
- package/dist/compile/forLoops/bindForLoops.d.ts.map +1 -0
- package/dist/compile/forLoops/bindForLoops.js +151 -0
- package/dist/compile/forLoops/bindForLoops.js.map +1 -0
- package/dist/compile/forLoops/createLoopManager.d.ts +51 -0
- package/dist/compile/forLoops/createLoopManager.d.ts.map +1 -0
- package/dist/compile/forLoops/createLoopManager.js +429 -0
- package/dist/compile/forLoops/createLoopManager.js.map +1 -0
- package/dist/compile/forLoops/index.d.ts +6 -0
- package/dist/compile/forLoops/index.d.ts.map +1 -0
- package/dist/compile/forLoops/index.js +8 -0
- package/dist/compile/forLoops/index.js.map +1 -0
- package/dist/compile/forLoops/loopHelpers.d.ts +33 -0
- package/dist/compile/forLoops/loopHelpers.d.ts.map +1 -0
- package/dist/compile/forLoops/loopHelpers.js +69 -0
- package/dist/compile/forLoops/loopHelpers.js.map +1 -0
- package/dist/compile/forLoops/loopSignals.d.ts +36 -0
- package/dist/compile/forLoops/loopSignals.d.ts.map +1 -0
- package/dist/compile/forLoops/loopSignals.js +64 -0
- package/dist/compile/forLoops/loopSignals.js.map +1 -0
- package/dist/compile/forLoops/loopTraversal.d.ts +18 -0
- package/dist/compile/forLoops/loopTraversal.d.ts.map +1 -0
- package/dist/compile/forLoops/loopTraversal.js +38 -0
- package/dist/compile/forLoops/loopTraversal.js.map +1 -0
- package/dist/compile/handleConditionalChain.d.ts +15 -0
- package/dist/compile/handleConditionalChain.d.ts.map +1 -0
- package/dist/compile/handleConditionalChain.js +181 -0
- package/dist/compile/handleConditionalChain.js.map +1 -0
- package/dist/compile/index.d.ts +15 -0
- package/dist/compile/index.d.ts.map +1 -0
- package/dist/compile/index.js +19 -0
- package/dist/compile/index.js.map +1 -0
- package/dist/compile/injectScopedStyle.d.ts +16 -0
- package/dist/compile/injectScopedStyle.d.ts.map +1 -0
- package/dist/compile/injectScopedStyle.js +50 -0
- package/dist/compile/injectScopedStyle.js.map +1 -0
- package/dist/compile/nodeExpando.d.ts +12 -0
- package/dist/compile/nodeExpando.d.ts.map +1 -0
- package/dist/compile/nodeExpando.js +4 -0
- package/dist/compile/nodeExpando.js.map +1 -0
- package/dist/compile/parseHtml.d.ts +70 -0
- package/dist/compile/parseHtml.d.ts.map +1 -0
- package/dist/compile/parseHtml.js +164 -0
- package/dist/compile/parseHtml.js.map +1 -0
- package/dist/compile/pithyTreeToDom.d.ts +16 -0
- package/dist/compile/pithyTreeToDom.d.ts.map +1 -0
- package/dist/compile/pithyTreeToDom.js +118 -0
- package/dist/compile/pithyTreeToDom.js.map +1 -0
- package/dist/compile/renderChildren.d.ts +50 -0
- package/dist/compile/renderChildren.d.ts.map +1 -0
- package/dist/compile/renderChildren.js +76 -0
- package/dist/compile/renderChildren.js.map +1 -0
- package/dist/compile/ssrMode.d.ts +17 -0
- package/dist/compile/ssrMode.d.ts.map +1 -0
- package/dist/compile/ssrMode.js +20 -0
- package/dist/compile/ssrMode.js.map +1 -0
- package/dist/compile/textValue.d.ts +28 -0
- package/dist/compile/textValue.d.ts.map +1 -0
- package/dist/compile/textValue.js +30 -0
- package/dist/compile/textValue.js.map +1 -0
- package/dist/config/index.d.ts +37 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +16 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/ref-signal.interface.d.ts +6 -0
- package/dist/interfaces/ref-signal.interface.d.ts.map +1 -0
- package/dist/interfaces/ref-signal.interface.js +2 -0
- package/dist/interfaces/ref-signal.interface.js.map +1 -0
- package/dist/interfaces/ref.interface.d.ts +5 -0
- package/dist/interfaces/ref.interface.d.ts.map +1 -0
- package/dist/interfaces/ref.interface.js +2 -0
- package/dist/interfaces/ref.interface.js.map +1 -0
- package/dist/runtime/applyBindings.d.ts +36 -0
- package/dist/runtime/applyBindings.d.ts.map +1 -0
- package/dist/runtime/applyBindings.js +165 -0
- package/dist/runtime/applyBindings.js.map +1 -0
- package/dist/runtime/bindings/applyComponent.d.ts +3 -0
- package/dist/runtime/bindings/applyComponent.d.ts.map +1 -0
- package/dist/runtime/bindings/applyComponent.js +190 -0
- package/dist/runtime/bindings/applyComponent.js.map +1 -0
- package/dist/runtime/bindings/applyDefer.d.ts +11 -0
- package/dist/runtime/bindings/applyDefer.d.ts.map +1 -0
- package/dist/runtime/bindings/applyDefer.js +422 -0
- package/dist/runtime/bindings/applyDefer.js.map +1 -0
- package/dist/runtime/bindings/applySimple.d.ts +11 -0
- package/dist/runtime/bindings/applySimple.d.ts.map +1 -0
- package/dist/runtime/bindings/applySimple.js +346 -0
- package/dist/runtime/bindings/applySimple.js.map +1 -0
- package/dist/runtime/bindings/applyStructural.d.ts +5 -0
- package/dist/runtime/bindings/applyStructural.d.ts.map +1 -0
- package/dist/runtime/bindings/applyStructural.js +312 -0
- package/dist/runtime/bindings/applyStructural.js.map +1 -0
- package/dist/runtime/bindings/helpers.d.ts +56 -0
- package/dist/runtime/bindings/helpers.d.ts.map +1 -0
- package/dist/runtime/bindings/helpers.js +121 -0
- package/dist/runtime/bindings/helpers.js.map +1 -0
- package/dist/runtime/bindings/index.d.ts +7 -0
- package/dist/runtime/bindings/index.d.ts.map +1 -0
- package/dist/runtime/bindings/index.js +6 -0
- package/dist/runtime/bindings/index.js.map +1 -0
- package/dist/runtime/bindings/types.d.ts +105 -0
- package/dist/runtime/bindings/types.d.ts.map +1 -0
- package/dist/runtime/bindings/types.js +5 -0
- package/dist/runtime/bindings/types.js.map +1 -0
- package/dist/runtime/bootstrap.d.ts +59 -0
- package/dist/runtime/bootstrap.d.ts.map +1 -0
- package/dist/runtime/bootstrap.js +82 -0
- package/dist/runtime/bootstrap.js.map +1 -0
- package/dist/runtime/createContextProxy.d.ts +10 -0
- package/dist/runtime/createContextProxy.d.ts.map +1 -0
- package/dist/runtime/createContextProxy.js +45 -0
- package/dist/runtime/createContextProxy.js.map +1 -0
- package/dist/runtime/deferHelpers.d.ts +49 -0
- package/dist/runtime/deferHelpers.d.ts.map +1 -0
- package/dist/runtime/deferHelpers.js +65 -0
- package/dist/runtime/deferHelpers.js.map +1 -0
- package/dist/runtime/html.d.ts +23 -0
- package/dist/runtime/html.d.ts.map +1 -0
- package/dist/runtime/html.js +97 -0
- package/dist/runtime/html.js.map +1 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +21 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/lazy.d.ts +144 -0
- package/dist/runtime/lazy.d.ts.map +1 -0
- package/dist/runtime/lazy.js +295 -0
- package/dist/runtime/lazy.js.map +1 -0
- package/dist/runtime/module-cache.d.ts +78 -0
- package/dist/runtime/module-cache.d.ts.map +1 -0
- package/dist/runtime/module-cache.js +109 -0
- package/dist/runtime/module-cache.js.map +1 -0
- package/dist/runtime/onDestroy.d.ts +29 -0
- package/dist/runtime/onDestroy.d.ts.map +1 -0
- package/dist/runtime/onDestroy.js +66 -0
- package/dist/runtime/onDestroy.js.map +1 -0
- package/dist/runtime/parseIdentifiersFromTemplate.d.ts +17 -0
- package/dist/runtime/parseIdentifiersFromTemplate.d.ts.map +1 -0
- package/dist/runtime/parseIdentifiersFromTemplate.js +68 -0
- package/dist/runtime/parseIdentifiersFromTemplate.js.map +1 -0
- package/dist/types/globals.d.ts +18 -0
- package/dist/types/globals.d.ts.map +1 -0
- package/dist/types/globals.js +4 -0
- package/dist/types/globals.js.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utilities/index.d.ts +3 -0
- package/dist/utilities/index.d.ts.map +1 -0
- package/dist/utilities/index.js +3 -0
- package/dist/utilities/index.js.map +1 -0
- package/dist/utilities/ref-signal.utility.d.ts +32 -0
- package/dist/utilities/ref-signal.utility.d.ts.map +1 -0
- package/dist/utilities/ref-signal.utility.js +46 -0
- package/dist/utilities/ref-signal.utility.js.map +1 -0
- package/dist/utilities/ref.untility.d.ts +37 -0
- package/dist/utilities/ref.untility.d.ts.map +1 -0
- package/dist/utilities/ref.untility.js +47 -0
- package/dist/utilities/ref.untility.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.a11y.focus-trap",
|
|
4
|
+
"title": "Focus Trap Utility - Accessible Modal/Dialog Support",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"FocusTrap","stability":"stable","signature":"class FocusTrap"} */
|
|
9
|
+
/**
|
|
10
|
+
* Focus Trap Instance
|
|
11
|
+
* Manages focus containment within an element for accessible modals/dialogs
|
|
12
|
+
*/
|
|
13
|
+
export class FocusTrap {
|
|
14
|
+
constructor(element, options = {}) {
|
|
15
|
+
this.isActive = false;
|
|
16
|
+
this.previouslyFocusedElement = null;
|
|
17
|
+
this.focusableElements = [];
|
|
18
|
+
// Event handlers stored for cleanup
|
|
19
|
+
this.keydownHandler = null;
|
|
20
|
+
this.clickHandler = null;
|
|
21
|
+
this.element = element;
|
|
22
|
+
this.options = {
|
|
23
|
+
initialFocus: options.initialFocus ?? undefined,
|
|
24
|
+
returnFocus: options.returnFocus ?? undefined,
|
|
25
|
+
clickOutsideDeactivates: options.clickOutsideDeactivates ?? false,
|
|
26
|
+
escapeDeactivates: options.escapeDeactivates ?? true,
|
|
27
|
+
onActivate: options.onActivate ?? (() => { }),
|
|
28
|
+
onDeactivate: options.onDeactivate ?? (() => { }),
|
|
29
|
+
preventScroll: options.preventScroll ?? true,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"activate","stability":"stable","signature":"activate(): void"} */
|
|
33
|
+
/**
|
|
34
|
+
* Activate the focus trap
|
|
35
|
+
* Traps keyboard focus within the element
|
|
36
|
+
*/
|
|
37
|
+
activate() {
|
|
38
|
+
if (this.isActive)
|
|
39
|
+
return;
|
|
40
|
+
// Store currently focused element to return to later
|
|
41
|
+
this.previouslyFocusedElement = document.activeElement;
|
|
42
|
+
// Update focusable elements list
|
|
43
|
+
this.updateFocusableElements();
|
|
44
|
+
// Set up keyboard navigation
|
|
45
|
+
this.keydownHandler = (e) => this.handleKeydown(e);
|
|
46
|
+
document.addEventListener('keydown', this.keydownHandler);
|
|
47
|
+
// Set up click outside handler if enabled
|
|
48
|
+
if (this.options.clickOutsideDeactivates) {
|
|
49
|
+
this.clickHandler = (e) => this.handleClickOutside(e);
|
|
50
|
+
document.addEventListener('click', this.clickHandler, true);
|
|
51
|
+
}
|
|
52
|
+
// Prevent body scroll if enabled
|
|
53
|
+
if (this.options.preventScroll) {
|
|
54
|
+
document.body.style.overflow = 'hidden';
|
|
55
|
+
}
|
|
56
|
+
// Focus initial element
|
|
57
|
+
this.focusInitialElement();
|
|
58
|
+
this.isActive = true;
|
|
59
|
+
this.options.onActivate();
|
|
60
|
+
}
|
|
61
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"deactivate","stability":"stable","signature":"deactivate(): void"} */
|
|
62
|
+
/**
|
|
63
|
+
* Deactivate the focus trap
|
|
64
|
+
* Returns focus to the previously focused element
|
|
65
|
+
*/
|
|
66
|
+
deactivate() {
|
|
67
|
+
if (!this.isActive)
|
|
68
|
+
return;
|
|
69
|
+
// Remove event listeners
|
|
70
|
+
if (this.keydownHandler) {
|
|
71
|
+
document.removeEventListener('keydown', this.keydownHandler);
|
|
72
|
+
this.keydownHandler = null;
|
|
73
|
+
}
|
|
74
|
+
if (this.clickHandler) {
|
|
75
|
+
document.removeEventListener('click', this.clickHandler, true);
|
|
76
|
+
this.clickHandler = null;
|
|
77
|
+
}
|
|
78
|
+
// Restore body scroll
|
|
79
|
+
if (this.options.preventScroll) {
|
|
80
|
+
document.body.style.overflow = '';
|
|
81
|
+
}
|
|
82
|
+
// Return focus to previously focused element
|
|
83
|
+
const returnTarget = this.getReturnFocusTarget();
|
|
84
|
+
if (returnTarget && returnTarget.focus) {
|
|
85
|
+
returnTarget.focus();
|
|
86
|
+
}
|
|
87
|
+
this.isActive = false;
|
|
88
|
+
this.options.onDeactivate();
|
|
89
|
+
}
|
|
90
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"updateFocusableElements","stability":"stable","signature":"updateFocusableElements(): void"} */
|
|
91
|
+
/**
|
|
92
|
+
* Update the list of focusable elements
|
|
93
|
+
* Call this if the DOM within the trap changes
|
|
94
|
+
*/
|
|
95
|
+
updateFocusableElements() {
|
|
96
|
+
const elements = Array.from(this.element.querySelectorAll(FocusTrap.FOCUSABLE_SELECTOR));
|
|
97
|
+
// Filter out hidden or disabled elements
|
|
98
|
+
this.focusableElements = elements.filter(el => {
|
|
99
|
+
return (el.offsetWidth > 0 &&
|
|
100
|
+
el.offsetHeight > 0 &&
|
|
101
|
+
!el.hasAttribute('disabled') &&
|
|
102
|
+
window.getComputedStyle(el).visibility !== 'hidden');
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the first focusable element in the trap
|
|
107
|
+
*/
|
|
108
|
+
getFirstFocusableElement() {
|
|
109
|
+
return this.focusableElements[0] || null;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get the last focusable element in the trap
|
|
113
|
+
*/
|
|
114
|
+
getLastFocusableElement() {
|
|
115
|
+
return this.focusableElements[this.focusableElements.length - 1] || null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Focus the initial element based on options
|
|
119
|
+
*/
|
|
120
|
+
focusInitialElement() {
|
|
121
|
+
let target = null;
|
|
122
|
+
// Check if initialFocus is specified
|
|
123
|
+
if (this.options.initialFocus) {
|
|
124
|
+
if (typeof this.options.initialFocus === 'string') {
|
|
125
|
+
target = this.element.querySelector(this.options.initialFocus);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
target = this.options.initialFocus;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Fallback to first focusable element
|
|
132
|
+
if (!target) {
|
|
133
|
+
target = this.getFirstFocusableElement();
|
|
134
|
+
}
|
|
135
|
+
// Fallback to the container itself
|
|
136
|
+
if (!target) {
|
|
137
|
+
target = this.element;
|
|
138
|
+
target.setAttribute('tabindex', '-1');
|
|
139
|
+
}
|
|
140
|
+
if (target && target.focus) {
|
|
141
|
+
target.focus();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Get the element to return focus to
|
|
146
|
+
*/
|
|
147
|
+
getReturnFocusTarget() {
|
|
148
|
+
if (this.options.returnFocus) {
|
|
149
|
+
if (typeof this.options.returnFocus === 'string') {
|
|
150
|
+
return document.querySelector(this.options.returnFocus);
|
|
151
|
+
}
|
|
152
|
+
return this.options.returnFocus;
|
|
153
|
+
}
|
|
154
|
+
return this.previouslyFocusedElement;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Handle keydown events for focus trapping
|
|
158
|
+
*/
|
|
159
|
+
handleKeydown(e) {
|
|
160
|
+
// Handle Escape key
|
|
161
|
+
if (e.key === 'Escape' && this.options.escapeDeactivates) {
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
e.stopPropagation();
|
|
164
|
+
this.deactivate();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Handle Tab key
|
|
168
|
+
if (e.key === 'Tab') {
|
|
169
|
+
this.handleTabKey(e);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Handle Tab key for focus cycling
|
|
174
|
+
*/
|
|
175
|
+
handleTabKey(e) {
|
|
176
|
+
const firstElement = this.getFirstFocusableElement();
|
|
177
|
+
const lastElement = this.getLastFocusableElement();
|
|
178
|
+
if (!firstElement || !lastElement) {
|
|
179
|
+
// No focusable elements, prevent tabbing out
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// Shift+Tab on first element -> focus last
|
|
184
|
+
if (e.shiftKey && document.activeElement === firstElement) {
|
|
185
|
+
e.preventDefault();
|
|
186
|
+
lastElement.focus();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
// Tab on last element -> focus first
|
|
190
|
+
if (!e.shiftKey && document.activeElement === lastElement) {
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
firstElement.focus();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Handle clicks outside the trapped element
|
|
198
|
+
*/
|
|
199
|
+
handleClickOutside(e) {
|
|
200
|
+
if (!this.element.contains(e.target)) {
|
|
201
|
+
this.deactivate();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"isActivated","stability":"stable","signature":"isActivated(): boolean"} */
|
|
205
|
+
/**
|
|
206
|
+
* Check if the trap is currently active
|
|
207
|
+
*/
|
|
208
|
+
isActivated() {
|
|
209
|
+
return this.isActive;
|
|
210
|
+
}
|
|
211
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"destroy","stability":"stable","signature":"destroy(): void"} */
|
|
212
|
+
/**
|
|
213
|
+
* Destroy the focus trap and clean up resources
|
|
214
|
+
*/
|
|
215
|
+
destroy() {
|
|
216
|
+
this.deactivate();
|
|
217
|
+
this.focusableElements = [];
|
|
218
|
+
this.previouslyFocusedElement = null;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Selector for focusable elements
|
|
223
|
+
* Based on what browsers consider naturally focusable
|
|
224
|
+
*/
|
|
225
|
+
FocusTrap.FOCUSABLE_SELECTOR = [
|
|
226
|
+
'a[href]',
|
|
227
|
+
'area[href]',
|
|
228
|
+
'input:not([disabled])',
|
|
229
|
+
'select:not([disabled])',
|
|
230
|
+
'textarea:not([disabled])',
|
|
231
|
+
'button:not([disabled])',
|
|
232
|
+
'iframe',
|
|
233
|
+
'object',
|
|
234
|
+
'embed',
|
|
235
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
236
|
+
'[contenteditable="true"]',
|
|
237
|
+
].join(',');
|
|
238
|
+
/** @codexApi {"parent":"pithy.a11y.focus-trap","name":"createFocusTrap","stability":"stable","signature":"(element: HTMLElement, options?: FocusTrapOptions) => FocusTrap"} */
|
|
239
|
+
/**
|
|
240
|
+
* Create a focus trap for an element
|
|
241
|
+
* Convenience function for creating and activating a focus trap
|
|
242
|
+
*/
|
|
243
|
+
export function createFocusTrap(element, options) {
|
|
244
|
+
return new FocusTrap(element, options);
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=focus-trap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus-trap.js","sourceRoot":"","sources":["../../src/a11y/focus-trap.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C,CAAC;;AA5PD;;;GAGG;AACqB,4BAAkB,GAAG;IAC3C,SAAS;IACT,YAAY;IACZ,uBAAuB;IACvB,wBAAwB;IACxB,0BAA0B;IAC1B,wBAAwB;IACxB,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,iCAAiC;IACjC,0BAA0B;CAC3B,CAAC,IAAI,CAAC,GAAG,CAAC,AAZ+B,CAY9B;AA+Od,+KAA+K;AAC/K;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAoB,EACpB,OAA0B;IAE1B,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.a11y",
|
|
4
|
+
"title": "Accessibility Utilities - WCAG 2.1 AA+ Support",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
export { FocusTrap, createFocusTrap, type FocusTrapOptions, } from './focus-trap';
|
|
9
|
+
export { SkipLink, SkipLinks, type SkipLinkOptions, type SkipLinkResult, } from './skip-link';
|
|
10
|
+
export { RovingTabindex, createRovingTabindex, type RovingTabindexOptions, } from './roving-tabindex';
|
|
11
|
+
export { isKey, isActivationKey, onEscape, onEnter, onSpace, onActivation, onArrowKeys, hasModifierKeys, onKeyboardShortcut, makeKeyboardAccessible, type ArrowKeyHandlers, type KeyboardHandler, type KeyboardHandlerOptions, } from './keyboard-utils';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/a11y/index.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAGF,OAAO,EACL,SAAS,EACT,eAAe,EACf,KAAK,gBAAgB,GACtB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,QAAQ,EACR,SAAS,EACT,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,KAAK,qBAAqB,GAC3B,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,KAAK,EACL,eAAe,EACf,QAAQ,EACR,OAAO,EACP,OAAO,EACP,YAAY,EACZ,WAAW,EACX,eAAe,EACf,kBAAkB,EAClB,sBAAsB,EACtB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,sBAAsB,GAC5B,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.a11y",
|
|
4
|
+
"title": "Accessibility Utilities - WCAG 2.1 AA+ Support",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
// Focus Trap
|
|
9
|
+
export { FocusTrap, createFocusTrap, } from './focus-trap';
|
|
10
|
+
// Skip Link
|
|
11
|
+
export { SkipLink, SkipLinks, } from './skip-link';
|
|
12
|
+
// Roving Tabindex
|
|
13
|
+
export { RovingTabindex, createRovingTabindex, } from './roving-tabindex';
|
|
14
|
+
// Keyboard Utilities
|
|
15
|
+
export { isKey, isActivationKey, onEscape, onEnter, onSpace, onActivation, onArrowKeys, hasModifierKeys, onKeyboardShortcut, makeKeyboardAccessible, } from './keyboard-utils';
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/a11y/index.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF,aAAa;AACb,OAAO,EACL,SAAS,EACT,eAAe,GAEhB,MAAM,cAAc,CAAC;AAEtB,YAAY;AACZ,OAAO,EACL,QAAQ,EACR,SAAS,GAGV,MAAM,aAAa,CAAC;AAErB,kBAAkB;AAClB,OAAO,EACL,cAAc,EACd,oBAAoB,GAErB,MAAM,mBAAmB,CAAC;AAE3B,qBAAqB;AACrB,OAAO,EACL,KAAK,EACL,eAAe,EACf,QAAQ,EACR,OAAO,EACP,OAAO,EACP,YAAY,EACZ,WAAW,EACX,eAAe,EACf,kBAAkB,EAClB,sBAAsB,GAIvB,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/** @codex
|
|
2
|
+
{
|
|
3
|
+
"id": "pithy.a11y.keyboard-utils",
|
|
4
|
+
"title": "Keyboard Event Utilities - Common Keyboard Patterns",
|
|
5
|
+
"category": "feature"
|
|
6
|
+
}
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Keyboard Event Handler Options
|
|
10
|
+
*/
|
|
11
|
+
export interface KeyboardHandlerOptions {
|
|
12
|
+
/** Prevent default browser behavior (default: true) */
|
|
13
|
+
preventDefault?: boolean;
|
|
14
|
+
/** Stop event propagation (default: false) */
|
|
15
|
+
stopPropagation?: boolean;
|
|
16
|
+
/** Only trigger if no modifier keys are pressed (default: false) */
|
|
17
|
+
noModifiers?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Keyboard Event Handler
|
|
21
|
+
*/
|
|
22
|
+
export type KeyboardHandler = (event: KeyboardEvent) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Arrow Key Handler Map
|
|
25
|
+
*/
|
|
26
|
+
export interface ArrowKeyHandlers {
|
|
27
|
+
up?: KeyboardHandler;
|
|
28
|
+
down?: KeyboardHandler;
|
|
29
|
+
left?: KeyboardHandler;
|
|
30
|
+
right?: KeyboardHandler;
|
|
31
|
+
}
|
|
32
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"isKey","stability":"stable","signature":"(event: KeyboardEvent, key: string) => boolean"} */
|
|
33
|
+
/**
|
|
34
|
+
* Check if a keyboard event matches a specific key
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* element.addEventListener('keydown', (e) => {
|
|
39
|
+
* if (isKey(e, 'Escape')) {
|
|
40
|
+
* closeModal();
|
|
41
|
+
* }
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function isKey(event: KeyboardEvent, key: string): boolean;
|
|
46
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"isActivationKey","stability":"stable","signature":"(event: KeyboardEvent) => boolean"} */
|
|
47
|
+
/**
|
|
48
|
+
* Check if keyboard event is an activation key (Enter or Space)
|
|
49
|
+
* Used for custom interactive elements (buttons, links, etc.)
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* element.addEventListener('keydown', (e) => {
|
|
54
|
+
* if (isActivationKey(e)) {
|
|
55
|
+
* handleClick();
|
|
56
|
+
* }
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function isActivationKey(event: KeyboardEvent): boolean;
|
|
61
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onEscape","stability":"stable","signature":"(handler: KeyboardHandler, options?: KeyboardHandlerOptions) => () => void"} */
|
|
62
|
+
/**
|
|
63
|
+
* Handle Escape key press
|
|
64
|
+
* Commonly used to close modals, dropdowns, tooltips, etc.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const cleanup = onEscape(() => {
|
|
69
|
+
* closeModal();
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function onEscape(handler: KeyboardHandler, options?: KeyboardHandlerOptions): () => void;
|
|
74
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onEnter","stability":"stable","signature":"(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions) => () => void"} */
|
|
75
|
+
/**
|
|
76
|
+
* Handle Enter key press
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const cleanup = onEnter(inputElement, (e) => {
|
|
81
|
+
* submitForm();
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function onEnter(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions): () => void;
|
|
86
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onSpace","stability":"stable","signature":"(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions) => () => void"} */
|
|
87
|
+
/**
|
|
88
|
+
* Handle Space key press
|
|
89
|
+
* Note: Space typically triggers on keydown for consistency with Enter
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* const cleanup = onSpace(buttonElement, (e) => {
|
|
94
|
+
* toggleState();
|
|
95
|
+
* });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function onSpace(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions): () => void;
|
|
99
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onActivation","stability":"stable","signature":"(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions) => () => void"} */
|
|
100
|
+
/**
|
|
101
|
+
* Handle activation keys (Enter or Space)
|
|
102
|
+
* Used for custom buttons, links, and other interactive elements
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const cleanup = onActivation(customButton, (e) => {
|
|
107
|
+
* performAction();
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function onActivation(element: HTMLElement, handler: KeyboardHandler, options?: KeyboardHandlerOptions): () => void;
|
|
112
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onArrowKeys","stability":"stable","signature":"(element: HTMLElement, handlers: ArrowKeyHandlers, options?: KeyboardHandlerOptions) => () => void"} */
|
|
113
|
+
/**
|
|
114
|
+
* Handle arrow key navigation
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* const cleanup = onArrowKeys(container, {
|
|
119
|
+
* up: () => selectPreviousItem(),
|
|
120
|
+
* down: () => selectNextItem(),
|
|
121
|
+
* left: () => navigateLeft(),
|
|
122
|
+
* right: () => navigateRight(),
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function onArrowKeys(element: HTMLElement, handlers: ArrowKeyHandlers, options?: KeyboardHandlerOptions): () => void;
|
|
127
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"hasModifierKeys","stability":"stable","signature":"(event: KeyboardEvent) => boolean"} */
|
|
128
|
+
/**
|
|
129
|
+
* Check if modifier keys are pressed
|
|
130
|
+
* Modifier keys: Ctrl, Alt, Shift, Meta (Command/Windows key)
|
|
131
|
+
*/
|
|
132
|
+
export declare function hasModifierKeys(event: KeyboardEvent): boolean;
|
|
133
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"onKeyboardShortcut","stability":"stable","signature":"(shortcut: string, handler: KeyboardHandler, options?: KeyboardHandlerOptions) => () => void"} */
|
|
134
|
+
/**
|
|
135
|
+
* Create a keyboard shortcut handler
|
|
136
|
+
* Supports combinations like "Ctrl+S", "Cmd+K", "Escape", etc.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const cleanup = onKeyboardShortcut('Ctrl+S', (e) => {
|
|
141
|
+
* saveDocument();
|
|
142
|
+
* });
|
|
143
|
+
*
|
|
144
|
+
* // Mac/Windows compatible
|
|
145
|
+
* const cleanup2 = onKeyboardShortcut('Mod+K', (e) => {
|
|
146
|
+
* openCommandPalette();
|
|
147
|
+
* }); // Mod = Ctrl on Windows/Linux, Cmd on Mac
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function onKeyboardShortcut(shortcut: string, handler: KeyboardHandler, options?: KeyboardHandlerOptions): () => void;
|
|
151
|
+
/** @codexApi {"parent":"pithy.a11y.keyboard-utils","name":"makeKeyboardAccessible","stability":"stable","signature":"(element: HTMLElement, handler: KeyboardHandler) => () => void"} */
|
|
152
|
+
/**
|
|
153
|
+
* Make an element keyboard-accessible by adding Enter/Space key handlers
|
|
154
|
+
* Useful for making divs/spans behave like buttons
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const div = document.querySelector('.custom-button');
|
|
159
|
+
* const cleanup = makeKeyboardAccessible(div, () => {
|
|
160
|
+
* performAction();
|
|
161
|
+
* });
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export declare function makeKeyboardAccessible(element: HTMLElement, handler: KeyboardHandler): () => void;
|
|
165
|
+
//# sourceMappingURL=keyboard-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyboard-utils.d.ts","sourceRoot":"","sources":["../../src/a11y/keyboard-utils.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AAEF;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,uDAAuD;IACvD,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,8CAA8C;IAC9C,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,oEAAoE;IACpE,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;AAE7D;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,eAAe,CAAC;IACrB,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB,IAAI,CAAC,EAAE,eAAe,CAAC;IACvB,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAED,wJAAwJ;AACxJ;;;;;;;;;;;GAWG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAE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