@glasshome/ui 1.33.1 → 1.34.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/dist/lib/shadow-layer.d.ts +45 -0
- package/dist/solid/context-menu.d.ts +1 -20
- package/dist/solid/index.d.ts +1 -0
- package/dist/solid/index.js +3298 -2969
- package/dist/solid/media-store.d.ts +2 -1
- package/dist/solid/portal-mount.d.ts +7 -0
- package/package.json +5 -4
- package/scripts/check-gallery.ts +1 -1
- package/src/lib/shadow-layer.ts +136 -0
- package/src/solid/alert-dialog.tsx +39 -6
- package/src/solid/bottom-sheet/bottom-sheet.tsx +37 -16
- package/src/solid/bottom-sheet/focus-trap.ts +8 -5
- package/src/solid/context-menu.tsx +42 -4
- package/src/solid/dialog.tsx +20 -4
- package/src/solid/docked-panel.tsx +2 -1
- package/src/solid/dropdown-menu.tsx +45 -7
- package/src/solid/hover-card.tsx +13 -3
- package/src/solid/index.ts +1 -0
- package/src/solid/media-store.ts +4 -2
- package/src/solid/menu-parts.tsx +40 -2
- package/src/solid/popover.tsx +29 -3
- package/src/solid/portal-mount.ts +10 -0
- package/src/solid/select.tsx +34 -4
- package/src/solid/sheet.tsx +33 -5
- package/src/solid/spotlight.tsx +2 -1
- package/src/solid/tooltip.tsx +13 -3
|
@@ -32,7 +32,8 @@ export interface MediaStore {
|
|
|
32
32
|
remove(id: string): Promise<void>;
|
|
33
33
|
url(id: string, variant?: MediaVariant): string;
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
/** `null` withholds the store from a tree, even where the host registered one. */
|
|
36
|
+
export declare const MediaStoreContext: import("solid-js").Context<MediaStore | null | undefined>;
|
|
36
37
|
/** Host apps call this once at startup; MediaStoreContext overrides it per-tree. */
|
|
37
38
|
export declare function provideMediaStore(store: MediaStore): void;
|
|
38
39
|
export declare function useMediaStore(): MediaStore | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where overlays (dialogs, sheets, popovers, menus, tooltips) portal to. Unset, they portal to
|
|
3
|
+
* `document.body`; a host that renders a tree inside a shadow root sets it to a node in that root,
|
|
4
|
+
* so the overlay keeps the root's styles and never reaches the page.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PortalMountContext: import("solid-js").Context<Node | undefined>;
|
|
7
|
+
export declare const usePortalMount: () => Node | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glasshome/ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.34.0",
|
|
4
4
|
"description": "SolidJS component library for GlassHome, built on Kobalte",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"components",
|
|
@@ -75,15 +75,15 @@
|
|
|
75
75
|
"access": "public"
|
|
76
76
|
},
|
|
77
77
|
"scripts": {
|
|
78
|
-
"build": "rm -rf dist && vite build && tsc",
|
|
78
|
+
"build": "rm -rf dist && vite build && tsc -p tsconfig.build.json",
|
|
79
79
|
"check:structural": "bun scripts/check-structural-classes.ts",
|
|
80
80
|
"check:dead": "fallow dead-code --fail-on-issues",
|
|
81
81
|
"check:gallery": "bun scripts/check-gallery.ts",
|
|
82
82
|
"check:styling": "fallow health --css",
|
|
83
83
|
"check:tokens": "bun scripts/check-tokens.ts",
|
|
84
|
-
"check:types": "tsc --noEmit
|
|
84
|
+
"check:types": "tsc --noEmit",
|
|
85
85
|
"check:publish": "publint && attw --pack . --profile esm-only --exclude-entrypoints ./styles ./styles/theme && bun scripts/check-pack.ts",
|
|
86
|
-
"dev": "tsc -b --force && vite build --watch",
|
|
86
|
+
"dev": "tsc -b tsconfig.build.json --force && vite build --watch",
|
|
87
87
|
"lint": "oxlint --type-aware . && oxfmt --check .",
|
|
88
88
|
"test": "vitest run",
|
|
89
89
|
"test:watch": "vitest",
|
|
@@ -110,6 +110,7 @@
|
|
|
110
110
|
"@iconify-json/simple-icons": "^1.2.95",
|
|
111
111
|
"@solidjs/testing-library": "^0.8.10",
|
|
112
112
|
"@tailwindcss/vite": "^4.1.13",
|
|
113
|
+
"@types/bun": "^1.4.0",
|
|
113
114
|
"@types/node": "^25.2.2",
|
|
114
115
|
"fallow": "3.17.0",
|
|
115
116
|
"happy-dom": "^20.14.0",
|
package/scripts/check-gallery.ts
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
import { ALLOW } from "../dev/coverage-allow";
|
|
23
23
|
|
|
24
24
|
const pkg = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
25
|
-
const CONFIG = join(pkg, "tsconfig.
|
|
25
|
+
const CONFIG = join(pkg, "tsconfig.json");
|
|
26
26
|
const ENTRY = join(pkg, "src/solid/index.ts");
|
|
27
27
|
const GROUPS = join(pkg, "dev/groups");
|
|
28
28
|
const SCREAMING_CASE = /^[A-Z][A-Z0-9_]*$/;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { type Accessor, createEffect, onCleanup } from "solid-js";
|
|
2
|
+
import { type InitialFocus, trapFocus } from "../solid/bottom-sheet/focus-trap.js";
|
|
3
|
+
|
|
4
|
+
/** True for a node inside a shadow root, where page-level layer logic sees only the root's host. */
|
|
5
|
+
export const inShadowRoot = (node: Node | undefined): boolean =>
|
|
6
|
+
typeof ShadowRoot !== "undefined" && node?.getRootNode() instanceof ShadowRoot;
|
|
7
|
+
|
|
8
|
+
/** Layers open in each shadow root, oldest first: only the newest dismisses on a press, as Kobalte's own stack does. */
|
|
9
|
+
const stacks = new WeakMap<ShadowRoot, Element[]>();
|
|
10
|
+
|
|
11
|
+
type OutsideEvent = { target: EventTarget | null; preventDefault(): void };
|
|
12
|
+
type AutoFocusEvent = { preventDefault(): void };
|
|
13
|
+
|
|
14
|
+
export interface LayerHandlers {
|
|
15
|
+
onInteractOutside: (e: OutsideEvent) => void;
|
|
16
|
+
onCloseAutoFocus: (e: AutoFocusEvent) => void;
|
|
17
|
+
onOpenAutoFocus: (e: AutoFocusEvent) => void;
|
|
18
|
+
/** The shadow root's host while the layer is in one: what the page sees as the target of anything inside. */
|
|
19
|
+
host: () => Element | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ShadowLayerOptions {
|
|
23
|
+
content: Accessor<HTMLElement | undefined>;
|
|
24
|
+
open: Accessor<boolean>;
|
|
25
|
+
/** Absent for layers that close on pointer leave (tooltips, hover cards): they never join the stack. */
|
|
26
|
+
dismiss?: () => void;
|
|
27
|
+
/** A press on it is the trigger's own toggle, never an outside press. */
|
|
28
|
+
trigger?: Accessor<Element | undefined>;
|
|
29
|
+
/** Menus: any press outside the layer and the layers above it dismisses it, newest or not, as Kobalte's menus do. */
|
|
30
|
+
tree?: boolean;
|
|
31
|
+
/** The layer keeps Tab inside itself (dialogs); its initial focus is then ui's, not Kobalte's. */
|
|
32
|
+
trapsFocus?: boolean;
|
|
33
|
+
initialFocus?: InitialFocus;
|
|
34
|
+
consumer: {
|
|
35
|
+
onInteractOutside?: (e: never) => void;
|
|
36
|
+
onCloseAutoFocus?: (e: never) => void;
|
|
37
|
+
onOpenAutoFocus?: (e: never) => void;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Kobalte decides "outside" from the page, where a press or focus inside a shadow root is one on
|
|
43
|
+
* the root's host, so a layer portaled into a shadow root would dismiss on every press inside it.
|
|
44
|
+
* The handlers ignore those; while the layer is open in a shadow root, a press elsewhere in that
|
|
45
|
+
* root dismisses the newest layer from a listener on the root itself. Outside a shadow root the
|
|
46
|
+
* handlers only pass the consumer's own through.
|
|
47
|
+
*/
|
|
48
|
+
export function shadowLayer(options: ShadowLayerOptions): LayerHandlers {
|
|
49
|
+
let dismissedIn: ShadowRoot | undefined;
|
|
50
|
+
createEffect(() => {
|
|
51
|
+
const el = options.content();
|
|
52
|
+
const dismiss = options.dismiss;
|
|
53
|
+
if (options.open()) dismissedIn = undefined;
|
|
54
|
+
if (!el || !dismiss || !options.open() || !inShadowRoot(el)) return;
|
|
55
|
+
const root = el.getRootNode() as ShadowRoot;
|
|
56
|
+
const stack = stacks.get(root) ?? [];
|
|
57
|
+
stacks.set(root, stack);
|
|
58
|
+
stack.push(el);
|
|
59
|
+
const inside = (e: Event) => {
|
|
60
|
+
const path = e.composedPath();
|
|
61
|
+
const trigger = options.trigger?.();
|
|
62
|
+
if (path.includes(el) || (trigger && path.includes(trigger))) return true;
|
|
63
|
+
return stack.slice(stack.indexOf(el) + 1).some((above) => path.includes(above));
|
|
64
|
+
};
|
|
65
|
+
const dismisses = (e: Event) => (options.tree || stack[stack.length - 1] === el) && !inside(e);
|
|
66
|
+
const close = () => {
|
|
67
|
+
dismissedIn = root;
|
|
68
|
+
dismiss();
|
|
69
|
+
// The caller refused the close (a controlled `open`): the next close is not this one.
|
|
70
|
+
if (options.open()) dismissedIn = undefined;
|
|
71
|
+
};
|
|
72
|
+
let pendingClick: ((e: Event) => void) | undefined;
|
|
73
|
+
const dropPendingClick = () => {
|
|
74
|
+
if (pendingClick) root.removeEventListener("click", pendingClick);
|
|
75
|
+
pendingClick = undefined;
|
|
76
|
+
};
|
|
77
|
+
const onDown = (e: Event) => {
|
|
78
|
+
dropPendingClick();
|
|
79
|
+
if (!dismisses(e)) return;
|
|
80
|
+
// A touch dismisses on its own click, judged again then: a scroll never clicks.
|
|
81
|
+
if ((e as PointerEvent).pointerType === "touch") {
|
|
82
|
+
pendingClick = (click) => {
|
|
83
|
+
pendingClick = undefined;
|
|
84
|
+
if (dismisses(click)) close();
|
|
85
|
+
};
|
|
86
|
+
root.addEventListener("click", pendingClick, { once: true });
|
|
87
|
+
} else {
|
|
88
|
+
close();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// Listening only after the opening press has finished, as Kobalte's own layers do.
|
|
92
|
+
const timer = setTimeout(() => root.addEventListener("pointerdown", onDown, true), 0);
|
|
93
|
+
if (options.trapsFocus) {
|
|
94
|
+
const trap = trapFocus(el, options.initialFocus ?? "first");
|
|
95
|
+
onCleanup(() => trap.release());
|
|
96
|
+
}
|
|
97
|
+
onCleanup(() => {
|
|
98
|
+
clearTimeout(timer);
|
|
99
|
+
root.removeEventListener("pointerdown", onDown, true);
|
|
100
|
+
dropPendingClick();
|
|
101
|
+
stack.splice(stack.indexOf(el), 1);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
const shadowHost = () => {
|
|
105
|
+
const root = options.content()?.getRootNode();
|
|
106
|
+
return typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot ? root.host : undefined;
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
host: shadowHost,
|
|
110
|
+
onInteractOutside: (e) => {
|
|
111
|
+
const host = shadowHost();
|
|
112
|
+
if (host && e.target === host) e.preventDefault();
|
|
113
|
+
options.consumer.onInteractOutside?.(e as never);
|
|
114
|
+
},
|
|
115
|
+
onCloseAutoFocus: (e) => {
|
|
116
|
+
const root = dismissedIn;
|
|
117
|
+
dismissedIn = undefined;
|
|
118
|
+
if (root) {
|
|
119
|
+
// Dismissed by a press elsewhere in the root: focus stays where that press put it, even
|
|
120
|
+
// for the Kobalte layers that focus their trigger past preventDefault.
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
const kept = root.activeElement;
|
|
123
|
+
queueMicrotask(() => {
|
|
124
|
+
if (kept instanceof HTMLElement && kept.isConnected && root.activeElement !== kept) {
|
|
125
|
+
kept.focus();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
options.consumer.onCloseAutoFocus?.(e as never);
|
|
130
|
+
},
|
|
131
|
+
onOpenAutoFocus: (e) => {
|
|
132
|
+
if (options.trapsFocus && shadowHost()) e.preventDefault();
|
|
133
|
+
options.consumer.onOpenAutoFocus?.(e as never);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { AlertDialog as AlertDialogPrimitive } from "@kobalte/core/alert-dialog";
|
|
2
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
2
3
|
import { useDialogContext } from "@kobalte/core/dialog";
|
|
3
4
|
import type { VariantProps } from "cva";
|
|
4
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
type Component,
|
|
7
|
+
type ComponentProps,
|
|
8
|
+
createSignal,
|
|
9
|
+
type ParentComponent,
|
|
10
|
+
splitProps,
|
|
11
|
+
} from "solid-js";
|
|
12
|
+
import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
|
|
5
13
|
import { buttonVariants } from "../lib/button-variants.js";
|
|
6
14
|
import { cn } from "../lib/utils.js";
|
|
7
15
|
import {
|
|
@@ -18,9 +26,10 @@ import {
|
|
|
18
26
|
type ModalSize,
|
|
19
27
|
} from "./dialog-parts.js";
|
|
20
28
|
|
|
21
|
-
const AlertDialog: ParentComponent<ComponentProps<typeof AlertDialogPrimitive>> = (props) =>
|
|
22
|
-
|
|
23
|
-
)
|
|
29
|
+
const AlertDialog: ParentComponent<ComponentProps<typeof AlertDialogPrimitive>> = (props) => {
|
|
30
|
+
const mount = usePortalMount();
|
|
31
|
+
return <AlertDialogPrimitive modal={!inShadowRoot(mount)} {...props} preventScroll={false} />;
|
|
32
|
+
};
|
|
24
33
|
|
|
25
34
|
const AlertDialogTrigger: Component<ComponentProps<typeof AlertDialogPrimitive.Trigger>> = (
|
|
26
35
|
props,
|
|
@@ -46,13 +55,37 @@ type AlertDialogContentProps = ComponentProps<typeof AlertDialogPrimitive.Conten
|
|
|
46
55
|
};
|
|
47
56
|
|
|
48
57
|
const AlertDialogContent: ParentComponent<AlertDialogContentProps> = (props) => {
|
|
49
|
-
const [local, rest] = splitProps(props, [
|
|
58
|
+
const [local, rest] = splitProps(props, [
|
|
59
|
+
"class",
|
|
60
|
+
"children",
|
|
61
|
+
"size",
|
|
62
|
+
"ariaLabel",
|
|
63
|
+
"ref",
|
|
64
|
+
"onInteractOutside",
|
|
65
|
+
"onCloseAutoFocus",
|
|
66
|
+
"onOpenAutoFocus",
|
|
67
|
+
]);
|
|
50
68
|
const context = useDialogContext();
|
|
69
|
+
const [panel, setPanel] = createSignal<HTMLElement>();
|
|
70
|
+
const layer = shadowLayer({
|
|
71
|
+
content: panel,
|
|
72
|
+
open: () => context.isOpen(),
|
|
73
|
+
dismiss: () => context.close(),
|
|
74
|
+
trapsFocus: true,
|
|
75
|
+
consumer: local,
|
|
76
|
+
});
|
|
51
77
|
return (
|
|
52
|
-
<AlertDialogPrimitive.Portal>
|
|
78
|
+
<AlertDialogPrimitive.Portal mount={usePortalMount()}>
|
|
53
79
|
<ModalScrollLock />
|
|
54
80
|
<AlertDialogOverlay />
|
|
55
81
|
<AlertDialogPrimitive.Content
|
|
82
|
+
ref={(el: HTMLElement) => {
|
|
83
|
+
setPanel(el);
|
|
84
|
+
if (typeof local.ref === "function") local.ref(el);
|
|
85
|
+
}}
|
|
86
|
+
onInteractOutside={layer.onInteractOutside}
|
|
87
|
+
onCloseAutoFocus={layer.onCloseAutoFocus}
|
|
88
|
+
onOpenAutoFocus={layer.onOpenAutoFocus}
|
|
56
89
|
data-slot="alert-dialog-content"
|
|
57
90
|
role="alertdialog"
|
|
58
91
|
aria-label={context.titleId() ? undefined : local.ariaLabel}
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
type ValidComponent,
|
|
16
16
|
} from "solid-js";
|
|
17
17
|
import { Dynamic, Portal } from "solid-js/web";
|
|
18
|
+
import { usePortalMount } from "../portal-mount.js";
|
|
18
19
|
import { Z } from "../../lib/layers.js";
|
|
19
20
|
import { OVERLAY_SURFACE_OPAQUE } from "../../lib/overlay-classes.js";
|
|
20
21
|
import { cn } from "../../lib/utils.js";
|
|
@@ -147,10 +148,11 @@ interface BottomSheetPortalProps {
|
|
|
147
148
|
|
|
148
149
|
const BottomSheetPortal: Component<BottomSheetPortalProps> = (props) => {
|
|
149
150
|
const ctx = useBottomSheetContext();
|
|
151
|
+
const portalMount = usePortalMount();
|
|
150
152
|
const mounted = () => ctx.state() !== "closed";
|
|
151
153
|
return (
|
|
152
154
|
<Show when={mounted()}>
|
|
153
|
-
<Portal mount={props.mount}>{props.children}</Portal>
|
|
155
|
+
<Portal mount={props.mount ?? portalMount}>{props.children}</Portal>
|
|
154
156
|
</Show>
|
|
155
157
|
);
|
|
156
158
|
};
|
|
@@ -283,28 +285,46 @@ const BottomSheetContent: ParentComponent<BottomSheetContentProps> = (props) =>
|
|
|
283
285
|
if (!ctx.dismissible()) return;
|
|
284
286
|
|
|
285
287
|
let attached = false;
|
|
286
|
-
let pendingClick: ((
|
|
288
|
+
let pendingClick: ((click: Event) => void) | null = null;
|
|
287
289
|
|
|
288
290
|
const PORTAL_LAYER_SELECTOR =
|
|
289
291
|
'[data-kb-top-layer], [role="dialog"], [role="menu"], [role="listbox"], [role="combobox"], [role="tooltip"]';
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
if (target
|
|
295
|
-
|
|
292
|
+
// Judged along the composed path: a press inside an icon's own shadow root is still inside the
|
|
293
|
+
// sheet. Only the part below the listening root counts: a realm's host inside a page dialog
|
|
294
|
+
// must not make every press in the realm look like one inside a layer.
|
|
295
|
+
const isOutside = (e: Event): boolean => {
|
|
296
|
+
if (!el || !(e.target instanceof Element)) return false;
|
|
297
|
+
const full = e.composedPath();
|
|
298
|
+
const listener = e.currentTarget;
|
|
299
|
+
const end = listener && listener !== document ? full.indexOf(listener) : -1;
|
|
300
|
+
const path = end >= 0 ? full.slice(0, end) : full;
|
|
301
|
+
if (path.includes(el)) return false;
|
|
302
|
+
return !path.some(
|
|
303
|
+
(node) =>
|
|
304
|
+
node instanceof Element &&
|
|
305
|
+
(node.matches("[data-sheet-content]") || node.matches(PORTAL_LAYER_SELECTOR)),
|
|
306
|
+
);
|
|
296
307
|
};
|
|
297
308
|
|
|
298
|
-
|
|
299
|
-
|
|
309
|
+
// Inside a shadow root the page sees every press there as one on the root's host, so the
|
|
310
|
+
// sheet judges those from its own root and only the page's other presses from the document.
|
|
311
|
+
const scope = (el?.getRootNode() ?? document) as Document | ShadowRoot;
|
|
312
|
+
const scopes: (Document | ShadowRoot)[] = scope === document ? [document] : [scope, document];
|
|
313
|
+
const host = scope instanceof ShadowRoot ? scope.host : undefined;
|
|
314
|
+
const onPointerDown = (e: Event) => {
|
|
315
|
+
if (e.currentTarget === document && host && e.target === host) return;
|
|
316
|
+
// Every press starts over: a touch that scrolled never clicked, and its close must not wait for the next tap.
|
|
317
|
+
if (pendingClick) document.removeEventListener("click", pendingClick);
|
|
318
|
+
pendingClick = null;
|
|
319
|
+
if (!isOutside(e)) return;
|
|
300
320
|
const dismiss = () => {
|
|
301
321
|
if (ctx.state() === "open") ctx.setOpen(false);
|
|
302
322
|
};
|
|
303
|
-
if (e.pointerType === "touch") {
|
|
304
|
-
|
|
305
|
-
pendingClick = () => {
|
|
306
|
-
dismiss();
|
|
323
|
+
if ((e as PointerEvent).pointerType === "touch") {
|
|
324
|
+
pendingClick = (click: Event) => {
|
|
307
325
|
pendingClick = null;
|
|
326
|
+
// The click lands where the touch ended; it closes only if that is still outside.
|
|
327
|
+
if (el && !click.composedPath().includes(el)) dismiss();
|
|
308
328
|
};
|
|
309
329
|
document.addEventListener("click", pendingClick, { once: true });
|
|
310
330
|
} else {
|
|
@@ -313,13 +333,14 @@ const BottomSheetContent: ParentComponent<BottomSheetContentProps> = (props) =>
|
|
|
313
333
|
};
|
|
314
334
|
|
|
315
335
|
const timer = window.setTimeout(() => {
|
|
316
|
-
|
|
336
|
+
for (const s of scopes) s.addEventListener("pointerdown", onPointerDown, true);
|
|
317
337
|
attached = true;
|
|
318
338
|
}, 0);
|
|
319
339
|
|
|
320
340
|
onCleanup(() => {
|
|
321
341
|
window.clearTimeout(timer);
|
|
322
|
-
if (attached)
|
|
342
|
+
if (attached)
|
|
343
|
+
for (const s of scopes) s.removeEventListener("pointerdown", onPointerDown, true);
|
|
323
344
|
if (pendingClick) document.removeEventListener("click", pendingClick);
|
|
324
345
|
});
|
|
325
346
|
});
|
|
@@ -10,7 +10,9 @@ interface TrapHandle {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export function trapFocus(root: HTMLElement, initial: InitialFocus = "container"): TrapHandle {
|
|
13
|
-
|
|
13
|
+
// Inside a shadow root the document's active element is the root's host; the root knows which node has focus.
|
|
14
|
+
const scope = root.getRootNode() as Document | ShadowRoot;
|
|
15
|
+
const previouslyFocused = (scope.activeElement as HTMLElement | null) ?? null;
|
|
14
16
|
|
|
15
17
|
// focus() runs focus and blur handlers synchronously; a caller's effect must not track their reads.
|
|
16
18
|
untrack(() => {
|
|
@@ -23,7 +25,8 @@ export function trapFocus(root: HTMLElement, initial: InitialFocus = "container"
|
|
|
23
25
|
focusTarget?.focus({ preventScroll: true });
|
|
24
26
|
});
|
|
25
27
|
|
|
26
|
-
const onKeyDown = (
|
|
28
|
+
const onKeyDown = (event: Event) => {
|
|
29
|
+
const e = event as KeyboardEvent;
|
|
27
30
|
if (e.key !== "Tab") return;
|
|
28
31
|
const focusable = Array.from(root.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).filter(
|
|
29
32
|
(el) => !el.hasAttribute("disabled") && el.offsetParent !== null,
|
|
@@ -35,7 +38,7 @@ export function trapFocus(root: HTMLElement, initial: InitialFocus = "container"
|
|
|
35
38
|
}
|
|
36
39
|
const first = focusable[0];
|
|
37
40
|
const last = focusable[focusable.length - 1];
|
|
38
|
-
const active =
|
|
41
|
+
const active = scope.activeElement as HTMLElement | null;
|
|
39
42
|
if (e.shiftKey) {
|
|
40
43
|
if (active === first || active === root || !root.contains(active)) {
|
|
41
44
|
e.preventDefault();
|
|
@@ -49,11 +52,11 @@ export function trapFocus(root: HTMLElement, initial: InitialFocus = "container"
|
|
|
49
52
|
}
|
|
50
53
|
};
|
|
51
54
|
|
|
52
|
-
|
|
55
|
+
scope.addEventListener("keydown", onKeyDown);
|
|
53
56
|
|
|
54
57
|
return {
|
|
55
58
|
release() {
|
|
56
|
-
|
|
59
|
+
scope.removeEventListener("keydown", onKeyDown);
|
|
57
60
|
untrack(() => previouslyFocused?.focus?.({ preventScroll: true }));
|
|
58
61
|
},
|
|
59
62
|
};
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import { ContextMenu as ContextMenuPrimitive } from "@kobalte/core/context-menu";
|
|
2
|
-
import {
|
|
2
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
3
|
+
import { useMenuContext } from "@kobalte/core/menu";
|
|
4
|
+
import {
|
|
5
|
+
type Component,
|
|
6
|
+
type ComponentProps,
|
|
7
|
+
createSignal,
|
|
8
|
+
type ParentComponent,
|
|
9
|
+
splitProps,
|
|
10
|
+
} from "solid-js";
|
|
3
11
|
import { OVERLAY_MOTION } from "../lib/motion-classes.js";
|
|
12
|
+
import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
|
|
4
13
|
import { cn } from "../lib/utils.js";
|
|
5
14
|
import {
|
|
6
15
|
MENU_CONTENT_CLASS,
|
|
@@ -14,7 +23,15 @@ import {
|
|
|
14
23
|
MenuSubTriggerPart,
|
|
15
24
|
} from "./menu-parts.js";
|
|
16
25
|
|
|
17
|
-
const
|
|
26
|
+
const ContextMenuRoot: Component<ComponentProps<typeof ContextMenuPrimitive>> = (props) => {
|
|
27
|
+
// Modal in a shadow root, Kobalte would aria-hide the realm's host, menu included.
|
|
28
|
+
const inShadow = inShadowRoot(usePortalMount());
|
|
29
|
+
return <ContextMenuPrimitive {...props} modal={inShadow ? false : props.modal} />;
|
|
30
|
+
};
|
|
31
|
+
const ContextMenu = Object.assign(ContextMenuRoot, ContextMenuPrimitive) as Component<
|
|
32
|
+
ComponentProps<typeof ContextMenuPrimitive>
|
|
33
|
+
> &
|
|
34
|
+
typeof ContextMenuPrimitive;
|
|
18
35
|
const ContextMenuGroup = ContextMenuPrimitive.Group;
|
|
19
36
|
const ContextMenuSub = ContextMenuPrimitive.Sub;
|
|
20
37
|
const ContextMenuRadioGroup = ContextMenuPrimitive.RadioGroup;
|
|
@@ -36,10 +53,31 @@ const ContextMenuSubContent: Component<ComponentProps<typeof ContextMenuPrimitiv
|
|
|
36
53
|
const ContextMenuContent: Component<ComponentProps<typeof ContextMenuPrimitive.Content>> = (
|
|
37
54
|
props,
|
|
38
55
|
) => {
|
|
39
|
-
const [local, rest] = splitProps(props, [
|
|
56
|
+
const [local, rest] = splitProps(props, [
|
|
57
|
+
"class",
|
|
58
|
+
"children",
|
|
59
|
+
"ref",
|
|
60
|
+
"onInteractOutside",
|
|
61
|
+
"onCloseAutoFocus",
|
|
62
|
+
]);
|
|
63
|
+
const menu = useMenuContext();
|
|
64
|
+
const [panel, setPanel] = createSignal<HTMLElement>();
|
|
65
|
+
const layer = shadowLayer({
|
|
66
|
+
content: panel,
|
|
67
|
+
open: () => menu.isOpen(),
|
|
68
|
+
dismiss: () => menu.close(true),
|
|
69
|
+
tree: true,
|
|
70
|
+
consumer: local,
|
|
71
|
+
});
|
|
40
72
|
return (
|
|
41
|
-
<ContextMenuPrimitive.Portal>
|
|
73
|
+
<ContextMenuPrimitive.Portal mount={usePortalMount()}>
|
|
42
74
|
<ContextMenuPrimitive.Content
|
|
75
|
+
ref={(el: HTMLElement) => {
|
|
76
|
+
setPanel(el);
|
|
77
|
+
if (typeof local.ref === "function") local.ref(el);
|
|
78
|
+
}}
|
|
79
|
+
onInteractOutside={layer.onInteractOutside}
|
|
80
|
+
onCloseAutoFocus={layer.onCloseAutoFocus}
|
|
43
81
|
data-slot="context-menu-content"
|
|
44
82
|
class={cn(
|
|
45
83
|
MENU_CONTENT_CLASS,
|
package/src/solid/dialog.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Dialog as DialogPrimitive, useDialogContext } from "@kobalte/core/dialog";
|
|
2
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
2
3
|
import type { VariantProps } from "cva";
|
|
3
4
|
import {
|
|
4
5
|
type Component,
|
|
@@ -10,6 +11,7 @@ import {
|
|
|
10
11
|
splitProps,
|
|
11
12
|
} from "solid-js";
|
|
12
13
|
import { overAnchor } from "../lib/anchor-placement.js";
|
|
14
|
+
import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
|
|
13
15
|
import { buttonVariants } from "../lib/button-variants.js";
|
|
14
16
|
import { cn } from "../lib/utils.js";
|
|
15
17
|
import {
|
|
@@ -30,9 +32,10 @@ import {
|
|
|
30
32
|
/* Kobalte's own scroll lock is off everywhere: one refcounted lock
|
|
31
33
|
* (bottom-sheet/scroll-lock.ts) serves every modal family, so a nested modal
|
|
32
34
|
* cannot release the page early. */
|
|
33
|
-
const Dialog: ParentComponent<ComponentProps<typeof DialogPrimitive>> = (props) =>
|
|
34
|
-
|
|
35
|
-
)
|
|
35
|
+
const Dialog: ParentComponent<ComponentProps<typeof DialogPrimitive>> = (props) => {
|
|
36
|
+
const mount = usePortalMount();
|
|
37
|
+
return <DialogPrimitive modal={!inShadowRoot(mount)} {...props} preventScroll={false} />;
|
|
38
|
+
};
|
|
36
39
|
|
|
37
40
|
const DialogTrigger: Component<ComponentProps<typeof DialogPrimitive.Trigger>> = (props) => (
|
|
38
41
|
<DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
|
|
@@ -88,10 +91,20 @@ const DialogContent: ParentComponent<DialogContentProps> = (props) => {
|
|
|
88
91
|
"anchor",
|
|
89
92
|
"style",
|
|
90
93
|
"ref",
|
|
94
|
+
"onInteractOutside",
|
|
95
|
+
"onCloseAutoFocus",
|
|
96
|
+
"onOpenAutoFocus",
|
|
91
97
|
]);
|
|
92
98
|
const context = useDialogContext();
|
|
93
99
|
const [viewport, setViewport] = createSignal(0);
|
|
94
100
|
const [panel, setPanel] = createSignal<HTMLElement>();
|
|
101
|
+
const layer = shadowLayer({
|
|
102
|
+
content: panel,
|
|
103
|
+
open: () => context.isOpen(),
|
|
104
|
+
dismiss: () => context.close(),
|
|
105
|
+
trapsFocus: true,
|
|
106
|
+
consumer: local,
|
|
107
|
+
});
|
|
95
108
|
const [height, setHeight] = createSignal<number>();
|
|
96
109
|
createEffect(() => {
|
|
97
110
|
if (!local.anchor || !context.isOpen()) return;
|
|
@@ -113,7 +126,7 @@ const DialogContent: ParentComponent<DialogContentProps> = (props) => {
|
|
|
113
126
|
return a ? overAnchor(a, ANCHORED_WIDTH[local.size ?? "lg"], height()) : undefined;
|
|
114
127
|
};
|
|
115
128
|
return (
|
|
116
|
-
<DialogPrimitive.Portal>
|
|
129
|
+
<DialogPrimitive.Portal mount={usePortalMount()}>
|
|
117
130
|
<ModalScrollLock />
|
|
118
131
|
<DialogPrimitive.Overlay data-slot="dialog-overlay" class={MODAL_SCRIM} />
|
|
119
132
|
<DialogPrimitive.Content
|
|
@@ -131,6 +144,9 @@ const DialogContent: ParentComponent<DialogContentProps> = (props) => {
|
|
|
131
144
|
local.class,
|
|
132
145
|
)}
|
|
133
146
|
style={{ ...(typeof local.style === "object" ? local.style : {}), ...placement() }}
|
|
147
|
+
onInteractOutside={layer.onInteractOutside}
|
|
148
|
+
onCloseAutoFocus={layer.onCloseAutoFocus}
|
|
149
|
+
onOpenAutoFocus={layer.onOpenAutoFocus}
|
|
134
150
|
{...others}
|
|
135
151
|
>
|
|
136
152
|
{local.children}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { JSX } from "solid-js";
|
|
2
2
|
import { Show } from "solid-js";
|
|
3
3
|
import { Portal } from "solid-js/web";
|
|
4
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
4
5
|
import { Z_CLASS } from "../lib/layers.js";
|
|
5
6
|
import { OVERLAY_SURFACE_OPAQUE } from "../lib/overlay-classes.js";
|
|
6
7
|
import { cn } from "../lib/utils.js";
|
|
@@ -21,7 +22,7 @@ export function DockedPanel(props: {
|
|
|
21
22
|
}) {
|
|
22
23
|
return (
|
|
23
24
|
<Show when={props.open}>
|
|
24
|
-
<Portal>
|
|
25
|
+
<Portal mount={usePortalMount()}>
|
|
25
26
|
<aside
|
|
26
27
|
data-slot="docked-panel"
|
|
27
28
|
aria-label={props.ariaLabel}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { DropdownMenu as DropdownMenuPrimitive } from "@kobalte/core/dropdown-menu";
|
|
2
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
2
3
|
import { useMenuContext } from "@kobalte/core/menu";
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type Component,
|
|
6
|
+
type ComponentProps,
|
|
7
|
+
createSignal,
|
|
8
|
+
type ParentComponent,
|
|
9
|
+
splitProps,
|
|
10
|
+
} from "solid-js";
|
|
4
11
|
import { MORPH_MOTION } from "../lib/motion-classes.js";
|
|
12
|
+
import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
|
|
5
13
|
import { anchorToTriggerTop } from "../lib/overlay-classes.js";
|
|
6
14
|
import { cn } from "../lib/utils.js";
|
|
7
15
|
import {
|
|
@@ -18,9 +26,18 @@ import {
|
|
|
18
26
|
|
|
19
27
|
/* The panel covers the trigger (see MORPH_MOTION); statics carried across so
|
|
20
28
|
* `DropdownMenu.Portal` keeps resolving. */
|
|
21
|
-
const DropdownMenuRoot: Component<ComponentProps<typeof DropdownMenuPrimitive>> = (props) =>
|
|
22
|
-
|
|
23
|
-
);
|
|
29
|
+
const DropdownMenuRoot: Component<ComponentProps<typeof DropdownMenuPrimitive>> = (props) => {
|
|
30
|
+
// Modal in a shadow root, Kobalte would aria-hide the realm's host, menu included.
|
|
31
|
+
const inShadow = inShadowRoot(usePortalMount());
|
|
32
|
+
return (
|
|
33
|
+
<DropdownMenuPrimitive
|
|
34
|
+
gutter={0}
|
|
35
|
+
getAnchorRect={anchorToTriggerTop}
|
|
36
|
+
{...props}
|
|
37
|
+
modal={inShadow ? false : props.modal}
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
24
41
|
const DropdownMenu = Object.assign(DropdownMenuRoot, DropdownMenuPrimitive) as Component<
|
|
25
42
|
ComponentProps<typeof DropdownMenuPrimitive>
|
|
26
43
|
> &
|
|
@@ -38,8 +55,23 @@ const DropdownMenuTrigger: Component<ComponentProps<typeof DropdownMenuPrimitive
|
|
|
38
55
|
const DropdownMenuContent: Component<ComponentProps<typeof DropdownMenuPrimitive.Content>> = (
|
|
39
56
|
props,
|
|
40
57
|
) => {
|
|
41
|
-
const [local, others] = splitProps(props, [
|
|
58
|
+
const [local, others] = splitProps(props, [
|
|
59
|
+
"class",
|
|
60
|
+
"children",
|
|
61
|
+
"ref",
|
|
62
|
+
"onInteractOutside",
|
|
63
|
+
"onCloseAutoFocus",
|
|
64
|
+
]);
|
|
42
65
|
const menu = useMenuContext();
|
|
66
|
+
const [panel, setPanel] = createSignal<HTMLElement>();
|
|
67
|
+
const layer = shadowLayer({
|
|
68
|
+
content: panel,
|
|
69
|
+
open: () => menu.isOpen(),
|
|
70
|
+
dismiss: () => menu.close(true),
|
|
71
|
+
trigger: () => menu.triggerRef(),
|
|
72
|
+
tree: true,
|
|
73
|
+
consumer: local,
|
|
74
|
+
});
|
|
43
75
|
// The morph starts as the trigger's box: its height and radius are read
|
|
44
76
|
// from the trigger itself, its width is kobalte's anchor width.
|
|
45
77
|
const writeMorphStart = (el: HTMLElement) => {
|
|
@@ -55,9 +87,15 @@ const DropdownMenuContent: Component<ComponentProps<typeof DropdownMenuPrimitive
|
|
|
55
87
|
el.style.setProperty("--morph-radius", `${radius}px`);
|
|
56
88
|
};
|
|
57
89
|
return (
|
|
58
|
-
<DropdownMenuPrimitive.Portal>
|
|
90
|
+
<DropdownMenuPrimitive.Portal mount={usePortalMount()}>
|
|
59
91
|
<DropdownMenuPrimitive.Content
|
|
60
|
-
ref={
|
|
92
|
+
ref={(el: HTMLElement) => {
|
|
93
|
+
setPanel(el);
|
|
94
|
+
writeMorphStart(el);
|
|
95
|
+
if (typeof local.ref === "function") local.ref(el);
|
|
96
|
+
}}
|
|
97
|
+
onInteractOutside={layer.onInteractOutside}
|
|
98
|
+
onCloseAutoFocus={layer.onCloseAutoFocus}
|
|
61
99
|
data-slot="dropdown-menu-content"
|
|
62
100
|
class={cn(MENU_CONTENT_CLASS, MORPH_MOTION, local.class)}
|
|
63
101
|
{...others}
|