@proyecto-viviana/solidaria 0.2.4 → 0.2.8
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/dist/actiongroup/createActionGroup.d.ts +29 -0
- package/dist/actiongroup/createActionGroup.d.ts.map +1 -0
- package/dist/actiongroup/index.d.ts +2 -0
- package/dist/actiongroup/index.d.ts.map +1 -0
- package/dist/autocomplete/createAutocomplete.d.ts +6 -2
- package/dist/autocomplete/createAutocomplete.d.ts.map +1 -1
- package/dist/breadcrumbs/createBreadcrumbs.d.ts +2 -0
- package/dist/breadcrumbs/createBreadcrumbs.d.ts.map +1 -1
- package/dist/button/createToggleButtonGroup.d.ts +32 -0
- package/dist/button/createToggleButtonGroup.d.ts.map +1 -0
- package/dist/button/index.d.ts +2 -0
- package/dist/button/index.d.ts.map +1 -1
- package/dist/calendar/createCalendarCell.d.ts +2 -0
- package/dist/calendar/createCalendarCell.d.ts.map +1 -1
- package/dist/calendar/createCalendarGrid.d.ts.map +1 -1
- package/dist/calendar/createRangeCalendarCell.d.ts +3 -1
- package/dist/calendar/createRangeCalendarCell.d.ts.map +1 -1
- package/dist/checkbox/createCheckboxGroup.d.ts +5 -1
- package/dist/checkbox/createCheckboxGroup.d.ts.map +1 -1
- package/dist/collections/index.d.ts +56 -0
- package/dist/collections/index.d.ts.map +1 -0
- package/dist/color/createColorArea.d.ts.map +1 -1
- package/dist/color/createColorSlider.d.ts.map +1 -1
- package/dist/color/createColorWheel.d.ts.map +1 -1
- package/dist/combobox/createComboBox.d.ts +6 -0
- package/dist/combobox/createComboBox.d.ts.map +1 -1
- package/dist/datepicker/createDatePicker.d.ts +6 -0
- package/dist/datepicker/createDatePicker.d.ts.map +1 -1
- package/dist/datepicker/createDateRangePicker.d.ts +40 -0
- package/dist/datepicker/createDateRangePicker.d.ts.map +1 -0
- package/dist/datepicker/createDateSegment.d.ts +1 -1
- package/dist/datepicker/createDateSegment.d.ts.map +1 -1
- package/dist/datepicker/createTimeSegment.d.ts +29 -0
- package/dist/datepicker/createTimeSegment.d.ts.map +1 -0
- package/dist/datepicker/index.d.ts +2 -0
- package/dist/datepicker/index.d.ts.map +1 -1
- package/dist/disclosure/createDisclosureGroup.d.ts +2 -1
- package/dist/disclosure/createDisclosureGroup.d.ts.map +1 -1
- package/dist/dnd/createDrag.d.ts.map +1 -1
- package/dist/dnd/createDraggableCollection.d.ts +4 -0
- package/dist/dnd/createDraggableCollection.d.ts.map +1 -1
- package/dist/dnd/createDraggableItem.d.ts.map +1 -1
- package/dist/dnd/createDrop.d.ts.map +1 -1
- package/dist/dnd/createDroppableCollection.d.ts +32 -1
- package/dist/dnd/createDroppableCollection.d.ts.map +1 -1
- package/dist/dnd/createDroppableItem.d.ts.map +1 -1
- package/dist/dnd/index.d.ts +1 -1
- package/dist/dnd/index.d.ts.map +1 -1
- package/dist/grid/createGrid.d.ts.map +1 -1
- package/dist/gridlist/createGridList.d.ts.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4659 -3452
- package/dist/index.js.map +1 -7
- package/dist/index.ssr.js +4659 -3452
- package/dist/index.ssr.js.map +1 -7
- package/dist/interactions/createFocus.d.ts.map +1 -1
- package/dist/interactions/createFocusWithin.d.ts.map +1 -1
- package/dist/link/createLink.d.ts +10 -0
- package/dist/link/createLink.d.ts.map +1 -1
- package/dist/listbox/createListBox.d.ts +1 -0
- package/dist/listbox/createListBox.d.ts.map +1 -1
- package/dist/listbox/createOption.d.ts.map +1 -1
- package/dist/menu/createMenu.d.ts +1 -0
- package/dist/menu/createMenu.d.ts.map +1 -1
- package/dist/meter/createMeter.d.ts.map +1 -1
- package/dist/numberfield/createNumberField.d.ts +18 -0
- package/dist/numberfield/createNumberField.d.ts.map +1 -1
- package/dist/overlays/createModal.d.ts +16 -0
- package/dist/overlays/createModal.d.ts.map +1 -1
- package/dist/overlays/createOverlay.d.ts.map +1 -1
- package/dist/overlays/index.d.ts +1 -1
- package/dist/overlays/index.d.ts.map +1 -1
- package/dist/popover/createOverlayPosition.d.ts.map +1 -1
- package/dist/popover/createPopover.d.ts.map +1 -1
- package/dist/progress/createProgressBar.d.ts.map +1 -1
- package/dist/radio/createRadioGroup.d.ts +2 -2
- package/dist/radio/createRadioGroup.d.ts.map +1 -1
- package/dist/searchfield/createSearchField.d.ts.map +1 -1
- package/dist/select/createHiddenSelect.d.ts.map +1 -1
- package/dist/select/createSelect.d.ts.map +1 -1
- package/dist/slider/createSlider.d.ts.map +1 -1
- package/dist/table/createTable.d.ts.map +1 -1
- package/dist/tabs/createTabs.d.ts +1 -1
- package/dist/tabs/createTabs.d.ts.map +1 -1
- package/dist/tag/createTag.d.ts.map +1 -1
- package/dist/tag/createTagGroup.d.ts.map +1 -1
- package/dist/toast/createToast.d.ts +4 -0
- package/dist/toast/createToast.d.ts.map +1 -1
- package/dist/toast/createToastRegion.d.ts.map +1 -1
- package/dist/toolbar/createToolbar.d.ts.map +1 -1
- package/dist/tooltip/createTooltipTrigger.d.ts.map +1 -1
- package/dist/tree/createTree.d.ts.map +1 -1
- package/dist/tree/createTreeItem.d.ts.map +1 -1
- package/dist/tree/types.d.ts +4 -0
- package/dist/tree/types.d.ts.map +1 -1
- package/dist/utils/env.d.ts +1 -1
- package/dist/utils/env.d.ts.map +1 -1
- package/dist/utils/platform.d.ts.map +1 -1
- package/dist/visually-hidden/createVisuallyHidden.d.ts.map +1 -1
- package/package.json +8 -6
- package/src/actiongroup/createActionGroup.ts +324 -0
- package/src/actiongroup/index.ts +8 -0
- package/src/autocomplete/createAutocomplete.ts +32 -9
- package/src/breadcrumbs/createBreadcrumbs.ts +10 -15
- package/src/button/createButton.ts +1 -1
- package/src/button/createToggleButtonGroup.ts +128 -0
- package/src/button/index.ts +9 -0
- package/src/calendar/createCalendarCell.ts +6 -4
- package/src/calendar/createCalendarGrid.ts +27 -18
- package/src/calendar/createRangeCalendarCell.ts +26 -9
- package/src/checkbox/createCheckboxGroup.ts +21 -4
- package/src/collections/index.ts +242 -0
- package/src/color/createColorArea.ts +380 -314
- package/src/color/createColorField.ts +137 -137
- package/src/color/createColorSlider.ts +286 -197
- package/src/color/createColorSwatch.ts +40 -40
- package/src/color/createColorWheel.ts +218 -208
- package/src/color/index.ts +24 -24
- package/src/color/types.ts +116 -116
- package/src/combobox/createComboBox.ts +670 -647
- package/src/combobox/index.ts +6 -6
- package/src/datepicker/createDatePicker.ts +54 -16
- package/src/datepicker/createDateRangePicker.ts +246 -0
- package/src/datepicker/createDateSegment.ts +185 -31
- package/src/datepicker/createTimeSegment.ts +370 -0
- package/src/datepicker/index.ts +14 -0
- package/src/dialog/createDialog.ts +120 -120
- package/src/dialog/index.ts +2 -2
- package/src/dialog/types.ts +19 -19
- package/src/disclosure/createDisclosureGroup.ts +5 -2
- package/src/dnd/createDrag.ts +224 -209
- package/src/dnd/createDraggableCollection.ts +96 -63
- package/src/dnd/createDraggableItem.ts +259 -243
- package/src/dnd/createDrop.ts +322 -321
- package/src/dnd/createDroppableCollection.ts +682 -293
- package/src/dnd/createDroppableItem.ts +215 -213
- package/src/dnd/index.ts +55 -47
- package/src/dnd/types.ts +89 -89
- package/src/dnd/utils.ts +294 -294
- package/src/focus/createAutoFocus.ts +321 -321
- package/src/focus/createFocusRestore.ts +313 -313
- package/src/focus/createVirtualFocus.ts +396 -396
- package/src/form/createFormValidation.ts +224 -224
- package/src/form/index.ts +11 -11
- package/src/grid/createGrid.ts +3 -1
- package/src/gridlist/createGridList.ts +16 -0
- package/src/gridlist/createGridListItem.ts +1 -1
- package/src/i18n/NumberFormatter.ts +266 -266
- package/src/i18n/createCollator.ts +79 -79
- package/src/i18n/createDateFormatter.ts +83 -83
- package/src/i18n/createFilter.ts +131 -131
- package/src/i18n/createNumberFormatter.ts +52 -52
- package/src/i18n/index.ts +40 -40
- package/src/i18n/locale.tsx +188 -188
- package/src/i18n/utils.ts +99 -99
- package/src/index.ts +51 -0
- package/src/interactions/createFocus.ts +6 -5
- package/src/interactions/createFocusWithin.ts +6 -5
- package/src/interactions/createLongPress.ts +174 -174
- package/src/interactions/createMove.ts +289 -289
- package/src/interactions/createPress.ts +5 -5
- package/src/landmark/createLandmark.ts +377 -377
- package/src/landmark/index.ts +8 -8
- package/src/link/createLink.ts +23 -8
- package/src/listbox/createListBox.ts +308 -269
- package/src/listbox/createOption.ts +162 -151
- package/src/listbox/index.ts +12 -12
- package/src/live-announcer/announce.ts +322 -322
- package/src/live-announcer/index.ts +9 -9
- package/src/menu/createMenu.ts +405 -396
- package/src/menu/createMenuItem.ts +149 -149
- package/src/menu/createMenuTrigger.ts +88 -88
- package/src/menu/index.ts +18 -18
- package/src/meter/createMeter.ts +1 -6
- package/src/numberfield/createNumberField.ts +311 -268
- package/src/numberfield/index.ts +5 -5
- package/src/overlays/ariaHideOutside.ts +219 -219
- package/src/overlays/createInteractOutside.ts +149 -149
- package/src/overlays/createModal.tsx +238 -202
- package/src/overlays/createOverlay.ts +165 -155
- package/src/overlays/createOverlayTrigger.ts +85 -85
- package/src/overlays/createPreventScroll.ts +266 -266
- package/src/overlays/index.ts +48 -44
- package/src/popover/calculatePosition.ts +6 -6
- package/src/popover/createOverlayPosition.ts +7 -4
- package/src/popover/createPopover.ts +21 -7
- package/src/progress/createProgressBar.ts +6 -1
- package/src/radio/createRadioGroup.ts +88 -14
- package/src/searchfield/createSearchField.ts +241 -186
- package/src/searchfield/index.ts +2 -2
- package/src/select/createHiddenSelect.tsx +263 -236
- package/src/select/createSelect.ts +373 -395
- package/src/select/index.ts +14 -14
- package/src/slider/createSlider.ts +364 -349
- package/src/slider/index.ts +2 -2
- package/src/ssr/index.tsx +370 -370
- package/src/table/createTable.ts +3 -1
- package/src/table/createTableColumnHeader.ts +1 -1
- package/src/table/createTableRow.ts +1 -1
- package/src/tabs/createTabs.ts +80 -51
- package/src/tag/createTag.ts +135 -6
- package/src/tag/createTagGroup.ts +7 -2
- package/src/toast/createToast.ts +8 -2
- package/src/toast/createToastRegion.ts +0 -1
- package/src/toolbar/createToolbar.ts +75 -1
- package/src/tooltip/createTooltip.ts +79 -79
- package/src/tooltip/createTooltipTrigger.ts +226 -222
- package/src/tooltip/index.ts +6 -6
- package/src/tree/createTree.ts +261 -246
- package/src/tree/createTreeItem.ts +282 -233
- package/src/tree/createTreeSelectionCheckbox.ts +68 -68
- package/src/tree/index.ts +16 -16
- package/src/tree/types.ts +91 -87
- package/src/utils/env.ts +55 -54
- package/src/utils/platform.ts +16 -6
- package/src/visually-hidden/createVisuallyHidden.ts +139 -124
- package/src/visually-hidden/index.ts +6 -6
|
@@ -1,313 +1,313 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Focus restoration utilities for solidaria
|
|
3
|
-
*
|
|
4
|
-
* Provides enhanced focus restoration with retry logic, cross-scope tracking,
|
|
5
|
-
* and safe restoration patterns.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { createEffect, onCleanup, onMount } from 'solid-js';
|
|
9
|
-
import { isServer } from 'solid-js/web';
|
|
10
|
-
import { getOwnerDocument } from '../utils';
|
|
11
|
-
import { focusSafely } from '../utils/focus';
|
|
12
|
-
|
|
13
|
-
// ============================================
|
|
14
|
-
// TYPES
|
|
15
|
-
// ============================================
|
|
16
|
-
|
|
17
|
-
export interface FocusRestoreOptions {
|
|
18
|
-
/**
|
|
19
|
-
* Whether to restore focus when the component unmounts.
|
|
20
|
-
* @default true
|
|
21
|
-
*/
|
|
22
|
-
restoreOnUnmount?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Maximum number of retries if the element is not in the DOM.
|
|
25
|
-
* @default 3
|
|
26
|
-
*/
|
|
27
|
-
maxRetries?: number;
|
|
28
|
-
/**
|
|
29
|
-
* Delay between retries in milliseconds.
|
|
30
|
-
* @default 50
|
|
31
|
-
*/
|
|
32
|
-
retryDelay?: number;
|
|
33
|
-
/**
|
|
34
|
-
* Callback when focus is successfully restored.
|
|
35
|
-
*/
|
|
36
|
-
onRestore?: (element: HTMLElement) => void;
|
|
37
|
-
/**
|
|
38
|
-
* Callback when focus restoration fails.
|
|
39
|
-
*/
|
|
40
|
-
onRestoreFailed?: () => void;
|
|
41
|
-
/**
|
|
42
|
-
* Whether to prevent scrolling when restoring focus.
|
|
43
|
-
* @default true
|
|
44
|
-
*/
|
|
45
|
-
preventScroll?: boolean;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface FocusRestoreResult {
|
|
49
|
-
/**
|
|
50
|
-
* Manually restore focus to the saved element.
|
|
51
|
-
*/
|
|
52
|
-
restore: () => boolean;
|
|
53
|
-
/**
|
|
54
|
-
* Get the saved element (if any).
|
|
55
|
-
*/
|
|
56
|
-
getSavedElement: () => HTMLElement | null;
|
|
57
|
-
/**
|
|
58
|
-
* Save the currently focused element.
|
|
59
|
-
*/
|
|
60
|
-
saveCurrentFocus: () => void;
|
|
61
|
-
/**
|
|
62
|
-
* Clear the saved element without restoring.
|
|
63
|
-
*/
|
|
64
|
-
clear: () => void;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ============================================
|
|
68
|
-
// GLOBAL FOCUS STACK
|
|
69
|
-
// ============================================
|
|
70
|
-
|
|
71
|
-
// Stack to track focus history across scopes
|
|
72
|
-
const focusStack: HTMLElement[] = [];
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Push an element onto the focus stack.
|
|
76
|
-
*/
|
|
77
|
-
export function pushFocusStack(element: HTMLElement): void {
|
|
78
|
-
focusStack.push(element);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Pop the last element from the focus stack.
|
|
83
|
-
*/
|
|
84
|
-
export function popFocusStack(): HTMLElement | undefined {
|
|
85
|
-
return focusStack.pop();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Get the current focus stack length.
|
|
90
|
-
*/
|
|
91
|
-
export function getFocusStackLength(): number {
|
|
92
|
-
return focusStack.length;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Clear the entire focus stack.
|
|
97
|
-
*/
|
|
98
|
-
export function clearFocusStack(): void {
|
|
99
|
-
focusStack.length = 0;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// ============================================
|
|
103
|
-
// UTILITIES
|
|
104
|
-
// ============================================
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Gets the active element, accounting for shadow DOM.
|
|
108
|
-
*/
|
|
109
|
-
function getActiveElement(doc: Document): HTMLElement | null {
|
|
110
|
-
let activeElement = doc.activeElement as HTMLElement | null;
|
|
111
|
-
while (activeElement?.shadowRoot?.activeElement) {
|
|
112
|
-
activeElement = activeElement.shadowRoot.activeElement as HTMLElement;
|
|
113
|
-
}
|
|
114
|
-
return activeElement;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Checks if an element is still valid for focus restoration.
|
|
119
|
-
*/
|
|
120
|
-
function isValidForRestore(element: HTMLElement | null): boolean {
|
|
121
|
-
if (!element) return false;
|
|
122
|
-
if (!document.body.contains(element)) return false;
|
|
123
|
-
if (element.hasAttribute('disabled')) return false;
|
|
124
|
-
if (element.getAttribute('aria-disabled') === 'true') return false;
|
|
125
|
-
if (element.getAttribute('aria-hidden') === 'true') return false;
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Attempts to restore focus with retries.
|
|
131
|
-
*/
|
|
132
|
-
function tryRestoreFocus(
|
|
133
|
-
element: HTMLElement | null,
|
|
134
|
-
options: Required<Pick<FocusRestoreOptions, 'maxRetries' | 'retryDelay' | 'preventScroll' | 'onRestore' | 'onRestoreFailed'>>
|
|
135
|
-
): void {
|
|
136
|
-
const { maxRetries, retryDelay, preventScroll, onRestore, onRestoreFailed } = options;
|
|
137
|
-
let attempts = 0;
|
|
138
|
-
|
|
139
|
-
const attempt = () => {
|
|
140
|
-
if (!element) {
|
|
141
|
-
onRestoreFailed?.();
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (isValidForRestore(element)) {
|
|
146
|
-
if (preventScroll) {
|
|
147
|
-
focusSafely(element);
|
|
148
|
-
} else {
|
|
149
|
-
element.focus();
|
|
150
|
-
}
|
|
151
|
-
onRestore?.(element);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
attempts++;
|
|
156
|
-
if (attempts < maxRetries) {
|
|
157
|
-
setTimeout(attempt, retryDelay);
|
|
158
|
-
} else {
|
|
159
|
-
onRestoreFailed?.();
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
// Use requestAnimationFrame for the first attempt to ensure DOM is ready
|
|
164
|
-
requestAnimationFrame(attempt);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// ============================================
|
|
168
|
-
// HOOK
|
|
169
|
-
// ============================================
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Creates a focus restoration manager.
|
|
173
|
-
*
|
|
174
|
-
* This hook saves the currently focused element when mounted and provides
|
|
175
|
-
* methods to restore focus later, with retry logic for reliability.
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```tsx
|
|
179
|
-
* function Modal(props) {
|
|
180
|
-
* const focusRestore = createFocusRestore({
|
|
181
|
-
* restoreOnUnmount: true,
|
|
182
|
-
* onRestore: () => console.log('Focus restored'),
|
|
183
|
-
* });
|
|
184
|
-
*
|
|
185
|
-
* return (
|
|
186
|
-
* <div role="dialog">
|
|
187
|
-
* {props.children}
|
|
188
|
-
* <button onClick={() => focusRestore.restore()}>
|
|
189
|
-
* Close
|
|
190
|
-
* </button>
|
|
191
|
-
* </div>
|
|
192
|
-
* );
|
|
193
|
-
* }
|
|
194
|
-
* ```
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* ```tsx
|
|
198
|
-
* // Manual focus management
|
|
199
|
-
* function Dropdown() {
|
|
200
|
-
* const focusRestore = createFocusRestore({ restoreOnUnmount: false });
|
|
201
|
-
*
|
|
202
|
-
* const onOpen = () => {
|
|
203
|
-
* focusRestore.saveCurrentFocus();
|
|
204
|
-
* // Focus dropdown content
|
|
205
|
-
* };
|
|
206
|
-
*
|
|
207
|
-
* const onClose = () => {
|
|
208
|
-
* focusRestore.restore();
|
|
209
|
-
* };
|
|
210
|
-
*
|
|
211
|
-
* return <div>...</div>;
|
|
212
|
-
* }
|
|
213
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
export function createFocusRestore(
|
|
216
|
-
options: FocusRestoreOptions = {}
|
|
217
|
-
): FocusRestoreResult {
|
|
218
|
-
const {
|
|
219
|
-
restoreOnUnmount = true,
|
|
220
|
-
maxRetries = 3,
|
|
221
|
-
retryDelay = 50,
|
|
222
|
-
onRestore,
|
|
223
|
-
onRestoreFailed,
|
|
224
|
-
preventScroll = true,
|
|
225
|
-
} = options;
|
|
226
|
-
|
|
227
|
-
// During SSR, return no-op functions
|
|
228
|
-
if (isServer) {
|
|
229
|
-
return {
|
|
230
|
-
restore: () => false,
|
|
231
|
-
getSavedElement: () => null,
|
|
232
|
-
saveCurrentFocus: () => {},
|
|
233
|
-
clear: () => {},
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
let savedElement: HTMLElement | null = null;
|
|
238
|
-
|
|
239
|
-
// Save focus on mount
|
|
240
|
-
onMount(() => {
|
|
241
|
-
saveCurrentFocus();
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
// Restore focus on cleanup
|
|
245
|
-
onCleanup(() => {
|
|
246
|
-
if (restoreOnUnmount && savedElement) {
|
|
247
|
-
tryRestoreFocus(savedElement, {
|
|
248
|
-
maxRetries,
|
|
249
|
-
retryDelay,
|
|
250
|
-
preventScroll,
|
|
251
|
-
onRestore: onRestore ?? (() => {}),
|
|
252
|
-
onRestoreFailed: onRestoreFailed ?? (() => {}),
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
function saveCurrentFocus(): void {
|
|
258
|
-
const doc = typeof document !== 'undefined' ? document : null;
|
|
259
|
-
if (!doc) return;
|
|
260
|
-
|
|
261
|
-
const active = getActiveElement(doc);
|
|
262
|
-
if (active && active !== doc.body) {
|
|
263
|
-
savedElement = active;
|
|
264
|
-
pushFocusStack(active);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function restore(): boolean {
|
|
269
|
-
if (!savedElement) return false;
|
|
270
|
-
|
|
271
|
-
if (isValidForRestore(savedElement)) {
|
|
272
|
-
if (preventScroll) {
|
|
273
|
-
focusSafely(savedElement);
|
|
274
|
-
} else {
|
|
275
|
-
savedElement.focus();
|
|
276
|
-
}
|
|
277
|
-
onRestore?.(savedElement);
|
|
278
|
-
return true;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Try the focus stack
|
|
282
|
-
while (focusStack.length > 0) {
|
|
283
|
-
const stackElement = popFocusStack();
|
|
284
|
-
if (stackElement && isValidForRestore(stackElement)) {
|
|
285
|
-
if (preventScroll) {
|
|
286
|
-
focusSafely(stackElement);
|
|
287
|
-
} else {
|
|
288
|
-
stackElement.focus();
|
|
289
|
-
}
|
|
290
|
-
onRestore?.(stackElement);
|
|
291
|
-
return true;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
onRestoreFailed?.();
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function getSavedElement(): HTMLElement | null {
|
|
300
|
-
return savedElement;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function clear(): void {
|
|
304
|
-
savedElement = null;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return {
|
|
308
|
-
restore,
|
|
309
|
-
getSavedElement,
|
|
310
|
-
saveCurrentFocus,
|
|
311
|
-
clear,
|
|
312
|
-
};
|
|
313
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Focus restoration utilities for solidaria
|
|
3
|
+
*
|
|
4
|
+
* Provides enhanced focus restoration with retry logic, cross-scope tracking,
|
|
5
|
+
* and safe restoration patterns.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createEffect, onCleanup, onMount } from 'solid-js';
|
|
9
|
+
import { isServer } from 'solid-js/web';
|
|
10
|
+
import { getOwnerDocument } from '../utils';
|
|
11
|
+
import { focusSafely } from '../utils/focus';
|
|
12
|
+
|
|
13
|
+
// ============================================
|
|
14
|
+
// TYPES
|
|
15
|
+
// ============================================
|
|
16
|
+
|
|
17
|
+
export interface FocusRestoreOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Whether to restore focus when the component unmounts.
|
|
20
|
+
* @default true
|
|
21
|
+
*/
|
|
22
|
+
restoreOnUnmount?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Maximum number of retries if the element is not in the DOM.
|
|
25
|
+
* @default 3
|
|
26
|
+
*/
|
|
27
|
+
maxRetries?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Delay between retries in milliseconds.
|
|
30
|
+
* @default 50
|
|
31
|
+
*/
|
|
32
|
+
retryDelay?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Callback when focus is successfully restored.
|
|
35
|
+
*/
|
|
36
|
+
onRestore?: (element: HTMLElement) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Callback when focus restoration fails.
|
|
39
|
+
*/
|
|
40
|
+
onRestoreFailed?: () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Whether to prevent scrolling when restoring focus.
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
preventScroll?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface FocusRestoreResult {
|
|
49
|
+
/**
|
|
50
|
+
* Manually restore focus to the saved element.
|
|
51
|
+
*/
|
|
52
|
+
restore: () => boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Get the saved element (if any).
|
|
55
|
+
*/
|
|
56
|
+
getSavedElement: () => HTMLElement | null;
|
|
57
|
+
/**
|
|
58
|
+
* Save the currently focused element.
|
|
59
|
+
*/
|
|
60
|
+
saveCurrentFocus: () => void;
|
|
61
|
+
/**
|
|
62
|
+
* Clear the saved element without restoring.
|
|
63
|
+
*/
|
|
64
|
+
clear: () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ============================================
|
|
68
|
+
// GLOBAL FOCUS STACK
|
|
69
|
+
// ============================================
|
|
70
|
+
|
|
71
|
+
// Stack to track focus history across scopes
|
|
72
|
+
const focusStack: HTMLElement[] = [];
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Push an element onto the focus stack.
|
|
76
|
+
*/
|
|
77
|
+
export function pushFocusStack(element: HTMLElement): void {
|
|
78
|
+
focusStack.push(element);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Pop the last element from the focus stack.
|
|
83
|
+
*/
|
|
84
|
+
export function popFocusStack(): HTMLElement | undefined {
|
|
85
|
+
return focusStack.pop();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the current focus stack length.
|
|
90
|
+
*/
|
|
91
|
+
export function getFocusStackLength(): number {
|
|
92
|
+
return focusStack.length;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Clear the entire focus stack.
|
|
97
|
+
*/
|
|
98
|
+
export function clearFocusStack(): void {
|
|
99
|
+
focusStack.length = 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ============================================
|
|
103
|
+
// UTILITIES
|
|
104
|
+
// ============================================
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Gets the active element, accounting for shadow DOM.
|
|
108
|
+
*/
|
|
109
|
+
function getActiveElement(doc: Document): HTMLElement | null {
|
|
110
|
+
let activeElement = doc.activeElement as HTMLElement | null;
|
|
111
|
+
while (activeElement?.shadowRoot?.activeElement) {
|
|
112
|
+
activeElement = activeElement.shadowRoot.activeElement as HTMLElement;
|
|
113
|
+
}
|
|
114
|
+
return activeElement;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Checks if an element is still valid for focus restoration.
|
|
119
|
+
*/
|
|
120
|
+
function isValidForRestore(element: HTMLElement | null): boolean {
|
|
121
|
+
if (!element) return false;
|
|
122
|
+
if (!document.body.contains(element)) return false;
|
|
123
|
+
if (element.hasAttribute('disabled')) return false;
|
|
124
|
+
if (element.getAttribute('aria-disabled') === 'true') return false;
|
|
125
|
+
if (element.getAttribute('aria-hidden') === 'true') return false;
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Attempts to restore focus with retries.
|
|
131
|
+
*/
|
|
132
|
+
function tryRestoreFocus(
|
|
133
|
+
element: HTMLElement | null,
|
|
134
|
+
options: Required<Pick<FocusRestoreOptions, 'maxRetries' | 'retryDelay' | 'preventScroll' | 'onRestore' | 'onRestoreFailed'>>
|
|
135
|
+
): void {
|
|
136
|
+
const { maxRetries, retryDelay, preventScroll, onRestore, onRestoreFailed } = options;
|
|
137
|
+
let attempts = 0;
|
|
138
|
+
|
|
139
|
+
const attempt = () => {
|
|
140
|
+
if (!element) {
|
|
141
|
+
onRestoreFailed?.();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (isValidForRestore(element)) {
|
|
146
|
+
if (preventScroll) {
|
|
147
|
+
focusSafely(element);
|
|
148
|
+
} else {
|
|
149
|
+
element.focus();
|
|
150
|
+
}
|
|
151
|
+
onRestore?.(element);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
attempts++;
|
|
156
|
+
if (attempts < maxRetries) {
|
|
157
|
+
setTimeout(attempt, retryDelay);
|
|
158
|
+
} else {
|
|
159
|
+
onRestoreFailed?.();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Use requestAnimationFrame for the first attempt to ensure DOM is ready
|
|
164
|
+
requestAnimationFrame(attempt);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ============================================
|
|
168
|
+
// HOOK
|
|
169
|
+
// ============================================
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates a focus restoration manager.
|
|
173
|
+
*
|
|
174
|
+
* This hook saves the currently focused element when mounted and provides
|
|
175
|
+
* methods to restore focus later, with retry logic for reliability.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```tsx
|
|
179
|
+
* function Modal(props) {
|
|
180
|
+
* const focusRestore = createFocusRestore({
|
|
181
|
+
* restoreOnUnmount: true,
|
|
182
|
+
* onRestore: () => console.log('Focus restored'),
|
|
183
|
+
* });
|
|
184
|
+
*
|
|
185
|
+
* return (
|
|
186
|
+
* <div role="dialog">
|
|
187
|
+
* {props.children}
|
|
188
|
+
* <button onClick={() => focusRestore.restore()}>
|
|
189
|
+
* Close
|
|
190
|
+
* </button>
|
|
191
|
+
* </div>
|
|
192
|
+
* );
|
|
193
|
+
* }
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```tsx
|
|
198
|
+
* // Manual focus management
|
|
199
|
+
* function Dropdown() {
|
|
200
|
+
* const focusRestore = createFocusRestore({ restoreOnUnmount: false });
|
|
201
|
+
*
|
|
202
|
+
* const onOpen = () => {
|
|
203
|
+
* focusRestore.saveCurrentFocus();
|
|
204
|
+
* // Focus dropdown content
|
|
205
|
+
* };
|
|
206
|
+
*
|
|
207
|
+
* const onClose = () => {
|
|
208
|
+
* focusRestore.restore();
|
|
209
|
+
* };
|
|
210
|
+
*
|
|
211
|
+
* return <div>...</div>;
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
export function createFocusRestore(
|
|
216
|
+
options: FocusRestoreOptions = {}
|
|
217
|
+
): FocusRestoreResult {
|
|
218
|
+
const {
|
|
219
|
+
restoreOnUnmount = true,
|
|
220
|
+
maxRetries = 3,
|
|
221
|
+
retryDelay = 50,
|
|
222
|
+
onRestore,
|
|
223
|
+
onRestoreFailed,
|
|
224
|
+
preventScroll = true,
|
|
225
|
+
} = options;
|
|
226
|
+
|
|
227
|
+
// During SSR, return no-op functions
|
|
228
|
+
if (isServer) {
|
|
229
|
+
return {
|
|
230
|
+
restore: () => false,
|
|
231
|
+
getSavedElement: () => null,
|
|
232
|
+
saveCurrentFocus: () => {},
|
|
233
|
+
clear: () => {},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let savedElement: HTMLElement | null = null;
|
|
238
|
+
|
|
239
|
+
// Save focus on mount
|
|
240
|
+
onMount(() => {
|
|
241
|
+
saveCurrentFocus();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Restore focus on cleanup
|
|
245
|
+
onCleanup(() => {
|
|
246
|
+
if (restoreOnUnmount && savedElement) {
|
|
247
|
+
tryRestoreFocus(savedElement, {
|
|
248
|
+
maxRetries,
|
|
249
|
+
retryDelay,
|
|
250
|
+
preventScroll,
|
|
251
|
+
onRestore: onRestore ?? (() => {}),
|
|
252
|
+
onRestoreFailed: onRestoreFailed ?? (() => {}),
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
function saveCurrentFocus(): void {
|
|
258
|
+
const doc = typeof document !== 'undefined' ? document : null;
|
|
259
|
+
if (!doc) return;
|
|
260
|
+
|
|
261
|
+
const active = getActiveElement(doc);
|
|
262
|
+
if (active && active !== doc.body) {
|
|
263
|
+
savedElement = active;
|
|
264
|
+
pushFocusStack(active);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function restore(): boolean {
|
|
269
|
+
if (!savedElement) return false;
|
|
270
|
+
|
|
271
|
+
if (isValidForRestore(savedElement)) {
|
|
272
|
+
if (preventScroll) {
|
|
273
|
+
focusSafely(savedElement);
|
|
274
|
+
} else {
|
|
275
|
+
savedElement.focus();
|
|
276
|
+
}
|
|
277
|
+
onRestore?.(savedElement);
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Try the focus stack
|
|
282
|
+
while (focusStack.length > 0) {
|
|
283
|
+
const stackElement = popFocusStack();
|
|
284
|
+
if (stackElement && isValidForRestore(stackElement)) {
|
|
285
|
+
if (preventScroll) {
|
|
286
|
+
focusSafely(stackElement);
|
|
287
|
+
} else {
|
|
288
|
+
stackElement.focus();
|
|
289
|
+
}
|
|
290
|
+
onRestore?.(stackElement);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
onRestoreFailed?.();
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function getSavedElement(): HTMLElement | null {
|
|
300
|
+
return savedElement;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function clear(): void {
|
|
304
|
+
savedElement = null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return {
|
|
308
|
+
restore,
|
|
309
|
+
getSavedElement,
|
|
310
|
+
saveCurrentFocus,
|
|
311
|
+
clear,
|
|
312
|
+
};
|
|
313
|
+
}
|