@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/dist/organisms/Dialog.js
CHANGED
|
@@ -2,8 +2,24 @@
|
|
|
2
2
|
* Dialog — a modal window over the page.
|
|
3
3
|
*
|
|
4
4
|
* `modalSurface` supplies the contract (trap, scroll lock, backdrop dismissal,
|
|
5
|
-
* hiding the page from assistive technology);
|
|
6
|
-
*
|
|
5
|
+
* hiding the page from assistive technology); these parts supply the markup.
|
|
6
|
+
*
|
|
7
|
+
* Dialog({ children: () => html`
|
|
8
|
+
* ${DialogTrigger({ children: "Edit" })}
|
|
9
|
+
* ${DialogContent({ children: () => html`
|
|
10
|
+
* ${DialogHeader({ children: () => html`
|
|
11
|
+
* ${DialogTitle({ children: "Edit profile" })}
|
|
12
|
+
* ${DialogDescription({ children: "Change it." })}
|
|
13
|
+
* ` })}
|
|
14
|
+
* ${DialogFooter({ children: () => Button({ children: "Save" }) })}
|
|
15
|
+
* ` })}
|
|
16
|
+
* ` })
|
|
17
|
+
*
|
|
18
|
+
* Every part is built during `Dialog`'s setup, including the ones inside
|
|
19
|
+
* `DialogContent`. That is not only the context rule — `DialogTitle` has to
|
|
20
|
+
* have registered before the surface is wired, because the panel names itself
|
|
21
|
+
* with the title's id and a panel rendered first would have had nothing to
|
|
22
|
+
* point at.
|
|
7
23
|
*
|
|
8
24
|
* The `panel` callback matters more than it looks. The overlay's root spans
|
|
9
25
|
* the viewport so the backdrop can, which means containment has to be answered
|
|
@@ -12,11 +28,11 @@
|
|
|
12
28
|
*
|
|
13
29
|
* `aria-labelledby` points at the title and `aria-describedby` at the
|
|
14
30
|
* description, so a screen reader announces what the dialog is for on open. A
|
|
15
|
-
* dialog with neither announces only "dialog", which is why
|
|
16
|
-
*
|
|
17
|
-
* no visible heading.
|
|
31
|
+
* dialog with neither announces only "dialog", which is why a missing
|
|
32
|
+
* `DialogTitle` is warned about — as upstream warns — rather than tolerated.
|
|
18
33
|
*/
|
|
19
|
-
import { component, html, signal } from "@c9up/aurora";
|
|
34
|
+
import { component, createContext, html, inject, onUnmount, provide, signal, } from "@c9up/aurora";
|
|
35
|
+
import { Button } from "../atoms/Button.js";
|
|
20
36
|
import { slot } from "../lib/children.js";
|
|
21
37
|
import { cn } from "../lib/cn.js";
|
|
22
38
|
import { XIcon } from "../lib/icons.js";
|
|
@@ -25,86 +41,195 @@ import { fadeInOut, zoomInOut } from "../lib/motion.js";
|
|
|
25
41
|
import { read } from "../lib/props.js";
|
|
26
42
|
import { controllable } from "../primitives/controllable.js";
|
|
27
43
|
import { modalSurface } from "../primitives/modalSurface.js";
|
|
28
|
-
|
|
29
|
-
export const
|
|
44
|
+
import { portal } from "../primitives/portal.js";
|
|
45
|
+
export const dialogBackdropClasses = "fixed inset-0 z-50 bg-black/50 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0";
|
|
46
|
+
export const dialogPanelClasses = "bg-background fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-lg border p-6 shadow-lg duration-200 outline-none sm:max-w-lg";
|
|
47
|
+
const closeButtonClasses = "ring-offset-background focus-visible:ring-ring absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden disabled:pointer-events-none [&_svg]:pointer-events-none [&_svg]:shrink-0";
|
|
48
|
+
const DialogContext = createContext("Dialog");
|
|
49
|
+
/**
|
|
50
|
+
* The state every part reads.
|
|
51
|
+
*
|
|
52
|
+
* Renders no element of its own, as upstream does not: `data-slot="dialog"`
|
|
53
|
+
* sits on a Radix Root that produces no DOM.
|
|
54
|
+
*/
|
|
30
55
|
export const Dialog = component((props) => {
|
|
31
56
|
const triggerId = uid("dialog-trigger");
|
|
32
57
|
const panelId = uid("dialog-panel");
|
|
33
58
|
const titleId = uid("dialog-title");
|
|
34
59
|
const descriptionId = uid("dialog-description");
|
|
60
|
+
let hasTitle = false;
|
|
61
|
+
let hasDescription = false;
|
|
62
|
+
let content;
|
|
35
63
|
const state = controllable({
|
|
36
64
|
value: props.open,
|
|
37
65
|
initial: props.defaultOpen ?? false,
|
|
38
66
|
onChange: props.onOpenChange,
|
|
39
67
|
});
|
|
40
|
-
|
|
68
|
+
provide(DialogContext, {
|
|
69
|
+
triggerId,
|
|
70
|
+
panelId,
|
|
71
|
+
titleId,
|
|
72
|
+
descriptionId,
|
|
41
73
|
open: () => state.current(),
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
aria-labelledby="${titleId}"
|
|
53
|
-
aria-describedby="${props.description === undefined ? undefined : descriptionId}"
|
|
54
|
-
tabindex="-1"
|
|
55
|
-
class="${cn(dialogPanelClasses, zoomInOut, read(props.contentClass))}"
|
|
56
|
-
>
|
|
57
|
-
<div data-slot="dialog-header" class="flex flex-col gap-2 text-center sm:text-left">
|
|
58
|
-
<h2
|
|
59
|
-
id="${titleId}"
|
|
60
|
-
data-slot="dialog-title"
|
|
61
|
-
class="${cn("text-lg leading-none font-semibold", props.srOnlyTitle === true ? "sr-only" : "")}"
|
|
62
|
-
>${slot(props.title)}</h2>
|
|
63
|
-
${props.description === undefined
|
|
64
|
-
? null
|
|
65
|
-
: html `<p
|
|
66
|
-
id="${descriptionId}"
|
|
67
|
-
data-slot="dialog-description"
|
|
68
|
-
class="text-muted-foreground text-sm"
|
|
69
|
-
>${slot(props.description)}</p>`}
|
|
70
|
-
</div>
|
|
71
|
-
${slot(props.children)}
|
|
72
|
-
${props.footer === undefined
|
|
73
|
-
? null
|
|
74
|
-
: html `<div
|
|
75
|
-
data-slot="dialog-footer"
|
|
76
|
-
class="flex flex-col-reverse gap-2 sm:flex-row sm:justify-end"
|
|
77
|
-
>${slot(props.footer)}</div>`}
|
|
78
|
-
${props.hideCloseButton === true
|
|
79
|
-
? null
|
|
80
|
-
: html `<button
|
|
81
|
-
type="button"
|
|
82
|
-
data-slot="dialog-close"
|
|
83
|
-
class="ring-offset-background focus-visible:ring-ring absolute top-4 right-4 rounded-xs opacity-70 transition-opacity hover:opacity-100 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none"
|
|
84
|
-
@click="${() => state.set(false)}"
|
|
85
|
-
>
|
|
86
|
-
${XIcon({ class: "size-4" })}
|
|
87
|
-
<span class="sr-only">Close</span>
|
|
88
|
-
</button>`}
|
|
89
|
-
</div>
|
|
90
|
-
</div>`,
|
|
74
|
+
setOpen: (next) => state.set(next),
|
|
75
|
+
declareTitle() {
|
|
76
|
+
hasTitle = true;
|
|
77
|
+
},
|
|
78
|
+
declareDescription() {
|
|
79
|
+
hasDescription = true;
|
|
80
|
+
},
|
|
81
|
+
register(registered) {
|
|
82
|
+
content = registered;
|
|
83
|
+
},
|
|
91
84
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
85
|
+
const parts = props.children?.();
|
|
86
|
+
if (content !== undefined) {
|
|
87
|
+
const registered = content;
|
|
88
|
+
if (!hasTitle) {
|
|
89
|
+
// Upstream warns rather than raises, and so does this: a dialog with
|
|
90
|
+
// no accessible name is a real defect, but not one worth taking a
|
|
91
|
+
// page down for at runtime.
|
|
92
|
+
console.warn("nebula: a Dialog has no DialogTitle. It will be announced only as “dialog”.");
|
|
93
|
+
}
|
|
94
|
+
modalSurface({
|
|
95
|
+
open: () => state.current(),
|
|
96
|
+
onClose: () => state.set(false),
|
|
97
|
+
panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
|
|
98
|
+
returnFocus: () => document.getElementById(triggerId),
|
|
99
|
+
dismissOnOutside: registered.dismissOnOutside,
|
|
100
|
+
dismissOnEscape: registered.dismissOnEscape,
|
|
101
|
+
content: () => renderSurface(registered, {
|
|
102
|
+
panelId,
|
|
103
|
+
titleId: hasTitle ? titleId : undefined,
|
|
104
|
+
descriptionId: hasDescription ? descriptionId : undefined,
|
|
105
|
+
close: () => state.set(false),
|
|
106
|
+
}),
|
|
107
|
+
});
|
|
97
108
|
}
|
|
109
|
+
return html `${parts}`;
|
|
110
|
+
});
|
|
111
|
+
export const DialogTrigger = component((props) => {
|
|
112
|
+
const dialog = inject(DialogContext);
|
|
98
113
|
return html `<button
|
|
99
114
|
type="button"
|
|
100
115
|
data-slot="dialog-trigger"
|
|
101
|
-
id="${triggerId}"
|
|
116
|
+
id="${dialog.triggerId}"
|
|
102
117
|
aria-haspopup="dialog"
|
|
103
|
-
aria-expanded="${() => (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
118
|
+
aria-expanded="${() => (dialog.open() ? "true" : "false")}"
|
|
119
|
+
data-state="${() => (dialog.open() ? "open" : "closed")}"
|
|
120
|
+
class="${() => cn(read(props.class))}"
|
|
121
|
+
@click="${() => dialog.setOpen(true)}"
|
|
122
|
+
>${slot(props.children)}</button>`;
|
|
107
123
|
});
|
|
124
|
+
/** Anything that dismisses the dialog — a Cancel button, the corner cross. */
|
|
125
|
+
export const DialogClose = component((props) => {
|
|
126
|
+
const dialog = inject(DialogContext);
|
|
127
|
+
return html `<button
|
|
128
|
+
type="button"
|
|
129
|
+
data-slot="dialog-close"
|
|
130
|
+
class="${() => cn(read(props.class))}"
|
|
131
|
+
@click="${() => dialog.setOpen(false)}"
|
|
132
|
+
>${slot(props.children)}</button>`;
|
|
133
|
+
});
|
|
134
|
+
/** The backdrop. Rendered by `DialogContent`; exported to compose your own. */
|
|
135
|
+
export const DialogOverlay = component((props) => html `<div
|
|
136
|
+
data-slot="dialog-overlay"
|
|
137
|
+
class="${() => cn(dialogBackdropClasses, read(props.class))}"
|
|
138
|
+
></div>`);
|
|
139
|
+
/**
|
|
140
|
+
* Mount children outside the component's own DOM position.
|
|
141
|
+
*
|
|
142
|
+
* `DialogContent` does NOT need it — `Dialog` portals the whole overlay
|
|
143
|
+
* through `modalSurface`, which also owns the trap and the scroll lock. It is
|
|
144
|
+
* exported because upstream exports it, and it does here what its name says
|
|
145
|
+
* rather than standing in as a passthrough.
|
|
146
|
+
*/
|
|
147
|
+
export const DialogPortal = component((props) => {
|
|
148
|
+
const mounted = portal(html `${slot(props.children)}`, {
|
|
149
|
+
container: props.container,
|
|
150
|
+
});
|
|
151
|
+
mounted.host.setAttribute("data-slot", "dialog-portal");
|
|
152
|
+
onUnmount(() => mounted.close());
|
|
153
|
+
return html ``;
|
|
154
|
+
});
|
|
155
|
+
/**
|
|
156
|
+
* The panel.
|
|
157
|
+
*
|
|
158
|
+
* Renders nothing where it is written: the dialog portals its overlay out of
|
|
159
|
+
* the tree when it opens. Its children are built HERE, during setup, so the
|
|
160
|
+
* parts inside see the context and register before the surface is wired.
|
|
161
|
+
*/
|
|
162
|
+
export const DialogContent = component((props) => {
|
|
163
|
+
const dialog = inject(DialogContext);
|
|
164
|
+
const body = props.children?.();
|
|
165
|
+
dialog.register({
|
|
166
|
+
body,
|
|
167
|
+
showCloseButton: props.showCloseButton ?? true,
|
|
168
|
+
dismissOnOutside: props.dismissOnOutside ?? true,
|
|
169
|
+
dismissOnEscape: props.dismissOnEscape ?? true,
|
|
170
|
+
class: props.class,
|
|
171
|
+
});
|
|
172
|
+
return html ``;
|
|
173
|
+
});
|
|
174
|
+
export const DialogHeader = component((props) => html `<div
|
|
175
|
+
data-slot="dialog-header"
|
|
176
|
+
class="${() => cn("flex flex-col gap-2 text-center sm:text-left", read(props.class))}"
|
|
177
|
+
>${slot(props.children)}</div>`);
|
|
178
|
+
export const DialogFooter = component((props) => {
|
|
179
|
+
const dialog = inject(DialogContext);
|
|
180
|
+
const trailing = props.showCloseButton === true
|
|
181
|
+
? html `<button
|
|
182
|
+
type="button"
|
|
183
|
+
data-slot="dialog-close"
|
|
184
|
+
@click="${() => dialog.setOpen(false)}"
|
|
185
|
+
>${Button({ variant: "outline", children: "Close" })}</button>`
|
|
186
|
+
: null;
|
|
187
|
+
return html `<div
|
|
188
|
+
data-slot="dialog-footer"
|
|
189
|
+
class="${() => cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", read(props.class))}"
|
|
190
|
+
>${slot(props.children)}${trailing}</div>`;
|
|
191
|
+
});
|
|
192
|
+
export const DialogTitle = component((props) => {
|
|
193
|
+
const dialog = inject(DialogContext);
|
|
194
|
+
dialog.declareTitle();
|
|
195
|
+
return html `<h2
|
|
196
|
+
id="${dialog.titleId}"
|
|
197
|
+
data-slot="dialog-title"
|
|
198
|
+
class="${() => cn("text-lg leading-none font-semibold", props.srOnly === true ? "sr-only" : "", read(props.class))}"
|
|
199
|
+
>${slot(props.children)}</h2>`;
|
|
200
|
+
});
|
|
201
|
+
export const DialogDescription = component((props) => {
|
|
202
|
+
const dialog = inject(DialogContext);
|
|
203
|
+
dialog.declareDescription();
|
|
204
|
+
return html `<p
|
|
205
|
+
id="${dialog.descriptionId}"
|
|
206
|
+
data-slot="dialog-description"
|
|
207
|
+
class="${() => cn("text-muted-foreground text-sm", read(props.class))}"
|
|
208
|
+
>${slot(props.children)}</p>`;
|
|
209
|
+
});
|
|
210
|
+
function renderSurface(registered, ids) {
|
|
211
|
+
const corner = registered.showCloseButton
|
|
212
|
+
? html `<button
|
|
213
|
+
type="button"
|
|
214
|
+
data-slot="dialog-close"
|
|
215
|
+
class="${closeButtonClasses}"
|
|
216
|
+
@click="${() => ids.close()}"
|
|
217
|
+
>${XIcon({ class: "size-4" })}<span class="sr-only">Close</span></button>`
|
|
218
|
+
: null;
|
|
219
|
+
return html `<div data-slot="dialog-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
|
|
220
|
+
<div data-slot="dialog-overlay" class="${dialogBackdropClasses}"></div>
|
|
221
|
+
<div
|
|
222
|
+
data-slot="dialog-content"
|
|
223
|
+
id="${ids.panelId}"
|
|
224
|
+
role="dialog"
|
|
225
|
+
aria-modal="true"
|
|
226
|
+
aria-labelledby="${ids.titleId}"
|
|
227
|
+
aria-describedby="${ids.descriptionId}"
|
|
228
|
+
tabindex="-1"
|
|
229
|
+
class="${cn(dialogPanelClasses, zoomInOut, read(registered.class))}"
|
|
230
|
+
>${registered.body}${corner}</div>
|
|
231
|
+
</div>`;
|
|
232
|
+
}
|
|
108
233
|
/** A dialog driven purely by a signal, with no trigger of its own. */
|
|
109
234
|
export function useDialog(initial = false) {
|
|
110
235
|
const open = signal(initial);
|
|
@@ -20,21 +20,44 @@
|
|
|
20
20
|
* Escape and the backdrop already do — a gesture must never be the only way
|
|
21
21
|
* out of a modal surface.
|
|
22
22
|
*/
|
|
23
|
-
import type
|
|
24
|
-
import {
|
|
23
|
+
import { type TemplateResult } from "@c9up/aurora";
|
|
24
|
+
import type { Parts, Slot } from "../lib/children.js";
|
|
25
25
|
import { type Reactive } from "../lib/props.js";
|
|
26
26
|
export interface DrawerProps {
|
|
27
|
-
trigger?: Slot;
|
|
28
|
-
title: Child;
|
|
29
|
-
description?: Child;
|
|
30
|
-
children?: Slot;
|
|
31
|
-
footer?: Slot;
|
|
32
27
|
open?: Reactive<boolean>;
|
|
33
28
|
defaultOpen?: boolean;
|
|
34
29
|
onOpenChange?: (open: boolean) => void;
|
|
30
|
+
children?: Parts;
|
|
31
|
+
}
|
|
32
|
+
export declare const Drawer: (props?: DrawerProps | undefined) => TemplateResult;
|
|
33
|
+
export interface DrawerTriggerProps {
|
|
34
|
+
children?: Slot;
|
|
35
|
+
class?: Reactive<string>;
|
|
36
|
+
}
|
|
37
|
+
export declare const DrawerTrigger: (props?: DrawerTriggerProps | undefined) => TemplateResult;
|
|
38
|
+
export interface DrawerCloseProps {
|
|
39
|
+
children?: Slot;
|
|
40
|
+
class?: Reactive<string>;
|
|
41
|
+
}
|
|
42
|
+
export declare const DrawerClose: (props?: DrawerCloseProps | undefined) => TemplateResult;
|
|
43
|
+
export interface DrawerOverlayProps {
|
|
44
|
+
class?: Reactive<string>;
|
|
45
|
+
}
|
|
46
|
+
export declare const DrawerOverlay: (props?: DrawerOverlayProps | undefined) => TemplateResult;
|
|
47
|
+
export interface DrawerContentProps {
|
|
48
|
+
children?: Parts;
|
|
49
|
+
/** The grab bar. Decorative — see `onPointerDown`. */
|
|
50
|
+
showHandle?: boolean;
|
|
35
51
|
/** Turn off drag-to-dismiss — for a drawer holding a scrollable list. */
|
|
36
52
|
disableDrag?: boolean;
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
class?: Reactive<string>;
|
|
54
|
+
}
|
|
55
|
+
export declare const DrawerContent: (props?: DrawerContentProps | undefined) => TemplateResult;
|
|
56
|
+
export interface DrawerSectionProps {
|
|
57
|
+
children?: Slot;
|
|
58
|
+
class?: Reactive<string>;
|
|
39
59
|
}
|
|
40
|
-
export declare const
|
|
60
|
+
export declare const DrawerHeader: (props?: DrawerSectionProps | undefined) => TemplateResult;
|
|
61
|
+
export declare const DrawerFooter: (props?: DrawerSectionProps | undefined) => TemplateResult;
|
|
62
|
+
export declare const DrawerTitle: (props?: DrawerSectionProps | undefined) => TemplateResult;
|
|
63
|
+
export declare const DrawerDescription: (props?: DrawerSectionProps | undefined) => TemplateResult;
|
package/dist/organisms/Drawer.js
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
* Escape and the backdrop already do — a gesture must never be the only way
|
|
21
21
|
* out of a modal surface.
|
|
22
22
|
*/
|
|
23
|
-
import { component, html } from "@c9up/aurora";
|
|
23
|
+
import { component, createContext, html, inject, provide, } from "@c9up/aurora";
|
|
24
24
|
import { slot } from "../lib/children.js";
|
|
25
25
|
import { cn } from "../lib/cn.js";
|
|
26
26
|
import { uid } from "../lib/id.js";
|
|
@@ -29,23 +29,32 @@ import { read } from "../lib/props.js";
|
|
|
29
29
|
import { controllable } from "../primitives/controllable.js";
|
|
30
30
|
import { modalSurface } from "../primitives/modalSurface.js";
|
|
31
31
|
import { dialogBackdropClasses } from "./Dialog.js";
|
|
32
|
-
/** Drag this far down and release to dismiss, in px. */
|
|
33
32
|
const DISMISS_DISTANCE = 120;
|
|
34
|
-
/** Or release faster than this, in px per millisecond. */
|
|
35
33
|
const DISMISS_VELOCITY = 0.5;
|
|
34
|
+
const panelClasses = "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";
|
|
35
|
+
const DrawerContext = createContext("Drawer");
|
|
36
36
|
export const Drawer = component((props) => {
|
|
37
37
|
const triggerId = uid("drawer-trigger");
|
|
38
38
|
const panelId = uid("drawer-panel");
|
|
39
39
|
const titleId = uid("drawer-title");
|
|
40
40
|
const descriptionId = uid("drawer-description");
|
|
41
|
+
let hasTitle = false;
|
|
42
|
+
let hasDescription = false;
|
|
43
|
+
let content;
|
|
41
44
|
const state = controllable({
|
|
42
45
|
value: props.open,
|
|
43
46
|
initial: props.defaultOpen ?? false,
|
|
44
47
|
onChange: props.onOpenChange,
|
|
45
48
|
});
|
|
49
|
+
/**
|
|
50
|
+
* Drag to dismiss.
|
|
51
|
+
*
|
|
52
|
+
* Distance OR velocity: a slow long drag and a quick flick both mean the
|
|
53
|
+
* same thing, and requiring distance alone makes a flick feel ignored.
|
|
54
|
+
* Everything it does, Escape and the backdrop already do — a gesture must
|
|
55
|
+
* never be the only way out of a modal surface.
|
|
56
|
+
*/
|
|
46
57
|
function onPointerDown(event) {
|
|
47
|
-
if (props.disableDrag === true)
|
|
48
|
-
return;
|
|
49
58
|
const panel = document.getElementById(panelId);
|
|
50
59
|
if (panel === null)
|
|
51
60
|
return;
|
|
@@ -65,67 +74,136 @@ export const Drawer = component((props) => {
|
|
|
65
74
|
panel.removeEventListener("pointercancel", onUp);
|
|
66
75
|
// Restore the transition before deciding, so both outcomes animate.
|
|
67
76
|
panel.style.transition = "";
|
|
77
|
+
panel.style.transform = "";
|
|
68
78
|
const velocity = offset / Math.max(1, performance.now() - startedAt);
|
|
69
79
|
if (offset > DISMISS_DISTANCE || velocity > DISMISS_VELOCITY) {
|
|
70
|
-
panel.style.transform = "";
|
|
71
80
|
state.set(false);
|
|
72
81
|
}
|
|
73
|
-
else {
|
|
74
|
-
panel.style.transform = "";
|
|
75
|
-
}
|
|
76
82
|
};
|
|
77
83
|
panel.addEventListener("pointermove", onMove);
|
|
78
84
|
panel.addEventListener("pointerup", onUp);
|
|
79
85
|
panel.addEventListener("pointercancel", onUp);
|
|
80
86
|
}
|
|
81
|
-
|
|
87
|
+
provide(DrawerContext, {
|
|
88
|
+
triggerId,
|
|
89
|
+
panelId,
|
|
90
|
+
titleId,
|
|
91
|
+
descriptionId,
|
|
82
92
|
open: () => state.current(),
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
aria-labelledby="${titleId}"
|
|
94
|
-
aria-describedby="${props.description === undefined ? undefined : descriptionId}"
|
|
95
|
-
tabindex="-1"
|
|
96
|
-
class="${cn("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", slideFrom("bottom"), read(props.contentClass))}"
|
|
97
|
-
@pointerdown="${onPointerDown}"
|
|
98
|
-
>
|
|
99
|
-
<div
|
|
100
|
-
data-slot="drawer-handle"
|
|
101
|
-
aria-hidden="true"
|
|
102
|
-
class="bg-muted mx-auto h-1.5 w-12 shrink-0 rounded-full"
|
|
103
|
-
></div>
|
|
104
|
-
<div class="flex flex-col gap-1.5 text-center sm:text-left">
|
|
105
|
-
<h2 id="${titleId}" class="text-foreground font-semibold">${props.title}</h2>
|
|
106
|
-
${props.description === undefined
|
|
107
|
-
? null
|
|
108
|
-
: html `<p id="${descriptionId}" class="text-muted-foreground text-sm">
|
|
109
|
-
${props.description}
|
|
110
|
-
</p>`}
|
|
111
|
-
</div>
|
|
112
|
-
<div class="flex-1 overflow-y-auto">${slot(props.children)}</div>
|
|
113
|
-
${props.footer === undefined
|
|
114
|
-
? null
|
|
115
|
-
: html `<div class="mt-auto flex flex-col gap-2">${slot(props.footer)}</div>`}
|
|
116
|
-
</div>
|
|
117
|
-
</div>`,
|
|
93
|
+
setOpen: (next) => state.set(next),
|
|
94
|
+
declareTitle() {
|
|
95
|
+
hasTitle = true;
|
|
96
|
+
},
|
|
97
|
+
declareDescription() {
|
|
98
|
+
hasDescription = true;
|
|
99
|
+
},
|
|
100
|
+
register(registered) {
|
|
101
|
+
content = registered;
|
|
102
|
+
},
|
|
118
103
|
});
|
|
119
|
-
|
|
120
|
-
|
|
104
|
+
const parts = props.children?.();
|
|
105
|
+
if (content !== undefined) {
|
|
106
|
+
const registered = content;
|
|
107
|
+
if (!hasTitle) {
|
|
108
|
+
console.warn("nebula: a Drawer has no DrawerTitle. It will be announced only as \u201cdialog\u201d.");
|
|
109
|
+
}
|
|
110
|
+
modalSurface({
|
|
111
|
+
open: () => state.current(),
|
|
112
|
+
onClose: () => state.set(false),
|
|
113
|
+
panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
|
|
114
|
+
returnFocus: () => document.getElementById(triggerId),
|
|
115
|
+
content: () => renderSurface(registered, {
|
|
116
|
+
panelId,
|
|
117
|
+
titleId: hasTitle ? titleId : undefined,
|
|
118
|
+
descriptionId: hasDescription ? descriptionId : undefined,
|
|
119
|
+
onPointerDown: registered.disableDrag ? undefined : onPointerDown,
|
|
120
|
+
}),
|
|
121
|
+
});
|
|
121
122
|
}
|
|
123
|
+
return html `${parts}`;
|
|
124
|
+
});
|
|
125
|
+
export const DrawerTrigger = component((props) => {
|
|
126
|
+
const drawer = inject(DrawerContext);
|
|
122
127
|
return html `<button
|
|
123
128
|
type="button"
|
|
124
129
|
data-slot="drawer-trigger"
|
|
125
|
-
id="${triggerId}"
|
|
130
|
+
id="${drawer.triggerId}"
|
|
126
131
|
aria-haspopup="dialog"
|
|
127
|
-
aria-expanded="${() => (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
aria-expanded="${() => (drawer.open() ? "true" : "false")}"
|
|
133
|
+
data-state="${() => (drawer.open() ? "open" : "closed")}"
|
|
134
|
+
class="${() => cn(read(props.class))}"
|
|
135
|
+
@click="${() => drawer.setOpen(true)}"
|
|
136
|
+
>${slot(props.children)}</button>`;
|
|
137
|
+
});
|
|
138
|
+
export const DrawerClose = component((props) => {
|
|
139
|
+
const drawer = inject(DrawerContext);
|
|
140
|
+
return html `<button
|
|
141
|
+
type="button"
|
|
142
|
+
data-slot="drawer-close"
|
|
143
|
+
class="${() => cn(read(props.class))}"
|
|
144
|
+
@click="${() => drawer.setOpen(false)}"
|
|
145
|
+
>${slot(props.children)}</button>`;
|
|
146
|
+
});
|
|
147
|
+
export const DrawerOverlay = component((props) => html `<div
|
|
148
|
+
data-slot="drawer-overlay"
|
|
149
|
+
class="${() => cn(dialogBackdropClasses, read(props.class))}"
|
|
150
|
+
></div>`);
|
|
151
|
+
export const DrawerContent = component((props) => {
|
|
152
|
+
const drawer = inject(DrawerContext);
|
|
153
|
+
drawer.register({
|
|
154
|
+
body: props.children?.(),
|
|
155
|
+
showHandle: props.showHandle !== false,
|
|
156
|
+
disableDrag: props.disableDrag === true,
|
|
157
|
+
class: props.class,
|
|
158
|
+
});
|
|
159
|
+
return html ``;
|
|
160
|
+
});
|
|
161
|
+
export const DrawerHeader = component((props) => html `<div
|
|
162
|
+
data-slot="drawer-header"
|
|
163
|
+
class="${() => cn("flex flex-col gap-1.5 p-4", read(props.class))}"
|
|
164
|
+
>${slot(props.children)}</div>`);
|
|
165
|
+
export const DrawerFooter = component((props) => html `<div
|
|
166
|
+
data-slot="drawer-footer"
|
|
167
|
+
class="${() => cn("mt-auto flex flex-col gap-2 p-4", read(props.class))}"
|
|
168
|
+
>${slot(props.children)}</div>`);
|
|
169
|
+
export const DrawerTitle = component((props) => {
|
|
170
|
+
const drawer = inject(DrawerContext);
|
|
171
|
+
drawer.declareTitle();
|
|
172
|
+
return html `<h2
|
|
173
|
+
id="${drawer.titleId}"
|
|
174
|
+
data-slot="drawer-title"
|
|
175
|
+
class="${() => cn("text-foreground font-semibold", read(props.class))}"
|
|
176
|
+
>${slot(props.children)}</h2>`;
|
|
177
|
+
});
|
|
178
|
+
export const DrawerDescription = component((props) => {
|
|
179
|
+
const drawer = inject(DrawerContext);
|
|
180
|
+
drawer.declareDescription();
|
|
181
|
+
return html `<p
|
|
182
|
+
id="${drawer.descriptionId}"
|
|
183
|
+
data-slot="drawer-description"
|
|
184
|
+
class="${() => cn("text-muted-foreground text-sm", read(props.class))}"
|
|
185
|
+
>${slot(props.children)}</p>`;
|
|
131
186
|
});
|
|
187
|
+
function renderSurface(registered, ids) {
|
|
188
|
+
const handle = registered.showHandle
|
|
189
|
+
? html `<div
|
|
190
|
+
data-slot="drawer-handle"
|
|
191
|
+
aria-hidden="true"
|
|
192
|
+
class="bg-muted mx-auto mt-4 h-1.5 w-12 shrink-0 rounded-full"
|
|
193
|
+
></div>`
|
|
194
|
+
: null;
|
|
195
|
+
return html `<div data-slot="drawer-portal" class="${cn("fixed inset-0 z-50", fadeInOut)}">
|
|
196
|
+
<div data-slot="drawer-overlay" class="${dialogBackdropClasses}"></div>
|
|
197
|
+
<div
|
|
198
|
+
data-slot="drawer-content"
|
|
199
|
+
id="${ids.panelId}"
|
|
200
|
+
role="dialog"
|
|
201
|
+
aria-modal="true"
|
|
202
|
+
aria-labelledby="${ids.titleId}"
|
|
203
|
+
aria-describedby="${ids.descriptionId}"
|
|
204
|
+
tabindex="-1"
|
|
205
|
+
class="${cn(panelClasses, slideFrom("bottom"), read(registered.class))}"
|
|
206
|
+
@pointerdown="${ids.onPointerDown}"
|
|
207
|
+
>${handle}${registered.body}</div>
|
|
208
|
+
</div>`;
|
|
209
|
+
}
|