@c9up/nebula 0.1.9 → 0.2.1
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/atoms/Avatar.d.ts +36 -9
- package/dist/atoms/Avatar.js +66 -27
- package/dist/atoms/index.d.ts +1 -1
- package/dist/atoms/index.js +1 -1
- package/dist/configure.d.ts +6 -0
- package/dist/configure.js +4 -22
- package/dist/lib/children.d.ts +30 -0
- package/dist/lib/id.d.ts +13 -29
- package/dist/lib/id.js +13 -39
- package/dist/lib/motion.d.ts +10 -0
- package/dist/lib/motion.js +10 -0
- package/dist/molecules/Accordion.d.ts +36 -15
- package/dist/molecules/Accordion.js +68 -45
- package/dist/molecules/Breadcrumb.d.ts +29 -11
- package/dist/molecules/Breadcrumb.js +52 -52
- package/dist/molecules/Collapsible.d.ts +18 -17
- package/dist/molecules/Collapsible.js +42 -34
- package/dist/molecules/Field.d.ts +66 -30
- package/dist/molecules/Field.js +106 -55
- package/dist/molecules/InputGroup.d.ts +39 -21
- package/dist/molecules/InputGroup.js +43 -27
- package/dist/molecules/InputOTP.d.ts +26 -3
- package/dist/molecules/InputOTP.js +109 -76
- package/dist/molecules/Message.js +5 -4
- package/dist/molecules/Pagination.d.ts +36 -11
- package/dist/molecules/Pagination.js +58 -74
- package/dist/molecules/RadioGroup.d.ts +25 -17
- package/dist/molecules/RadioGroup.js +47 -43
- package/dist/molecules/Resizable.d.ts +32 -7
- package/dist/molecules/Resizable.js +76 -49
- package/dist/molecules/Tabs.d.ts +49 -11
- package/dist/molecules/Tabs.js +107 -51
- package/dist/molecules/ToggleGroup.d.ts +16 -10
- package/dist/molecules/ToggleGroup.js +37 -21
- package/dist/molecules/index.d.ts +11 -11
- package/dist/molecules/index.js +11 -11
- package/dist/organisms/AlertDialog.d.ts +80 -28
- package/dist/organisms/AlertDialog.js +192 -67
- package/dist/organisms/Carousel.d.ts +25 -3
- package/dist/organisms/Carousel.js +75 -43
- package/dist/organisms/Combobox.d.ts +2 -1
- package/dist/organisms/Combobox.js +46 -16
- package/dist/organisms/Command.d.ts +73 -32
- package/dist/organisms/Command.js +209 -131
- package/dist/organisms/CommandDialog.js +5 -5
- package/dist/organisms/ContextMenu.d.ts +41 -19
- package/dist/organisms/ContextMenu.js +112 -71
- package/dist/organisms/DataTable.js +42 -8
- package/dist/organisms/Dialog.d.ts +97 -31
- package/dist/organisms/Dialog.js +193 -68
- package/dist/organisms/Drawer.d.ts +33 -10
- package/dist/organisms/Drawer.js +130 -52
- package/dist/organisms/DropdownMenu.d.ts +58 -11
- package/dist/organisms/DropdownMenu.js +137 -50
- package/dist/organisms/Form.js +9 -11
- package/dist/organisms/HoverCard.d.ts +39 -19
- package/dist/organisms/HoverCard.js +116 -40
- package/dist/organisms/Menubar.d.ts +55 -19
- package/dist/organisms/Menubar.js +166 -82
- package/dist/organisms/NavigationMenu.d.ts +77 -31
- package/dist/organisms/NavigationMenu.js +169 -98
- package/dist/organisms/Popover.d.ts +72 -12
- package/dist/organisms/Popover.js +132 -25
- package/dist/organisms/Questionnaire.js +5 -5
- package/dist/organisms/Select.d.ts +99 -18
- package/dist/organisms/Select.js +253 -111
- package/dist/organisms/Sheet.d.ts +57 -13
- package/dist/organisms/Sheet.js +141 -54
- package/dist/organisms/Sidebar.d.ts +146 -56
- package/dist/organisms/Sidebar.js +236 -128
- package/dist/organisms/Tooltip.d.ts +88 -14
- package/dist/organisms/Tooltip.js +210 -47
- package/dist/organisms/index.d.ts +15 -15
- package/dist/organisms/index.js +15 -15
- package/dist/primitives/floating.js +25 -0
- package/dist/primitives/floatingSurface.d.ts +11 -0
- package/dist/primitives/floatingSurface.js +8 -0
- package/dist/primitives/menu.d.ts +45 -1
- package/dist/primitives/menu.js +45 -14
- package/dist/primitives/menuParts.d.ts +113 -0
- package/dist/primitives/menuParts.js +207 -0
- package/dist/stubs.d.ts +13 -0
- package/dist/stubs.js +14 -0
- package/nebula.css +1 -1
- package/package.json +4 -3
- package/registry.json +18 -6
- package/src/atoms/Avatar.ts +141 -37
- package/src/atoms/index.ts +13 -1
- package/src/configure.ts +10 -23
- package/src/lib/children.ts +31 -0
- package/src/lib/id.ts +11 -39
- package/src/lib/motion.ts +12 -0
- package/src/molecules/Accordion.ts +120 -60
- package/src/molecules/Breadcrumb.ts +77 -76
- package/src/molecules/Collapsible.ts +77 -41
- package/src/molecules/Field.ts +187 -72
- package/src/molecules/InputGroup.ts +88 -35
- package/src/molecules/InputOTP.ts +150 -86
- package/src/molecules/Message.ts +6 -4
- package/src/molecules/Pagination.ts +96 -101
- package/src/molecules/RadioGroup.ts +70 -59
- package/src/molecules/Resizable.ts +138 -75
- package/src/molecules/Tabs.ts +187 -74
- package/src/molecules/ToggleGroup.ts +77 -38
- package/src/molecules/index.ts +85 -8
- package/src/organisms/AlertDialog.ts +319 -82
- package/src/organisms/Carousel.ts +129 -52
- package/src/organisms/Combobox.ts +64 -19
- package/src/organisms/Command.ts +342 -159
- package/src/organisms/CommandDialog.ts +6 -6
- package/src/organisms/ContextMenu.ts +185 -83
- package/src/organisms/DataTable.ts +56 -10
- package/src/organisms/Dialog.ts +312 -102
- package/src/organisms/Drawer.ts +219 -71
- package/src/organisms/DropdownMenu.ts +219 -61
- package/src/organisms/Form.ts +48 -34
- package/src/organisms/HoverCard.ts +187 -53
- package/src/organisms/Menubar.ts +247 -87
- package/src/organisms/NavigationMenu.ts +306 -128
- package/src/organisms/Popover.ts +215 -39
- package/src/organisms/Questionnaire.ts +9 -6
- package/src/organisms/Select.ts +397 -130
- package/src/organisms/Sheet.ts +252 -83
- package/src/organisms/Sidebar.ts +438 -185
- package/src/organisms/Tooltip.ts +315 -65
- package/src/organisms/index.ts +284 -14
- package/src/primitives/floating.ts +33 -0
- package/src/primitives/floatingSurface.ts +19 -0
- package/src/primitives/menu.ts +79 -18
- package/src/primitives/menuParts.ts +371 -0
- package/src/stubs.ts +16 -0
- package/stubs/config/nebula.stub +21 -0
package/src/organisms/Drawer.ts
CHANGED
|
@@ -21,9 +21,16 @@
|
|
|
21
21
|
* out of a modal surface.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
import {
|
|
25
|
+
component,
|
|
26
|
+
createContext,
|
|
27
|
+
html,
|
|
28
|
+
inject,
|
|
29
|
+
provide,
|
|
30
|
+
type TemplateResult,
|
|
31
|
+
} from "@c9up/aurora";
|
|
32
|
+
import type { Child, Parts, Slot } from "../lib/children.js";
|
|
33
|
+
import { slot } from "../lib/children.js";
|
|
27
34
|
import { cn } from "../lib/cn.js";
|
|
28
35
|
import { uid } from "../lib/id.js";
|
|
29
36
|
import { fadeInOut, slideFrom } from "../lib/motion.js";
|
|
@@ -32,24 +39,38 @@ import { controllable } from "../primitives/controllable.js";
|
|
|
32
39
|
import { modalSurface } from "../primitives/modalSurface.js";
|
|
33
40
|
import { dialogBackdropClasses } from "./Dialog.js";
|
|
34
41
|
|
|
35
|
-
/** Drag this far down and release to dismiss, in px. */
|
|
36
42
|
const DISMISS_DISTANCE = 120;
|
|
37
|
-
/** Or release faster than this, in px per millisecond. */
|
|
38
43
|
const DISMISS_VELOCITY = 0.5;
|
|
39
44
|
|
|
45
|
+
const panelClasses =
|
|
46
|
+
"bg-background fixed inset-x-0 bottom-0 z-50 mt-24 flex max-h-[80vh] touch-none flex-col gap-4 rounded-t-lg border-t shadow-lg outline-none";
|
|
47
|
+
|
|
48
|
+
interface RegisteredContent {
|
|
49
|
+
readonly body: Child;
|
|
50
|
+
readonly showHandle: boolean;
|
|
51
|
+
readonly disableDrag: boolean;
|
|
52
|
+
readonly class?: Reactive<string>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface DrawerApi {
|
|
56
|
+
readonly triggerId: string;
|
|
57
|
+
readonly panelId: string;
|
|
58
|
+
readonly titleId: string;
|
|
59
|
+
readonly descriptionId: string;
|
|
60
|
+
readonly open: () => boolean;
|
|
61
|
+
setOpen(next: boolean): void;
|
|
62
|
+
declareTitle(): void;
|
|
63
|
+
declareDescription(): void;
|
|
64
|
+
register(content: RegisteredContent): void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DrawerContext = createContext<DrawerApi>("Drawer");
|
|
68
|
+
|
|
40
69
|
export interface DrawerProps {
|
|
41
|
-
trigger?: Slot;
|
|
42
|
-
title: Child;
|
|
43
|
-
description?: Child;
|
|
44
|
-
children?: Slot;
|
|
45
|
-
footer?: Slot;
|
|
46
70
|
open?: Reactive<boolean>;
|
|
47
71
|
defaultOpen?: boolean;
|
|
48
72
|
onOpenChange?: (open: boolean) => void;
|
|
49
|
-
|
|
50
|
-
disableDrag?: boolean;
|
|
51
|
-
triggerClass?: Reactive<string>;
|
|
52
|
-
contentClass?: Reactive<string>;
|
|
73
|
+
children?: Parts;
|
|
53
74
|
}
|
|
54
75
|
|
|
55
76
|
export const Drawer = component<DrawerProps>((props) => {
|
|
@@ -57,6 +78,9 @@ export const Drawer = component<DrawerProps>((props) => {
|
|
|
57
78
|
const panelId = uid("drawer-panel");
|
|
58
79
|
const titleId = uid("drawer-title");
|
|
59
80
|
const descriptionId = uid("drawer-description");
|
|
81
|
+
let hasTitle = false;
|
|
82
|
+
let hasDescription = false;
|
|
83
|
+
let content: RegisteredContent | undefined;
|
|
60
84
|
|
|
61
85
|
const state = controllable<boolean>({
|
|
62
86
|
value: props.open,
|
|
@@ -64,8 +88,15 @@ export const Drawer = component<DrawerProps>((props) => {
|
|
|
64
88
|
onChange: props.onOpenChange,
|
|
65
89
|
});
|
|
66
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Drag to dismiss.
|
|
93
|
+
*
|
|
94
|
+
* Distance OR velocity: a slow long drag and a quick flick both mean the
|
|
95
|
+
* same thing, and requiring distance alone makes a flick feel ignored.
|
|
96
|
+
* Everything it does, Escape and the backdrop already do — a gesture must
|
|
97
|
+
* never be the only way out of a modal surface.
|
|
98
|
+
*/
|
|
67
99
|
function onPointerDown(event: PointerEvent): void {
|
|
68
|
-
if (props.disableDrag === true) return;
|
|
69
100
|
const panel = document.getElementById(panelId);
|
|
70
101
|
if (panel === null) return;
|
|
71
102
|
|
|
@@ -88,13 +119,10 @@ export const Drawer = component<DrawerProps>((props) => {
|
|
|
88
119
|
|
|
89
120
|
// Restore the transition before deciding, so both outcomes animate.
|
|
90
121
|
panel.style.transition = "";
|
|
122
|
+
panel.style.transform = "";
|
|
91
123
|
const velocity = offset / Math.max(1, performance.now() - startedAt);
|
|
92
|
-
|
|
93
124
|
if (offset > DISMISS_DISTANCE || velocity > DISMISS_VELOCITY) {
|
|
94
|
-
panel.style.transform = "";
|
|
95
125
|
state.set(false);
|
|
96
|
-
} else {
|
|
97
|
-
panel.style.transform = "";
|
|
98
126
|
}
|
|
99
127
|
};
|
|
100
128
|
|
|
@@ -103,65 +131,185 @@ export const Drawer = component<DrawerProps>((props) => {
|
|
|
103
131
|
panel.addEventListener("pointercancel", onUp);
|
|
104
132
|
}
|
|
105
133
|
|
|
106
|
-
|
|
134
|
+
provide<DrawerApi>(DrawerContext, {
|
|
135
|
+
triggerId,
|
|
136
|
+
panelId,
|
|
137
|
+
titleId,
|
|
138
|
+
descriptionId,
|
|
107
139
|
open: () => state.current(),
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
aria-modal="true"
|
|
119
|
-
aria-labelledby="${titleId}"
|
|
120
|
-
aria-describedby="${props.description === undefined ? undefined : descriptionId}"
|
|
121
|
-
tabindex="-1"
|
|
122
|
-
class="${cn(
|
|
123
|
-
"bg-background fixed inset-x-0 bottom-0 z-50 mt-24 flex max-h-[80vh] touch-none flex-col gap-4 rounded-t-lg border-t p-6 shadow-lg outline-none",
|
|
124
|
-
slideFrom("bottom"),
|
|
125
|
-
read(props.contentClass),
|
|
126
|
-
)}"
|
|
127
|
-
@pointerdown="${onPointerDown}"
|
|
128
|
-
>
|
|
129
|
-
<div
|
|
130
|
-
data-slot="drawer-handle"
|
|
131
|
-
aria-hidden="true"
|
|
132
|
-
class="bg-muted mx-auto h-1.5 w-12 shrink-0 rounded-full"
|
|
133
|
-
></div>
|
|
134
|
-
<div class="flex flex-col gap-1.5 text-center sm:text-left">
|
|
135
|
-
<h2 id="${titleId}" class="text-foreground font-semibold">${props.title}</h2>
|
|
136
|
-
${
|
|
137
|
-
props.description === undefined
|
|
138
|
-
? null
|
|
139
|
-
: html`<p id="${descriptionId}" class="text-muted-foreground text-sm">
|
|
140
|
-
${props.description}
|
|
141
|
-
</p>`
|
|
142
|
-
}
|
|
143
|
-
</div>
|
|
144
|
-
<div class="flex-1 overflow-y-auto">${slot(props.children)}</div>
|
|
145
|
-
${
|
|
146
|
-
props.footer === undefined
|
|
147
|
-
? null
|
|
148
|
-
: html`<div class="mt-auto flex flex-col gap-2">${slot(props.footer)}</div>`
|
|
149
|
-
}
|
|
150
|
-
</div>
|
|
151
|
-
</div>`,
|
|
140
|
+
setOpen: (next) => state.set(next),
|
|
141
|
+
declareTitle() {
|
|
142
|
+
hasTitle = true;
|
|
143
|
+
},
|
|
144
|
+
declareDescription() {
|
|
145
|
+
hasDescription = true;
|
|
146
|
+
},
|
|
147
|
+
register(registered) {
|
|
148
|
+
content = registered;
|
|
149
|
+
},
|
|
152
150
|
});
|
|
153
151
|
|
|
154
|
-
|
|
155
|
-
|
|
152
|
+
const parts = props.children?.();
|
|
153
|
+
|
|
154
|
+
if (content !== undefined) {
|
|
155
|
+
const registered = content;
|
|
156
|
+
if (!hasTitle) {
|
|
157
|
+
console.warn(
|
|
158
|
+
"nebula: a Drawer has no DrawerTitle. It will be announced only as \u201cdialog\u201d.",
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
modalSurface({
|
|
162
|
+
open: () => state.current(),
|
|
163
|
+
onClose: () => state.set(false),
|
|
164
|
+
panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
|
|
165
|
+
returnFocus: () => document.getElementById(triggerId),
|
|
166
|
+
content: () =>
|
|
167
|
+
renderSurface(registered, {
|
|
168
|
+
panelId,
|
|
169
|
+
titleId: hasTitle ? titleId : undefined,
|
|
170
|
+
descriptionId: hasDescription ? descriptionId : undefined,
|
|
171
|
+
onPointerDown: registered.disableDrag ? undefined : onPointerDown,
|
|
172
|
+
}),
|
|
173
|
+
});
|
|
156
174
|
}
|
|
157
175
|
|
|
176
|
+
return html`${parts}`;
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
export interface DrawerTriggerProps {
|
|
180
|
+
children?: Slot;
|
|
181
|
+
class?: Reactive<string>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const DrawerTrigger = component<DrawerTriggerProps>((props) => {
|
|
185
|
+
const drawer = inject(DrawerContext);
|
|
158
186
|
return html`<button
|
|
159
187
|
type="button"
|
|
160
188
|
data-slot="drawer-trigger"
|
|
161
|
-
id="${triggerId}"
|
|
189
|
+
id="${drawer.triggerId}"
|
|
162
190
|
aria-haspopup="dialog"
|
|
163
|
-
aria-expanded="${() => (
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
191
|
+
aria-expanded="${() => (drawer.open() ? "true" : "false")}"
|
|
192
|
+
data-state="${() => (drawer.open() ? "open" : "closed")}"
|
|
193
|
+
class="${() => cn(read(props.class))}"
|
|
194
|
+
@click="${() => drawer.setOpen(true)}"
|
|
195
|
+
>${slot(props.children)}</button>`;
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
export interface DrawerCloseProps {
|
|
199
|
+
children?: Slot;
|
|
200
|
+
class?: Reactive<string>;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export const DrawerClose = component<DrawerCloseProps>((props) => {
|
|
204
|
+
const drawer = inject(DrawerContext);
|
|
205
|
+
return html`<button
|
|
206
|
+
type="button"
|
|
207
|
+
data-slot="drawer-close"
|
|
208
|
+
class="${() => cn(read(props.class))}"
|
|
209
|
+
@click="${() => drawer.setOpen(false)}"
|
|
210
|
+
>${slot(props.children)}</button>`;
|
|
167
211
|
});
|
|
212
|
+
|
|
213
|
+
export interface DrawerOverlayProps {
|
|
214
|
+
class?: Reactive<string>;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export const DrawerOverlay = component<DrawerOverlayProps>(
|
|
218
|
+
(props) => html`<div
|
|
219
|
+
data-slot="drawer-overlay"
|
|
220
|
+
class="${() => cn(dialogBackdropClasses, read(props.class))}"
|
|
221
|
+
></div>`,
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
export interface DrawerContentProps {
|
|
225
|
+
children?: Parts;
|
|
226
|
+
/** The grab bar. Decorative — see `onPointerDown`. */
|
|
227
|
+
showHandle?: boolean;
|
|
228
|
+
/** Turn off drag-to-dismiss — for a drawer holding a scrollable list. */
|
|
229
|
+
disableDrag?: boolean;
|
|
230
|
+
class?: Reactive<string>;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export const DrawerContent = component<DrawerContentProps>((props) => {
|
|
234
|
+
const drawer = inject(DrawerContext);
|
|
235
|
+
drawer.register({
|
|
236
|
+
body: props.children?.(),
|
|
237
|
+
showHandle: props.showHandle !== false,
|
|
238
|
+
disableDrag: props.disableDrag === true,
|
|
239
|
+
class: props.class,
|
|
240
|
+
});
|
|
241
|
+
return html``;
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
export interface DrawerSectionProps {
|
|
245
|
+
children?: Slot;
|
|
246
|
+
class?: Reactive<string>;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export const DrawerHeader = component<DrawerSectionProps>(
|
|
250
|
+
(props) => html`<div
|
|
251
|
+
data-slot="drawer-header"
|
|
252
|
+
class="${() => cn("flex flex-col gap-1.5 p-4", read(props.class))}"
|
|
253
|
+
>${slot(props.children)}</div>`,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
export const DrawerFooter = component<DrawerSectionProps>(
|
|
257
|
+
(props) => html`<div
|
|
258
|
+
data-slot="drawer-footer"
|
|
259
|
+
class="${() => cn("mt-auto flex flex-col gap-2 p-4", read(props.class))}"
|
|
260
|
+
>${slot(props.children)}</div>`,
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
export const DrawerTitle = component<DrawerSectionProps>((props) => {
|
|
264
|
+
const drawer = inject(DrawerContext);
|
|
265
|
+
drawer.declareTitle();
|
|
266
|
+
return html`<h2
|
|
267
|
+
id="${drawer.titleId}"
|
|
268
|
+
data-slot="drawer-title"
|
|
269
|
+
class="${() => cn("text-foreground font-semibold", read(props.class))}"
|
|
270
|
+
>${slot(props.children)}</h2>`;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
export const DrawerDescription = component<DrawerSectionProps>((props) => {
|
|
274
|
+
const drawer = inject(DrawerContext);
|
|
275
|
+
drawer.declareDescription();
|
|
276
|
+
return html`<p
|
|
277
|
+
id="${drawer.descriptionId}"
|
|
278
|
+
data-slot="drawer-description"
|
|
279
|
+
class="${() => cn("text-muted-foreground text-sm", read(props.class))}"
|
|
280
|
+
>${slot(props.children)}</p>`;
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
interface SurfaceIds {
|
|
284
|
+
readonly panelId: string;
|
|
285
|
+
readonly titleId: string | undefined;
|
|
286
|
+
readonly descriptionId: string | undefined;
|
|
287
|
+
readonly onPointerDown: ((event: PointerEvent) => void) | undefined;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function renderSurface(
|
|
291
|
+
registered: RegisteredContent,
|
|
292
|
+
ids: SurfaceIds,
|
|
293
|
+
): TemplateResult {
|
|
294
|
+
const handle = registered.showHandle
|
|
295
|
+
? html`<div
|
|
296
|
+
data-slot="drawer-handle"
|
|
297
|
+
aria-hidden="true"
|
|
298
|
+
class="bg-muted mx-auto mt-4 h-1.5 w-12 shrink-0 rounded-full"
|
|
299
|
+
></div>`
|
|
300
|
+
: null;
|
|
301
|
+
return html`<div data-slot="drawer-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
|
|
302
|
+
<div data-slot="drawer-overlay" class="${dialogBackdropClasses}"></div>
|
|
303
|
+
<div
|
|
304
|
+
data-slot="drawer-content"
|
|
305
|
+
id="${ids.panelId}"
|
|
306
|
+
role="dialog"
|
|
307
|
+
aria-modal="true"
|
|
308
|
+
aria-labelledby="${ids.titleId}"
|
|
309
|
+
aria-describedby="${ids.descriptionId}"
|
|
310
|
+
tabindex="-1"
|
|
311
|
+
class="${cn(panelClasses, slideFrom("bottom"), read(registered.class))}"
|
|
312
|
+
@pointerdown="${ids.onPointerDown}"
|
|
313
|
+
>${handle}${registered.body}</div>
|
|
314
|
+
</div>`;
|
|
315
|
+
}
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* DropdownMenu — a menu opened from a button.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
4
|
+
* The keyboard model comes from `wireMenu`: roving focus, type-ahead, arrows,
|
|
5
|
+
* Home/End, Escape and submenu traversal. It works on the RENDERED DOM — the
|
|
6
|
+
* `[data-nebula-item]` rows and their `role` — never on a description of it,
|
|
7
|
+
* which is what lets these parts compose freely and still behave like one
|
|
8
|
+
* menu.
|
|
9
|
+
*
|
|
10
|
+
* DropdownMenu({ children: () => html`
|
|
11
|
+
* ${DropdownMenuTrigger({ children: "Open" })}
|
|
12
|
+
* ${DropdownMenuContent({ children: () => html`
|
|
13
|
+
* ${DropdownMenuLabel({ children: "Account" })}
|
|
14
|
+
* ${DropdownMenuItem({ children: "Profile", onSelect: openProfile })}
|
|
15
|
+
* ${DropdownMenuSeparator({})}
|
|
16
|
+
* ${DropdownMenuItem({ children: "Delete", variant: "destructive" })}
|
|
17
|
+
* ` })}
|
|
18
|
+
* ` })
|
|
6
19
|
*
|
|
7
20
|
* Two behaviours are specific to a button-opened menu:
|
|
8
21
|
*
|
|
@@ -13,31 +26,57 @@
|
|
|
13
26
|
* behaviour of every native menu button and the one users try first.
|
|
14
27
|
*/
|
|
15
28
|
|
|
16
|
-
import {
|
|
17
|
-
|
|
29
|
+
import {
|
|
30
|
+
component,
|
|
31
|
+
createContext,
|
|
32
|
+
html,
|
|
33
|
+
inject,
|
|
34
|
+
provide,
|
|
35
|
+
type TemplateResult,
|
|
36
|
+
} from "@c9up/aurora";
|
|
37
|
+
import type { Child, Parts, Slot } from "../lib/children.js";
|
|
38
|
+
import { slot } from "../lib/children.js";
|
|
18
39
|
import { cn } from "../lib/cn.js";
|
|
19
40
|
import { uid } from "../lib/id.js";
|
|
41
|
+
import { slideInFromSide, zoomInOut } from "../lib/motion.js";
|
|
20
42
|
import { type Reactive, read } from "../lib/props.js";
|
|
21
43
|
import { controllable } from "../primitives/controllable.js";
|
|
22
|
-
import type { Placement } from "../primitives/floating.js";
|
|
44
|
+
import type { Align, Placement, Side } from "../primitives/floating.js";
|
|
23
45
|
import { floatingSurface } from "../primitives/floatingSurface.js";
|
|
24
|
-
import {
|
|
46
|
+
import { menuPanelClasses, wireMenu } from "../primitives/menu.js";
|
|
47
|
+
import { createMenuParts, type MenuOwner } from "../primitives/menuParts.js";
|
|
48
|
+
|
|
49
|
+
interface RegisteredContent {
|
|
50
|
+
readonly body: Child;
|
|
51
|
+
readonly side: Side;
|
|
52
|
+
readonly align: Align;
|
|
53
|
+
readonly sideOffset: number;
|
|
54
|
+
readonly class?: Reactive<string>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface DropdownMenuApi extends MenuOwner {
|
|
58
|
+
readonly triggerId: string;
|
|
59
|
+
readonly contentId: string;
|
|
60
|
+
readonly open: () => boolean;
|
|
61
|
+
setOpen(next: boolean): void;
|
|
62
|
+
/** Open and land on the first item — the keyboard path. */
|
|
63
|
+
openAndEnter(): void;
|
|
64
|
+
register(content: RegisteredContent): void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const DropdownMenuContext = createContext<DropdownMenuApi>("DropdownMenu");
|
|
25
68
|
|
|
26
69
|
export interface DropdownMenuProps {
|
|
27
|
-
trigger?: Slot;
|
|
28
|
-
entries: readonly MenuEntry[];
|
|
29
70
|
open?: Reactive<boolean>;
|
|
30
71
|
defaultOpen?: boolean;
|
|
31
72
|
onOpenChange?: (open: boolean) => void;
|
|
32
|
-
|
|
33
|
-
class?: Reactive<string>;
|
|
34
|
-
triggerClass?: Reactive<string>;
|
|
35
|
-
contentClass?: Reactive<string>;
|
|
73
|
+
children?: Parts;
|
|
36
74
|
}
|
|
37
75
|
|
|
38
76
|
export const DropdownMenu = component<DropdownMenuProps>((props) => {
|
|
39
|
-
const triggerId = uid("dropdown-trigger");
|
|
40
|
-
const contentId = uid("dropdown-content");
|
|
77
|
+
const triggerId = uid("dropdown-menu-trigger");
|
|
78
|
+
const contentId = uid("dropdown-menu-content");
|
|
79
|
+
let content: RegisteredContent | undefined;
|
|
41
80
|
|
|
42
81
|
const state = controllable<boolean>({
|
|
43
82
|
value: props.open,
|
|
@@ -55,58 +94,177 @@ export const DropdownMenu = component<DropdownMenuProps>((props) => {
|
|
|
55
94
|
state.set(false);
|
|
56
95
|
}
|
|
57
96
|
|
|
58
|
-
|
|
59
|
-
|
|
97
|
+
provide<DropdownMenuApi>(DropdownMenuContext, {
|
|
98
|
+
triggerId,
|
|
99
|
+
contentId,
|
|
60
100
|
open: () => state.current(),
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
menuPanel({
|
|
66
|
-
id: contentId,
|
|
67
|
-
entries: props.entries,
|
|
68
|
-
onCloseAll: close,
|
|
69
|
-
labelledBy: triggerId,
|
|
70
|
-
class: read(props.contentClass),
|
|
71
|
-
}),
|
|
72
|
-
onOpened: (panel) => {
|
|
73
|
-
const enterItems = enterOnOpen;
|
|
74
|
-
enterOnOpen = false;
|
|
75
|
-
unwire = wireMenu(panel, {
|
|
76
|
-
onCloseAll: close,
|
|
77
|
-
autoFocusFirst: enterItems,
|
|
78
|
-
});
|
|
79
|
-
// The panel takes focus only when no item did — it needs to hold focus
|
|
80
|
-
// so Escape and the arrows reach it rather than the page behind, but
|
|
81
|
-
// focusing it unconditionally steals focus straight back from the item
|
|
82
|
-
// `autoFocusFirst` just landed on, and opening with the keyboard would
|
|
83
|
-
// never reach the first entry.
|
|
84
|
-
if (!enterItems) panel.focus({ preventScroll: true });
|
|
101
|
+
setOpen: (next) => state.set(next),
|
|
102
|
+
openAndEnter() {
|
|
103
|
+
enterOnOpen = true;
|
|
104
|
+
state.set(true);
|
|
85
105
|
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
closeAll: close,
|
|
107
|
+
register(registered) {
|
|
108
|
+
content = registered;
|
|
89
109
|
},
|
|
90
110
|
});
|
|
91
111
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
112
|
+
const parts = props.children?.();
|
|
113
|
+
|
|
114
|
+
if (content !== undefined) {
|
|
115
|
+
const registered = content;
|
|
116
|
+
const placement: Placement =
|
|
117
|
+
registered.align === "center"
|
|
118
|
+
? registered.side
|
|
119
|
+
: `${registered.side}-${registered.align}`;
|
|
120
|
+
floatingSurface({
|
|
121
|
+
anchor: () => document.getElementById(triggerId),
|
|
122
|
+
open: () => state.current(),
|
|
123
|
+
onClose: close,
|
|
124
|
+
placement,
|
|
125
|
+
offset: registered.sideOffset,
|
|
126
|
+
content: () =>
|
|
127
|
+
renderPanel(registered, { id: contentId, labelledBy: triggerId }),
|
|
128
|
+
onOpened: (panel) => {
|
|
129
|
+
const enterItems = enterOnOpen;
|
|
130
|
+
enterOnOpen = false;
|
|
131
|
+
unwire = wireMenu(panel, {
|
|
132
|
+
onCloseAll: close,
|
|
133
|
+
autoFocusFirst: enterItems,
|
|
134
|
+
});
|
|
135
|
+
// The panel takes focus only when no item did — it needs to hold
|
|
136
|
+
// focus so Escape and the arrows reach it rather than the page
|
|
137
|
+
// behind, but focusing it unconditionally steals focus straight back
|
|
138
|
+
// from the item `autoFocusFirst` just landed on, and opening with
|
|
139
|
+
// the keyboard would never reach the first entry.
|
|
140
|
+
if (!enterItems) panel.focus({ preventScroll: true });
|
|
141
|
+
},
|
|
142
|
+
onClosed: () => {
|
|
143
|
+
unwire?.();
|
|
144
|
+
unwire = null;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
98
147
|
}
|
|
99
148
|
|
|
100
|
-
return html
|
|
101
|
-
type="button"
|
|
102
|
-
data-slot="dropdown-menu-trigger"
|
|
103
|
-
id="${triggerId}"
|
|
104
|
-
aria-haspopup="menu"
|
|
105
|
-
aria-expanded="${() => (state.current() ? "true" : "false")}"
|
|
106
|
-
aria-controls="${() => (state.current() ? contentId : undefined)}"
|
|
107
|
-
data-state="${() => (state.current() ? "open" : "closed")}"
|
|
108
|
-
class="${() => cn(read(props.triggerClass))}"
|
|
109
|
-
@click="${() => state.set(!state.current())}"
|
|
110
|
-
@keydown="${onTriggerKeyDown}"
|
|
111
|
-
>${slot(props.trigger)}</button>`;
|
|
149
|
+
return html`${parts}`;
|
|
112
150
|
});
|
|
151
|
+
|
|
152
|
+
export interface DropdownMenuTriggerProps {
|
|
153
|
+
children?: Slot;
|
|
154
|
+
class?: Reactive<string>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const DropdownMenuTrigger = component<DropdownMenuTriggerProps>(
|
|
158
|
+
(props) => {
|
|
159
|
+
const menu = inject(DropdownMenuContext);
|
|
160
|
+
function onKeyDown(event: KeyboardEvent): void {
|
|
161
|
+
if (
|
|
162
|
+
event.key !== "ArrowDown" &&
|
|
163
|
+
event.key !== "Enter" &&
|
|
164
|
+
event.key !== " "
|
|
165
|
+
) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
event.preventDefault();
|
|
169
|
+
menu.openAndEnter();
|
|
170
|
+
}
|
|
171
|
+
return html`<button
|
|
172
|
+
type="button"
|
|
173
|
+
data-slot="dropdown-menu-trigger"
|
|
174
|
+
id="${menu.triggerId}"
|
|
175
|
+
aria-haspopup="menu"
|
|
176
|
+
aria-expanded="${() => (menu.open() ? "true" : "false")}"
|
|
177
|
+
aria-controls="${() => (menu.open() ? menu.contentId : undefined)}"
|
|
178
|
+
data-state="${() => (menu.open() ? "open" : "closed")}"
|
|
179
|
+
class="${() => cn(read(props.class))}"
|
|
180
|
+
@click="${() => menu.setOpen(!menu.open())}"
|
|
181
|
+
@keydown="${onKeyDown}"
|
|
182
|
+
>${slot(props.children)}</button>`;
|
|
183
|
+
},
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
export interface DropdownMenuContentProps {
|
|
187
|
+
children?: Parts;
|
|
188
|
+
side?: Side;
|
|
189
|
+
align?: Align;
|
|
190
|
+
sideOffset?: number;
|
|
191
|
+
class?: Reactive<string>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The panel.
|
|
196
|
+
*
|
|
197
|
+
* Renders nothing where it is written — the menu portals it on open — and
|
|
198
|
+
* builds its children HERE, during setup, so every row sees the context.
|
|
199
|
+
*/
|
|
200
|
+
export const DropdownMenuContent = component<DropdownMenuContentProps>(
|
|
201
|
+
(props) => {
|
|
202
|
+
const menu = inject(DropdownMenuContext);
|
|
203
|
+
const body = props.children?.();
|
|
204
|
+
menu.register({
|
|
205
|
+
body,
|
|
206
|
+
side: props.side ?? "bottom",
|
|
207
|
+
align: props.align ?? "start",
|
|
208
|
+
sideOffset: props.sideOffset ?? 4,
|
|
209
|
+
class: props.class,
|
|
210
|
+
});
|
|
211
|
+
return html``;
|
|
212
|
+
},
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const parts = createMenuParts("dropdown-menu", DropdownMenuContext);
|
|
216
|
+
|
|
217
|
+
export const DropdownMenuGroup = parts.Group;
|
|
218
|
+
export const DropdownMenuItem = parts.Item;
|
|
219
|
+
export const DropdownMenuCheckboxItem = parts.CheckboxItem;
|
|
220
|
+
export const DropdownMenuRadioGroup = parts.RadioGroup;
|
|
221
|
+
export const DropdownMenuRadioItem = parts.RadioItem;
|
|
222
|
+
export const DropdownMenuLabel = parts.Label;
|
|
223
|
+
export const DropdownMenuSeparator = parts.Separator;
|
|
224
|
+
export const DropdownMenuShortcut = parts.Shortcut;
|
|
225
|
+
export const DropdownMenuSub = parts.Sub;
|
|
226
|
+
export const DropdownMenuSubTrigger = parts.SubTrigger;
|
|
227
|
+
export const DropdownMenuSubContent = parts.SubContent;
|
|
228
|
+
/**
|
|
229
|
+
* Mount children outside the component's own DOM position.
|
|
230
|
+
*
|
|
231
|
+
* `DropdownMenuContent` does not need it — the menu portals its own panel —
|
|
232
|
+
* and it is exported because upstream exports it, doing here what its name
|
|
233
|
+
* says rather than standing in as a passthrough.
|
|
234
|
+
*/
|
|
235
|
+
export const DropdownMenuPortal = parts.Portal;
|
|
236
|
+
|
|
237
|
+
export type {
|
|
238
|
+
MenuCheckboxItemProps as DropdownMenuCheckboxItemProps,
|
|
239
|
+
MenuGroupProps as DropdownMenuGroupProps,
|
|
240
|
+
MenuItemProps as DropdownMenuItemProps,
|
|
241
|
+
MenuLabelProps as DropdownMenuLabelProps,
|
|
242
|
+
MenuPortalProps as DropdownMenuPortalProps,
|
|
243
|
+
MenuRadioGroupProps as DropdownMenuRadioGroupProps,
|
|
244
|
+
MenuRadioItemProps as DropdownMenuRadioItemProps,
|
|
245
|
+
MenuSeparatorProps as DropdownMenuSeparatorProps,
|
|
246
|
+
MenuShortcutProps as DropdownMenuShortcutProps,
|
|
247
|
+
MenuSubContentProps as DropdownMenuSubContentProps,
|
|
248
|
+
MenuSubProps as DropdownMenuSubProps,
|
|
249
|
+
MenuSubTriggerProps as DropdownMenuSubTriggerProps,
|
|
250
|
+
} from "../primitives/menuParts.js";
|
|
251
|
+
|
|
252
|
+
function renderPanel(
|
|
253
|
+
registered: RegisteredContent,
|
|
254
|
+
ids: { id: string; labelledBy: string },
|
|
255
|
+
): TemplateResult {
|
|
256
|
+
return html`<div
|
|
257
|
+
data-slot="dropdown-menu-content"
|
|
258
|
+
id="${ids.id}"
|
|
259
|
+
role="menu"
|
|
260
|
+
aria-labelledby="${ids.labelledBy}"
|
|
261
|
+
aria-orientation="vertical"
|
|
262
|
+
tabindex="-1"
|
|
263
|
+
class="${cn(
|
|
264
|
+
menuPanelClasses,
|
|
265
|
+
zoomInOut,
|
|
266
|
+
slideInFromSide,
|
|
267
|
+
read(registered.class),
|
|
268
|
+
)}"
|
|
269
|
+
>${registered.body}</div>`;
|
|
270
|
+
}
|