@proyecto-viviana/solidaria 0.2.2 → 0.2.4
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/dist/autocomplete/createAutocomplete.d.ts +2 -2
- package/dist/autocomplete/createAutocomplete.d.ts.map +1 -1
- package/dist/index.js +233 -234
- package/dist/index.js.map +2 -2
- package/dist/index.ssr.js +233 -234
- package/dist/index.ssr.js.map +2 -2
- package/dist/interactions/PressEvent.d.ts +13 -10
- package/dist/interactions/PressEvent.d.ts.map +1 -1
- package/dist/interactions/createPress.d.ts.map +1 -1
- package/dist/interactions/index.d.ts +1 -1
- package/dist/interactions/index.d.ts.map +1 -1
- package/dist/select/createHiddenSelect.d.ts.map +1 -1
- package/dist/toolbar/createToolbar.d.ts.map +1 -1
- package/dist/tooltip/createTooltipTrigger.d.ts.map +1 -1
- package/package.json +9 -7
- package/src/autocomplete/createAutocomplete.ts +341 -0
- package/src/autocomplete/index.ts +9 -0
- package/src/breadcrumbs/createBreadcrumbs.ts +196 -0
- package/src/breadcrumbs/index.ts +8 -0
- package/src/button/createButton.ts +142 -0
- package/src/button/createToggleButton.ts +101 -0
- package/src/button/index.ts +4 -0
- package/src/button/types.ts +78 -0
- package/src/calendar/createCalendar.ts +138 -0
- package/src/calendar/createCalendarCell.ts +187 -0
- package/src/calendar/createCalendarGrid.ts +140 -0
- package/src/calendar/createRangeCalendar.ts +136 -0
- package/src/calendar/createRangeCalendarCell.ts +186 -0
- package/src/calendar/index.ts +34 -0
- package/src/checkbox/createCheckbox.ts +135 -0
- package/src/checkbox/createCheckboxGroup.ts +137 -0
- package/src/checkbox/createCheckboxGroupItem.ts +117 -0
- package/src/checkbox/createCheckboxGroupState.ts +193 -0
- package/src/checkbox/index.ts +13 -0
- package/src/color/createColorArea.ts +314 -0
- package/src/color/createColorField.ts +137 -0
- package/src/color/createColorSlider.ts +197 -0
- package/src/color/createColorSwatch.ts +40 -0
- package/src/color/createColorWheel.ts +208 -0
- package/src/color/index.ts +24 -0
- package/src/color/types.ts +116 -0
- package/src/combobox/createComboBox.ts +647 -0
- package/src/combobox/index.ts +6 -0
- package/src/combobox/intl/en-US.json +7 -0
- package/src/combobox/intl/es-ES.json +7 -0
- package/src/combobox/intl/index.ts +23 -0
- package/src/datepicker/createDateField.ts +154 -0
- package/src/datepicker/createDatePicker.ts +206 -0
- package/src/datepicker/createDateSegment.ts +229 -0
- package/src/datepicker/createTimeField.ts +154 -0
- package/src/datepicker/index.ts +28 -0
- package/src/dialog/createDialog.ts +120 -0
- package/src/dialog/index.ts +2 -0
- package/src/dialog/types.ts +19 -0
- package/src/disclosure/createDisclosure.ts +131 -0
- package/src/disclosure/createDisclosureGroup.ts +62 -0
- package/src/disclosure/index.ts +11 -0
- package/src/dnd/createDrag.ts +209 -0
- package/src/dnd/createDraggableCollection.ts +63 -0
- package/src/dnd/createDraggableItem.ts +243 -0
- package/src/dnd/createDrop.ts +321 -0
- package/src/dnd/createDroppableCollection.ts +293 -0
- package/src/dnd/createDroppableItem.ts +213 -0
- package/src/dnd/index.ts +47 -0
- package/src/dnd/types.ts +89 -0
- package/src/dnd/utils.ts +294 -0
- package/src/focus/FocusScope.tsx +408 -0
- package/src/focus/createAutoFocus.ts +321 -0
- package/src/focus/createFocusRestore.ts +313 -0
- package/src/focus/createVirtualFocus.ts +396 -0
- package/src/focus/index.ts +35 -0
- package/src/form/createFormReset.ts +51 -0
- package/src/form/createFormValidation.ts +224 -0
- package/src/form/index.ts +11 -0
- package/src/grid/GridKeyboardDelegate.ts +429 -0
- package/src/grid/createGrid.ts +261 -0
- package/src/grid/createGridCell.ts +182 -0
- package/src/grid/createGridRow.ts +153 -0
- package/src/grid/index.ts +18 -0
- package/src/grid/types.ts +133 -0
- package/src/gridlist/createGridList.ts +185 -0
- package/src/gridlist/createGridListItem.ts +180 -0
- package/src/gridlist/createGridListSelectionCheckbox.ts +59 -0
- package/src/gridlist/index.ts +16 -0
- package/src/gridlist/types.ts +81 -0
- package/src/i18n/NumberFormatter.ts +266 -0
- package/src/i18n/createCollator.ts +79 -0
- package/src/i18n/createDateFormatter.ts +83 -0
- package/src/i18n/createFilter.ts +131 -0
- package/src/i18n/createNumberFormatter.ts +52 -0
- package/src/i18n/createStringFormatter.ts +87 -0
- package/src/i18n/index.ts +40 -0
- package/src/i18n/locale.tsx +188 -0
- package/src/i18n/utils.ts +99 -0
- package/src/index.ts +670 -0
- package/src/interactions/FocusableProvider.tsx +44 -0
- package/src/interactions/PressEvent.ts +126 -0
- package/src/interactions/createFocus.ts +163 -0
- package/src/interactions/createFocusRing.ts +89 -0
- package/src/interactions/createFocusWithin.ts +206 -0
- package/src/interactions/createFocusable.ts +168 -0
- package/src/interactions/createHover.ts +254 -0
- package/src/interactions/createInteractionModality.ts +424 -0
- package/src/interactions/createKeyboard.ts +82 -0
- package/src/interactions/createLongPress.ts +174 -0
- package/src/interactions/createMove.ts +289 -0
- package/src/interactions/createPress.ts +834 -0
- package/src/interactions/index.ts +78 -0
- package/src/label/createField.ts +145 -0
- package/src/label/createLabel.ts +117 -0
- package/src/label/createLabels.ts +50 -0
- package/src/label/index.ts +19 -0
- package/src/landmark/createLandmark.ts +377 -0
- package/src/landmark/index.ts +8 -0
- package/src/link/createLink.ts +182 -0
- package/src/link/index.ts +1 -0
- package/src/listbox/createListBox.ts +269 -0
- package/src/listbox/createOption.ts +151 -0
- package/src/listbox/index.ts +12 -0
- package/src/live-announcer/announce.ts +322 -0
- package/src/live-announcer/index.ts +9 -0
- package/src/menu/createMenu.ts +396 -0
- package/src/menu/createMenuItem.ts +149 -0
- package/src/menu/createMenuTrigger.ts +88 -0
- package/src/menu/index.ts +18 -0
- package/src/meter/createMeter.ts +75 -0
- package/src/meter/index.ts +1 -0
- package/src/numberfield/createNumberField.ts +268 -0
- package/src/numberfield/index.ts +5 -0
- package/src/overlays/ariaHideOutside.ts +219 -0
- package/src/overlays/createInteractOutside.ts +149 -0
- package/src/overlays/createModal.tsx +202 -0
- package/src/overlays/createOverlay.ts +155 -0
- package/src/overlays/createOverlayTrigger.ts +85 -0
- package/src/overlays/createPreventScroll.ts +266 -0
- package/src/overlays/index.ts +44 -0
- package/src/popover/calculatePosition.ts +766 -0
- package/src/popover/createOverlayPosition.ts +356 -0
- package/src/popover/createPopover.ts +170 -0
- package/src/popover/index.ts +24 -0
- package/src/progress/createProgressBar.ts +128 -0
- package/src/progress/index.ts +5 -0
- package/src/radio/createRadio.ts +287 -0
- package/src/radio/createRadioGroup.ts +189 -0
- package/src/radio/createRadioGroupState.ts +201 -0
- package/src/radio/index.ts +23 -0
- package/src/searchfield/createSearchField.ts +186 -0
- package/src/searchfield/index.ts +2 -0
- package/src/select/createHiddenSelect.tsx +236 -0
- package/src/select/createSelect.ts +395 -0
- package/src/select/index.ts +14 -0
- package/src/selection/createTypeSelect.ts +201 -0
- package/src/selection/index.ts +6 -0
- package/src/separator/createSeparator.ts +82 -0
- package/src/separator/index.ts +6 -0
- package/src/slider/createSlider.ts +349 -0
- package/src/slider/index.ts +2 -0
- package/src/ssr/index.tsx +370 -0
- package/src/switch/createSwitch.ts +70 -0
- package/src/switch/index.ts +1 -0
- package/src/table/createTable.ts +526 -0
- package/src/table/createTableCell.ts +147 -0
- package/src/table/createTableColumnHeader.ts +115 -0
- package/src/table/createTableHeaderRow.ts +40 -0
- package/src/table/createTableRow.ts +155 -0
- package/src/table/createTableRowGroup.ts +32 -0
- package/src/table/createTableSelectAllCheckbox.ts +73 -0
- package/src/table/createTableSelectionCheckbox.ts +59 -0
- package/src/table/index.ts +30 -0
- package/src/table/types.ts +165 -0
- package/src/tabs/createTabs.ts +472 -0
- package/src/tabs/index.ts +14 -0
- package/src/tag/createTag.ts +194 -0
- package/src/tag/createTagGroup.ts +154 -0
- package/src/tag/index.ts +12 -0
- package/src/textfield/createTextField.ts +198 -0
- package/src/textfield/index.ts +5 -0
- package/src/toast/createToast.ts +118 -0
- package/src/toast/createToastRegion.ts +100 -0
- package/src/toast/index.ts +11 -0
- package/src/toggle/createToggle.ts +223 -0
- package/src/toggle/createToggleState.ts +94 -0
- package/src/toggle/index.ts +7 -0
- package/src/toolbar/createToolbar.ts +369 -0
- package/src/toolbar/index.ts +6 -0
- package/src/tooltip/createTooltip.ts +79 -0
- package/src/tooltip/createTooltipTrigger.ts +222 -0
- package/src/tooltip/index.ts +6 -0
- package/src/tree/createTree.ts +246 -0
- package/src/tree/createTreeItem.ts +233 -0
- package/src/tree/createTreeSelectionCheckbox.ts +68 -0
- package/src/tree/index.ts +16 -0
- package/src/tree/types.ts +87 -0
- package/src/utils/createDescription.ts +137 -0
- package/src/utils/dom.ts +327 -0
- package/src/utils/env.ts +54 -0
- package/src/utils/events.ts +106 -0
- package/src/utils/filterDOMProps.ts +116 -0
- package/src/utils/focus.ts +151 -0
- package/src/utils/geometry.ts +115 -0
- package/src/utils/globalListeners.ts +142 -0
- package/src/utils/index.ts +80 -0
- package/src/utils/mergeProps.ts +52 -0
- package/src/utils/platform.ts +52 -0
- package/src/utils/reactivity.ts +36 -0
- package/src/utils/textSelection.ts +114 -0
- package/src/visually-hidden/createVisuallyHidden.ts +124 -0
- package/src/visually-hidden/index.ts +6 -0
- package/dist/index.jsx +0 -15845
- package/dist/index.jsx.map +0 -7
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PressEvent class that matches React-Aria's PressEvent interface.
|
|
3
|
+
* Wraps native events with press-specific data.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type PointerType = 'mouse' | 'pen' | 'touch' | 'keyboard' | 'virtual';
|
|
7
|
+
export type PressEventType = 'pressstart' | 'pressend' | 'pressup' | 'press';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Minimal event data needed to create a PressEvent.
|
|
11
|
+
* Allows both native Event objects and synthetic event-like objects.
|
|
12
|
+
*/
|
|
13
|
+
export interface PressEventSource {
|
|
14
|
+
currentTarget?: EventTarget | null;
|
|
15
|
+
shiftKey?: boolean;
|
|
16
|
+
ctrlKey?: boolean;
|
|
17
|
+
metaKey?: boolean;
|
|
18
|
+
altKey?: boolean;
|
|
19
|
+
clientX?: number;
|
|
20
|
+
clientY?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface IPressEvent {
|
|
24
|
+
/** The type of press event being fired. */
|
|
25
|
+
type: PressEventType;
|
|
26
|
+
/** The pointer type that triggered the press event. */
|
|
27
|
+
pointerType: PointerType;
|
|
28
|
+
/** The target element of the press event. */
|
|
29
|
+
target: Element;
|
|
30
|
+
/** Whether the shift keyboard modifier was held during the press event. */
|
|
31
|
+
shiftKey: boolean;
|
|
32
|
+
/** Whether the ctrl keyboard modifier was held during the press event. */
|
|
33
|
+
ctrlKey: boolean;
|
|
34
|
+
/** Whether the meta keyboard modifier was held during the press event. */
|
|
35
|
+
metaKey: boolean;
|
|
36
|
+
/** Whether the alt keyboard modifier was held during the press event. */
|
|
37
|
+
altKey: boolean;
|
|
38
|
+
/** X position of the press relative to the target element. */
|
|
39
|
+
x: number;
|
|
40
|
+
/** Y position of the press relative to the target element. */
|
|
41
|
+
y: number;
|
|
42
|
+
/**
|
|
43
|
+
* By default, press events stop propagation to parent elements.
|
|
44
|
+
* Call continuePropagation() to allow the event to bubble up.
|
|
45
|
+
*/
|
|
46
|
+
continuePropagation(): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* PressEvent class that provides all press event data.
|
|
51
|
+
* Based on React-Aria's PressEvent implementation.
|
|
52
|
+
*/
|
|
53
|
+
export class PressEvent implements IPressEvent {
|
|
54
|
+
type: PressEventType;
|
|
55
|
+
pointerType: PointerType;
|
|
56
|
+
target: Element;
|
|
57
|
+
shiftKey: boolean;
|
|
58
|
+
ctrlKey: boolean;
|
|
59
|
+
metaKey: boolean;
|
|
60
|
+
altKey: boolean;
|
|
61
|
+
x: number;
|
|
62
|
+
y: number;
|
|
63
|
+
|
|
64
|
+
#shouldStopPropagation = true;
|
|
65
|
+
|
|
66
|
+
constructor(
|
|
67
|
+
type: PressEventType,
|
|
68
|
+
pointerType: PointerType,
|
|
69
|
+
originalEvent: PressEventSource | null,
|
|
70
|
+
target: Element | null
|
|
71
|
+
) {
|
|
72
|
+
this.type = type;
|
|
73
|
+
this.pointerType = pointerType;
|
|
74
|
+
const eventTarget = target ?? (originalEvent?.currentTarget as Element | undefined);
|
|
75
|
+
this.target = eventTarget as Element;
|
|
76
|
+
|
|
77
|
+
// Extract modifier keys from the original event
|
|
78
|
+
this.shiftKey = originalEvent?.shiftKey ?? false;
|
|
79
|
+
this.ctrlKey = originalEvent?.ctrlKey ?? false;
|
|
80
|
+
this.metaKey = originalEvent?.metaKey ?? false;
|
|
81
|
+
this.altKey = originalEvent?.altKey ?? false;
|
|
82
|
+
|
|
83
|
+
// Calculate position relative to target
|
|
84
|
+
this.x = 0;
|
|
85
|
+
this.y = 0;
|
|
86
|
+
|
|
87
|
+
if (originalEvent && originalEvent.clientX !== undefined && eventTarget) {
|
|
88
|
+
const rect = eventTarget.getBoundingClientRect();
|
|
89
|
+
this.x = originalEvent.clientX - rect.left;
|
|
90
|
+
this.y = (originalEvent.clientY ?? 0) - rect.top;
|
|
91
|
+
} else if (eventTarget) {
|
|
92
|
+
// For keyboard events, use center of element
|
|
93
|
+
const rect = eventTarget.getBoundingClientRect();
|
|
94
|
+
this.x = rect.width / 2;
|
|
95
|
+
this.y = rect.height / 2;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Call this to allow the press event to propagate to parent elements.
|
|
101
|
+
* By default, press events stop propagation.
|
|
102
|
+
*/
|
|
103
|
+
continuePropagation(): void {
|
|
104
|
+
this.#shouldStopPropagation = false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Whether the event should stop propagation.
|
|
109
|
+
* Used internally by the press handler.
|
|
110
|
+
*/
|
|
111
|
+
get shouldStopPropagation(): boolean {
|
|
112
|
+
return this.#shouldStopPropagation;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Creates a PressEvent from a native event or event-like source.
|
|
118
|
+
*/
|
|
119
|
+
export function createPressEvent(
|
|
120
|
+
type: PressEventType,
|
|
121
|
+
pointerType: PointerType,
|
|
122
|
+
originalEvent: PressEventSource | null,
|
|
123
|
+
target: Element | null
|
|
124
|
+
): PressEvent {
|
|
125
|
+
return new PressEvent(type, pointerType, originalEvent, target);
|
|
126
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createFocus - Handles focus events for the immediate target.
|
|
3
|
+
*
|
|
4
|
+
* This is a 1-1 port of React-Aria's useFocus hook adapted for SolidJS.
|
|
5
|
+
* Focus events on child elements will be ignored.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { JSX, onCleanup } from 'solid-js';
|
|
9
|
+
import { getOwnerDocument, getEventTarget } from '../utils';
|
|
10
|
+
function getActiveElement(doc: Document): Element | null {
|
|
11
|
+
let activeElement = doc.activeElement;
|
|
12
|
+
while (activeElement && (activeElement as Element).shadowRoot?.activeElement) {
|
|
13
|
+
activeElement = (activeElement as Element).shadowRoot?.activeElement ?? null;
|
|
14
|
+
}
|
|
15
|
+
return activeElement;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FocusEvents {
|
|
19
|
+
/** Handler that is called when the element receives focus. */
|
|
20
|
+
onFocus?: (e: FocusEvent) => void;
|
|
21
|
+
/** Handler that is called when the element loses focus. */
|
|
22
|
+
onBlur?: (e: FocusEvent) => void;
|
|
23
|
+
/** Handler that is called when the element's focus status changes. */
|
|
24
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CreateFocusProps extends FocusEvents {
|
|
28
|
+
/** Whether the focus events should be disabled. */
|
|
29
|
+
isDisabled?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FocusResult {
|
|
33
|
+
/** Props to spread onto the target element. */
|
|
34
|
+
focusProps: JSX.HTMLAttributes<HTMLElement>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Synthetic blur event handler for Firefox bug workaround.
|
|
39
|
+
* React (and we) don't fire onBlur when an element is disabled.
|
|
40
|
+
* Most browsers fire a native focusout event in this case, except for Firefox.
|
|
41
|
+
* We use a MutationObserver to watch for the disabled attribute.
|
|
42
|
+
*/
|
|
43
|
+
function createSyntheticBlurHandler(
|
|
44
|
+
onBlur: ((e: FocusEvent) => void) | undefined
|
|
45
|
+
): (_e: FocusEvent, target: Element) => (() => void) | undefined {
|
|
46
|
+
let isFocused = false;
|
|
47
|
+
let observer: MutationObserver | null = null;
|
|
48
|
+
|
|
49
|
+
return (_e: FocusEvent, target: Element) => {
|
|
50
|
+
if (
|
|
51
|
+
target instanceof HTMLButtonElement ||
|
|
52
|
+
target instanceof HTMLInputElement ||
|
|
53
|
+
target instanceof HTMLTextAreaElement ||
|
|
54
|
+
target instanceof HTMLSelectElement
|
|
55
|
+
) {
|
|
56
|
+
isFocused = true;
|
|
57
|
+
|
|
58
|
+
const onBlurHandler = (_blurEvent: Event) => {
|
|
59
|
+
isFocused = false;
|
|
60
|
+
|
|
61
|
+
if (observer) {
|
|
62
|
+
observer.disconnect();
|
|
63
|
+
observer = null;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
target.addEventListener('focusout', onBlurHandler, { once: true });
|
|
68
|
+
|
|
69
|
+
observer = new MutationObserver(() => {
|
|
70
|
+
if (isFocused && (target as HTMLButtonElement).disabled) {
|
|
71
|
+
observer?.disconnect();
|
|
72
|
+
const relatedTarget = target === document.activeElement ? null : document.activeElement;
|
|
73
|
+
target.dispatchEvent(new FocusEvent('blur', { relatedTarget }));
|
|
74
|
+
target.dispatchEvent(new FocusEvent('focusout', { bubbles: true, relatedTarget }));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
observer.observe(target, { attributes: true, attributeFilter: ['disabled'] });
|
|
79
|
+
|
|
80
|
+
// Return cleanup function
|
|
81
|
+
return () => {
|
|
82
|
+
if (observer) {
|
|
83
|
+
observer.disconnect();
|
|
84
|
+
observer = null;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return undefined;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Handles focus events for the immediate target.
|
|
95
|
+
* Focus events on child elements will be ignored.
|
|
96
|
+
*
|
|
97
|
+
* Based on react-aria's useFocus but adapted for SolidJS.
|
|
98
|
+
*/
|
|
99
|
+
export function createFocus(props: CreateFocusProps = {}): FocusResult {
|
|
100
|
+
const { isDisabled, onFocus: onFocusProp, onBlur: onBlurProp, onFocusChange } = props;
|
|
101
|
+
|
|
102
|
+
let cleanupRef: (() => void) | undefined;
|
|
103
|
+
const syntheticBlurHandler = createSyntheticBlurHandler(onBlurProp);
|
|
104
|
+
|
|
105
|
+
// Cleanup on unmount
|
|
106
|
+
onCleanup(() => {
|
|
107
|
+
if (cleanupRef) {
|
|
108
|
+
cleanupRef();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const onBlur: JSX.EventHandler<HTMLElement, FocusEvent> = (e) => {
|
|
113
|
+
// Only handle if target is the currentTarget (not bubbled from children)
|
|
114
|
+
if (e.target === e.currentTarget) {
|
|
115
|
+
if (onBlurProp) {
|
|
116
|
+
onBlurProp(e);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (onFocusChange) {
|
|
120
|
+
onFocusChange(false);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
cleanupRef?.();
|
|
124
|
+
cleanupRef = undefined;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const onFocus: JSX.EventHandler<HTMLElement, FocusEvent> = (e) => {
|
|
129
|
+
// Double check that document.activeElement actually matches e.target
|
|
130
|
+
// in case a previously chained focus handler already moved focus somewhere else.
|
|
131
|
+
const ownerDocument = getOwnerDocument(e.target);
|
|
132
|
+
const activeElement = ownerDocument ? getActiveElement(ownerDocument) : null;
|
|
133
|
+
|
|
134
|
+
if (e.target === e.currentTarget && activeElement === getEventTarget(e)) {
|
|
135
|
+
if (onFocusProp) {
|
|
136
|
+
onFocusProp(e);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (onFocusChange) {
|
|
140
|
+
onFocusChange(true);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Set up synthetic blur handler for Firefox bug
|
|
144
|
+
cleanupRef = syntheticBlurHandler(e, e.target);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// If disabled or no handlers, return empty props
|
|
149
|
+
if (isDisabled) {
|
|
150
|
+
return {
|
|
151
|
+
focusProps: {},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const hasHandlers = onFocusProp || onFocusChange || onBlurProp;
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
focusProps: {
|
|
159
|
+
onFocus: hasHandlers ? onFocus : undefined,
|
|
160
|
+
onBlur: onBlurProp || onFocusChange ? onBlur : undefined,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createFocusRing hook for Solidaria
|
|
3
|
+
*
|
|
4
|
+
* Determines whether a focus ring should be visible for a given element.
|
|
5
|
+
* Focus rings are visible when the user navigates with keyboard, but hidden
|
|
6
|
+
* when using mouse/touch.
|
|
7
|
+
*
|
|
8
|
+
* Port of @react-aria/focus useFocusRing.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { type JSX, type Accessor, createSignal, createEffect, onCleanup, createMemo } from 'solid-js';
|
|
12
|
+
import { createFocus } from './createFocus';
|
|
13
|
+
import { createFocusWithin } from './createFocusWithin';
|
|
14
|
+
import {
|
|
15
|
+
createFocusVisibleListener,
|
|
16
|
+
isFocusVisible as isGlobalFocusVisible,
|
|
17
|
+
} from './createInteractionModality';
|
|
18
|
+
|
|
19
|
+
// ============================================
|
|
20
|
+
// TYPES
|
|
21
|
+
// ============================================
|
|
22
|
+
|
|
23
|
+
export interface FocusRingProps {
|
|
24
|
+
/** Whether the element is a text input. */
|
|
25
|
+
isTextInput?: boolean;
|
|
26
|
+
/** Whether the element will be auto focused. */
|
|
27
|
+
autoFocus?: boolean;
|
|
28
|
+
/** Whether focus should be tracked within the element. */
|
|
29
|
+
within?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FocusRingResult {
|
|
33
|
+
/** Whether the element is currently focused. */
|
|
34
|
+
isFocused: Accessor<boolean>;
|
|
35
|
+
/** Whether the focus ring should be visible. */
|
|
36
|
+
isFocusVisible: Accessor<boolean>;
|
|
37
|
+
/** Props to spread on the element to track focus. */
|
|
38
|
+
focusProps: JSX.HTMLAttributes<HTMLElement>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ============================================
|
|
42
|
+
// IMPLEMENTATION
|
|
43
|
+
// ============================================
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Determines whether a focus ring should be visible for a given element.
|
|
47
|
+
*
|
|
48
|
+
* Focus rings are visible when:
|
|
49
|
+
* - The element is focused AND
|
|
50
|
+
* - The user is navigating with keyboard (not mouse/touch)
|
|
51
|
+
*
|
|
52
|
+
* For text inputs, focus rings are always visible when focused.
|
|
53
|
+
*/
|
|
54
|
+
export function createFocusRing(props: FocusRingProps = {}): FocusRingResult {
|
|
55
|
+
const { isTextInput = false, autoFocus = false, within = false } = props;
|
|
56
|
+
|
|
57
|
+
const [isFocused, setIsFocused] = createSignal(false);
|
|
58
|
+
const [focusVisibleFlag, setFocusVisibleFlag] = createSignal(
|
|
59
|
+
autoFocus || isGlobalFocusVisible()
|
|
60
|
+
);
|
|
61
|
+
const isFocusVisible = createMemo(() => isFocused() && focusVisibleFlag());
|
|
62
|
+
|
|
63
|
+
createEffect(() => {
|
|
64
|
+
const cleanup = createFocusVisibleListener((visible) => {
|
|
65
|
+
setFocusVisibleFlag(visible);
|
|
66
|
+
}, { isTextInput });
|
|
67
|
+
onCleanup(cleanup);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const onFocusChange = (focused: boolean) => {
|
|
71
|
+
setIsFocused(focused);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const focusResult = createFocus({
|
|
75
|
+
isDisabled: within,
|
|
76
|
+
onFocusChange,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const focusWithinResult = createFocusWithin({
|
|
80
|
+
isDisabled: !within,
|
|
81
|
+
onFocusWithinChange: onFocusChange,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
isFocused,
|
|
86
|
+
isFocusVisible,
|
|
87
|
+
focusProps: (within ? focusWithinResult.focusWithinProps : focusResult.focusProps) as JSX.HTMLAttributes<HTMLElement>,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createFocusWithin - Handles focus events for the target and its descendants.
|
|
3
|
+
*
|
|
4
|
+
* This is a 1-1 port of React-Aria's useFocusWithin hook adapted for SolidJS.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { JSX, onCleanup } from 'solid-js';
|
|
8
|
+
import { getOwnerDocument, getEventTarget, nodeContains, createGlobalListeners } from '../utils';
|
|
9
|
+
import { setEventTarget } from '../utils/events';
|
|
10
|
+
|
|
11
|
+
export interface FocusWithinProps {
|
|
12
|
+
/** Whether the focus within events should be disabled. */
|
|
13
|
+
isDisabled?: boolean;
|
|
14
|
+
/** Handler that is called when the target element or a descendant receives focus. */
|
|
15
|
+
onFocusWithin?: (e: FocusEvent) => void;
|
|
16
|
+
/** Handler that is called when the target element and all descendants lose focus. */
|
|
17
|
+
onBlurWithin?: (e: FocusEvent) => void;
|
|
18
|
+
/** Handler that is called when the focus within state changes. */
|
|
19
|
+
onFocusWithinChange?: (isFocusWithin: boolean) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface FocusWithinResult {
|
|
23
|
+
/** Props to spread onto the target element. */
|
|
24
|
+
focusWithinProps: JSX.HTMLAttributes<HTMLElement>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getActiveElement(doc: Document): Element | null {
|
|
28
|
+
let activeElement = doc.activeElement;
|
|
29
|
+
while (activeElement && (activeElement as Element).shadowRoot?.activeElement) {
|
|
30
|
+
activeElement = (activeElement as Element).shadowRoot?.activeElement ?? null;
|
|
31
|
+
}
|
|
32
|
+
return activeElement;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createSyntheticBlurHandler(
|
|
36
|
+
onBlurWithin: ((e: FocusEvent) => void) | undefined
|
|
37
|
+
): (_e: FocusEvent, target: Element) => (() => void) | undefined {
|
|
38
|
+
let isFocused = false;
|
|
39
|
+
let observer: MutationObserver | null = null;
|
|
40
|
+
|
|
41
|
+
return (_e: FocusEvent, target: Element) => {
|
|
42
|
+
if (
|
|
43
|
+
target instanceof HTMLButtonElement ||
|
|
44
|
+
target instanceof HTMLInputElement ||
|
|
45
|
+
target instanceof HTMLTextAreaElement ||
|
|
46
|
+
target instanceof HTMLSelectElement
|
|
47
|
+
) {
|
|
48
|
+
isFocused = true;
|
|
49
|
+
|
|
50
|
+
const onBlurHandler = () => {
|
|
51
|
+
isFocused = false;
|
|
52
|
+
|
|
53
|
+
if (observer) {
|
|
54
|
+
observer.disconnect();
|
|
55
|
+
observer = null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
target.addEventListener('focusout', onBlurHandler, { once: true });
|
|
60
|
+
|
|
61
|
+
observer = new MutationObserver(() => {
|
|
62
|
+
if (isFocused && (target as HTMLButtonElement).disabled) {
|
|
63
|
+
observer?.disconnect();
|
|
64
|
+
const relatedTarget = target === document.activeElement ? null : document.activeElement;
|
|
65
|
+
target.dispatchEvent(new FocusEvent('blur', { relatedTarget }));
|
|
66
|
+
target.dispatchEvent(new FocusEvent('focusout', { bubbles: true, relatedTarget }));
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
observer.observe(target, { attributes: true, attributeFilter: ['disabled'] });
|
|
71
|
+
|
|
72
|
+
return () => {
|
|
73
|
+
if (observer) {
|
|
74
|
+
observer.disconnect();
|
|
75
|
+
observer = null;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return undefined;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Handles focus events for the target and its descendants.
|
|
86
|
+
*
|
|
87
|
+
* Based on react-aria's useFocusWithin but adapted for SolidJS.
|
|
88
|
+
*/
|
|
89
|
+
export function createFocusWithin(props: FocusWithinProps = {}): FocusWithinResult {
|
|
90
|
+
const { isDisabled, onBlurWithin, onFocusWithin, onFocusWithinChange } = props;
|
|
91
|
+
|
|
92
|
+
// State tracking
|
|
93
|
+
let isFocusWithin = false;
|
|
94
|
+
|
|
95
|
+
// Global listeners manager
|
|
96
|
+
const { addGlobalListener, removeAllGlobalListeners } = createGlobalListeners();
|
|
97
|
+
const syntheticBlurHandler = createSyntheticBlurHandler(onBlurWithin);
|
|
98
|
+
let cleanupRef: (() => void) | undefined;
|
|
99
|
+
|
|
100
|
+
// Cleanup on unmount
|
|
101
|
+
onCleanup(() => {
|
|
102
|
+
cleanupRef?.();
|
|
103
|
+
removeAllGlobalListeners();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const onBlur: JSX.EventHandler<HTMLElement, FocusEvent> = (e) => {
|
|
107
|
+
// Ignore events bubbling through portals
|
|
108
|
+
if (!e.currentTarget.contains(e.target as Node)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again
|
|
113
|
+
// when moving focus inside the element. Only trigger if the currentTarget doesn't
|
|
114
|
+
// include the relatedTarget (where focus is moving).
|
|
115
|
+
if (isFocusWithin && !e.currentTarget.contains(e.relatedTarget as Node)) {
|
|
116
|
+
isFocusWithin = false;
|
|
117
|
+
removeAllGlobalListeners();
|
|
118
|
+
cleanupRef?.();
|
|
119
|
+
cleanupRef = undefined;
|
|
120
|
+
|
|
121
|
+
if (onBlurWithin) {
|
|
122
|
+
onBlurWithin(e);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (onFocusWithinChange) {
|
|
126
|
+
onFocusWithinChange(false);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const onFocus: JSX.EventHandler<HTMLElement, FocusEvent> = (e) => {
|
|
132
|
+
// Ignore events bubbling through portals
|
|
133
|
+
if (!e.currentTarget.contains(e.target as Node)) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Double check that document.activeElement actually matches e.target
|
|
138
|
+
// in case a previously chained focus handler already moved focus somewhere else.
|
|
139
|
+
const ownerDocument = getOwnerDocument(e.target);
|
|
140
|
+
const activeElement = ownerDocument ? getActiveElement(ownerDocument) : null;
|
|
141
|
+
|
|
142
|
+
if (!isFocusWithin && activeElement === getEventTarget(e)) {
|
|
143
|
+
if (onFocusWithin) {
|
|
144
|
+
onFocusWithin(e);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (onFocusWithinChange) {
|
|
148
|
+
onFocusWithinChange(true);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
isFocusWithin = true;
|
|
152
|
+
cleanupRef = syntheticBlurHandler(e, e.target);
|
|
153
|
+
|
|
154
|
+
// Browsers don't fire blur events when elements are removed from the DOM.
|
|
155
|
+
// However, if a focus event occurs outside the element we're tracking, we
|
|
156
|
+
// can manually fire onBlur.
|
|
157
|
+
const currentTarget = e.currentTarget;
|
|
158
|
+
|
|
159
|
+
addGlobalListener(
|
|
160
|
+
'focus',
|
|
161
|
+
(focusEvent: Event) => {
|
|
162
|
+
if (isFocusWithin && !nodeContains(currentTarget, (focusEvent as FocusEvent).target as Element)) {
|
|
163
|
+
// Create a synthetic blur event
|
|
164
|
+
const window = ownerDocument?.defaultView;
|
|
165
|
+
if (window) {
|
|
166
|
+
const nativeEvent = new window.FocusEvent('blur', {
|
|
167
|
+
relatedTarget: (focusEvent as FocusEvent).target as Element,
|
|
168
|
+
});
|
|
169
|
+
setEventTarget(nativeEvent, currentTarget);
|
|
170
|
+
|
|
171
|
+
isFocusWithin = false;
|
|
172
|
+
removeAllGlobalListeners();
|
|
173
|
+
|
|
174
|
+
if (onBlurWithin) {
|
|
175
|
+
onBlurWithin(nativeEvent);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (onFocusWithinChange) {
|
|
179
|
+
onFocusWithinChange(false);
|
|
180
|
+
}
|
|
181
|
+
cleanupRef?.();
|
|
182
|
+
cleanupRef = undefined;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{ capture: true }
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
if (isDisabled) {
|
|
192
|
+
return {
|
|
193
|
+
focusWithinProps: {
|
|
194
|
+
onFocus: undefined,
|
|
195
|
+
onBlur: undefined,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
focusWithinProps: {
|
|
202
|
+
onFocus,
|
|
203
|
+
onBlur,
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|