@inertiaui/vanilla 1.3.1 → 1.4.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/README.md +73 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +143 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -282,6 +282,7 @@ cleanup()
|
|
|
282
282
|
| `orientation` | `'vertical' \| 'horizontal'` | `'vertical'` | Arrow key direction |
|
|
283
283
|
| `loop` | `boolean` | `true` | Wrap focus from last to first item |
|
|
284
284
|
| `typeAhead` | `boolean` | `true` | Enable type-ahead character search |
|
|
285
|
+
| `initialFocus` | `'first' \| 'last'` | `'first'` | Initial enabled item to focus and give the active tab stop |
|
|
285
286
|
| `onActivate` | `(item: HTMLElement) => void` | `undefined` | Called when an item is activated via Enter or Space |
|
|
286
287
|
|
|
287
288
|
### Keyboard Support
|
|
@@ -687,6 +688,75 @@ function setupTooltip(trigger: HTMLElement, tooltip: HTMLElement) {
|
|
|
687
688
|
|
|
688
689
|
## Native Popovers
|
|
689
690
|
|
|
691
|
+
### Managed popovers and menus
|
|
692
|
+
|
|
693
|
+
`createPopoverController` composes the disclosure, positioning, dismissal, and menu helpers into a framework-independent lifecycle. It supports native auto popovers and a positioned fallback. The existing `createNativePopoverDisclosure` API below remains available for consumers that need to manage each step themselves.
|
|
694
|
+
|
|
695
|
+
```html
|
|
696
|
+
<button id="trigger" aria-haspopup="menu" aria-expanded="false" aria-controls="menu">Actions</button>
|
|
697
|
+
<div id="menu" role="menu" hidden>
|
|
698
|
+
<button role="menuitem">Edit</button>
|
|
699
|
+
<button role="menuitem">Archive</button>
|
|
700
|
+
</div>
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
```typescript
|
|
704
|
+
import { createPopoverController } from '@inertiaui/vanilla'
|
|
705
|
+
|
|
706
|
+
const trigger = document.querySelector<HTMLElement>('#trigger')!
|
|
707
|
+
const menu = document.querySelector<HTMLElement>('#menu')!
|
|
708
|
+
const controller = createPopoverController({
|
|
709
|
+
reference: () => trigger,
|
|
710
|
+
popover: () => menu,
|
|
711
|
+
position: { placement: 'bottom-start', offset: 8 },
|
|
712
|
+
menu: true,
|
|
713
|
+
onOpenChange(open) {
|
|
714
|
+
trigger.setAttribute('aria-expanded', String(open))
|
|
715
|
+
menu.hidden = !open
|
|
716
|
+
if (open) queueMicrotask(() => controller.mount())
|
|
717
|
+
},
|
|
718
|
+
})
|
|
719
|
+
|
|
720
|
+
if (controller.native) menu.setAttribute('popover', 'auto')
|
|
721
|
+
trigger.addEventListener('pointerdown', controller.onPointerDown)
|
|
722
|
+
trigger.addEventListener('click', controller.toggle)
|
|
723
|
+
trigger.addEventListener('keydown', controller.onKeydown)
|
|
724
|
+
trigger.addEventListener('focusout', controller.onFocusOut)
|
|
725
|
+
|
|
726
|
+
// On disposal, also remove the four trigger listeners if the trigger remains in the DOM.
|
|
727
|
+
// The caller owns rendering and removing the panel.
|
|
728
|
+
controller.cleanup()
|
|
729
|
+
```
|
|
730
|
+
|
|
731
|
+
In Vue, call `mount()` after the open panel has rendered with `nextTick()`. In React, call it from a layout effect and return `cleanup()` from that effect. Cleanup preserves logical open state so an effect can safely clean up and remount. It hides the native panel and removes the controller's listeners, timers, positioning observers, menu navigation, and mount-hook resources. Use `close()` for a state change and a close notification.
|
|
732
|
+
|
|
733
|
+
Keep native panels in their original DOM ancestry: the browser places them in the top layer without losing CSS inheritance. Fallback panels need a suitable DOM container; inside a modal dialog, keep them within that dialog. The controller does not move elements, assign classes, copy theme tokens, or animate. Set the panel's `role`, trigger ARIA attributes, and `popover="auto"` when `native` is true. Without `menu: true`, callers choose initial panel focus through `onOpened`.
|
|
734
|
+
|
|
735
|
+
| Option | Description |
|
|
736
|
+
|--------|-------------|
|
|
737
|
+
| `reference`, `popover` | Getters returning the current elements or `null` while unmounted |
|
|
738
|
+
| `position` | `TopLayerPopoverPositionOptions`; getters can supply current placement and offset |
|
|
739
|
+
| `menu` | Enables menu navigation, ArrowDown/ArrowUp opening, and closing after a menu-item click |
|
|
740
|
+
| `onOpenChange(open)` | Updates the framework's rendering state |
|
|
741
|
+
| `onMount({ reference, popover, native })` | Applies consumer behavior after positioning; may return a cleanup function |
|
|
742
|
+
| `onOpened()` | Runs after the panel is mounted and its handlers are installed |
|
|
743
|
+
| `onClosed()` | Runs when explicitly closed or dismissed; cleanup alone does not call it |
|
|
744
|
+
|
|
745
|
+
The controller resolves top/bottom `start` and `end` placement against the reference element's computed direction, including local RTL scopes. It follows scrolling and resizing, constrains panels to the viewport, restores trigger focus on Escape, and preserves focus when the user moves to another control. Menu clicks finish bubbling before dismissal, allowing framework-delegated item handlers to run. ArrowUp opens at the last enabled item with a matching roving tab stop.
|
|
746
|
+
|
|
747
|
+
| Controller member | Description |
|
|
748
|
+
|-------------------|-------------|
|
|
749
|
+
| `native`, `isOpen` | Read-only native support and current logical open state |
|
|
750
|
+
| `open()`, `close(restoreFocus?)` | Change open state; `close()` normally restores focus only when focus is inside the panel or on the body |
|
|
751
|
+
| `toggle(event?)` | Toggles state; pass the click event together with the pointerdown handler to avoid reopening after native light dismissal |
|
|
752
|
+
| `mount()` | Attaches behavior after rendering the open panel |
|
|
753
|
+
| `cleanup()` | Detaches behavior without changing logical open state |
|
|
754
|
+
| `onPointerDown`, `onKeydown`, `onFocusOut` | Handlers to bind to the trigger |
|
|
755
|
+
|
|
756
|
+
Consumer-specific theme or animation behavior can live in `onMount`. Return its cleanup so it also runs when a panel is dismissed, unmounted, or remounted. Use `prefersReducedMotion()` before starting consumer animations.
|
|
757
|
+
|
|
758
|
+
### Low-level native disclosure
|
|
759
|
+
|
|
690
760
|
`createNativePopoverDisclosure` coordinates open state, native `showPopover()`/`hidePopover()`, top-layer positioning, auto-updates, toggle-event sync, and optional focus-out dismissal.
|
|
691
761
|
|
|
692
762
|
```typescript
|
|
@@ -1587,6 +1657,9 @@ import type {
|
|
|
1587
1657
|
ListboxNavigationOptions,
|
|
1588
1658
|
ListboxNavigationResult,
|
|
1589
1659
|
MenuNavigationOptions,
|
|
1660
|
+
PopoverController,
|
|
1661
|
+
PopoverControllerOptions,
|
|
1662
|
+
PopoverMountContext,
|
|
1590
1663
|
NativePopoverCloseOptions,
|
|
1591
1664
|
NativePopoverDisclosureController,
|
|
1592
1665
|
NativePopoverDisclosureOptions,
|
package/dist/index.d.ts
CHANGED
|
@@ -94,6 +94,9 @@ export declare function createMenuNavigation(container: HTMLElement, options?: M
|
|
|
94
94
|
|
|
95
95
|
export declare function createNativePopoverDisclosure(options: NativePopoverDisclosureOptions): NativePopoverDisclosureController;
|
|
96
96
|
|
|
97
|
+
/** Mount after rendering the open panel; cleanup detaches behavior without discarding open state. */
|
|
98
|
+
export declare function createPopoverController(options: PopoverControllerOptions): PopoverController;
|
|
99
|
+
|
|
97
100
|
/**
|
|
98
101
|
* Framework-neutral list reorder controller. It combines item registration,
|
|
99
102
|
* live pointer preview ordering, keyboard moves, pointer commits, bounds, and
|
|
@@ -262,6 +265,7 @@ export declare interface MenuNavigationOptions {
|
|
|
262
265
|
orientation?: 'vertical' | 'horizontal';
|
|
263
266
|
loop?: boolean;
|
|
264
267
|
typeAhead?: boolean;
|
|
268
|
+
initialFocus?: 'first' | 'last';
|
|
265
269
|
onActivate?: (item: HTMLElement) => void;
|
|
266
270
|
}
|
|
267
271
|
|
|
@@ -399,6 +403,36 @@ export declare function parseRgbColor(input: string): ParsedColor | null;
|
|
|
399
403
|
|
|
400
404
|
export declare type Placement = 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end';
|
|
401
405
|
|
|
406
|
+
export declare interface PopoverController {
|
|
407
|
+
readonly native: boolean;
|
|
408
|
+
readonly isOpen: boolean;
|
|
409
|
+
open: () => void;
|
|
410
|
+
close: (restoreFocus?: boolean) => void;
|
|
411
|
+
toggle: (event?: MouseEvent) => void;
|
|
412
|
+
mount: () => void;
|
|
413
|
+
cleanup: CleanupFunction;
|
|
414
|
+
onKeydown: (event: KeyboardEvent) => void;
|
|
415
|
+
onFocusOut: (event: FocusEvent) => void;
|
|
416
|
+
onPointerDown: () => void;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export declare interface PopoverControllerOptions {
|
|
420
|
+
reference: () => HTMLElement | null;
|
|
421
|
+
popover: () => HTMLElement | null;
|
|
422
|
+
position: TopLayerPopoverPositionOptions;
|
|
423
|
+
menu?: boolean;
|
|
424
|
+
onOpenChange?: (open: boolean) => void;
|
|
425
|
+
onMount?: (context: PopoverMountContext) => CleanupFunction | void;
|
|
426
|
+
onOpened?: () => void;
|
|
427
|
+
onClosed?: () => void;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export declare interface PopoverMountContext {
|
|
431
|
+
reference: HTMLElement;
|
|
432
|
+
popover: HTMLElement;
|
|
433
|
+
native: boolean;
|
|
434
|
+
}
|
|
435
|
+
|
|
402
436
|
declare interface PositionOptions_2 {
|
|
403
437
|
placement?: Placement;
|
|
404
438
|
offset?: number;
|
package/dist/index.js
CHANGED
|
@@ -490,7 +490,7 @@ function onClickOutside(elements, callback) {
|
|
|
490
490
|
//#endregion
|
|
491
491
|
//#region src/menu.ts
|
|
492
492
|
function createMenuNavigation(container, options = {}) {
|
|
493
|
-
const { itemSelector = "[role=\"menuitem\"]:not([disabled]):not([aria-disabled=\"true\"])", orientation = "vertical", loop = true, typeAhead = true, onActivate } = options;
|
|
493
|
+
const { itemSelector = "[role=\"menuitem\"]:not([disabled]):not([aria-disabled=\"true\"])", orientation = "vertical", loop = true, typeAhead = true, initialFocus = "first", onActivate } = options;
|
|
494
494
|
let typeAheadBuffer = "";
|
|
495
495
|
let typeAheadTimeout;
|
|
496
496
|
function getItems() {
|
|
@@ -559,11 +559,12 @@ function createMenuNavigation(container, options = {}) {
|
|
|
559
559
|
}
|
|
560
560
|
container.addEventListener("keydown", handleKeyDown);
|
|
561
561
|
const items = getItems();
|
|
562
|
-
|
|
562
|
+
const initialIndex = initialFocus === "last" ? items.length - 1 : 0;
|
|
563
|
+
items.forEach((el, i) => el.setAttribute("tabindex", i === initialIndex ? "0" : "-1"));
|
|
563
564
|
let rafId;
|
|
564
565
|
if (items.length > 0) rafId = requestAnimationFrame(() => {
|
|
565
566
|
rafId = void 0;
|
|
566
|
-
items[
|
|
567
|
+
items[initialIndex].focus();
|
|
567
568
|
});
|
|
568
569
|
let cleaned = false;
|
|
569
570
|
return function cleanup() {
|
|
@@ -2084,6 +2085,144 @@ function createNativePopoverDisclosure(options) {
|
|
|
2084
2085
|
};
|
|
2085
2086
|
}
|
|
2086
2087
|
//#endregion
|
|
2088
|
+
//#region src/popover.ts
|
|
2089
|
+
/** Mount after rendering the open panel; cleanup detaches behavior without discarding open state. */
|
|
2090
|
+
function createPopoverController(options) {
|
|
2091
|
+
const native = typeof HTMLElement !== "undefined" && "showPopover" in HTMLElement.prototype;
|
|
2092
|
+
let cleanups = [];
|
|
2093
|
+
let mountedPanel = null;
|
|
2094
|
+
let pointerStartedOpen = false;
|
|
2095
|
+
let focusLast = false;
|
|
2096
|
+
const disclosure = createNativePopoverDisclosure({
|
|
2097
|
+
reference: options.reference,
|
|
2098
|
+
popover: options.popover,
|
|
2099
|
+
get position() {
|
|
2100
|
+
let placement = options.position.placement ?? "bottom-start";
|
|
2101
|
+
const reference = options.reference();
|
|
2102
|
+
if (reference && getComputedStyle(reference).direction === "rtl" && /^(top|bottom)-/.test(placement)) placement = placement.replace(/start|end/, (side) => side === "start" ? "end" : "start");
|
|
2103
|
+
return {
|
|
2104
|
+
...options.position,
|
|
2105
|
+
placement,
|
|
2106
|
+
anchorPositioning: native && options.position.anchorPositioning !== false
|
|
2107
|
+
};
|
|
2108
|
+
},
|
|
2109
|
+
focusOut: {
|
|
2110
|
+
container: options.reference,
|
|
2111
|
+
shouldIgnore: () => !!options.popover()?.contains(document.activeElement),
|
|
2112
|
+
onDismiss: () => close(false)
|
|
2113
|
+
},
|
|
2114
|
+
onOpenChange: options.onOpenChange
|
|
2115
|
+
});
|
|
2116
|
+
function cleanup(hide = true) {
|
|
2117
|
+
cleanups.forEach((cleanup) => cleanup());
|
|
2118
|
+
cleanups = [];
|
|
2119
|
+
disclosure.cleanupPopover();
|
|
2120
|
+
if (hide && native && mountedPanel?.matches(":popover-open")) mountedPanel.hidePopover();
|
|
2121
|
+
mountedPanel = null;
|
|
2122
|
+
}
|
|
2123
|
+
function returnFocus() {
|
|
2124
|
+
const reference = options.reference();
|
|
2125
|
+
(reference?.matches("button, input, select, textarea, a[href], [tabindex]") ? reference : reference?.querySelector("button:not([disabled]), input, select, textarea, a[href], [tabindex]"))?.focus({ preventScroll: true });
|
|
2126
|
+
}
|
|
2127
|
+
function shouldReturnFocus() {
|
|
2128
|
+
return typeof document !== "undefined" && (!!mountedPanel?.contains(document.activeElement) || document.activeElement === document.body);
|
|
2129
|
+
}
|
|
2130
|
+
function close(restoreFocus = shouldReturnFocus()) {
|
|
2131
|
+
disclosure.closePopover({
|
|
2132
|
+
hide: false,
|
|
2133
|
+
onClose() {
|
|
2134
|
+
cleanup();
|
|
2135
|
+
if (restoreFocus) returnFocus();
|
|
2136
|
+
options.onClosed?.();
|
|
2137
|
+
}
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
function onKeydown(event) {
|
|
2141
|
+
if (event.key === "Escape" && disclosure.isOpen) {
|
|
2142
|
+
event.preventDefault();
|
|
2143
|
+
event.stopPropagation();
|
|
2144
|
+
close(true);
|
|
2145
|
+
} else if (options.menu && !disclosure.isOpen && ["ArrowDown", "ArrowUp"].includes(event.key)) {
|
|
2146
|
+
event.preventDefault();
|
|
2147
|
+
focusLast = event.key === "ArrowUp";
|
|
2148
|
+
disclosure.openPopover();
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
function mount() {
|
|
2152
|
+
const reference = options.reference();
|
|
2153
|
+
const panel = options.popover();
|
|
2154
|
+
if (!disclosure.isOpen || !reference || !panel) return;
|
|
2155
|
+
cleanup();
|
|
2156
|
+
mountedPanel = panel;
|
|
2157
|
+
const onBeforeToggle = (event) => {
|
|
2158
|
+
const restoreFocus = shouldReturnFocus();
|
|
2159
|
+
disclosure.handlePopoverToggle(event, { onClose() {
|
|
2160
|
+
cleanup(false);
|
|
2161
|
+
if (restoreFocus) returnFocus();
|
|
2162
|
+
options.onClosed?.();
|
|
2163
|
+
} });
|
|
2164
|
+
};
|
|
2165
|
+
panel.addEventListener("beforetoggle", onBeforeToggle);
|
|
2166
|
+
panel.addEventListener("keydown", onKeydown);
|
|
2167
|
+
panel.addEventListener("focusout", disclosure.handleFocusOut);
|
|
2168
|
+
cleanups.push(() => {
|
|
2169
|
+
panel.removeEventListener("beforetoggle", onBeforeToggle);
|
|
2170
|
+
panel.removeEventListener("keydown", onKeydown);
|
|
2171
|
+
panel.removeEventListener("focusout", disclosure.handleFocusOut);
|
|
2172
|
+
});
|
|
2173
|
+
if (native) disclosure.showPopover();
|
|
2174
|
+
else {
|
|
2175
|
+
disclosure.updatePosition();
|
|
2176
|
+
cleanups.push(onClickOutside([reference, panel], () => close()));
|
|
2177
|
+
cleanups.push(onEscapeKey(() => close(true)));
|
|
2178
|
+
}
|
|
2179
|
+
disclosure.startAutoUpdate();
|
|
2180
|
+
const cleanupMount = options.onMount?.({
|
|
2181
|
+
reference,
|
|
2182
|
+
popover: panel,
|
|
2183
|
+
native
|
|
2184
|
+
});
|
|
2185
|
+
if (cleanupMount) cleanups.push(cleanupMount);
|
|
2186
|
+
if (options.menu) {
|
|
2187
|
+
cleanups.push(createMenuNavigation(panel, { initialFocus: focusLast ? "last" : "first" }));
|
|
2188
|
+
focusLast = false;
|
|
2189
|
+
const onClick = (event) => {
|
|
2190
|
+
if (event.target.closest("[role=\"menuitem\"]")) {
|
|
2191
|
+
const timer = setTimeout(() => {
|
|
2192
|
+
if (mountedPanel === panel) close();
|
|
2193
|
+
}, 0);
|
|
2194
|
+
cleanups.push(() => clearTimeout(timer));
|
|
2195
|
+
}
|
|
2196
|
+
};
|
|
2197
|
+
panel.addEventListener("click", onClick);
|
|
2198
|
+
cleanups.push(() => panel.removeEventListener("click", onClick));
|
|
2199
|
+
}
|
|
2200
|
+
options.onOpened?.();
|
|
2201
|
+
}
|
|
2202
|
+
function toggle(event) {
|
|
2203
|
+
const wasOpen = !!event?.detail && pointerStartedOpen;
|
|
2204
|
+
pointerStartedOpen = false;
|
|
2205
|
+
if (wasOpen) close();
|
|
2206
|
+
else disclosure.togglePopover(() => disclosure.openPopover(), () => close());
|
|
2207
|
+
}
|
|
2208
|
+
return {
|
|
2209
|
+
native,
|
|
2210
|
+
get isOpen() {
|
|
2211
|
+
return disclosure.isOpen;
|
|
2212
|
+
},
|
|
2213
|
+
open: () => disclosure.openPopover(),
|
|
2214
|
+
close,
|
|
2215
|
+
mount,
|
|
2216
|
+
cleanup,
|
|
2217
|
+
toggle,
|
|
2218
|
+
onKeydown,
|
|
2219
|
+
onFocusOut: disclosure.handleFocusOut,
|
|
2220
|
+
onPointerDown: () => {
|
|
2221
|
+
pointerStartedOpen = disclosure.isOpen;
|
|
2222
|
+
}
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
//#endregion
|
|
2087
2226
|
//#region src/blank.ts
|
|
2088
2227
|
/**
|
|
2089
2228
|
* Port of Laravel's `blank` function.
|
|
@@ -2475,4 +2614,4 @@ function formatColor(color, format = "hex", options = {}) {
|
|
|
2475
2614
|
return includeAlpha ? `${normalized.hex}${Math.round(normalized.alpha * 255).toString(16).padStart(2, "0")}` : normalized.hex;
|
|
2476
2615
|
}
|
|
2477
2616
|
//#endregion
|
|
2478
|
-
export { REORDERABLE_LIST_HANDLE_ATTRIBUTE, animate, animateFromSnapshot, autoUpdate, autoUpdateTopLayerPopover, blank, cancelAnimations, captureAnimationSnapshot, computePosition, createDebouncer, createFocusOutDismiss, createFocusTrap, createMenuNavigation, createNativePopoverDisclosure, createReorderableList, debounce, detectFramerate, easings, except, focusFirstEnabledElement, formatAlpha, formatColor, generateId, hexToHsl, hexToRgb, hslToHex, isRtl, isStandardDomEvent, kebabCase, lockScroll, markAriaHidden, markInert, normalizeAlpha, normalizeHue, normalizePercent, onClickOutside, onEscapeKey, onRtlChange, onceChildrenRendered, only, parseAlphaChannel, parseColorString, parseHexColor, parseHslColor, parseRgbChannel, parseRgbColor, parsedFromHex, positionTopLayerPopover, prefersDarkMode, prefersReducedMotion, rejectNullValues, resolveListboxNavigation, rgbToHex, sameUrlPath, supportsAnchorPositioning, supportsTopLayerAnchorPositioning, supportsWebAnimations };
|
|
2617
|
+
export { REORDERABLE_LIST_HANDLE_ATTRIBUTE, animate, animateFromSnapshot, autoUpdate, autoUpdateTopLayerPopover, blank, cancelAnimations, captureAnimationSnapshot, computePosition, createDebouncer, createFocusOutDismiss, createFocusTrap, createMenuNavigation, createNativePopoverDisclosure, createPopoverController, createReorderableList, debounce, detectFramerate, easings, except, focusFirstEnabledElement, formatAlpha, formatColor, generateId, hexToHsl, hexToRgb, hslToHex, isRtl, isStandardDomEvent, kebabCase, lockScroll, markAriaHidden, markInert, normalizeAlpha, normalizeHue, normalizePercent, onClickOutside, onEscapeKey, onRtlChange, onceChildrenRendered, only, parseAlphaChannel, parseColorString, parseHexColor, parseHslColor, parseRgbChannel, parseRgbColor, parsedFromHex, positionTopLayerPopover, prefersDarkMode, prefersReducedMotion, rejectNullValues, resolveListboxNavigation, rgbToHex, sameUrlPath, supportsAnchorPositioning, supportsTopLayerAnchorPositioning, supportsWebAnimations };
|