@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,6 +1,15 @@
1
1
  /**
2
2
  * Breadcrumb — the trail back up the hierarchy.
3
3
  *
4
+ * Breadcrumb({ children: BreadcrumbList({ children: html`
5
+ * ${BreadcrumbItem({ children: BreadcrumbLink({ href: "/", children: "Home" }) })}
6
+ * ${BreadcrumbSeparator({})}
7
+ * ${BreadcrumbItem({ children: BreadcrumbPage({ children: "Settings" }) })}
8
+ * ` }) })
9
+ *
10
+ * Every part is presentational, so none of them takes `Parts` — there is no
11
+ * context here to be inside of.
12
+ *
4
13
  * `aria-label="breadcrumb"` on the `<nav>` and `aria-current="page"` on the
5
14
  * last entry. Both matter: the label is how a screen reader distinguishes this
6
15
  * navigation from the other landmarks on the page, and `aria-current` is what
@@ -13,95 +22,87 @@
13
22
  */
14
23
 
15
24
  import { component, html } from "@c9up/aurora";
16
- import type { Child } from "../lib/children.js";
25
+ import { type Slot, slot } from "../lib/children.js";
17
26
  import { cn } from "../lib/cn.js";
18
27
  import { ChevronRightIcon, MoreHorizontalIcon } from "../lib/icons.js";
19
28
  import { type Reactive, read } from "../lib/props.js";
20
29
 
21
- export interface Crumb {
22
- label: Child;
23
- /** Absent on the current page, and on a collapsed ellipsis. */
24
- href?: string;
25
- /** Renders an ellipsis standing in for hidden ancestors. */
26
- ellipsis?: boolean;
27
- }
28
-
29
30
  export interface BreadcrumbProps {
30
- items: readonly Crumb[];
31
- /** Replaces the chevron between entries. */
32
- separator?: Child;
31
+ children?: Slot;
33
32
  class?: Reactive<string>;
34
33
  }
35
34
 
36
- export const Breadcrumb = component<BreadcrumbProps>((props) => {
37
- return html`<nav
35
+ export const Breadcrumb = component<BreadcrumbProps>(
36
+ (props) => html`<nav
38
37
  data-slot="breadcrumb"
39
38
  aria-label="breadcrumb"
40
39
  class="${() => cn(read(props.class))}"
41
- >
42
- <ol
43
- data-slot="breadcrumb-list"
44
- class="text-muted-foreground flex flex-wrap items-center gap-1.5 text-sm break-words sm:gap-2.5"
45
- >
46
- ${props.items.map((item, index) =>
47
- renderCrumb(
48
- item,
49
- index === props.items.length - 1,
50
- index > 0,
51
- props.separator,
52
- ),
53
- )}
54
- </ol>
55
- </nav>`;
56
- });
40
+ >${slot(props.children)}</nav>`,
41
+ );
57
42
 
58
- function renderCrumb(
59
- item: Crumb,
60
- isLast: boolean,
61
- needsSeparator: boolean,
62
- separator: Child,
63
- ): Child {
64
- return [
65
- needsSeparator
66
- ? html`<li
67
- data-slot="breadcrumb-separator"
68
- role="presentation"
69
- aria-hidden="true"
70
- class="[&>svg]:size-3.5"
71
- >${separator ?? ChevronRightIcon()}</li>`
72
- : null,
73
- html`<li data-slot="breadcrumb-item" class="inline-flex items-center gap-1.5">
74
- ${renderCrumbBody(item, isLast)}
75
- </li>`,
76
- ];
77
- }
43
+ export const BreadcrumbList = component<BreadcrumbProps>(
44
+ (props) => html`<ol
45
+ data-slot="breadcrumb-list"
46
+ class="${() =>
47
+ cn(
48
+ "text-muted-foreground flex flex-wrap items-center gap-1.5 text-sm break-words sm:gap-2.5",
49
+ read(props.class),
50
+ )}"
51
+ >${slot(props.children)}</ol>`,
52
+ );
78
53
 
79
- function renderCrumbBody(item: Crumb, isLast: boolean): Child {
80
- if (item.ellipsis === true) {
81
- return html`<span
82
- data-slot="breadcrumb-ellipsis"
83
- role="presentation"
84
- aria-hidden="true"
85
- class="flex size-9 items-center justify-center"
86
- >
87
- ${MoreHorizontalIcon({ class: "size-4" })}
88
- <span class="sr-only">More</span>
89
- </span>`;
90
- }
54
+ export const BreadcrumbItem = component<BreadcrumbProps>(
55
+ (props) => html`<li
56
+ data-slot="breadcrumb-item"
57
+ class="${() => cn("inline-flex items-center gap-1.5", read(props.class))}"
58
+ >${slot(props.children)}</li>`,
59
+ );
91
60
 
92
- if (isLast || item.href === undefined) {
93
- return html`<span
94
- data-slot="breadcrumb-page"
95
- role="link"
96
- aria-disabled="true"
97
- aria-current="page"
98
- class="text-foreground font-normal"
99
- >${item.label}</span>`;
100
- }
61
+ export interface BreadcrumbLinkProps extends BreadcrumbProps {
62
+ href?: string;
63
+ }
101
64
 
102
- return html`<a
65
+ export const BreadcrumbLink = component<BreadcrumbLinkProps>(
66
+ (props) => html`<a
103
67
  data-slot="breadcrumb-link"
104
- href="${item.href}"
105
- class="hover:text-foreground transition-colors"
106
- >${item.label}</a>`;
107
- }
68
+ href="${props.href}"
69
+ class="${() => cn("hover:text-foreground transition-colors", read(props.class))}"
70
+ >${slot(props.children)}</a>`,
71
+ );
72
+
73
+ /**
74
+ * The crumb for where you already are.
75
+ *
76
+ * `role="link"` with `aria-disabled` rather than an `<a>` without an `href`:
77
+ * upstream's shape, and it keeps the crumb in the same role as its siblings so
78
+ * a reader announces a consistent list.
79
+ */
80
+ export const BreadcrumbPage = component<BreadcrumbProps>(
81
+ (props) => html`<span
82
+ data-slot="breadcrumb-page"
83
+ role="link"
84
+ aria-disabled="true"
85
+ aria-current="page"
86
+ class="${() => cn("text-foreground font-normal", read(props.class))}"
87
+ >${slot(props.children)}</span>`,
88
+ );
89
+
90
+ /** The chevron. Defaults to one; give children to replace it. */
91
+ export const BreadcrumbSeparator = component<BreadcrumbProps>(
92
+ (props) => html`<li
93
+ data-slot="breadcrumb-separator"
94
+ role="presentation"
95
+ aria-hidden="true"
96
+ class="${() => cn("[&>svg]:size-3.5", read(props.class))}"
97
+ >${props.children === undefined ? ChevronRightIcon() : slot(props.children)}</li>`,
98
+ );
99
+
100
+ /** Stands in for ancestors that did not fit. */
101
+ export const BreadcrumbEllipsis = component<BreadcrumbProps>(
102
+ (props) => html`<span
103
+ data-slot="breadcrumb-ellipsis"
104
+ role="presentation"
105
+ aria-hidden="true"
106
+ class="${() => cn("flex size-9 items-center justify-center", read(props.class))}"
107
+ >${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only">More</span></span>`,
108
+ );
@@ -1,17 +1,13 @@
1
1
  /**
2
2
  * Collapsible — a trigger that shows and hides a panel.
3
3
  *
4
- * Two things differ from Radix, both deliberate.
4
+ * Collapsible({ children: () => html`
5
+ * ${CollapsibleTrigger({ children: "Show more" })}
6
+ * ${CollapsibleContent({ children: "…" })}
7
+ * ` })
5
8
  *
6
- * **The API.** Radix composes through React context: `<Collapsible>` publishes
7
- * state that `<CollapsibleTrigger>` and `<CollapsibleContent>` read from
8
- * anywhere below it. Aurora has no context, and the usual workarounds — a
9
- * factory returning bound parts, or threading a handle through props — trade a
10
- * real problem for a clumsier one. So the parts are props: `trigger` and
11
- * `children`. The rendered markup is identical to shadcn's; only the call
12
- * shape changes.
13
- *
14
- * **The animation.** Radix measures the content and publishes its height as
9
+ * One deviation from upstream remains, and it is the animation. Radix measures
10
+ * the content and publishes its height as
15
11
  * `--radix-collapsible-content-height` for the keyframes to interpolate,
16
12
  * because `height: auto` is not animatable. A CSS grid whose single row goes
17
13
  * from `0fr` to `1fr` animates the same transition with no measurement, no
@@ -23,25 +19,31 @@
23
19
  * that is not on screen.
24
20
  */
25
21
 
26
- import { component, html } from "@c9up/aurora";
27
- import { type Slot, slot } from "../lib/children.js";
22
+ import { component, createContext, html, inject, provide } from "@c9up/aurora";
23
+ import type { Parts, Slot } from "../lib/children.js";
24
+ import { slot } from "../lib/children.js";
28
25
  import { cn } from "../lib/cn.js";
29
26
  import { uid } from "../lib/id.js";
30
27
  import { type Reactive, read, readOr } from "../lib/props.js";
31
28
  import { controllable } from "../primitives/controllable.js";
32
29
 
30
+ interface CollapsibleApi {
31
+ readonly triggerId: string;
32
+ readonly contentId: string;
33
+ readonly open: () => boolean;
34
+ readonly disabled: () => boolean;
35
+ toggle(): void;
36
+ }
37
+
38
+ const CollapsibleContext = createContext<CollapsibleApi>("Collapsible");
39
+
33
40
  export interface CollapsibleProps {
34
- /** The clickable summary. Rendered inside a button. */
35
- trigger?: Slot;
36
- /** The panel revealed when open. */
37
- children?: Slot;
38
41
  open?: Reactive<boolean>;
39
42
  defaultOpen?: boolean;
40
43
  disabled?: Reactive<boolean>;
41
44
  onOpenChange?: (open: boolean) => void;
42
45
  class?: Reactive<string>;
43
- triggerClass?: Reactive<string>;
44
- contentClass?: Reactive<string>;
46
+ children?: Parts;
45
47
  }
46
48
 
47
49
  export const Collapsible = component<CollapsibleProps>((props) => {
@@ -54,36 +56,70 @@ export const Collapsible = component<CollapsibleProps>((props) => {
54
56
  const contentId = uid("collapsible-content");
55
57
  const triggerId = uid("collapsible-trigger");
56
58
 
57
- const toggle = (): void => {
58
- if (readOr(props.disabled, false)) return;
59
- state.set(!state.current());
60
- };
59
+ provide<CollapsibleApi>(CollapsibleContext, {
60
+ triggerId,
61
+ contentId,
62
+ open: () => state.current(),
63
+ disabled: () => readOr(props.disabled, false),
64
+ toggle() {
65
+ if (readOr(props.disabled, false)) return;
66
+ state.set(!state.current());
67
+ },
68
+ });
61
69
 
62
70
  return html`<div
63
71
  data-slot="collapsible"
64
72
  data-state="${() => (state.current() ? "open" : "closed")}"
65
73
  class="${() => cn("flex flex-col", read(props.class))}"
66
- >
67
- <button
74
+ >${props.children?.()}</div>`;
75
+ });
76
+
77
+ export interface CollapsibleTriggerProps {
78
+ children?: Slot;
79
+ class?: Reactive<string>;
80
+ }
81
+
82
+ export const CollapsibleTrigger = component<CollapsibleTriggerProps>(
83
+ (props) => {
84
+ const collapsible = inject(CollapsibleContext);
85
+ return html`<button
68
86
  type="button"
69
87
  data-slot="collapsible-trigger"
70
- id="${triggerId}"
71
- aria-expanded="${() => (state.current() ? "true" : "false")}"
72
- aria-controls="${contentId}"
73
- ?disabled="${() => readOr(props.disabled, false)}"
74
- class="${() => cn("flex items-center justify-between gap-2 outline-none disabled:opacity-50", read(props.triggerClass))}"
75
- @click="${toggle}"
76
- >${slot(props.trigger)}</button>
77
- <div
88
+ id="${collapsible.triggerId}"
89
+ data-state="${() => (collapsible.open() ? "open" : "closed")}"
90
+ aria-expanded="${() => (collapsible.open() ? "true" : "false")}"
91
+ aria-controls="${collapsible.contentId}"
92
+ ?disabled="${() => collapsible.disabled()}"
93
+ class="${() =>
94
+ cn(
95
+ "flex items-center justify-between gap-2 outline-none disabled:opacity-50",
96
+ read(props.class),
97
+ )}"
98
+ @click="${() => collapsible.toggle()}"
99
+ >${slot(props.children)}</button>`;
100
+ },
101
+ );
102
+
103
+ export interface CollapsibleContentProps {
104
+ children?: Slot;
105
+ class?: Reactive<string>;
106
+ }
107
+
108
+ export const CollapsibleContent = component<CollapsibleContentProps>(
109
+ (props) => {
110
+ const collapsible = inject(CollapsibleContext);
111
+ return html`<div
78
112
  data-slot="collapsible-content"
79
- id="${contentId}"
113
+ id="${collapsible.contentId}"
80
114
  role="region"
81
- aria-labelledby="${triggerId}"
82
- ?inert="${() => !state.current()}"
115
+ data-state="${() => (collapsible.open() ? "open" : "closed")}"
116
+ aria-labelledby="${collapsible.triggerId}"
117
+ ?inert="${() => !collapsible.open()}"
83
118
  class="grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none"
84
- style="${() => `grid-template-rows: ${state.current() ? "1fr" : "0fr"}`}"
85
- >
86
- <div class="${() => cn("overflow-hidden", read(props.contentClass))}">${slot(props.children)}</div>
87
- </div>
88
- </div>`;
89
- });
119
+ style="${() =>
120
+ `grid-template-rows: ${collapsible.open() ? "1fr" : "0fr"}`}"
121
+ ><div class="${() => cn("overflow-hidden", read(props.class))}">${slot(
122
+ props.children,
123
+ )}</div></div>`;
124
+ },
125
+ );
@@ -1,31 +1,29 @@
1
1
  /**
2
- * Field — a form control with its label, hint and error.
2
+ * Field — a labelled control with its help text and its error.
3
3
  *
4
- * The reason this exists rather than each form writing the four elements by
5
- * hand is the wiring. A control must point at its description and its error
6
- * with `aria-describedby`, and at its own label with `for`/`id`. Done by hand
7
- * that is three ids to invent and keep in step per field, and the failure is
8
- * silent — a sighted user sees the error, a screen-reader user does not.
4
+ * Field({ ids, children: () => html`
5
+ * ${FieldLabel({ children: "Email" })}
6
+ * ${Input({ id: ids.control, describedBy: ids.describedBy })}
7
+ * ${FieldDescription({ children: "We never share it." })}
8
+ * ${FieldError({ children: error })}
9
+ * ` })
9
10
  *
10
- * `Field` mints the ids and returns them, so the control gets them by
11
- * destructuring rather than by convention:
11
+ * The ids are the whole point of the component. Upstream leaves them to the
12
+ * caller — `<FieldLabel htmlFor="x">` beside `<Input id="x">` — which works
13
+ * until the two drift. `fieldIds()` mints a matching set and `Field` shares it
14
+ * through context, so the label, the description and the error all point at
15
+ * the right thing without anyone repeating a string. A caller still needs the
16
+ * set for the CONTROL, which is why `fieldIds()` is exported and may be passed
17
+ * in; left out, the field mints its own.
12
18
  *
13
- * const email = fieldIds()
14
- * Field({
15
- * ids: email,
16
- * label: "Email",
17
- * error: () => errors.email,
18
- * children: Input({ id: email.control, describedBy: email.describedBy, invalid: … }),
19
- * })
20
- *
21
- * The error slot is reactive and empty-tolerant: passing `() => errors.email`
22
- * renders nothing until there is something to render, so a field does not
23
- * reserve a blank line for an error that has not happened.
19
+ * `describedBy` names the description AND the error unconditionally. A screen
20
+ * reader skips an id that resolves to nothing, so listing an absent error is
21
+ * harmless — whereas recomputing the attribute when an error appears means the
22
+ * control has to re-render, which Aurora does not do.
24
23
  */
25
24
 
26
- import { component, html } from "@c9up/aurora";
27
- import { Label } from "../atoms/Label.js";
28
- import type { Child, Slot } from "../lib/children.js";
25
+ import { component, createContext, html, inject, provide } from "@c9up/aurora";
26
+ import type { Parts, Slot } from "../lib/children.js";
29
27
  import { slot } from "../lib/children.js";
30
28
  import { cn } from "../lib/cn.js";
31
29
  import { uid } from "../lib/id.js";
@@ -42,7 +40,6 @@ export interface FieldIds {
42
40
  readonly describedBy: string;
43
41
  }
44
42
 
45
- /** Mint the id set for one field. Call once, in the component that owns it. */
46
43
  export function fieldIds(): FieldIds {
47
44
  const control = uid("field");
48
45
  const description = `${control}-description`;
@@ -52,80 +49,198 @@ export function fieldIds(): FieldIds {
52
49
  label: `${control}-label`,
53
50
  description,
54
51
  error,
55
- // Both are named unconditionally. A screen reader skips an id that
56
- // resolves to nothing, so listing an absent error is harmless — whereas
57
- // recomputing the attribute when an error appears means the control has to
58
- // re-render, which Aurora does not do.
59
52
  describedBy: `${description} ${error}`,
60
53
  };
61
54
  }
62
55
 
56
+ interface FieldApi {
57
+ readonly ids: FieldIds;
58
+ readonly required: boolean;
59
+ }
60
+
61
+ const FieldContext = createContext<FieldApi>("Field");
62
+
63
63
  export interface FieldProps {
64
- ids: FieldIds;
65
- label?: Child;
66
- /** The control itself. */
67
- children?: Slot;
68
- /** Static help text under the control. */
69
- description?: Child;
70
- /** Validation message. Reactive — renders nothing while absent. */
71
- error?: Reactive<string | undefined>;
64
+ /** Reuse a set the caller already put on the control. */
65
+ ids?: FieldIds;
72
66
  required?: boolean;
73
67
  disabled?: Reactive<boolean>;
74
68
  orientation?: "vertical" | "horizontal";
75
69
  class?: Reactive<string>;
70
+ children?: Parts;
76
71
  }
77
72
 
78
73
  export const Field = component<FieldProps>((props) => {
79
- const horizontal = props.orientation === "horizontal";
74
+ const ids = props.ids ?? fieldIds();
75
+ const orientation = props.orientation ?? "vertical";
76
+
77
+ provide<FieldApi>(FieldContext, { ids, required: props.required === true });
80
78
 
81
79
  return html`<div
82
80
  data-slot="field"
81
+ data-orientation="${orientation}"
83
82
  data-disabled="${() => (read(props.disabled) === true ? "true" : undefined)}"
84
83
  class="${() =>
85
84
  cn(
86
- "group flex gap-2",
87
- horizontal ? "flex-row items-center justify-between" : "flex-col",
85
+ "group/field flex w-full gap-3 data-[invalid=true]:text-destructive",
86
+ orientation === "horizontal"
87
+ ? "flex-row items-center justify-between"
88
+ : "flex-col",
88
89
  read(props.class),
89
90
  )}"
90
- >
91
- ${
92
- props.label === undefined
93
- ? null
94
- : Label({
95
- for: props.ids.control,
96
- id: props.ids.label,
97
- children: html`${props.label}${
98
- props.required === true
99
- ? html`<span aria-hidden="true" class="text-destructive">*</span>`
100
- : null
101
- }`,
102
- })
103
- }
104
- ${slot(props.children)}
105
- ${
106
- props.description === undefined
107
- ? null
108
- : html`<p
109
- data-slot="field-description"
110
- id="${props.ids.description}"
111
- class="text-muted-foreground text-sm"
112
- >${props.description}</p>`
91
+ >${props.children?.()}</div>`;
92
+ });
93
+
94
+ export interface FieldSectionProps {
95
+ children?: Slot;
96
+ class?: Reactive<string>;
97
+ }
98
+
99
+ /**
100
+ * The label.
101
+ *
102
+ * A `<label>` with `for`, not a `<div>`: clicking it focuses the control, and
103
+ * that is not something `aria-labelledby` gives you.
104
+ */
105
+ export const FieldLabel = component<FieldSectionProps>((props) => {
106
+ const field = inject(FieldContext);
107
+ return html`<label
108
+ data-slot="field-label"
109
+ id="${field.ids.label}"
110
+ for="${field.ids.control}"
111
+ class="${() =>
112
+ cn(
113
+ "group/field-label peer/field-label flex w-fit gap-2 text-sm leading-snug font-medium select-none group-data-[disabled=true]/field:opacity-50",
114
+ read(props.class),
115
+ )}"
116
+ >${slot(props.children)}${
117
+ field.required
118
+ ? html`<span aria-hidden="true" class="text-destructive">*</span>`
119
+ : null
120
+ }</label>`;
121
+ });
122
+
123
+ /** A heading inside a field that is not the control's label. */
124
+ export const FieldTitle = component<FieldSectionProps>(
125
+ (props) => html`<div
126
+ data-slot="field-label"
127
+ class="${() =>
128
+ cn(
129
+ "flex w-fit items-center gap-2 text-sm leading-snug font-medium group-data-[disabled=true]/field:opacity-50",
130
+ read(props.class),
131
+ )}"
132
+ >${slot(props.children)}</div>`,
133
+ );
134
+
135
+ export const FieldDescription = component<FieldSectionProps>((props) => {
136
+ const field = inject(FieldContext);
137
+ return html`<p
138
+ data-slot="field-description"
139
+ id="${field.ids.description}"
140
+ class="${() =>
141
+ cn(
142
+ "text-muted-foreground text-sm leading-normal font-normal group-has-[[data-orientation=horizontal]]/field:text-balance",
143
+ read(props.class),
144
+ )}"
145
+ >${slot(props.children)}</p>`;
146
+ });
147
+
148
+ export interface FieldErrorProps {
149
+ /** Reactive — renders nothing while absent. */
150
+ children?: Reactive<string | undefined>;
151
+ /** Several messages, rendered as a list. */
152
+ errors?: Reactive<readonly string[] | undefined>;
153
+ class?: Reactive<string>;
154
+ }
155
+
156
+ /**
157
+ * The validation message.
158
+ *
159
+ * Always in the DOM, `empty:hidden` rather than conditionally rendered: the id
160
+ * `describedBy` names has to resolve to something the moment an error appears,
161
+ * and a node added later is announced late or not at all.
162
+ */
163
+ export const FieldError = component<FieldErrorProps>((props) => {
164
+ const field = inject(FieldContext);
165
+ return html`<p
166
+ data-slot="field-error"
167
+ id="${field.ids.error}"
168
+ role="alert"
169
+ class="${() =>
170
+ cn(
171
+ "text-destructive text-sm font-normal empty:hidden",
172
+ read(props.class),
173
+ )}"
174
+ >${() => {
175
+ const many = read(props.errors);
176
+ if (many !== undefined && many.length > 0) {
177
+ return many.length === 1
178
+ ? (many[0] ?? "")
179
+ : html`<ul class="ml-4 flex list-disc flex-col gap-1">${many.map(
180
+ (message) => html`<li>${message}</li>`,
181
+ )}</ul>`;
113
182
  }
114
- <p
115
- data-slot="field-error"
116
- id="${props.ids.error}"
117
- role="alert"
118
- class="text-destructive text-sm empty:hidden"
119
- >${() => read(props.error) ?? ""}</p>
120
- </div>`;
183
+ return read(props.children) ?? "";
184
+ }}</p>`;
121
185
  });
122
186
 
187
+ /** Several fields stacked, with the spacing upstream gives a form. */
123
188
  export const FieldGroup = styledDiv(
124
189
  "field-group",
125
- "flex w-full flex-col gap-6",
190
+ "group/field-group @container/field-group flex w-full flex-col gap-7 data-[slot=checkbox-group]:gap-3 [&>[data-slot=field-group]]:gap-4",
191
+ );
192
+
193
+ /** The text half of a horizontal field, beside its control. */
194
+ export const FieldContent = styledDiv(
195
+ "field-content",
196
+ "group/field-content flex flex-1 flex-col gap-1.5 leading-snug",
197
+ );
198
+
199
+ /** A `<fieldset>`, for a group of fields that answer one question together. */
200
+ export const FieldSet = component<FieldSectionProps>(
201
+ (props) => html`<fieldset
202
+ data-slot="field-set"
203
+ class="${() => cn("flex flex-col gap-6", read(props.class))}"
204
+ >${slot(props.children)}</fieldset>`,
126
205
  );
127
206
 
128
- export const FieldSeparator = styledDiv(
129
- "field-separator",
130
- "bg-border h-px w-full",
207
+ export interface FieldLegendProps extends FieldSectionProps {
208
+ variant?: "legend" | "label";
209
+ }
210
+
211
+ export const FieldLegend = component<FieldLegendProps>(
212
+ (props) => html`<legend
213
+ data-slot="field-legend"
214
+ data-variant="${props.variant ?? "legend"}"
215
+ class="${() =>
216
+ cn(
217
+ "mb-3 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base",
218
+ read(props.class),
219
+ )}"
220
+ >${slot(props.children)}</legend>`,
221
+ );
222
+
223
+ export interface FieldSeparatorProps {
224
+ /** A word in the middle of the rule — "or". */
225
+ children?: Slot;
226
+ class?: Reactive<string>;
227
+ }
228
+
229
+ export const FieldSeparator = component<FieldSeparatorProps>(
230
+ (props) => html`<div
231
+ data-slot="field-separator"
232
+ data-content="${props.children === undefined ? undefined : ""}"
233
+ class="${() =>
234
+ cn(
235
+ "relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
236
+ read(props.class),
237
+ )}"
238
+ ><span class="bg-border absolute inset-x-0 top-1/2 h-px"></span>${
239
+ props.children === undefined
240
+ ? null
241
+ : html`<span
242
+ data-slot="field-separator-content"
243
+ class="bg-background text-muted-foreground relative mx-auto block w-fit px-2"
244
+ >${slot(props.children)}</span>`
245
+ }</div>`,
131
246
  );