@c9up/nebula 0.1.9 → 0.2.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.
Files changed (122) hide show
  1. package/dist/atoms/Avatar.d.ts +36 -9
  2. package/dist/atoms/Avatar.js +66 -27
  3. package/dist/atoms/index.d.ts +1 -1
  4. package/dist/atoms/index.js +1 -1
  5. package/dist/lib/children.d.ts +30 -0
  6. package/dist/lib/motion.d.ts +10 -0
  7. package/dist/lib/motion.js +10 -0
  8. package/dist/molecules/Accordion.d.ts +36 -15
  9. package/dist/molecules/Accordion.js +68 -45
  10. package/dist/molecules/Breadcrumb.d.ts +29 -11
  11. package/dist/molecules/Breadcrumb.js +52 -52
  12. package/dist/molecules/Collapsible.d.ts +18 -17
  13. package/dist/molecules/Collapsible.js +42 -34
  14. package/dist/molecules/Field.d.ts +66 -30
  15. package/dist/molecules/Field.js +106 -55
  16. package/dist/molecules/InputGroup.d.ts +39 -21
  17. package/dist/molecules/InputGroup.js +43 -27
  18. package/dist/molecules/InputOTP.d.ts +26 -3
  19. package/dist/molecules/InputOTP.js +109 -76
  20. package/dist/molecules/Message.js +5 -4
  21. package/dist/molecules/Pagination.d.ts +36 -11
  22. package/dist/molecules/Pagination.js +58 -74
  23. package/dist/molecules/RadioGroup.d.ts +25 -17
  24. package/dist/molecules/RadioGroup.js +47 -43
  25. package/dist/molecules/Resizable.d.ts +32 -7
  26. package/dist/molecules/Resizable.js +76 -49
  27. package/dist/molecules/Tabs.d.ts +49 -11
  28. package/dist/molecules/Tabs.js +107 -51
  29. package/dist/molecules/ToggleGroup.d.ts +16 -10
  30. package/dist/molecules/ToggleGroup.js +37 -21
  31. package/dist/molecules/index.d.ts +11 -11
  32. package/dist/molecules/index.js +11 -11
  33. package/dist/organisms/AlertDialog.d.ts +80 -28
  34. package/dist/organisms/AlertDialog.js +192 -67
  35. package/dist/organisms/Carousel.d.ts +25 -3
  36. package/dist/organisms/Carousel.js +75 -43
  37. package/dist/organisms/Combobox.d.ts +2 -1
  38. package/dist/organisms/Combobox.js +46 -16
  39. package/dist/organisms/Command.d.ts +73 -32
  40. package/dist/organisms/Command.js +209 -131
  41. package/dist/organisms/CommandDialog.js +5 -5
  42. package/dist/organisms/ContextMenu.d.ts +41 -19
  43. package/dist/organisms/ContextMenu.js +112 -71
  44. package/dist/organisms/DataTable.js +42 -8
  45. package/dist/organisms/Dialog.d.ts +97 -31
  46. package/dist/organisms/Dialog.js +193 -68
  47. package/dist/organisms/Drawer.d.ts +33 -10
  48. package/dist/organisms/Drawer.js +130 -52
  49. package/dist/organisms/DropdownMenu.d.ts +58 -11
  50. package/dist/organisms/DropdownMenu.js +137 -50
  51. package/dist/organisms/Form.js +9 -11
  52. package/dist/organisms/HoverCard.d.ts +39 -19
  53. package/dist/organisms/HoverCard.js +116 -40
  54. package/dist/organisms/Menubar.d.ts +55 -19
  55. package/dist/organisms/Menubar.js +166 -82
  56. package/dist/organisms/NavigationMenu.d.ts +77 -31
  57. package/dist/organisms/NavigationMenu.js +169 -98
  58. package/dist/organisms/Popover.d.ts +72 -12
  59. package/dist/organisms/Popover.js +132 -25
  60. package/dist/organisms/Questionnaire.js +5 -5
  61. package/dist/organisms/Select.d.ts +99 -18
  62. package/dist/organisms/Select.js +253 -111
  63. package/dist/organisms/Sheet.d.ts +57 -13
  64. package/dist/organisms/Sheet.js +141 -54
  65. package/dist/organisms/Sidebar.d.ts +146 -56
  66. package/dist/organisms/Sidebar.js +236 -128
  67. package/dist/organisms/Tooltip.d.ts +88 -14
  68. package/dist/organisms/Tooltip.js +210 -47
  69. package/dist/organisms/index.d.ts +15 -15
  70. package/dist/organisms/index.js +15 -15
  71. package/dist/primitives/floating.js +25 -0
  72. package/dist/primitives/floatingSurface.d.ts +11 -0
  73. package/dist/primitives/floatingSurface.js +8 -0
  74. package/dist/primitives/menu.d.ts +45 -1
  75. package/dist/primitives/menu.js +45 -14
  76. package/dist/primitives/menuParts.d.ts +113 -0
  77. package/dist/primitives/menuParts.js +207 -0
  78. package/nebula.css +1 -1
  79. package/package.json +3 -3
  80. package/registry.json +18 -6
  81. package/src/atoms/Avatar.ts +141 -37
  82. package/src/atoms/index.ts +13 -1
  83. package/src/lib/children.ts +31 -0
  84. package/src/lib/motion.ts +12 -0
  85. package/src/molecules/Accordion.ts +120 -60
  86. package/src/molecules/Breadcrumb.ts +77 -76
  87. package/src/molecules/Collapsible.ts +77 -41
  88. package/src/molecules/Field.ts +187 -72
  89. package/src/molecules/InputGroup.ts +88 -35
  90. package/src/molecules/InputOTP.ts +150 -86
  91. package/src/molecules/Message.ts +6 -4
  92. package/src/molecules/Pagination.ts +96 -101
  93. package/src/molecules/RadioGroup.ts +70 -59
  94. package/src/molecules/Resizable.ts +138 -75
  95. package/src/molecules/Tabs.ts +187 -74
  96. package/src/molecules/ToggleGroup.ts +77 -38
  97. package/src/molecules/index.ts +85 -8
  98. package/src/organisms/AlertDialog.ts +319 -82
  99. package/src/organisms/Carousel.ts +129 -52
  100. package/src/organisms/Combobox.ts +64 -19
  101. package/src/organisms/Command.ts +342 -159
  102. package/src/organisms/CommandDialog.ts +6 -6
  103. package/src/organisms/ContextMenu.ts +185 -83
  104. package/src/organisms/DataTable.ts +56 -10
  105. package/src/organisms/Dialog.ts +312 -102
  106. package/src/organisms/Drawer.ts +219 -71
  107. package/src/organisms/DropdownMenu.ts +219 -61
  108. package/src/organisms/Form.ts +48 -34
  109. package/src/organisms/HoverCard.ts +187 -53
  110. package/src/organisms/Menubar.ts +247 -87
  111. package/src/organisms/NavigationMenu.ts +306 -128
  112. package/src/organisms/Popover.ts +215 -39
  113. package/src/organisms/Questionnaire.ts +9 -6
  114. package/src/organisms/Select.ts +397 -130
  115. package/src/organisms/Sheet.ts +252 -83
  116. package/src/organisms/Sidebar.ts +438 -185
  117. package/src/organisms/Tooltip.ts +315 -65
  118. package/src/organisms/index.ts +284 -14
  119. package/src/primitives/floating.ts +33 -0
  120. package/src/primitives/floatingSurface.ts +19 -0
  121. package/src/primitives/menu.ts +79 -18
  122. package/src/primitives/menuParts.ts +371 -0
@@ -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); this component supplies the
6
- * markup and the trigger.
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 `title` is
16
- * required rather than optional — `srOnlyTitle` is there for designs that show
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
- export const dialogBackdropClasses = "fixed inset-0 z-50 bg-black/50";
29
- 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 outline-none sm:max-w-lg";
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
- modalSurface({
68
+ provide(DialogContext, {
69
+ triggerId,
70
+ panelId,
71
+ titleId,
72
+ descriptionId,
41
73
  open: () => state.current(),
42
- onClose: () => state.set(false),
43
- panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
44
- returnFocus: () => document.getElementById(triggerId),
45
- content: () => html `<div data-slot="dialog-overlay" class="${cn("fixed inset-0 z-50", fadeInOut)}">
46
- <div data-slot="dialog-backdrop" class="${dialogBackdropClasses}"></div>
47
- <div
48
- data-slot="dialog-content"
49
- id="${panelId}"
50
- role="dialog"
51
- aria-modal="true"
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
- if (props.trigger === undefined) {
93
- // No trigger: the dialog is driven entirely by the `open` prop. Something
94
- // still has to be in the tree for the component to mount into, and the
95
- // surface itself lives in a portal.
96
- return html `<span data-slot="dialog" hidden></span>`;
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="${() => (state.current() ? "true" : "false")}"
104
- class="${() => cn(read(props.triggerClass))}"
105
- @click="${() => state.set(true)}"
106
- >${slot(props.trigger)}</button>`;
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 { Child } from "../lib/children.js";
24
- import { type Slot } from "../lib/children.js";
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
- triggerClass?: Reactive<string>;
38
- contentClass?: Reactive<string>;
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 Drawer: (props?: DrawerProps | undefined) => import("@c9up/aurora").TemplateResult;
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;
@@ -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
- modalSurface({
87
+ provide(DrawerContext, {
88
+ triggerId,
89
+ panelId,
90
+ titleId,
91
+ descriptionId,
82
92
  open: () => state.current(),
83
- onClose: () => state.set(false),
84
- panel: (root) => root.querySelector(`#${CSS.escape(panelId)}`),
85
- returnFocus: () => document.getElementById(triggerId),
86
- content: () => html `<div data-slot="drawer-overlay" class="${cn("fixed inset-0 z-50", fadeInOut)}">
87
- <div class="${dialogBackdropClasses}"></div>
88
- <div
89
- data-slot="drawer-content"
90
- id="${panelId}"
91
- role="dialog"
92
- aria-modal="true"
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
- if (props.trigger === undefined) {
120
- return html `<span data-slot="drawer" hidden></span>`;
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="${() => (state.current() ? "true" : "false")}"
128
- class="${() => cn(read(props.triggerClass))}"
129
- @click="${() => state.set(true)}"
130
- >${slot(props.trigger)}</button>`;
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
+ }