@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.
Files changed (132) 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/configure.d.ts +6 -0
  6. package/dist/configure.js +4 -22
  7. package/dist/lib/children.d.ts +30 -0
  8. package/dist/lib/id.d.ts +13 -29
  9. package/dist/lib/id.js +13 -39
  10. package/dist/lib/motion.d.ts +10 -0
  11. package/dist/lib/motion.js +10 -0
  12. package/dist/molecules/Accordion.d.ts +36 -15
  13. package/dist/molecules/Accordion.js +68 -45
  14. package/dist/molecules/Breadcrumb.d.ts +29 -11
  15. package/dist/molecules/Breadcrumb.js +52 -52
  16. package/dist/molecules/Collapsible.d.ts +18 -17
  17. package/dist/molecules/Collapsible.js +42 -34
  18. package/dist/molecules/Field.d.ts +66 -30
  19. package/dist/molecules/Field.js +106 -55
  20. package/dist/molecules/InputGroup.d.ts +39 -21
  21. package/dist/molecules/InputGroup.js +43 -27
  22. package/dist/molecules/InputOTP.d.ts +26 -3
  23. package/dist/molecules/InputOTP.js +109 -76
  24. package/dist/molecules/Message.js +5 -4
  25. package/dist/molecules/Pagination.d.ts +36 -11
  26. package/dist/molecules/Pagination.js +58 -74
  27. package/dist/molecules/RadioGroup.d.ts +25 -17
  28. package/dist/molecules/RadioGroup.js +47 -43
  29. package/dist/molecules/Resizable.d.ts +32 -7
  30. package/dist/molecules/Resizable.js +76 -49
  31. package/dist/molecules/Tabs.d.ts +49 -11
  32. package/dist/molecules/Tabs.js +107 -51
  33. package/dist/molecules/ToggleGroup.d.ts +16 -10
  34. package/dist/molecules/ToggleGroup.js +37 -21
  35. package/dist/molecules/index.d.ts +11 -11
  36. package/dist/molecules/index.js +11 -11
  37. package/dist/organisms/AlertDialog.d.ts +80 -28
  38. package/dist/organisms/AlertDialog.js +192 -67
  39. package/dist/organisms/Carousel.d.ts +25 -3
  40. package/dist/organisms/Carousel.js +75 -43
  41. package/dist/organisms/Combobox.d.ts +2 -1
  42. package/dist/organisms/Combobox.js +46 -16
  43. package/dist/organisms/Command.d.ts +73 -32
  44. package/dist/organisms/Command.js +209 -131
  45. package/dist/organisms/CommandDialog.js +5 -5
  46. package/dist/organisms/ContextMenu.d.ts +41 -19
  47. package/dist/organisms/ContextMenu.js +112 -71
  48. package/dist/organisms/DataTable.js +42 -8
  49. package/dist/organisms/Dialog.d.ts +97 -31
  50. package/dist/organisms/Dialog.js +193 -68
  51. package/dist/organisms/Drawer.d.ts +33 -10
  52. package/dist/organisms/Drawer.js +130 -52
  53. package/dist/organisms/DropdownMenu.d.ts +58 -11
  54. package/dist/organisms/DropdownMenu.js +137 -50
  55. package/dist/organisms/Form.js +9 -11
  56. package/dist/organisms/HoverCard.d.ts +39 -19
  57. package/dist/organisms/HoverCard.js +116 -40
  58. package/dist/organisms/Menubar.d.ts +55 -19
  59. package/dist/organisms/Menubar.js +166 -82
  60. package/dist/organisms/NavigationMenu.d.ts +77 -31
  61. package/dist/organisms/NavigationMenu.js +169 -98
  62. package/dist/organisms/Popover.d.ts +72 -12
  63. package/dist/organisms/Popover.js +132 -25
  64. package/dist/organisms/Questionnaire.js +5 -5
  65. package/dist/organisms/Select.d.ts +99 -18
  66. package/dist/organisms/Select.js +253 -111
  67. package/dist/organisms/Sheet.d.ts +57 -13
  68. package/dist/organisms/Sheet.js +141 -54
  69. package/dist/organisms/Sidebar.d.ts +146 -56
  70. package/dist/organisms/Sidebar.js +236 -128
  71. package/dist/organisms/Tooltip.d.ts +88 -14
  72. package/dist/organisms/Tooltip.js +210 -47
  73. package/dist/organisms/index.d.ts +15 -15
  74. package/dist/organisms/index.js +15 -15
  75. package/dist/primitives/floating.js +25 -0
  76. package/dist/primitives/floatingSurface.d.ts +11 -0
  77. package/dist/primitives/floatingSurface.js +8 -0
  78. package/dist/primitives/menu.d.ts +45 -1
  79. package/dist/primitives/menu.js +45 -14
  80. package/dist/primitives/menuParts.d.ts +113 -0
  81. package/dist/primitives/menuParts.js +207 -0
  82. package/dist/stubs.d.ts +13 -0
  83. package/dist/stubs.js +14 -0
  84. package/nebula.css +1 -1
  85. package/package.json +4 -3
  86. package/registry.json +18 -6
  87. package/src/atoms/Avatar.ts +141 -37
  88. package/src/atoms/index.ts +13 -1
  89. package/src/configure.ts +10 -23
  90. package/src/lib/children.ts +31 -0
  91. package/src/lib/id.ts +11 -39
  92. package/src/lib/motion.ts +12 -0
  93. package/src/molecules/Accordion.ts +120 -60
  94. package/src/molecules/Breadcrumb.ts +77 -76
  95. package/src/molecules/Collapsible.ts +77 -41
  96. package/src/molecules/Field.ts +187 -72
  97. package/src/molecules/InputGroup.ts +88 -35
  98. package/src/molecules/InputOTP.ts +150 -86
  99. package/src/molecules/Message.ts +6 -4
  100. package/src/molecules/Pagination.ts +96 -101
  101. package/src/molecules/RadioGroup.ts +70 -59
  102. package/src/molecules/Resizable.ts +138 -75
  103. package/src/molecules/Tabs.ts +187 -74
  104. package/src/molecules/ToggleGroup.ts +77 -38
  105. package/src/molecules/index.ts +85 -8
  106. package/src/organisms/AlertDialog.ts +319 -82
  107. package/src/organisms/Carousel.ts +129 -52
  108. package/src/organisms/Combobox.ts +64 -19
  109. package/src/organisms/Command.ts +342 -159
  110. package/src/organisms/CommandDialog.ts +6 -6
  111. package/src/organisms/ContextMenu.ts +185 -83
  112. package/src/organisms/DataTable.ts +56 -10
  113. package/src/organisms/Dialog.ts +312 -102
  114. package/src/organisms/Drawer.ts +219 -71
  115. package/src/organisms/DropdownMenu.ts +219 -61
  116. package/src/organisms/Form.ts +48 -34
  117. package/src/organisms/HoverCard.ts +187 -53
  118. package/src/organisms/Menubar.ts +247 -87
  119. package/src/organisms/NavigationMenu.ts +306 -128
  120. package/src/organisms/Popover.ts +215 -39
  121. package/src/organisms/Questionnaire.ts +9 -6
  122. package/src/organisms/Select.ts +397 -130
  123. package/src/organisms/Sheet.ts +252 -83
  124. package/src/organisms/Sidebar.ts +438 -185
  125. package/src/organisms/Tooltip.ts +315 -65
  126. package/src/organisms/index.ts +284 -14
  127. package/src/primitives/floating.ts +33 -0
  128. package/src/primitives/floatingSurface.ts +19 -0
  129. package/src/primitives/menu.ts +79 -18
  130. package/src/primitives/menuParts.ts +371 -0
  131. package/src/stubs.ts +16 -0
  132. package/stubs/config/nebula.stub +21 -0
@@ -1,67 +1,171 @@
1
1
  /**
2
2
  * Avatar — a user's picture, with a fallback when there is none.
3
3
  *
4
- * shadcn splits this into `Avatar` / `AvatarImage` / `AvatarFallback` because
5
- * Radix needs three components to track the image's load state across them.
6
- * nebula tracks it in one signal inside one component, so the API is a `src`
7
- * and a `fallback`.
4
+ * Avatar({ children: () => html`
5
+ * ${AvatarImage({ src: user.photo, alt: user.name })}
6
+ * ${AvatarFallback({ children: initials })}
7
+ * ` })
8
8
  *
9
9
  * Three states, not two. An image that has not loaded *yet* must not show the
10
10
  * fallback — swapping initials in and a photo over them a moment later is the
11
11
  * flicker this component exists to avoid. So the fallback appears only once
12
- * the image has actually failed, or when there was no `src` to begin with.
12
+ * the image has actually failed, or when there is no `AvatarImage` at all.
13
+ *
14
+ * That is what the context carries: the image reports its own load state up,
15
+ * the fallback reads it, and neither knows the other exists. Radix needs the
16
+ * same three pieces for the same reason.
13
17
  */
14
18
 
15
- import { component, html, signal } from "@c9up/aurora";
16
- import { type Slot, slot } from "../lib/children.js";
19
+ import {
20
+ component,
21
+ createContext,
22
+ html,
23
+ inject,
24
+ provide,
25
+ signal,
26
+ } from "@c9up/aurora";
27
+ import type { Parts, Slot } from "../lib/children.js";
28
+ import { slot } from "../lib/children.js";
17
29
  import { cn } from "../lib/cn.js";
18
30
  import { type Reactive, read, readOr } from "../lib/props.js";
19
31
 
20
- type LoadState = "pending" | "loaded" | "failed";
32
+ type LoadState = "absent" | "pending" | "loaded" | "failed";
33
+
34
+ interface AvatarApi {
35
+ readonly state: () => LoadState;
36
+ setState(next: LoadState): void;
37
+ }
38
+
39
+ const AvatarContext = createContext<AvatarApi>("Avatar");
21
40
 
22
41
  export interface AvatarProps {
42
+ size?: "sm" | "default" | "lg";
43
+ class?: Reactive<string>;
44
+ children?: Parts;
45
+ }
46
+
47
+ export const Avatar = component<AvatarProps>((props) => {
48
+ // `absent` until an image says otherwise: an avatar with no `AvatarImage`
49
+ // is a fallback-only avatar, and it must not wait for a load that will
50
+ // never happen.
51
+ const state = signal<LoadState>("absent");
52
+
53
+ provide<AvatarApi>(AvatarContext, {
54
+ state: () => state(),
55
+ setState: (next) => state(next),
56
+ });
57
+
58
+ return html`<span
59
+ data-slot="avatar"
60
+ data-size="${props.size ?? "default"}"
61
+ class="${() =>
62
+ cn(
63
+ "group/avatar relative flex size-8 shrink-0 overflow-hidden rounded-full select-none data-[size=lg]:size-10 data-[size=sm]:size-6",
64
+ read(props.class),
65
+ )}"
66
+ >${props.children?.()}</span>`;
67
+ });
68
+
69
+ export interface AvatarImageProps {
23
70
  src?: Reactive<string | undefined>;
24
71
  alt?: Reactive<string>;
25
- /** Shown when there is no image, or it failed. Usually initials. */
26
- fallback?: Slot;
27
72
  class?: Reactive<string>;
28
73
  }
29
74
 
30
- export const Avatar = component<AvatarProps>((props) => {
31
- const state = signal<LoadState>("pending");
75
+ export const AvatarImage = component<AvatarImageProps>((props) => {
76
+ const avatar = inject(AvatarContext);
32
77
 
33
78
  const hasSrc = (): boolean => {
34
79
  const src = read(props.src);
35
80
  return src !== undefined && src !== "";
36
81
  };
37
82
 
38
- const showFallback = (): boolean => !hasSrc() || state() === "failed";
83
+ // Reported during setup, so the fallback built after it already reads the
84
+ // right state rather than flashing through `absent`.
85
+ avatar.setState(hasSrc() ? "pending" : "absent");
39
86
 
40
- return html`<span
41
- data-slot="avatar"
87
+ return html`${() =>
88
+ hasSrc() && avatar.state() !== "failed"
89
+ ? html`<img
90
+ data-slot="avatar-image"
91
+ src="${() => read(props.src)}"
92
+ alt="${() => readOr(props.alt, "")}"
93
+ class="${() => cn("aspect-square size-full object-cover", read(props.class))}"
94
+ @load="${() => avatar.setState("loaded")}"
95
+ @error="${() => avatar.setState("failed")}"
96
+ />`
97
+ : null}`;
98
+ });
99
+
100
+ export interface AvatarFallbackProps {
101
+ children?: Slot;
102
+ class?: Reactive<string>;
103
+ }
104
+
105
+ export const AvatarFallback = component<AvatarFallbackProps>((props) => {
106
+ const avatar = inject(AvatarContext);
107
+ const show = (): boolean => {
108
+ const state = avatar.state();
109
+ return state === "absent" || state === "failed";
110
+ };
111
+ return html`${() =>
112
+ show()
113
+ ? html`<span
114
+ data-slot="avatar-fallback"
115
+ class="${() =>
116
+ cn(
117
+ "bg-muted text-muted-foreground flex size-full items-center justify-center rounded-full text-sm group-data-[size=sm]/avatar:text-xs",
118
+ read(props.class),
119
+ )}"
120
+ >${slot(props.children)}</span>`
121
+ : null}`;
122
+ });
123
+
124
+ export interface AvatarBadgeProps {
125
+ children?: Slot;
126
+ class?: Reactive<string>;
127
+ }
128
+
129
+ /** A status dot in the corner — online, unread, verified. */
130
+ export const AvatarBadge = component<AvatarBadgeProps>(
131
+ (props) => html`<span
132
+ data-slot="avatar-badge"
42
133
  class="${() =>
43
134
  cn(
44
- "relative flex size-8 shrink-0 overflow-hidden rounded-full",
135
+ "bg-primary text-primary-foreground ring-background absolute right-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full ring-2 select-none",
136
+ "group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",
137
+ "group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",
138
+ "group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",
45
139
  read(props.class),
46
140
  )}"
47
- >
48
- ${() =>
49
- hasSrc() && state() !== "failed"
50
- ? html`<img
51
- data-slot="avatar-image"
52
- src="${() => read(props.src)}"
53
- alt="${() => readOr(props.alt, "")}"
54
- class="aspect-square size-full object-cover"
55
- @load="${() => state("loaded")}"
56
- @error="${() => state("failed")}"
57
- />`
58
- : null}
59
- ${() =>
60
- showFallback()
61
- ? html`<span
62
- data-slot="avatar-fallback"
63
- class="bg-muted flex size-full items-center justify-center rounded-full text-xs font-medium"
64
- >${slot(props.fallback)}</span>`
65
- : null}
66
- </span>`;
67
- });
141
+ >${slot(props.children)}</span>`,
142
+ );
143
+
144
+ export interface AvatarGroupProps {
145
+ children?: Slot;
146
+ class?: Reactive<string>;
147
+ }
148
+
149
+ /** Overlapping avatars, each ringed so the stack reads as separate faces. */
150
+ export const AvatarGroup = component<AvatarGroupProps>(
151
+ (props) => html`<div
152
+ data-slot="avatar-group"
153
+ class="${() =>
154
+ cn(
155
+ "group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",
156
+ read(props.class),
157
+ )}"
158
+ >${slot(props.children)}</div>`,
159
+ );
160
+
161
+ /** The "+3" that closes a group. */
162
+ export const AvatarGroupCount = component<AvatarGroupProps>(
163
+ (props) => html`<span
164
+ data-slot="avatar-group-count"
165
+ class="${() =>
166
+ cn(
167
+ "bg-muted text-muted-foreground ring-background relative flex size-8 shrink-0 items-center justify-center rounded-full text-sm ring-2 group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3",
168
+ read(props.class),
169
+ )}"
170
+ >${slot(props.children)}</span>`,
171
+ );
@@ -13,7 +13,19 @@
13
13
  */
14
14
 
15
15
  export { AspectRatio, type AspectRatioProps } from "./AspectRatio.js";
16
- export { Avatar, type AvatarProps } from "./Avatar.js";
16
+ export {
17
+ Avatar,
18
+ AvatarBadge,
19
+ type AvatarBadgeProps,
20
+ AvatarFallback,
21
+ type AvatarFallbackProps,
22
+ AvatarGroup,
23
+ AvatarGroupCount,
24
+ type AvatarGroupProps,
25
+ AvatarImage,
26
+ type AvatarImageProps,
27
+ type AvatarProps,
28
+ } from "./Avatar.js";
17
29
  export {
18
30
  Badge,
19
31
  type BadgeProps,
package/src/configure.ts CHANGED
@@ -21,6 +21,7 @@ import { resolve } from "node:path";
21
21
  import { adapterFor, isAdapterName } from "./adapters/index.js";
22
22
  import type { GeneratedFile } from "./adapters/types.js";
23
23
  import { type AdapterName, resolveConfig } from "./config.js";
24
+ import { stubsRoot } from "./stubs.js";
24
25
 
25
26
  /**
26
27
  * The subset of the CLI's codemods this hook uses.
@@ -38,33 +39,17 @@ interface Codemods {
38
39
  ): Promise<void>;
39
40
  /** Appends to `reamrc.commands` — the channel for package-shipped commands. */
40
41
  registerCommand(importPath: string): Promise<void>;
42
+ makeUsingStub(
43
+ stubsRoot: string,
44
+ stubPath: string,
45
+ state?: Record<string, string | number | boolean>,
46
+ options?: { force?: boolean },
47
+ ): Promise<{ path: string; contents: string }>;
41
48
  }
42
49
 
43
50
  /** Flags forwarded from `ream add` / `ream configure`, as the CLI encodes them. */
44
51
  type Flags = Record<string, string[] | undefined>;
45
52
 
46
- function configFile(adapter: AdapterName): string {
47
- return `import { defineConfig } from '@c9up/nebula'
48
-
49
- /**
50
- * nebula — component registry and style adapter.
51
- *
52
- * Changing \`adapter\` and re-running \`ream configure @c9up/nebula\` swaps the
53
- * CSS engine. The components themselves are untouched: all three adapters
54
- * consume the same class names.
55
- */
56
- export default defineConfig({
57
- adapter: '${adapter}',
58
- paths: {
59
- // Where \`ream nebula:add\` copies components. The atomic layers live under it.
60
- components: 'resources/pages',
61
- css: 'resources/css/app.css',
62
- output: 'public/app.css',
63
- },
64
- })
65
- `;
66
- }
67
-
68
53
  /**
69
54
  * Read the adapter from the forwarded flags.
70
55
  *
@@ -91,7 +76,9 @@ export async function configure(
91
76
  const adapter = adapterFor(name);
92
77
  const config = resolveConfig({ adapter: name });
93
78
 
94
- await codemods.writeFile("config/nebula.ts", configFile(name));
79
+ await codemods.makeUsingStub(stubsRoot, "config/nebula.stub", {
80
+ adapter: name,
81
+ });
95
82
 
96
83
  // `reamrc.commands` is the channel Ream provides for commands a package
97
84
  // ships, which directory discovery cannot see. `ream` forwards any
@@ -29,6 +29,37 @@ export type Child =
29
29
  /** Content that may be constant or recomputed. */
30
30
  export type Slot = Reactive<Child>;
31
31
 
32
+ /**
33
+ * The children of a component that PROVIDES context, as a function.
34
+ *
35
+ * A compound component — `Tooltip`, `Select`, `DropdownMenu` — shares its
36
+ * state with its parts through Aurora's context, and context reaches only what
37
+ * the provider creates inside its own setup. Aurora evaluates eagerly, so
38
+ * children handed over already built ran before the provider existed:
39
+ *
40
+ * Tooltip({ children: TooltipTrigger({ … }) }) // ✗ built too early
41
+ * Tooltip({ children: () => TooltipTrigger({ … }) }) // ✓ the setup calls it
42
+ *
43
+ * This is the aurora spelling of what JSX gets from deferring an element tree.
44
+ * `Parts` names it so the type makes the requirement, rather than a blank
45
+ * surface at runtime.
46
+ *
47
+ * Called ONCE, during setup — the structure of a compound component is fixed,
48
+ * and its moving pieces are signals inside the parts.
49
+ *
50
+ * The same rule read from the other end: a part that READS context must be
51
+ * built eagerly too, so it must not be hidden inside a `Slot`. `Slot` exists
52
+ * for content that CHANGES, and the renderer calls it later, by which time
53
+ * there is no context left to read:
54
+ *
55
+ * DialogHeader({ children: html`${DialogTitle(…)}` }) // ✓ a value
56
+ * DialogHeader({ children: () => html`${DialogTitle(…)}` }) // ✗ too late
57
+ *
58
+ * So: write `() =>` for a component that PROVIDES (its type says `Parts`), and
59
+ * a plain value everywhere else unless the content really does change.
60
+ */
61
+ export type Parts = () => Child;
62
+
32
63
  /**
33
64
  * Bind content into a template so it stays live.
34
65
  *
package/src/lib/id.ts CHANGED
@@ -1,44 +1,16 @@
1
1
  /**
2
- * Stable element ids.
2
+ * Stable element ids — aurora's counter, re-exported.
3
3
  *
4
- * Radix leans on React's `useId` to wire ARIA relationships — a trigger's
5
- * `aria-controls` has to name the id its content actually carries. Aurora has
6
- * no equivalent, and nebula needs one for two reasons: the ARIA wiring above,
7
- * and element lookup. Aurora templates have no `ref` directive, so a component
8
- * that must measure or focus a node finds it by id inside `onMount()`.
4
+ * It was nebula's own, and that was the bug: aurora resets its sequence at the
5
+ * start of each render pass (`renderPage` server-side, `hydrate` in the
6
+ * browser), and a second counter living here saw none of those resets. A
7
+ * long-lived server therefore shipped `tooltip-trigger-14` while the browser,
8
+ * hydrating from zero, minted `tooltip-trigger-1` and looked it up — so
9
+ * `byId` answered `null`, `floatingSurface` had no anchor, and the tooltip
10
+ * never opened although its mount hook had run and its trigger was wired.
9
11
  *
10
- * The counter is monotonic per module instance, so the sequence depends only
11
- * on the order components are constructed in. That order is identical on the
12
- * server and in the browser for the same tree, which is what makes the ids
13
- * survive hydration — the same property React's `useId` relies on.
14
- *
15
- * `resetIds()` exists for the server: a long-lived process renders many
16
- * requests, and without a reset the counter climbs forever and every response
17
- * ships different markup, defeating any HTML cache. Call it once per render
18
- * pass, before the tree is built.
12
+ * Every id nebula mints has to come from the same counter aurora resets; the
13
+ * names are kept so nothing importing them has to change.
19
14
  */
20
15
 
21
- let counter = 0;
22
-
23
- /** Mint an id unique within this render pass. */
24
- export function uid(prefix = "nebula"): string {
25
- counter += 1;
26
- return `${prefix}-${counter}`;
27
- }
28
-
29
- /** Restart the sequence. Call once per SSR render, before building the tree. */
30
- export function resetIds(): void {
31
- counter = 0;
32
- }
33
-
34
- /**
35
- * Look an element up by the id a component minted for it.
36
- *
37
- * Returns `null` off the DOM (SSR) or before mount rather than throwing, so
38
- * callers can run the same code in both environments. Every consumer inside
39
- * nebula is in `onMount`, where the node is live.
40
- */
41
- export function byId(id: string): HTMLElement | null {
42
- if (typeof document === "undefined") return null;
43
- return document.getElementById(id);
44
- }
16
+ export { byId, resetIds, uid } from "@c9up/aurora";
package/src/lib/motion.ts CHANGED
@@ -33,6 +33,18 @@ import type { Side } from "../primitives/floating.js";
33
33
  export const zoomInOut =
34
34
  "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=open]:fade-in-0 data-[state=closed]:fade-out-0 data-[state=open]:zoom-in-95 data-[state=closed]:zoom-out-95 motion-reduce:animate-none";
35
35
 
36
+ /**
37
+ * The entrance offset every anchored surface shares.
38
+ *
39
+ * shadcn puts these four on popover, dropdown, select, tooltip, hover card,
40
+ * menubar and context-menu content alike, so the surface slides in from the
41
+ * edge it is anchored against and the motion points back at whatever opened
42
+ * it. `data-side` is written by `autoPosition` after any flip, so a menu that
43
+ * flipped above its trigger slides the other way without a second class.
44
+ */
45
+ export const slideInFromSide =
46
+ "data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2";
47
+
36
48
  /** Backdrops and tooltips — no movement, just opacity. */
37
49
  export const fadeInOut =
38
50
  "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=open]:fade-in-0 data-[state=closed]:fade-out-0 motion-reduce:animate-none";
@@ -1,18 +1,33 @@
1
1
  /**
2
2
  * Accordion — a stack of collapsible sections.
3
3
  *
4
- * Data-driven rather than composed: an accordion is a list, and describing it
5
- * as one is both shorter to write and the only shape that lets the group
6
- * enforce `type: "single"` — which needs to know about every section at once,
7
- * something Radix can only do through context.
4
+ * Accordion({ type: "single", children: () => html`
5
+ * ${AccordionItem({ value: "a", children: () => html`
6
+ * ${AccordionTrigger({ children: "Shipping" })}
7
+ * ${AccordionContent({ children: "Two to four days." })}
8
+ * ` })}
9
+ * ` })
10
+ *
11
+ * `type: "single"` needs to know about every section at once, which is what
12
+ * the context carries: the group owns the open SET and each item asks whether
13
+ * it is in it. Radix does the same, for the same reason.
8
14
  *
9
15
  * The keyboard behaviour is the WAI-ARIA accordion pattern, delegated to
10
16
  * `rovingFocus` over the headers: arrows move between sections, Home and End
11
17
  * jump to the ends, and the headers are one tab stop rather than one each.
12
18
  */
13
19
 
14
- import { component, html, onMount, onUnmount, signal } from "@c9up/aurora";
15
- import type { Child, Slot } from "../lib/children.js";
20
+ import {
21
+ component,
22
+ createContext,
23
+ html,
24
+ inject,
25
+ onMount,
26
+ onUnmount,
27
+ provide,
28
+ signal,
29
+ } from "@c9up/aurora";
30
+ import type { Parts, Slot } from "../lib/children.js";
16
31
  import { slot } from "../lib/children.js";
17
32
  import { cn } from "../lib/cn.js";
18
33
  import { ChevronDownIcon } from "../lib/icons.js";
@@ -20,18 +35,26 @@ import { uid } from "../lib/id.js";
20
35
  import { type Reactive, read } from "../lib/props.js";
21
36
  import { type RovingFocus, rovingFocus } from "../primitives/rovingFocus.js";
22
37
 
23
- export interface AccordionItem {
24
- /** Stable key. Used for the open-state set and for the ARIA ids. */
25
- value: string;
26
- /** The header content. */
27
- trigger: Child;
28
- /** The panel content. */
29
- content: Slot;
30
- disabled?: boolean;
38
+ interface AccordionApi {
39
+ isOpen(value: string): boolean;
40
+ toggle(value: string): void;
31
41
  }
32
42
 
43
+ const AccordionContext = createContext<AccordionApi>("Accordion");
44
+
45
+ interface AccordionItemApi {
46
+ readonly value: string;
47
+ readonly triggerId: string;
48
+ readonly panelId: string;
49
+ readonly disabled: boolean;
50
+ readonly open: () => boolean;
51
+ toggle(): void;
52
+ }
53
+
54
+ const AccordionItemContext = createContext<AccordionItemApi>("AccordionItem");
55
+
33
56
  export interface AccordionProps {
34
- items: readonly AccordionItem[];
57
+ children?: Parts;
35
58
  /** `"single"` closes the open section when another opens. Default `"single"`. */
36
59
  type?: "single" | "multiple";
37
60
  /** Open at first render. A string, or several for `type: "multiple"`. */
@@ -71,64 +94,101 @@ export const Accordion = component<AccordionProps>((props) => {
71
94
  });
72
95
  onUnmount(() => group?.destroy());
73
96
 
97
+ provide<AccordionApi>(AccordionContext, { isOpen, toggle });
98
+
74
99
  return html`<div
75
100
  data-slot="accordion"
76
101
  id="${rootId}"
77
102
  class="${() => cn("flex w-full flex-col", read(props.class))}"
78
- >
79
- ${props.items.map((item) => renderItem(item, isOpen, toggle))}
80
- </div>`;
103
+ >${props.children?.()}</div>`;
81
104
  });
82
105
 
83
- function renderItem(
84
- item: AccordionItem,
85
- isOpen: (value: string) => boolean,
86
- toggle: (value: string) => void,
87
- ): Child {
106
+ export interface AccordionItemProps {
107
+ /** Stable key. Used for the open-state set and for the ARIA ids. */
108
+ value: string;
109
+ disabled?: boolean;
110
+ class?: Reactive<string>;
111
+ children?: Parts;
112
+ }
113
+
114
+ /**
115
+ * One section.
116
+ *
117
+ * Provides a second context of its own, so the trigger and the panel inside it
118
+ * share their ids without the caller repeating the value on each.
119
+ */
120
+ export const AccordionItem = component<AccordionItemProps>((props) => {
121
+ const accordion = inject(AccordionContext);
88
122
  const triggerId = uid("accordion-trigger");
89
123
  const panelId = uid("accordion-panel");
90
- const state = (): string => (isOpen(item.value) ? "open" : "closed");
124
+ const open = (): boolean => accordion.isOpen(props.value);
125
+
126
+ provide<AccordionItemApi>(AccordionItemContext, {
127
+ value: props.value,
128
+ triggerId,
129
+ panelId,
130
+ disabled: props.disabled === true,
131
+ open,
132
+ toggle: () => accordion.toggle(props.value),
133
+ });
91
134
 
92
135
  return html`<div
93
136
  data-slot="accordion-item"
94
- data-state="${state}"
95
- class="border-b last:border-b-0"
96
- >
97
- <h3 class="flex">
98
- <button
99
- type="button"
100
- data-slot="accordion-trigger"
101
- data-nebula-item
102
- data-state="${state}"
103
- data-disabled="${item.disabled === true ? "" : undefined}"
104
- id="${triggerId}"
105
- aria-expanded="${() => (isOpen(item.value) ? "true" : "false")}"
106
- aria-controls="${panelId}"
107
- ?disabled="${item.disabled === true}"
108
- class="focus-visible:border-ring focus-visible:ring-ring/50 flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-left text-sm font-medium outline-none transition-all hover:underline focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50 [&[data-state=open]>svg]:rotate-180"
109
- @click="${() => toggle(item.value)}"
110
- >
111
- ${item.trigger}
112
- ${ChevronDownIcon({
113
- class:
114
- "text-muted-foreground pointer-events-none size-4 shrink-0 translate-y-0.5 transition-transform duration-200",
115
- })}
116
- </button>
117
- </h3>
118
- <div
119
- data-slot="accordion-content"
120
- id="${panelId}"
121
- role="region"
122
- aria-labelledby="${triggerId}"
123
- ?inert="${() => !isOpen(item.value)}"
124
- class="grid text-sm transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none"
125
- style="${() => `grid-template-rows: ${isOpen(item.value) ? "1fr" : "0fr"}`}"
126
- >
127
- <div class="overflow-hidden"><div class="pt-0 pb-4">${slot(item.content)}</div></div>
128
- </div>
129
- </div>`;
137
+ data-state="${() => (open() ? "open" : "closed")}"
138
+ class="${() => cn("border-b last:border-b-0", read(props.class))}"
139
+ >${props.children?.()}</div>`;
140
+ });
141
+
142
+ export interface AccordionTriggerProps {
143
+ children?: Slot;
144
+ class?: Reactive<string>;
130
145
  }
131
146
 
147
+ export const AccordionTrigger = component<AccordionTriggerProps>((props) => {
148
+ const item = inject(AccordionItemContext);
149
+ return html`<h3 class="flex"><button
150
+ type="button"
151
+ data-slot="accordion-trigger"
152
+ data-nebula-item
153
+ data-state="${() => (item.open() ? "open" : "closed")}"
154
+ data-disabled="${item.disabled ? "" : undefined}"
155
+ id="${item.triggerId}"
156
+ aria-expanded="${() => (item.open() ? "true" : "false")}"
157
+ aria-controls="${item.panelId}"
158
+ ?disabled="${item.disabled}"
159
+ class="${() =>
160
+ cn(
161
+ "focus-visible:border-ring focus-visible:ring-ring/50 flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-left text-sm font-medium outline-none transition-all hover:underline focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50 [&[data-state=open]>svg]:rotate-180",
162
+ read(props.class),
163
+ )}"
164
+ @click="${() => item.toggle()}"
165
+ >${slot(props.children)}${ChevronDownIcon({
166
+ class:
167
+ "text-muted-foreground pointer-events-none size-4 shrink-0 translate-y-0.5 transition-transform duration-200",
168
+ })}</button></h3>`;
169
+ });
170
+
171
+ export interface AccordionContentProps {
172
+ children?: Slot;
173
+ class?: Reactive<string>;
174
+ }
175
+
176
+ export const AccordionContent = component<AccordionContentProps>((props) => {
177
+ const item = inject(AccordionItemContext);
178
+ return html`<div
179
+ data-slot="accordion-content"
180
+ id="${item.panelId}"
181
+ role="region"
182
+ data-state="${() => (item.open() ? "open" : "closed")}"
183
+ aria-labelledby="${item.triggerId}"
184
+ ?inert="${() => !item.open()}"
185
+ class="grid text-sm transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none"
186
+ style="${() => `grid-template-rows: ${item.open() ? "1fr" : "0fr"}`}"
187
+ ><div class="overflow-hidden"><div
188
+ class="${() => cn("pt-0 pb-4", read(props.class))}"
189
+ >${slot(props.children)}</div></div></div>`;
190
+ });
191
+
132
192
  function normaliseInitial(
133
193
  value: string | readonly string[] | undefined,
134
194
  ): readonly string[] {