@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
@@ -1,139 +1,210 @@
1
1
  /**
2
- * NavigationMenu — a site navigation bar with drop-down panels.
2
+ * NavigationMenu — a site's top-level navigation, with panels.
3
3
  *
4
- * Not a menu, despite the name Radix gave it, and the distinction changes the
5
- * markup. A menu is a list of *commands*; this is a list of *links*. So it is
6
- * a `<nav>` containing a list of anchors, with `aria-expanded` on the entries
7
- * that open a panel — never `role="menu"`, which would have a screen reader
8
- * announce a navigation bar as an application menu and offer the wrong
9
- * shortcuts for it.
4
+ * NavigationMenu({ children: () => NavigationMenuList({ children: html`
5
+ * ${NavigationMenuItem({ children: () => html`
6
+ * ${NavigationMenuTrigger({ children: "Products" })}
7
+ * ${NavigationMenuContent({ children: () => html`
8
+ * ${NavigationMenuLink({ href: "/a", children: "Analytics" })}
9
+ * ` })}
10
+ * ` })}
11
+ * ${NavigationMenuItem({ children: () =>
12
+ * NavigationMenuLink({ href: "/pricing", children: "Pricing" })
13
+ * })}
14
+ * ` }) })
10
15
  *
11
- * Entries are either a plain link or a link with a panel. Hover opens a panel
12
- * after a short delay and keeps it open briefly on leave, because the pointer
13
- * has to travel from the trigger to the panel and a naive `mouseleave` closes
14
- * it in the gap between them.
16
+ * One surface for the whole bar, as in `Menubar`: only one panel is ever open,
17
+ * and moving between triggers is a change of anchor and contents rather than a
18
+ * close and an open.
15
19
  *
16
- * Every panel is reachable by keyboard: the trigger is a real button, Enter
17
- * and ArrowDown open it, Escape closes it and returns focus.
20
+ * The delays are what make it usable with a pointer. Opening waits, so
21
+ * crossing the bar does not flash every panel on the way; closing waits
22
+ * longer, so travelling diagonally from a trigger into its panel does not
23
+ * dismiss it halfway. A plain link closes an open panel on hover, because the
24
+ * pointer has left that branch of the bar.
18
25
  */
19
- import { component, html, onUnmount, signal } from "@c9up/aurora";
26
+ import { component, createContext, html, inject, onUnmount, provide, signal, } from "@c9up/aurora";
20
27
  import { slot } from "../lib/children.js";
21
28
  import { cn } from "../lib/cn.js";
22
29
  import { ChevronDownIcon } from "../lib/icons.js";
23
30
  import { uid } from "../lib/id.js";
24
- import { zoomInOut } from "../lib/motion.js";
31
+ import { slideInFromSide, zoomInOut } from "../lib/motion.js";
25
32
  import { read } from "../lib/props.js";
26
33
  import { floatingSurface } from "../primitives/floatingSurface.js";
27
34
  const OPEN_DELAY_MS = 150;
28
35
  const CLOSE_DELAY_MS = 250;
29
- export const triggerClasses = "group inline-flex h-9 w-max items-center justify-center gap-1 rounded-md bg-background px-4 py-2 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground focus:outline-none disabled:pointer-events-none disabled:opacity-50 data-[state=open]:bg-accent/50";
36
+ export const triggerClasses = "group bg-background hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground focus-visible:ring-ring/50 data-[state=open]:bg-accent/50 inline-flex h-9 w-max items-center justify-center gap-1 rounded-md px-4 py-2 text-sm font-medium transition-[color,box-shadow] outline-none focus-visible:ring-[3px] focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50";
37
+ const NavigationMenuContext = createContext("NavigationMenu");
38
+ const NavigationMenuItemContext = createContext("NavigationMenuItem");
30
39
  export const NavigationMenu = component((props) => {
31
40
  const contentId = uid("navigation-menu-content");
32
- const triggerIds = props.items.map(() => uid("navigation-menu-trigger"));
41
+ const panels = [];
33
42
  const openIndex = signal(-1);
34
43
  let timer;
35
- function clear() {
44
+ function cancelPending() {
36
45
  if (timer !== undefined)
37
46
  clearTimeout(timer);
38
47
  timer = undefined;
39
48
  }
40
49
  function schedule(index, delay) {
41
- clear();
50
+ cancelPending();
51
+ if (delay === 0) {
52
+ openIndex(index);
53
+ return;
54
+ }
42
55
  timer = setTimeout(() => openIndex(index), delay);
43
56
  }
44
- onUnmount(clear);
45
57
  function close() {
46
- clear();
58
+ cancelPending();
47
59
  openIndex(-1);
48
60
  }
61
+ onUnmount(cancelPending);
62
+ provide(NavigationMenuContext, {
63
+ contentId,
64
+ claim(triggerId) {
65
+ panels.push({ triggerId, body: null, panelClass: undefined });
66
+ return panels.length - 1;
67
+ },
68
+ describe(index, patch) {
69
+ const entry = panels[index];
70
+ if (entry === undefined)
71
+ return;
72
+ Object.assign(entry, patch);
73
+ },
74
+ openIndex: () => openIndex(),
75
+ scheduleOpen: (index, immediate) => schedule(index, immediate ? 0 : OPEN_DELAY_MS),
76
+ scheduleClose: () => schedule(-1, CLOSE_DELAY_MS),
77
+ cancelPending,
78
+ close,
79
+ });
80
+ const parts = props.children?.();
49
81
  floatingSurface({
50
82
  anchor: () => {
51
- const id = triggerIds[openIndex()];
52
- return id === undefined ? null : document.getElementById(id);
83
+ const entry = panels[openIndex()];
84
+ return entry === undefined
85
+ ? null
86
+ : document.getElementById(entry.triggerId);
53
87
  },
54
88
  open: () => openIndex() !== -1,
55
89
  onClose: close,
56
90
  placement: "bottom-start",
57
91
  offset: 6,
58
- content: () => html `<div
59
- data-slot="navigation-menu-content"
60
- id="${contentId}"
61
- class="${cn("bg-popover text-popover-foreground z-50 rounded-md border p-4 shadow-md outline-none", zoomInOut)}"
62
- @pointerenter="${clear}"
63
- @pointerleave="${() => schedule(-1, CLOSE_DELAY_MS)}"
64
- >${renderPanel(props.items[openIndex()])}</div>`,
92
+ content: () => renderPanel(panels[openIndex()], contentId, {
93
+ enter: cancelPending,
94
+ leave: () => schedule(-1, CLOSE_DELAY_MS),
95
+ }),
65
96
  });
66
97
  return html `<nav
67
98
  data-slot="navigation-menu"
68
- class="${() => cn("relative flex max-w-max flex-1 items-center justify-center", read(props.class))}"
69
- >
70
- <ul class="flex flex-1 list-none items-center justify-center gap-1">
71
- ${props.items.map((item, index) => renderEntry(item, index))}
72
- </ul>
73
- </nav>`;
74
- function renderEntry(item, index) {
75
- if (item.href !== undefined) {
76
- return html `<li>
77
- <a
78
- href="${item.href}"
79
- data-slot="navigation-menu-link"
80
- class="${triggerClasses}"
81
- @pointerenter="${() => {
82
- // Hovering a plain link while a panel is open closes it: the
83
- // pointer has left the panel's branch of the bar.
84
- if (openIndex() !== -1)
85
- schedule(-1, CLOSE_DELAY_MS);
86
- }}"
87
- >${item.label}</a>
88
- </li>`;
99
+ class="${() => cn("group/navigation-menu relative flex max-w-max flex-1 items-center justify-center", read(props.class))}"
100
+ >${parts}</nav>`;
101
+ });
102
+ export const NavigationMenuList = component((props) => html `<ul
103
+ data-slot="navigation-menu-list"
104
+ class="${() => cn("group flex flex-1 list-none items-center justify-center gap-1", read(props.class))}"
105
+ >${slot(props.children)}</ul>`);
106
+ /**
107
+ * One entry in the bar.
108
+ *
109
+ * Claims a panel slot as it is built, whether or not it holds one: the index
110
+ * is what the trigger and the content use to find each other, and an item that
111
+ * is only a link simply never fills its slot.
112
+ */
113
+ export const NavigationMenuItem = component((props) => {
114
+ const menu = inject(NavigationMenuContext);
115
+ const triggerId = uid("navigation-menu-trigger");
116
+ const index = menu.claim(triggerId);
117
+ provide(NavigationMenuItemContext, {
118
+ index,
119
+ triggerId,
120
+ });
121
+ return html `<li
122
+ data-slot="navigation-menu-item"
123
+ class="${() => cn("relative", read(props.class))}"
124
+ >${props.children?.()}</li>`;
125
+ });
126
+ export const NavigationMenuTrigger = component((props) => {
127
+ const menu = inject(NavigationMenuContext);
128
+ const item = inject(NavigationMenuItemContext);
129
+ const open = () => menu.openIndex() === item.index;
130
+ function onKeyDown(event) {
131
+ if (event.key === "ArrowDown" ||
132
+ event.key === "Enter" ||
133
+ event.key === " ") {
134
+ event.preventDefault();
135
+ menu.scheduleOpen(item.index, true);
89
136
  }
90
- return html `<li>
91
- <button
92
- type="button"
93
- id="${triggerIds[index]}"
94
- data-slot="navigation-menu-trigger"
95
- aria-expanded="${() => (openIndex() === index ? "true" : "false")}"
96
- aria-controls="${() => (openIndex() === index ? contentId : undefined)}"
97
- data-state="${() => (openIndex() === index ? "open" : "closed")}"
98
- class="${triggerClasses}"
99
- @click="${() => (openIndex() === index ? close() : openIndex(index))}"
100
- @pointerenter="${() => schedule(index, OPEN_DELAY_MS)}"
101
- @pointerleave="${() => schedule(-1, CLOSE_DELAY_MS)}"
102
- @keydown="${(event) => {
103
- if (event.key === "ArrowDown" ||
104
- event.key === "Enter" ||
105
- event.key === " ") {
106
- event.preventDefault();
107
- openIndex(index);
108
- }
109
- }}"
110
- >
111
- ${item.label}
112
- ${ChevronDownIcon({
113
- class: "relative top-px size-3 transition-transform duration-200 group-data-[state=open]:rotate-180",
114
- })}
115
- </button>
116
- </li>`;
117
137
  }
138
+ return html `<button
139
+ type="button"
140
+ data-slot="navigation-menu-trigger"
141
+ id="${item.triggerId}"
142
+ aria-expanded="${() => (open() ? "true" : "false")}"
143
+ aria-controls="${() => (open() ? menu.contentId : undefined)}"
144
+ data-state="${() => (open() ? "open" : "closed")}"
145
+ class="${() => cn(triggerClasses, read(props.class))}"
146
+ @click="${() => open() ? menu.close() : menu.scheduleOpen(item.index, true)}"
147
+ @pointerenter="${() => menu.scheduleOpen(item.index, false)}"
148
+ @pointerleave="${() => menu.scheduleClose()}"
149
+ @keydown="${onKeyDown}"
150
+ >${slot(props.children)}${ChevronDownIcon({
151
+ class: "relative top-px size-3 transition-transform duration-200 group-data-[state=open]:rotate-180",
152
+ })}</button>`;
153
+ });
154
+ export const NavigationMenuContent = component((props) => {
155
+ const menu = inject(NavigationMenuContext);
156
+ const item = inject(NavigationMenuItemContext);
157
+ menu.describe(item.index, {
158
+ body: props.children?.(),
159
+ panelClass: props.class,
160
+ });
161
+ return html ``;
162
+ });
163
+ export const NavigationMenuLink = component((props) => {
164
+ const menu = inject(NavigationMenuContext);
165
+ return html `<a
166
+ data-slot="navigation-menu-link"
167
+ href="${props.href}"
168
+ data-active="${() => (read(props.active) === true ? "true" : undefined)}"
169
+ aria-current="${() => (read(props.active) === true ? "page" : undefined)}"
170
+ class="${() => cn("hover:bg-accent hover:text-accent-foreground focus:bg-accent focus:text-accent-foreground focus-visible:ring-ring/50 data-[active=true]:bg-accent/50 data-[active=true]:focus:bg-accent data-[active=true]:hover:bg-accent flex flex-col gap-1 rounded-sm p-2 text-sm transition-all outline-none focus-visible:ring-[3px] focus-visible:outline-1 [&_svg:not([class*='size-'])]:size-4", read(props.class))}"
171
+ @pointerenter="${() => {
172
+ // Hovering a link in the BAR while a panel is open closes it: the
173
+ // pointer has left that branch. A link inside the panel is harmless
174
+ // — the panel's own `pointerenter` has already cancelled the close.
175
+ if (menu.openIndex() !== -1)
176
+ menu.scheduleClose();
177
+ }}"
178
+ >${slot(props.children)}</a>`;
179
+ });
180
+ /**
181
+ * Where the panel is drawn.
182
+ *
183
+ * NAMED DEVIATION — inert here. Radix renders every panel into one viewport
184
+ * element positioned under the bar, which is what its width and height
185
+ * animations interpolate against. The panel is portalled and positioned by
186
+ * `floatingSurface` instead, so this exists for a layout copied from upstream
187
+ * to keep its shape rather than to hold anything.
188
+ */
189
+ export const NavigationMenuViewport = component((props) => html `<div
190
+ data-slot="navigation-menu-viewport"
191
+ class="${() => cn("absolute top-full left-0 isolate z-50 flex justify-center", read(props.class))}"
192
+ ></div>`);
193
+ /** The arrow that points at the open trigger. */
194
+ export const NavigationMenuIndicator = component((props) => {
195
+ const menu = inject(NavigationMenuContext);
196
+ return html `<div
197
+ data-slot="navigation-menu-indicator"
198
+ data-state="${() => (menu.openIndex() === -1 ? "hidden" : "visible")}"
199
+ class="${() => cn("top-full z-[1] flex h-1.5 items-end justify-center overflow-hidden data-[state=hidden]:animate-out data-[state=hidden]:fade-out data-[state=visible]:animate-in data-[state=visible]:fade-in", read(props.class))}"
200
+ ><div class="bg-border relative top-[60%] h-2 w-2 rotate-45 rounded-tl-sm shadow-md"></div></div>`;
118
201
  });
119
- function renderPanel(item) {
120
- if (item === undefined)
121
- return null;
122
- if (item.content !== undefined)
123
- return slot(item.content)();
124
- return html `<ul class="grid w-[400px] gap-1 md:w-[500px] md:grid-cols-2">
125
- ${(item.links ?? []).map((link) => html `<li>
126
- <a
127
- href="${link.href}"
128
- class="hover:bg-accent hover:text-accent-foreground focus:bg-accent block select-none space-y-1 rounded-md p-3 leading-none no-underline outline-none transition-colors"
129
- >
130
- <div class="text-sm leading-none font-medium">${link.label}</div>
131
- ${link.description === undefined
132
- ? null
133
- : html `<p class="text-muted-foreground line-clamp-2 text-sm leading-snug">
134
- ${link.description}
135
- </p>`}
136
- </a>
137
- </li>`)}
138
- </ul>`;
202
+ function renderPanel(entry, contentId, handlers) {
203
+ return html `<div
204
+ data-slot="navigation-menu-content"
205
+ id="${contentId}"
206
+ class="${cn("bg-popover text-popover-foreground z-50 origin-(--nebula-transform-origin) rounded-md border p-4 shadow-md outline-hidden", zoomInOut, slideInFromSide, read(entry?.panelClass))}"
207
+ @pointerenter="${() => handlers.enter()}"
208
+ @pointerleave="${() => handlers.leave()}"
209
+ >${entry?.body}</div>`;
139
210
  }
@@ -5,29 +5,89 @@
5
5
  * anchored overlays in this directory are this component with a different
6
6
  * opening gesture and different contents.
7
7
  *
8
+ * Popover({ children: () => html`
9
+ * ${PopoverTrigger({ children: "Open" })}
10
+ * ${PopoverContent({ children: () => html`
11
+ * ${PopoverHeader({ children: () => html`
12
+ * ${PopoverTitle({ children: "Dimensions" })}
13
+ * ${PopoverDescription({ children: "Set the box size." })}
14
+ * ` })}
15
+ * ` })}
16
+ * ` })
17
+ *
18
+ * The `() =>` on a part that provides context is load-bearing — see `Parts`.
19
+ * The purely presentational parts below take ordinary children.
20
+ *
8
21
  * `modal` decides whether focus is trapped. A popover holding a form should
9
22
  * trap — tabbing out of a half-filled form into the page behind loses the
10
23
  * user's place. A popover holding a paragraph of help should not, because
11
24
  * trapping focus in something the user can only read is a dead end.
12
25
  */
13
- import { type Slot } from "../lib/children.js";
26
+ import { type TemplateResult } from "@c9up/aurora";
27
+ import type { Parts, Slot } from "../lib/children.js";
14
28
  import { type Reactive } from "../lib/props.js";
15
- import type { Placement } from "../primitives/floating.js";
16
- export declare const popoverContentClasses = "bg-popover text-popover-foreground z-50 w-72 rounded-md border p-4 shadow-md outline-none";
29
+ import type { Align, Side } from "../primitives/floating.js";
30
+ export declare const popoverContentClasses = "bg-popover text-popover-foreground z-50 w-72 origin-(--nebula-transform-origin) rounded-md border p-4 shadow-md outline-hidden";
17
31
  export interface PopoverProps {
18
- /** Rendered inside the trigger button. */
19
- trigger?: Slot;
20
- /** The panel's contents. */
21
- children?: Slot;
22
32
  open?: Reactive<boolean>;
23
33
  defaultOpen?: boolean;
24
34
  onOpenChange?: (open: boolean) => void;
25
- placement?: Placement;
26
- offset?: number;
35
+ children?: Parts;
36
+ }
37
+ /**
38
+ * The state its trigger and its content both read.
39
+ *
40
+ * Renders no element of its own, as upstream does not: a wrapper would put a
41
+ * box into a layout that did not ask for one, and `data-slot="popover"` is
42
+ * inert upstream for the same reason.
43
+ */
44
+ export declare const Popover: (props?: PopoverProps | undefined) => TemplateResult;
45
+ export interface PopoverTriggerProps {
46
+ children?: Slot;
47
+ class?: Reactive<string>;
48
+ }
49
+ export declare const PopoverTrigger: (props?: PopoverTriggerProps | undefined) => TemplateResult;
50
+ export interface PopoverAnchorProps {
51
+ children?: Slot;
52
+ class?: Reactive<string>;
53
+ }
54
+ /**
55
+ * Position the panel against something other than the trigger.
56
+ *
57
+ * A cell menu whose button sits in the corner but whose panel should line up
58
+ * with the whole cell, or a mention popover anchored to a caret marker rather
59
+ * than to any control.
60
+ */
61
+ export declare const PopoverAnchor: (props?: PopoverAnchorProps | undefined) => TemplateResult;
62
+ export interface PopoverContentProps {
63
+ children?: Slot;
64
+ /** Which edge of the anchor to sit on. Flips when there is no room. */
65
+ side?: Side;
66
+ align?: Align;
67
+ sideOffset?: number;
27
68
  /** Trap focus inside. Use for panels holding form controls. */
28
69
  modal?: boolean;
29
70
  class?: Reactive<string>;
30
- triggerClass?: Reactive<string>;
31
- contentClass?: Reactive<string>;
32
71
  }
33
- export declare const Popover: (props?: PopoverProps | undefined) => import("@c9up/aurora").TemplateResult;
72
+ /**
73
+ * The panel.
74
+ *
75
+ * Renders nothing where it is written: the popover portals its content out of
76
+ * the tree when it opens, so this registers what to build and returns an empty
77
+ * template.
78
+ */
79
+ export declare const PopoverContent: (props?: PopoverContentProps | undefined) => TemplateResult;
80
+ export interface PopoverSectionProps {
81
+ children?: Slot;
82
+ class?: Reactive<string>;
83
+ }
84
+ export declare const PopoverHeader: (props?: PopoverSectionProps | undefined) => TemplateResult;
85
+ /**
86
+ * NAMED DEVIATION — a `<div>`, matching what upstream RENDERS rather than what
87
+ * it types. shadcn declares `PopoverTitle` as `ComponentProps<"h2">` and then
88
+ * returns a `<div>`; a heading here would land inside a `role="dialog"` and
89
+ * change how a screen reader announces the panel, which is not a difference to
90
+ * introduce silently. The panel is already named by its trigger.
91
+ */
92
+ export declare const PopoverTitle: (props?: PopoverSectionProps | undefined) => TemplateResult;
93
+ export declare const PopoverDescription: (props?: PopoverSectionProps | undefined) => TemplateResult;
@@ -5,54 +5,161 @@
5
5
  * anchored overlays in this directory are this component with a different
6
6
  * opening gesture and different contents.
7
7
  *
8
+ * Popover({ children: () => html`
9
+ * ${PopoverTrigger({ children: "Open" })}
10
+ * ${PopoverContent({ children: () => html`
11
+ * ${PopoverHeader({ children: () => html`
12
+ * ${PopoverTitle({ children: "Dimensions" })}
13
+ * ${PopoverDescription({ children: "Set the box size." })}
14
+ * ` })}
15
+ * ` })}
16
+ * ` })
17
+ *
18
+ * The `() =>` on a part that provides context is load-bearing — see `Parts`.
19
+ * The purely presentational parts below take ordinary children.
20
+ *
8
21
  * `modal` decides whether focus is trapped. A popover holding a form should
9
22
  * trap — tabbing out of a half-filled form into the page behind loses the
10
23
  * user's place. A popover holding a paragraph of help should not, because
11
24
  * trapping focus in something the user can only read is a dead end.
12
25
  */
13
- import { component, html } from "@c9up/aurora";
26
+ import { component, createContext, html, inject, provide, } from "@c9up/aurora";
14
27
  import { slot } from "../lib/children.js";
15
28
  import { cn } from "../lib/cn.js";
16
29
  import { uid } from "../lib/id.js";
17
- import { zoomInOut } from "../lib/motion.js";
30
+ import { slideInFromSide, zoomInOut } from "../lib/motion.js";
18
31
  import { read } from "../lib/props.js";
19
32
  import { controllable } from "../primitives/controllable.js";
20
33
  import { floatingSurface } from "../primitives/floatingSurface.js";
21
- export const popoverContentClasses = "bg-popover text-popover-foreground z-50 w-72 rounded-md border p-4 shadow-md outline-none";
34
+ export const popoverContentClasses = "bg-popover text-popover-foreground z-50 w-72 origin-(--nebula-transform-origin) rounded-md border p-4 shadow-md outline-hidden";
35
+ const PopoverContext = createContext("Popover");
36
+ /**
37
+ * The state its trigger and its content both read.
38
+ *
39
+ * Renders no element of its own, as upstream does not: a wrapper would put a
40
+ * box into a layout that did not ask for one, and `data-slot="popover"` is
41
+ * inert upstream for the same reason.
42
+ */
22
43
  export const Popover = component((props) => {
23
44
  const triggerId = uid("popover-trigger");
24
45
  const contentId = uid("popover-content");
46
+ let anchorId = triggerId;
47
+ let content;
25
48
  const state = controllable({
26
49
  value: props.open,
27
50
  initial: props.defaultOpen ?? false,
28
51
  onChange: props.onOpenChange,
29
52
  });
30
- floatingSurface({
31
- anchor: () => document.getElementById(triggerId),
53
+ provide(PopoverContext, {
54
+ triggerId,
55
+ contentId,
32
56
  open: () => state.current(),
33
- onClose: () => state.set(false),
34
- placement: props.placement ?? "bottom",
35
- offset: props.offset ?? 4,
36
- trapFocus: props.modal === true,
37
- autoFocus: props.modal !== true,
38
- content: () => html `<div
39
- data-slot="popover-content"
40
- id="${contentId}"
41
- role="dialog"
42
- aria-labelledby="${triggerId}"
43
- tabindex="-1"
44
- class="${cn(popoverContentClasses, zoomInOut, read(props.contentClass))}"
45
- >${slot(props.children)}</div>`,
57
+ toggle: () => state.set(!state.current()),
58
+ setOpen: (next) => state.set(next),
59
+ useAnchor(id) {
60
+ anchorId = id;
61
+ },
62
+ register(registered) {
63
+ content = registered;
64
+ },
46
65
  });
66
+ // Built HERE so the parts see the context, and so the content has
67
+ // registered by the time the surface below needs it.
68
+ const parts = props.children?.();
69
+ if (content !== undefined) {
70
+ const registered = content;
71
+ const placement = registered.align === "center"
72
+ ? registered.side
73
+ : `${registered.side}-${registered.align}`;
74
+ floatingSurface({
75
+ anchor: () => document.getElementById(anchorId),
76
+ open: () => state.current(),
77
+ onClose: () => state.set(false),
78
+ placement,
79
+ offset: registered.sideOffset,
80
+ trapFocus: registered.modal,
81
+ autoFocus: !registered.modal,
82
+ content: () => renderContent(registered, contentId, triggerId),
83
+ });
84
+ }
85
+ return html `${parts}`;
86
+ });
87
+ export const PopoverTrigger = component((props) => {
88
+ const popover = inject(PopoverContext);
47
89
  return html `<button
48
90
  type="button"
49
91
  data-slot="popover-trigger"
50
- id="${triggerId}"
92
+ id="${popover.triggerId}"
51
93
  aria-haspopup="dialog"
52
- aria-expanded="${() => (state.current() ? "true" : "false")}"
53
- aria-controls="${() => (state.current() ? contentId : undefined)}"
54
- data-state="${() => (state.current() ? "open" : "closed")}"
55
- class="${() => cn(read(props.triggerClass))}"
56
- @click="${() => state.set(!state.current())}"
57
- >${slot(props.trigger)}</button>`;
94
+ aria-expanded="${() => (popover.open() ? "true" : "false")}"
95
+ aria-controls="${() => (popover.open() ? popover.contentId : undefined)}"
96
+ data-state="${() => (popover.open() ? "open" : "closed")}"
97
+ class="${() => cn(read(props.class))}"
98
+ @click="${() => popover.toggle()}"
99
+ >${slot(props.children)}</button>`;
58
100
  });
101
+ /**
102
+ * Position the panel against something other than the trigger.
103
+ *
104
+ * A cell menu whose button sits in the corner but whose panel should line up
105
+ * with the whole cell, or a mention popover anchored to a caret marker rather
106
+ * than to any control.
107
+ */
108
+ export const PopoverAnchor = component((props) => {
109
+ const popover = inject(PopoverContext);
110
+ const anchorId = uid("popover-anchor");
111
+ popover.useAnchor(anchorId);
112
+ return html `<span
113
+ data-slot="popover-anchor"
114
+ id="${anchorId}"
115
+ class="${() => cn(read(props.class))}"
116
+ >${slot(props.children)}</span>`;
117
+ });
118
+ /**
119
+ * The panel.
120
+ *
121
+ * Renders nothing where it is written: the popover portals its content out of
122
+ * the tree when it opens, so this registers what to build and returns an empty
123
+ * template.
124
+ */
125
+ export const PopoverContent = component((props) => {
126
+ const popover = inject(PopoverContext);
127
+ popover.register({
128
+ render: () => slot(props.children)(),
129
+ side: props.side ?? "bottom",
130
+ align: props.align ?? "center",
131
+ sideOffset: props.sideOffset ?? 4,
132
+ modal: props.modal === true,
133
+ class: props.class,
134
+ });
135
+ return html ``;
136
+ });
137
+ export const PopoverHeader = component((props) => html `<div
138
+ data-slot="popover-header"
139
+ class="${() => cn("flex flex-col gap-1 text-sm", read(props.class))}"
140
+ >${slot(props.children)}</div>`);
141
+ /**
142
+ * NAMED DEVIATION — a `<div>`, matching what upstream RENDERS rather than what
143
+ * it types. shadcn declares `PopoverTitle` as `ComponentProps<"h2">` and then
144
+ * returns a `<div>`; a heading here would land inside a `role="dialog"` and
145
+ * change how a screen reader announces the panel, which is not a difference to
146
+ * introduce silently. The panel is already named by its trigger.
147
+ */
148
+ export const PopoverTitle = component((props) => html `<div
149
+ data-slot="popover-title"
150
+ class="${() => cn("font-medium", read(props.class))}"
151
+ >${slot(props.children)}</div>`);
152
+ export const PopoverDescription = component((props) => html `<p
153
+ data-slot="popover-description"
154
+ class="${() => cn("text-muted-foreground", read(props.class))}"
155
+ >${slot(props.children)}</p>`);
156
+ function renderContent(registered, contentId, triggerId) {
157
+ return html `<div
158
+ data-slot="popover-content"
159
+ id="${contentId}"
160
+ role="dialog"
161
+ aria-labelledby="${triggerId}"
162
+ tabindex="-1"
163
+ class="${cn(popoverContentClasses, zoomInOut, slideInFromSide, read(registered.class))}"
164
+ >${registered.render()}</div>`;
165
+ }
@@ -31,7 +31,7 @@ import { Textarea } from "../atoms/Textarea.js";
31
31
  import { cn } from "../lib/cn.js";
32
32
  import { uid } from "../lib/id.js";
33
33
  import { read } from "../lib/props.js";
34
- import { RadioGroup } from "../molecules/RadioGroup.js";
34
+ import { RadioGroup, RadioGroupItem } from "../molecules/RadioGroup.js";
35
35
  function isSingle(question) {
36
36
  return question.type === "single";
37
37
  }
@@ -187,13 +187,13 @@ export const Questionnaire = component((props) => {
187
187
  if (isSingle(question)) {
188
188
  return RadioGroup({
189
189
  name: question.id,
190
- options: question.options.map((option) => ({
190
+ value: () => asValue(answerOf(question)),
191
+ onValueChange: (value) => record(question, value),
192
+ children: () => question.options.map((option) => RadioGroupItem({
191
193
  value: option.value,
192
- label: option.label,
194
+ children: option.label,
193
195
  description: option.description,
194
196
  })),
195
- value: () => asValue(answerOf(question)),
196
- onValueChange: (value) => record(question, value),
197
197
  });
198
198
  }
199
199
  if (isMultiple(question)) {