@glasshome/ui 1.40.0 → 1.42.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/SPEC.md +25 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/lib/anchor-placement.d.ts +30 -0
- package/dist/lib/behavior/anchored.d.ts +12 -0
- package/dist/lib/behavior/collection.d.ts +24 -0
- package/dist/lib/behavior/controllable.d.ts +8 -0
- package/dist/lib/behavior/dismiss.d.ts +11 -0
- package/dist/lib/behavior/focus-scope.d.ts +18 -0
- package/dist/lib/behavior/hide-outside.d.ts +7 -0
- package/dist/lib/behavior/layer-stack.d.ts +33 -0
- package/dist/lib/behavior/modal-root.d.ts +66 -0
- package/dist/lib/behavior/polymorphic.d.ts +6 -0
- package/dist/lib/behavior/presence.d.ts +18 -0
- package/dist/lib/behavior/solid.d.ts +13 -0
- package/dist/lib/motion-classes.d.ts +5 -2
- package/dist/lib/overlay-classes.d.ts +4 -1
- package/dist/lib/page-cover.d.ts +8 -0
- package/dist/lib/shadow-layer.d.ts +8 -6
- package/dist/solid/alert-dialog.d.ts +13 -15
- package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -6
- package/dist/solid/bottom-sheet/constants.d.ts +1 -1
- package/dist/solid/confirm-dialog.d.ts +22 -0
- package/dist/solid/dialog-parts.d.ts +78 -15
- package/dist/solid/dialog.d.ts +9 -15
- package/dist/solid/docked-panel.d.ts +1 -5
- package/dist/solid/icon-nodes.d.ts +13 -0
- package/dist/solid/index.d.ts +9 -5
- package/dist/solid/index.js +8211 -7974
- package/dist/solid/responsive-dialog.d.ts +5 -6
- package/dist/solid/sheet.d.ts +6 -13
- package/dist/solid/stale-veil.d.ts +7 -0
- package/dist/solid/tabs.d.ts +29 -8
- package/dist/solid/toggle-group.d.ts +30 -5
- package/dist/solid/tooltip.d.ts +71 -9
- package/dist/{utils-DXEj_oXa.js → utils-CxAii-e9.js} +606 -414
- package/package.json +4 -2
- package/src/index.ts +1 -0
- package/src/lib/anchor-placement.ts +68 -2
- package/src/lib/behavior/anchored.ts +88 -0
- package/src/lib/behavior/collection.ts +181 -0
- package/src/lib/behavior/controllable.ts +22 -0
- package/src/lib/behavior/dismiss.ts +145 -0
- package/src/lib/behavior/focus-scope.ts +202 -0
- package/src/lib/behavior/hide-outside.ts +85 -0
- package/src/lib/behavior/layer-stack.ts +125 -0
- package/src/lib/behavior/modal-root.tsx +446 -0
- package/src/lib/behavior/polymorphic.tsx +30 -0
- package/src/lib/behavior/presence.ts +89 -0
- package/src/lib/behavior/solid.ts +30 -0
- package/src/lib/clipboard.ts +3 -2
- package/src/lib/motion-classes.ts +27 -2
- package/src/lib/overlay-classes.ts +9 -4
- package/src/lib/page-cover.ts +47 -0
- package/src/lib/shadow-layer.ts +48 -66
- package/src/solid/alert-dialog.tsx +45 -81
- package/src/solid/alert.tsx +1 -1
- package/src/solid/area-picker.tsx +2 -1
- package/src/solid/badge.tsx +5 -5
- package/src/solid/bottom-sheet/bottom-sheet.tsx +72 -128
- package/src/solid/bottom-sheet/constants.ts +3 -1
- package/src/solid/bottom-sheet/drag-controller.ts +11 -0
- package/src/solid/breadcrumb.tsx +4 -4
- package/src/solid/button-group.tsx +4 -4
- package/src/solid/card.tsx +5 -4
- package/src/solid/confirm-dialog.tsx +62 -0
- package/src/solid/context-menu.tsx +12 -2
- package/src/solid/dialog-parts.tsx +181 -42
- package/src/solid/dialog.tsx +46 -74
- package/src/solid/dock-edit.tsx +5 -4
- package/src/solid/dock.tsx +3 -3
- package/src/solid/dropdown-menu.tsx +15 -7
- package/src/solid/entity-selector.tsx +2 -1
- package/src/solid/form.tsx +4 -3
- package/src/solid/hover-card.tsx +7 -2
- package/src/solid/icon-nodes.ts +71 -0
- package/src/solid/icon-picker.tsx +2 -1
- package/src/solid/icon.tsx +42 -39
- package/src/solid/image-picker.tsx +14 -13
- package/src/solid/index.ts +9 -2
- package/src/solid/input-group.tsx +5 -3
- package/src/solid/input-otp.tsx +2 -1
- package/src/solid/item.tsx +4 -4
- package/src/solid/logo.tsx +5 -4
- package/src/solid/picker-row.tsx +5 -4
- package/src/solid/picker-search.tsx +2 -1
- package/src/solid/popover.tsx +18 -10
- package/src/solid/responsive-dialog.tsx +17 -10
- package/src/solid/sheet.tsx +47 -79
- package/src/solid/spotlight.tsx +23 -11
- package/src/solid/stale-veil.tsx +20 -0
- package/src/solid/switch.tsx +8 -6
- package/src/solid/tabs.tsx +373 -53
- package/src/solid/toggle-group.tsx +305 -93
- package/src/solid/tooltip.tsx +285 -26
- package/src/styles/globals.css +6 -2
- package/src/styles/theme.css +10 -5
- package/dist/lib/when-animations-settle.d.ts +0 -4
- package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
- package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
- package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
- package/src/lib/when-animations-settle.ts +0 -12
- package/src/solid/bottom-sheet/focus-trap.ts +0 -63
- package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
- package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glasshome/ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.42.0",
|
|
4
4
|
"description": "SolidJS component library for GlassHome, built on Kobalte",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"components",
|
|
@@ -85,7 +85,9 @@
|
|
|
85
85
|
"check:publish": "publint && attw --pack . --profile esm-only --exclude-entrypoints ./styles ./styles/theme && bun scripts/check-pack.ts",
|
|
86
86
|
"dev": "tsc -b tsconfig.build.json --force && vite build --watch",
|
|
87
87
|
"lint": "oxlint --type-aware . && oxfmt --check .",
|
|
88
|
-
"test": "vitest run --project '!visual'",
|
|
88
|
+
"test": "vitest run --project '!visual' --project '!webkit'",
|
|
89
|
+
"test:webkit": "vitest run --project webkit",
|
|
90
|
+
"test:webkit:image": "sh dev/webkit-in-image.sh",
|
|
89
91
|
"test:visual": "vitest run --project visual",
|
|
90
92
|
"test:visual:image": "sh dev/visual-in-image.sh",
|
|
91
93
|
"test:visual:update": "sh dev/visual-in-image.sh --update",
|
package/src/index.ts
CHANGED
|
@@ -83,6 +83,7 @@ export {
|
|
|
83
83
|
SCRIM_MOTION,
|
|
84
84
|
SETTLE_MOTION,
|
|
85
85
|
STAGGER,
|
|
86
|
+
STALE_CONTENT,
|
|
86
87
|
} from "./lib/motion-classes.js";
|
|
87
88
|
export { createHold, HOLD_GRACE_MS, HOLD_MS, spendLongPress } from "./lib/hold.js";
|
|
88
89
|
export type { Hold, HoldOptions, HoldPress } from "./lib/hold.js";
|
|
@@ -2,13 +2,80 @@ import type { JSX } from "solid-js";
|
|
|
2
2
|
|
|
3
3
|
const EDGE = 16;
|
|
4
4
|
|
|
5
|
-
interface Box {
|
|
5
|
+
export interface Box {
|
|
6
6
|
left: number;
|
|
7
7
|
top: number;
|
|
8
8
|
width: number;
|
|
9
9
|
height: number;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
export type Side = "top" | "bottom" | "left" | "right";
|
|
13
|
+
export type Align = "start" | "center" | "end";
|
|
14
|
+
export interface AnchorPlace {
|
|
15
|
+
side: Side;
|
|
16
|
+
align: Align;
|
|
17
|
+
offset: number;
|
|
18
|
+
matchAnchorWidth: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface Placement {
|
|
21
|
+
left: number;
|
|
22
|
+
top: number;
|
|
23
|
+
side: Side;
|
|
24
|
+
maxWidth: number;
|
|
25
|
+
maxHeight: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(v, hi));
|
|
29
|
+
|
|
30
|
+
const OPPOSITE: Record<Side, Side> = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
31
|
+
|
|
32
|
+
const crossStart = (align: Align, start: number, size: number, own: number): number =>
|
|
33
|
+
start + { start: 0, center: (size - own) / 2, end: size - own }[align];
|
|
34
|
+
|
|
35
|
+
/** Where a part of this size sits beside its anchor: on the chosen side, or the opposite when that
|
|
36
|
+
* side lacks room and the other has more, aligned along the edge and held 16px inside the viewport. */
|
|
37
|
+
export function placeBeside(
|
|
38
|
+
anchor: Box,
|
|
39
|
+
floating: { width: number; height: number },
|
|
40
|
+
viewport: { width: number; height: number },
|
|
41
|
+
place: AnchorPlace,
|
|
42
|
+
): Placement {
|
|
43
|
+
const { offset } = place;
|
|
44
|
+
const rooms: Record<Side, number> = {
|
|
45
|
+
bottom: viewport.height - EDGE - (anchor.top + anchor.height + offset),
|
|
46
|
+
top: anchor.top - offset - EDGE,
|
|
47
|
+
right: viewport.width - EDGE - (anchor.left + anchor.width + offset),
|
|
48
|
+
left: anchor.left - offset - EDGE,
|
|
49
|
+
};
|
|
50
|
+
const vertical = place.side === "top" || place.side === "bottom";
|
|
51
|
+
const need = vertical ? floating.height : floating.width;
|
|
52
|
+
const other = OPPOSITE[place.side];
|
|
53
|
+
const side = rooms[place.side] >= need || rooms[other] <= rooms[place.side] ? place.side : other;
|
|
54
|
+
const maxWidth = viewport.width - 2 * EDGE;
|
|
55
|
+
const maxHeight = viewport.height - 2 * EDGE;
|
|
56
|
+
const width = Math.min(
|
|
57
|
+
Math.max(floating.width, place.matchAnchorWidth && vertical ? anchor.width : 0),
|
|
58
|
+
maxWidth,
|
|
59
|
+
);
|
|
60
|
+
const height = Math.min(floating.height, maxHeight);
|
|
61
|
+
const along: Record<Side, number> = {
|
|
62
|
+
bottom: anchor.top + anchor.height + offset,
|
|
63
|
+
top: anchor.top - offset - height,
|
|
64
|
+
right: anchor.left + anchor.width + offset,
|
|
65
|
+
left: anchor.left - offset - width,
|
|
66
|
+
};
|
|
67
|
+
const beside = side === "top" || side === "bottom";
|
|
68
|
+
const left = beside ? crossStart(place.align, anchor.left, anchor.width, width) : along[side];
|
|
69
|
+
const top = beside ? along[side] : crossStart(place.align, anchor.top, anchor.height, height);
|
|
70
|
+
return {
|
|
71
|
+
left: clamp(left, EDGE, viewport.width - EDGE - width),
|
|
72
|
+
top: clamp(top, EDGE, viewport.height - EDGE - height),
|
|
73
|
+
side,
|
|
74
|
+
maxWidth,
|
|
75
|
+
maxHeight,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
12
79
|
/** Where a panel of this size sits over its anchor: centred on it, shifted only as far as the
|
|
13
80
|
* screen needs, as wide as the anchor at least. Before the panel is measured (`height`
|
|
14
81
|
* undefined) its top sits on the anchor's top. */
|
|
@@ -18,7 +85,6 @@ function anchoredBox(
|
|
|
18
85
|
width: number,
|
|
19
86
|
height?: number,
|
|
20
87
|
): Box {
|
|
21
|
-
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(v, hi));
|
|
22
88
|
const w = Math.min(Math.max(width, anchor.width), viewport.width - 2 * EDGE);
|
|
23
89
|
const x = clamp(anchor.left + (anchor.width - w) / 2, EDGE, viewport.width - EDGE - w);
|
|
24
90
|
const h = Math.min(Math.max(height ?? 0, anchor.height), viewport.height - 2 * EDGE);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Accessor } from "solid-js";
|
|
2
|
+
import { type Align, placeBeside, type Side } from "../anchor-placement";
|
|
3
|
+
import { effect } from "./solid";
|
|
4
|
+
|
|
5
|
+
export interface AnchoredOptions {
|
|
6
|
+
anchor: Accessor<Element | undefined>;
|
|
7
|
+
floating: Accessor<HTMLElement | undefined>;
|
|
8
|
+
open: Accessor<boolean>;
|
|
9
|
+
side?: Accessor<Side>;
|
|
10
|
+
align?: Accessor<Align>;
|
|
11
|
+
offset?: number;
|
|
12
|
+
matchAnchorWidth?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const SHADOW_ROOT_NODE = 11;
|
|
16
|
+
|
|
17
|
+
export function createAnchored(options: AnchoredOptions): void {
|
|
18
|
+
effect(
|
|
19
|
+
() =>
|
|
20
|
+
[
|
|
21
|
+
options.open(),
|
|
22
|
+
options.anchor(),
|
|
23
|
+
options.floating(),
|
|
24
|
+
options.side?.() ?? "bottom",
|
|
25
|
+
options.align?.() ?? "center",
|
|
26
|
+
] as const,
|
|
27
|
+
([open, anchor, floating, side, align]) => {
|
|
28
|
+
if (!open || !anchor || !floating) return;
|
|
29
|
+
const offset = options.offset ?? 0;
|
|
30
|
+
const matchAnchorWidth = options.matchAnchorWidth ?? false;
|
|
31
|
+
const written = new Map<string, string>();
|
|
32
|
+
const write = (name: string, value: string) => {
|
|
33
|
+
if (written.get(name) === value) return;
|
|
34
|
+
written.set(name, value);
|
|
35
|
+
floating.style.setProperty(name, value);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let size = { width: 0, height: 0 };
|
|
39
|
+
const place = (measure: boolean) => {
|
|
40
|
+
if (measure) {
|
|
41
|
+
write("position", "fixed");
|
|
42
|
+
write("margin", "0");
|
|
43
|
+
write("right", "auto");
|
|
44
|
+
write("bottom", "auto");
|
|
45
|
+
size = { width: floating.offsetWidth, height: floating.offsetHeight };
|
|
46
|
+
}
|
|
47
|
+
const box = anchor.getBoundingClientRect();
|
|
48
|
+
const placed = placeBeside(
|
|
49
|
+
{ left: box.left, top: box.top, width: box.width, height: box.height },
|
|
50
|
+
size,
|
|
51
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
52
|
+
{ side, align, offset, matchAnchorWidth },
|
|
53
|
+
);
|
|
54
|
+
write("top", `${placed.top}px`);
|
|
55
|
+
write("left", `${placed.left}px`);
|
|
56
|
+
write("max-width", `${placed.maxWidth}px`);
|
|
57
|
+
write("max-height", `${placed.maxHeight}px`);
|
|
58
|
+
if (matchAnchorWidth) write("min-width", `${box.width}px`);
|
|
59
|
+
if (floating.getAttribute("data-side") !== placed.side)
|
|
60
|
+
floating.setAttribute("data-side", placed.side);
|
|
61
|
+
};
|
|
62
|
+
place(true);
|
|
63
|
+
|
|
64
|
+
let frame: number | undefined;
|
|
65
|
+
const onScroll = () => {
|
|
66
|
+
if (frame !== undefined) return;
|
|
67
|
+
frame = requestAnimationFrame(() => {
|
|
68
|
+
frame = undefined;
|
|
69
|
+
place(false);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const onResize = () => place(true);
|
|
73
|
+
|
|
74
|
+
const root = typeof anchor.getRootNode === "function" ? anchor.getRootNode() : document;
|
|
75
|
+
const scrollTargets: EventTarget[] =
|
|
76
|
+
root.nodeType === SHADOW_ROOT_NODE ? [document, root] : [document];
|
|
77
|
+
for (const target of scrollTargets)
|
|
78
|
+
target.addEventListener("scroll", onScroll, { capture: true, passive: true });
|
|
79
|
+
window.addEventListener("resize", onResize);
|
|
80
|
+
return () => {
|
|
81
|
+
for (const target of scrollTargets)
|
|
82
|
+
target.removeEventListener("scroll", onScroll, { capture: true });
|
|
83
|
+
window.removeEventListener("resize", onResize);
|
|
84
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { type Accessor, createSignal, onCleanup } from "solid-js";
|
|
2
|
+
import { focusWithoutTracking } from "./focus-scope";
|
|
3
|
+
import { effect } from "./solid";
|
|
4
|
+
|
|
5
|
+
export const TYPEAHEAD_MS = 1000;
|
|
6
|
+
|
|
7
|
+
type Orientation = "horizontal" | "vertical";
|
|
8
|
+
type HighlightBy = "keyboard" | "pointer";
|
|
9
|
+
|
|
10
|
+
export interface CollectionOptions {
|
|
11
|
+
root: Accessor<HTMLElement | undefined>;
|
|
12
|
+
itemSelector: string;
|
|
13
|
+
orientation: Accessor<Orientation>;
|
|
14
|
+
focus: "roving" | "virtual";
|
|
15
|
+
loop?: Accessor<boolean>;
|
|
16
|
+
dir?: Accessor<"ltr" | "rtl">;
|
|
17
|
+
/** Keys that hold the tab stop while focus is outside, first present wins (a selected tab). */
|
|
18
|
+
preferred?: Accessor<readonly string[]>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Collection {
|
|
22
|
+
highlighted: Accessor<string | undefined>;
|
|
23
|
+
highlight: (key: string, by: HighlightBy) => void;
|
|
24
|
+
tabIndex: (key: string) => 0 | -1;
|
|
25
|
+
onKeyDown: (event: KeyboardEvent) => void;
|
|
26
|
+
/** Items call this as they mount, unmount or change disabled, so the tab stop always sits on a present, enabled item. */
|
|
27
|
+
refresh: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isDisabled = (el: HTMLElement) =>
|
|
31
|
+
el.getAttribute("aria-disabled") === "true" ||
|
|
32
|
+
el.hasAttribute("data-disabled") ||
|
|
33
|
+
el.matches(":disabled");
|
|
34
|
+
|
|
35
|
+
const keyOf = (el: HTMLElement) => el.dataset.key ?? "";
|
|
36
|
+
|
|
37
|
+
function focusIsWithin(root: HTMLElement): boolean {
|
|
38
|
+
const active = (root.getRootNode() as Document | ShadowRoot).activeElement;
|
|
39
|
+
return !!active && root.contains(active);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function step(
|
|
43
|
+
items: HTMLElement[],
|
|
44
|
+
from: number,
|
|
45
|
+
by: number,
|
|
46
|
+
loop: boolean,
|
|
47
|
+
): HTMLElement | undefined {
|
|
48
|
+
if (from < 0) return by > 0 ? items[0] : items.at(-1);
|
|
49
|
+
const next = from + by;
|
|
50
|
+
if (loop) return items[(next + items.length) % items.length];
|
|
51
|
+
return items[Math.max(0, Math.min(items.length - 1, next))];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function forwardKeys(orientation: Orientation, rtl: boolean): { next: string; prev: string } {
|
|
55
|
+
if (orientation === "vertical") return { next: "ArrowDown", prev: "ArrowUp" };
|
|
56
|
+
return rtl
|
|
57
|
+
? { next: "ArrowLeft", prev: "ArrowRight" }
|
|
58
|
+
: { next: "ArrowRight", prev: "ArrowLeft" };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const textOf = (el: HTMLElement) => (el.textContent ?? "").trim().toLowerCase();
|
|
62
|
+
|
|
63
|
+
function matchText(items: HTMLElement[], from: number, buffer: string): HTMLElement | undefined {
|
|
64
|
+
const first = buffer.charAt(0);
|
|
65
|
+
const cycling = buffer.length > 1 && buffer === first.repeat(buffer.length);
|
|
66
|
+
const search = cycling ? first : buffer;
|
|
67
|
+
const start = cycling || buffer.length === 1 ? from + 1 : Math.max(from, 0);
|
|
68
|
+
for (let i = 0; i < items.length; i++) {
|
|
69
|
+
const item = items[(start + i) % items.length];
|
|
70
|
+
if (item && textOf(item).startsWith(search)) return item;
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function createCollection(options: CollectionOptions): Collection {
|
|
76
|
+
const [own, setOwn] = createSignal<string>();
|
|
77
|
+
const highlighted = () => own() ?? options.preferred?.()[0];
|
|
78
|
+
const enabledItems = (root: HTMLElement) =>
|
|
79
|
+
[...root.querySelectorAll<HTMLElement>(options.itemSelector)].filter((el) => !isDisabled(el));
|
|
80
|
+
|
|
81
|
+
function placeStop(root: HTMLElement | undefined, preferred: readonly string[]) {
|
|
82
|
+
if (!root || focusIsWithin(root)) return;
|
|
83
|
+
const items = enabledItems(root);
|
|
84
|
+
const stop =
|
|
85
|
+
items.find((el) => preferred.includes(keyOf(el))) ??
|
|
86
|
+
items.find((el) => keyOf(el) === own()) ??
|
|
87
|
+
items[0];
|
|
88
|
+
setOwn(stop ? keyOf(stop) : undefined);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let disposed = false;
|
|
92
|
+
let queued = false;
|
|
93
|
+
onCleanup(() => (disposed = true));
|
|
94
|
+
|
|
95
|
+
// A microtask, so an unmounting item has left the DOM and a burst of changes places the stop once.
|
|
96
|
+
function refresh() {
|
|
97
|
+
if (options.focus !== "roving" || queued) return;
|
|
98
|
+
queued = true;
|
|
99
|
+
queueMicrotask(() => {
|
|
100
|
+
queued = false;
|
|
101
|
+
if (!disposed) placeStop(options.root(), options.preferred?.() ?? []);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (options.focus === "roving") {
|
|
106
|
+
effect(
|
|
107
|
+
() => [options.root(), options.preferred?.() ?? []] as const,
|
|
108
|
+
([root, preferred]) => placeStop(root, preferred),
|
|
109
|
+
);
|
|
110
|
+
effect(options.root, (root) => {
|
|
111
|
+
if (!root) return;
|
|
112
|
+
const leave = (event: FocusEvent) => {
|
|
113
|
+
if (!(event.relatedTarget instanceof Node && root.contains(event.relatedTarget))) refresh();
|
|
114
|
+
};
|
|
115
|
+
root.addEventListener("focusout", leave);
|
|
116
|
+
return () => root.removeEventListener("focusout", leave);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let buffer = "";
|
|
121
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
122
|
+
onCleanup(() => clearTimeout(timer));
|
|
123
|
+
|
|
124
|
+
function moveTo(el: HTMLElement | undefined) {
|
|
125
|
+
if (!el) return;
|
|
126
|
+
setOwn(keyOf(el));
|
|
127
|
+
if (options.focus === "roving") focusWithoutTracking(el);
|
|
128
|
+
el.scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function highlight(key: string, by: HighlightBy) {
|
|
132
|
+
if (by === "pointer") return void setOwn(key);
|
|
133
|
+
const root = options.root();
|
|
134
|
+
const el = root
|
|
135
|
+
? [...root.querySelectorAll<HTMLElement>(options.itemSelector)].find((i) => keyOf(i) === key)
|
|
136
|
+
: undefined;
|
|
137
|
+
if (el) moveTo(el);
|
|
138
|
+
else setOwn(key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function typeahead(event: KeyboardEvent, items: HTMLElement[], current: number): boolean {
|
|
142
|
+
const char = event.key;
|
|
143
|
+
if (char.length !== 1 || (char === " " && buffer === "")) return false;
|
|
144
|
+
buffer += char.toLowerCase();
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
timer = setTimeout(() => (buffer = ""), TYPEAHEAD_MS);
|
|
147
|
+
moveTo(matchText(items, current, buffer));
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function onKeyDown(event: KeyboardEvent) {
|
|
152
|
+
if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
|
|
153
|
+
const root = options.root();
|
|
154
|
+
if (!root) return;
|
|
155
|
+
const items = enabledItems(root);
|
|
156
|
+
const key = highlighted();
|
|
157
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
158
|
+
let current = items.findIndex((el) => keyOf(el) === key);
|
|
159
|
+
if (current < 0 && target) current = items.findIndex((el) => el.contains(target));
|
|
160
|
+
const { next, prev } = forwardKeys(options.orientation(), options.dir?.() === "rtl");
|
|
161
|
+
const loop = options.loop?.() ?? false;
|
|
162
|
+
const destinations: Record<string, () => HTMLElement | undefined> = {
|
|
163
|
+
[next]: () => step(items, current, 1, loop),
|
|
164
|
+
[prev]: () => step(items, current, -1, loop),
|
|
165
|
+
Home: () => items[0],
|
|
166
|
+
End: () => items.at(-1),
|
|
167
|
+
};
|
|
168
|
+
const destination = destinations[event.key];
|
|
169
|
+
if (destination) moveTo(destination());
|
|
170
|
+
else if (!typeahead(event, items, current)) return;
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
highlighted,
|
|
176
|
+
highlight,
|
|
177
|
+
tabIndex: (key) => (options.focus === "roving" && key === highlighted() ? 0 : -1),
|
|
178
|
+
onKeyDown,
|
|
179
|
+
refresh,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createSignal, untrack, type Accessor } from "solid-js";
|
|
2
|
+
|
|
3
|
+
export interface ControllableOptions<T> {
|
|
4
|
+
value: Accessor<T | undefined>;
|
|
5
|
+
defaultValue: Accessor<T>;
|
|
6
|
+
onChange?: (next: T) => void;
|
|
7
|
+
equals?: (a: T, b: T) => boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function createControllableSignal<T>(
|
|
11
|
+
options: ControllableOptions<T>,
|
|
12
|
+
): [Accessor<T>, (next: T) => void] {
|
|
13
|
+
const [stored, setStored] = createSignal<T>(untrack(options.defaultValue), { equals: false });
|
|
14
|
+
const get = (): T => options.value() ?? stored();
|
|
15
|
+
const set = (next: T) => {
|
|
16
|
+
const same = options.equals ?? Object.is;
|
|
17
|
+
if (same(next, untrack(get))) return;
|
|
18
|
+
setStored(() => next);
|
|
19
|
+
options.onChange?.(next);
|
|
20
|
+
};
|
|
21
|
+
return [get, set];
|
|
22
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Accessor } from "solid-js";
|
|
2
|
+
import { type Layer, type LayerHandle, layerStack } from "./layer-stack";
|
|
3
|
+
import { effect } from "./solid";
|
|
4
|
+
|
|
5
|
+
export type DismissReason = "escape" | "outside-press";
|
|
6
|
+
|
|
7
|
+
export interface DismissOptions {
|
|
8
|
+
layer: LayerHandle;
|
|
9
|
+
/** `event` is the keydown for `escape` and the pointerdown for `outside-press`. */
|
|
10
|
+
onDismiss: (reason: DismissReason, event: KeyboardEvent | PointerEvent) => void;
|
|
11
|
+
outsidePress?: Accessor<boolean>;
|
|
12
|
+
escapeFrom?: "layer" | "document";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const SHADOW_ROOT_NODE = 11;
|
|
16
|
+
|
|
17
|
+
function rootOf(element: HTMLElement): Document | ShadowRoot {
|
|
18
|
+
const root = typeof element.getRootNode === "function" ? element.getRootNode() : document;
|
|
19
|
+
return root.nodeType === SHADOW_ROOT_NODE ? (root as ShadowRoot) : document;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const takesKeys = (entry: Layer): boolean => entry.element() !== undefined;
|
|
23
|
+
|
|
24
|
+
function createOutsidePress(options: DismissOptions) {
|
|
25
|
+
const { layer } = options;
|
|
26
|
+
effect(
|
|
27
|
+
() => [layer.layer()?.element(), options.outsidePress?.() ?? true] as const,
|
|
28
|
+
([element, enabled]) => {
|
|
29
|
+
if (!element || !enabled) return;
|
|
30
|
+
const root = rootOf(element);
|
|
31
|
+
const host = root === document ? undefined : (root as ShadowRoot).host;
|
|
32
|
+
const targets = host ? [root, document] : [root];
|
|
33
|
+
|
|
34
|
+
const outside = (event: Event): boolean => {
|
|
35
|
+
const mine = layer.layer();
|
|
36
|
+
if (!mine || (mine.kind !== "menu" && !layer.isTop())) return false;
|
|
37
|
+
const full = event.composedPath();
|
|
38
|
+
const listener = event.currentTarget;
|
|
39
|
+
const end = listener === document || !listener ? -1 : full.indexOf(listener);
|
|
40
|
+
return !layer.containsPath(end >= 0 ? full.slice(0, end) : full);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
let pending: { target: EventTarget; click: (e: Event) => void } | undefined;
|
|
44
|
+
const dropPending = () => {
|
|
45
|
+
if (pending) pending.target.removeEventListener("click", pending.click);
|
|
46
|
+
pending = undefined;
|
|
47
|
+
};
|
|
48
|
+
const onDown = (event: PointerEvent) => {
|
|
49
|
+
if (event.currentTarget === document && host && event.target === host) return;
|
|
50
|
+
dropPending();
|
|
51
|
+
if (!outside(event)) return;
|
|
52
|
+
const target = event.currentTarget;
|
|
53
|
+
if (event.pointerType === "touch" && target) {
|
|
54
|
+
const click = (e: Event) => {
|
|
55
|
+
pending = undefined;
|
|
56
|
+
if (outside(e)) options.onDismiss("outside-press", event);
|
|
57
|
+
};
|
|
58
|
+
pending = { target, click };
|
|
59
|
+
target.addEventListener("click", click, { once: true });
|
|
60
|
+
} else {
|
|
61
|
+
options.onDismiss("outside-press", event);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const listener = onDown as EventListener;
|
|
66
|
+
const timer = setTimeout(() => {
|
|
67
|
+
for (const target of targets) target.addEventListener("pointerdown", listener, true);
|
|
68
|
+
}, 0);
|
|
69
|
+
return () => {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
for (const target of targets) target.removeEventListener("pointerdown", listener, true);
|
|
72
|
+
dropPending();
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function createTooltipEscape(options: DismissOptions) {
|
|
79
|
+
const { layer } = options;
|
|
80
|
+
const onKey = (event: KeyboardEvent) => {
|
|
81
|
+
if (event.key !== "Escape" || event.isComposing) return;
|
|
82
|
+
const mine = layer.layer();
|
|
83
|
+
if (!mine || !mine.escape() || !takesKeys(mine)) return;
|
|
84
|
+
const stack = layerStack.layers();
|
|
85
|
+
if (stack.slice(stack.indexOf(mine) + 1).some((entry) => entry.kind !== "tooltip")) return;
|
|
86
|
+
options.onDismiss("escape", event);
|
|
87
|
+
};
|
|
88
|
+
document.addEventListener("keydown", onKey, true);
|
|
89
|
+
return () => document.removeEventListener("keydown", onKey, true);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const escapeHandlers = new WeakMap<Layer, (event: KeyboardEvent) => void>();
|
|
93
|
+
const claimedKeys = new WeakSet<Event>();
|
|
94
|
+
|
|
95
|
+
function createEscape(options: DismissOptions) {
|
|
96
|
+
const { layer } = options;
|
|
97
|
+
effect(
|
|
98
|
+
() =>
|
|
99
|
+
[
|
|
100
|
+
layer.layer(),
|
|
101
|
+
layer.layer()?.element(),
|
|
102
|
+
layer.layer()?.kind === "tooltip" ? "tooltip" : (options.escapeFrom ?? "layer"),
|
|
103
|
+
] as const,
|
|
104
|
+
([entry, element, from]) => {
|
|
105
|
+
if (!entry || !element) return;
|
|
106
|
+
if (from === "tooltip") return createTooltipEscape(options);
|
|
107
|
+
escapeHandlers.set(entry, (event) => options.onDismiss("escape", event));
|
|
108
|
+
// The root element, so the key is taken before Kobalte's document listeners hear it.
|
|
109
|
+
const target = from === "document" ? document.documentElement : element;
|
|
110
|
+
const onKey = (event: KeyboardEvent) => {
|
|
111
|
+
if (event.key !== "Escape" || event.isComposing || claimedKeys.has(event)) return;
|
|
112
|
+
const mine = layer.layer();
|
|
113
|
+
if (!mine || !takesKeys(mine)) return;
|
|
114
|
+
const stack = layerStack.layers().slice(layerStack.layers().indexOf(mine));
|
|
115
|
+
const topmost = stack.findLast((other) => other.kind !== "tooltip" && takesKeys(other));
|
|
116
|
+
if (from === "document" && topmost !== mine && topmost?.readsOwnEscape) return;
|
|
117
|
+
const claims = stack.findLast(
|
|
118
|
+
(other) =>
|
|
119
|
+
other.kind !== "tooltip" &&
|
|
120
|
+
takesKeys(other) &&
|
|
121
|
+
(other.escape() || (other.kind === "modal" && !other.readsOwnEscape)),
|
|
122
|
+
);
|
|
123
|
+
if (!claims) return;
|
|
124
|
+
claimedKeys.add(event);
|
|
125
|
+
event.stopPropagation();
|
|
126
|
+
if (claims.escape()) {
|
|
127
|
+
const handler = escapeHandlers.get(claims);
|
|
128
|
+
if (handler) handler(event);
|
|
129
|
+
else claims.dismiss?.();
|
|
130
|
+
}
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
};
|
|
133
|
+
target.addEventListener("keydown", onKey as EventListener);
|
|
134
|
+
return () => {
|
|
135
|
+
target.removeEventListener("keydown", onKey as EventListener);
|
|
136
|
+
escapeHandlers.delete(entry);
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function createDismiss(options: DismissOptions): void {
|
|
143
|
+
createOutsidePress(options);
|
|
144
|
+
createEscape(options);
|
|
145
|
+
}
|