@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,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
@@ -12,62 +21,53 @@
12
21
  * conveys the sequence.
13
22
  */
14
23
  import { component, html } from "@c9up/aurora";
24
+ import { slot } from "../lib/children.js";
15
25
  import { cn } from "../lib/cn.js";
16
26
  import { ChevronRightIcon, MoreHorizontalIcon } from "../lib/icons.js";
17
27
  import { read } from "../lib/props.js";
18
- export const Breadcrumb = component((props) => {
19
- return html `<nav
28
+ export const Breadcrumb = component((props) => html `<nav
20
29
  data-slot="breadcrumb"
21
30
  aria-label="breadcrumb"
22
31
  class="${() => cn(read(props.class))}"
23
- >
24
- <ol
25
- data-slot="breadcrumb-list"
26
- class="text-muted-foreground flex flex-wrap items-center gap-1.5 text-sm break-words sm:gap-2.5"
27
- >
28
- ${props.items.map((item, index) => renderCrumb(item, index === props.items.length - 1, index > 0, props.separator))}
29
- </ol>
30
- </nav>`;
31
- });
32
- function renderCrumb(item, isLast, needsSeparator, separator) {
33
- return [
34
- needsSeparator
35
- ? html `<li
36
- data-slot="breadcrumb-separator"
37
- role="presentation"
38
- aria-hidden="true"
39
- class="[&>svg]:size-3.5"
40
- >${separator ?? ChevronRightIcon()}</li>`
41
- : null,
42
- html `<li data-slot="breadcrumb-item" class="inline-flex items-center gap-1.5">
43
- ${renderCrumbBody(item, isLast)}
44
- </li>`,
45
- ];
46
- }
47
- function renderCrumbBody(item, isLast) {
48
- if (item.ellipsis === true) {
49
- return html `<span
50
- data-slot="breadcrumb-ellipsis"
51
- role="presentation"
52
- aria-hidden="true"
53
- class="flex size-9 items-center justify-center"
54
- >
55
- ${MoreHorizontalIcon({ class: "size-4" })}
56
- <span class="sr-only">More</span>
57
- </span>`;
58
- }
59
- if (isLast || item.href === undefined) {
60
- return html `<span
61
- data-slot="breadcrumb-page"
62
- role="link"
63
- aria-disabled="true"
64
- aria-current="page"
65
- class="text-foreground font-normal"
66
- >${item.label}</span>`;
67
- }
68
- return html `<a
32
+ >${slot(props.children)}</nav>`);
33
+ export const BreadcrumbList = component((props) => html `<ol
34
+ data-slot="breadcrumb-list"
35
+ class="${() => cn("text-muted-foreground flex flex-wrap items-center gap-1.5 text-sm break-words sm:gap-2.5", read(props.class))}"
36
+ >${slot(props.children)}</ol>`);
37
+ export const BreadcrumbItem = component((props) => html `<li
38
+ data-slot="breadcrumb-item"
39
+ class="${() => cn("inline-flex items-center gap-1.5", read(props.class))}"
40
+ >${slot(props.children)}</li>`);
41
+ export const BreadcrumbLink = component((props) => html `<a
69
42
  data-slot="breadcrumb-link"
70
- href="${item.href}"
71
- class="hover:text-foreground transition-colors"
72
- >${item.label}</a>`;
73
- }
43
+ href="${props.href}"
44
+ class="${() => cn("hover:text-foreground transition-colors", read(props.class))}"
45
+ >${slot(props.children)}</a>`);
46
+ /**
47
+ * The crumb for where you already are.
48
+ *
49
+ * `role="link"` with `aria-disabled` rather than an `<a>` without an `href`:
50
+ * upstream's shape, and it keeps the crumb in the same role as its siblings so
51
+ * a reader announces a consistent list.
52
+ */
53
+ export const BreadcrumbPage = component((props) => html `<span
54
+ data-slot="breadcrumb-page"
55
+ role="link"
56
+ aria-disabled="true"
57
+ aria-current="page"
58
+ class="${() => cn("text-foreground font-normal", read(props.class))}"
59
+ >${slot(props.children)}</span>`);
60
+ /** The chevron. Defaults to one; give children to replace it. */
61
+ export const BreadcrumbSeparator = component((props) => html `<li
62
+ data-slot="breadcrumb-separator"
63
+ role="presentation"
64
+ aria-hidden="true"
65
+ class="${() => cn("[&>svg]:size-3.5", read(props.class))}"
66
+ >${props.children === undefined ? ChevronRightIcon() : slot(props.children)}</li>`);
67
+ /** Stands in for ancestors that did not fit. */
68
+ export const BreadcrumbEllipsis = component((props) => html `<span
69
+ data-slot="breadcrumb-ellipsis"
70
+ role="presentation"
71
+ aria-hidden="true"
72
+ class="${() => cn("flex size-9 items-center justify-center", read(props.class))}"
73
+ >${MoreHorizontalIcon({ class: "size-4" })}<span class="sr-only">More</span></span>`);
@@ -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
@@ -22,19 +18,24 @@
22
18
  * and stays in the accessibility tree, and a keyboard user tabs into content
23
19
  * that is not on screen.
24
20
  */
25
- import { type Slot } from "../lib/children.js";
21
+ import type { Parts, Slot } from "../lib/children.js";
26
22
  import { type Reactive } from "../lib/props.js";
27
23
  export interface CollapsibleProps {
28
- /** The clickable summary. Rendered inside a button. */
29
- trigger?: Slot;
30
- /** The panel revealed when open. */
31
- children?: Slot;
32
24
  open?: Reactive<boolean>;
33
25
  defaultOpen?: boolean;
34
26
  disabled?: Reactive<boolean>;
35
27
  onOpenChange?: (open: boolean) => void;
36
28
  class?: Reactive<string>;
37
- triggerClass?: Reactive<string>;
38
- contentClass?: Reactive<string>;
29
+ children?: Parts;
39
30
  }
40
31
  export declare const Collapsible: (props?: CollapsibleProps | undefined) => import("@c9up/aurora").TemplateResult;
32
+ export interface CollapsibleTriggerProps {
33
+ children?: Slot;
34
+ class?: Reactive<string>;
35
+ }
36
+ export declare const CollapsibleTrigger: (props?: CollapsibleTriggerProps | undefined) => import("@c9up/aurora").TemplateResult;
37
+ export interface CollapsibleContentProps {
38
+ children?: Slot;
39
+ class?: Reactive<string>;
40
+ }
41
+ export declare const CollapsibleContent: (props?: CollapsibleContentProps | undefined) => import("@c9up/aurora").TemplateResult;
@@ -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
@@ -22,12 +18,13 @@
22
18
  * and stays in the accessibility tree, and a keyboard user tabs into content
23
19
  * that is not on screen.
24
20
  */
25
- import { component, html } from "@c9up/aurora";
21
+ import { component, createContext, html, inject, provide } from "@c9up/aurora";
26
22
  import { slot } from "../lib/children.js";
27
23
  import { cn } from "../lib/cn.js";
28
24
  import { uid } from "../lib/id.js";
29
25
  import { read, readOr } from "../lib/props.js";
30
26
  import { controllable } from "../primitives/controllable.js";
27
+ const CollapsibleContext = createContext("Collapsible");
31
28
  export const Collapsible = component((props) => {
32
29
  const state = controllable({
33
30
  value: props.open,
@@ -36,36 +33,47 @@ export const Collapsible = component((props) => {
36
33
  });
37
34
  const contentId = uid("collapsible-content");
38
35
  const triggerId = uid("collapsible-trigger");
39
- const toggle = () => {
40
- if (readOr(props.disabled, false))
41
- return;
42
- state.set(!state.current());
43
- };
36
+ provide(CollapsibleContext, {
37
+ triggerId,
38
+ contentId,
39
+ open: () => state.current(),
40
+ disabled: () => readOr(props.disabled, false),
41
+ toggle() {
42
+ if (readOr(props.disabled, false))
43
+ return;
44
+ state.set(!state.current());
45
+ },
46
+ });
44
47
  return html `<div
45
48
  data-slot="collapsible"
46
49
  data-state="${() => (state.current() ? "open" : "closed")}"
47
50
  class="${() => cn("flex flex-col", read(props.class))}"
48
- >
49
- <button
51
+ >${props.children?.()}</div>`;
52
+ });
53
+ export const CollapsibleTrigger = component((props) => {
54
+ const collapsible = inject(CollapsibleContext);
55
+ return html `<button
50
56
  type="button"
51
57
  data-slot="collapsible-trigger"
52
- id="${triggerId}"
53
- aria-expanded="${() => (state.current() ? "true" : "false")}"
54
- aria-controls="${contentId}"
55
- ?disabled="${() => readOr(props.disabled, false)}"
56
- class="${() => cn("flex items-center justify-between gap-2 outline-none disabled:opacity-50", read(props.triggerClass))}"
57
- @click="${toggle}"
58
- >${slot(props.trigger)}</button>
59
- <div
58
+ id="${collapsible.triggerId}"
59
+ data-state="${() => (collapsible.open() ? "open" : "closed")}"
60
+ aria-expanded="${() => (collapsible.open() ? "true" : "false")}"
61
+ aria-controls="${collapsible.contentId}"
62
+ ?disabled="${() => collapsible.disabled()}"
63
+ class="${() => cn("flex items-center justify-between gap-2 outline-none disabled:opacity-50", read(props.class))}"
64
+ @click="${() => collapsible.toggle()}"
65
+ >${slot(props.children)}</button>`;
66
+ });
67
+ export const CollapsibleContent = component((props) => {
68
+ const collapsible = inject(CollapsibleContext);
69
+ return html `<div
60
70
  data-slot="collapsible-content"
61
- id="${contentId}"
71
+ id="${collapsible.contentId}"
62
72
  role="region"
63
- aria-labelledby="${triggerId}"
64
- ?inert="${() => !state.current()}"
73
+ data-state="${() => (collapsible.open() ? "open" : "closed")}"
74
+ aria-labelledby="${collapsible.triggerId}"
75
+ ?inert="${() => !collapsible.open()}"
65
76
  class="grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none"
66
- style="${() => `grid-template-rows: ${state.current() ? "1fr" : "0fr"}`}"
67
- >
68
- <div class="${() => cn("overflow-hidden", read(props.contentClass))}">${slot(props.children)}</div>
69
- </div>
70
- </div>`;
77
+ style="${() => `grid-template-rows: ${collapsible.open() ? "1fr" : "0fr"}`}"
78
+ ><div class="${() => cn("overflow-hidden", read(props.class))}">${slot(props.children)}</div></div>`;
71
79
  });
@@ -1,28 +1,27 @@
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
- import type { Child, Slot } from "../lib/children.js";
24
+ import type { Parts, Slot } from "../lib/children.js";
26
25
  import { type Reactive } from "../lib/props.js";
27
26
  export interface FieldIds {
28
27
  /** Put on the control. The label's `for` points here. */
@@ -33,22 +32,59 @@ export interface FieldIds {
33
32
  /** Ready-made `aria-describedby` naming both the description and the error. */
34
33
  readonly describedBy: string;
35
34
  }
36
- /** Mint the id set for one field. Call once, in the component that owns it. */
37
35
  export declare function fieldIds(): FieldIds;
38
36
  export interface FieldProps {
39
- ids: FieldIds;
40
- label?: Child;
41
- /** The control itself. */
42
- children?: Slot;
43
- /** Static help text under the control. */
44
- description?: Child;
45
- /** Validation message. Reactive — renders nothing while absent. */
46
- error?: Reactive<string | undefined>;
37
+ /** Reuse a set the caller already put on the control. */
38
+ ids?: FieldIds;
47
39
  required?: boolean;
48
40
  disabled?: Reactive<boolean>;
49
41
  orientation?: "vertical" | "horizontal";
50
42
  class?: Reactive<string>;
43
+ children?: Parts;
51
44
  }
52
45
  export declare const Field: (props?: FieldProps | undefined) => import("@c9up/aurora").TemplateResult;
46
+ export interface FieldSectionProps {
47
+ children?: Slot;
48
+ class?: Reactive<string>;
49
+ }
50
+ /**
51
+ * The label.
52
+ *
53
+ * A `<label>` with `for`, not a `<div>`: clicking it focuses the control, and
54
+ * that is not something `aria-labelledby` gives you.
55
+ */
56
+ export declare const FieldLabel: (props?: FieldSectionProps | undefined) => import("@c9up/aurora").TemplateResult;
57
+ /** A heading inside a field that is not the control's label. */
58
+ export declare const FieldTitle: (props?: FieldSectionProps | undefined) => import("@c9up/aurora").TemplateResult;
59
+ export declare const FieldDescription: (props?: FieldSectionProps | undefined) => import("@c9up/aurora").TemplateResult;
60
+ export interface FieldErrorProps {
61
+ /** Reactive — renders nothing while absent. */
62
+ children?: Reactive<string | undefined>;
63
+ /** Several messages, rendered as a list. */
64
+ errors?: Reactive<readonly string[] | undefined>;
65
+ class?: Reactive<string>;
66
+ }
67
+ /**
68
+ * The validation message.
69
+ *
70
+ * Always in the DOM, `empty:hidden` rather than conditionally rendered: the id
71
+ * `describedBy` names has to resolve to something the moment an error appears,
72
+ * and a node added later is announced late or not at all.
73
+ */
74
+ export declare const FieldError: (props?: FieldErrorProps | undefined) => import("@c9up/aurora").TemplateResult;
75
+ /** Several fields stacked, with the spacing upstream gives a form. */
53
76
  export declare const FieldGroup: (props?: import("./Card.js").CardProps) => import("@c9up/aurora").TemplateResult;
54
- export declare const FieldSeparator: (props?: import("./Card.js").CardProps) => import("@c9up/aurora").TemplateResult;
77
+ /** The text half of a horizontal field, beside its control. */
78
+ export declare const FieldContent: (props?: import("./Card.js").CardProps) => import("@c9up/aurora").TemplateResult;
79
+ /** A `<fieldset>`, for a group of fields that answer one question together. */
80
+ export declare const FieldSet: (props?: FieldSectionProps | undefined) => import("@c9up/aurora").TemplateResult;
81
+ export interface FieldLegendProps extends FieldSectionProps {
82
+ variant?: "legend" | "label";
83
+ }
84
+ export declare const FieldLegend: (props?: FieldLegendProps | undefined) => import("@c9up/aurora").TemplateResult;
85
+ export interface FieldSeparatorProps {
86
+ /** A word in the middle of the rule — "or". */
87
+ children?: Slot;
88
+ class?: Reactive<string>;
89
+ }
90
+ export declare const FieldSeparator: (props?: FieldSeparatorProps | undefined) => import("@c9up/aurora").TemplateResult;
@@ -1,35 +1,32 @@
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
- import { component, html } from "@c9up/aurora";
26
- import { Label } from "../atoms/Label.js";
24
+ import { component, createContext, html, inject, provide } from "@c9up/aurora";
27
25
  import { slot } from "../lib/children.js";
28
26
  import { cn } from "../lib/cn.js";
29
27
  import { uid } from "../lib/id.js";
30
28
  import { read } from "../lib/props.js";
31
29
  import { styledDiv } from "../lib/styled.js";
32
- /** Mint the id set for one field. Call once, in the component that owns it. */
33
30
  export function fieldIds() {
34
31
  const control = uid("field");
35
32
  const description = `${control}-description`;
@@ -39,44 +36,98 @@ export function fieldIds() {
39
36
  label: `${control}-label`,
40
37
  description,
41
38
  error,
42
- // Both are named unconditionally. A screen reader skips an id that
43
- // resolves to nothing, so listing an absent error is harmless — whereas
44
- // recomputing the attribute when an error appears means the control has to
45
- // re-render, which Aurora does not do.
46
39
  describedBy: `${description} ${error}`,
47
40
  };
48
41
  }
42
+ const FieldContext = createContext("Field");
49
43
  export const Field = component((props) => {
50
- const horizontal = props.orientation === "horizontal";
44
+ const ids = props.ids ?? fieldIds();
45
+ const orientation = props.orientation ?? "vertical";
46
+ provide(FieldContext, { ids, required: props.required === true });
51
47
  return html `<div
52
48
  data-slot="field"
49
+ data-orientation="${orientation}"
53
50
  data-disabled="${() => (read(props.disabled) === true ? "true" : undefined)}"
54
- class="${() => cn("group flex gap-2", horizontal ? "flex-row items-center justify-between" : "flex-col", read(props.class))}"
55
- >
56
- ${props.label === undefined
57
- ? null
58
- : Label({
59
- for: props.ids.control,
60
- id: props.ids.label,
61
- children: html `${props.label}${props.required === true
62
- ? html `<span aria-hidden="true" class="text-destructive">*</span>`
63
- : null}`,
64
- })}
65
- ${slot(props.children)}
66
- ${props.description === undefined
67
- ? null
68
- : html `<p
69
- data-slot="field-description"
70
- id="${props.ids.description}"
71
- class="text-muted-foreground text-sm"
72
- >${props.description}</p>`}
73
- <p
74
- data-slot="field-error"
75
- id="${props.ids.error}"
76
- role="alert"
77
- class="text-destructive text-sm empty:hidden"
78
- >${() => read(props.error) ?? ""}</p>
79
- </div>`;
51
+ class="${() => cn("group/field flex w-full gap-3 data-[invalid=true]:text-destructive", orientation === "horizontal"
52
+ ? "flex-row items-center justify-between"
53
+ : "flex-col", read(props.class))}"
54
+ >${props.children?.()}</div>`;
55
+ });
56
+ /**
57
+ * The label.
58
+ *
59
+ * A `<label>` with `for`, not a `<div>`: clicking it focuses the control, and
60
+ * that is not something `aria-labelledby` gives you.
61
+ */
62
+ export const FieldLabel = component((props) => {
63
+ const field = inject(FieldContext);
64
+ return html `<label
65
+ data-slot="field-label"
66
+ id="${field.ids.label}"
67
+ for="${field.ids.control}"
68
+ class="${() => cn("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", read(props.class))}"
69
+ >${slot(props.children)}${field.required
70
+ ? html `<span aria-hidden="true" class="text-destructive">*</span>`
71
+ : null}</label>`;
72
+ });
73
+ /** A heading inside a field that is not the control's label. */
74
+ export const FieldTitle = component((props) => html `<div
75
+ data-slot="field-label"
76
+ class="${() => cn("flex w-fit items-center gap-2 text-sm leading-snug font-medium group-data-[disabled=true]/field:opacity-50", read(props.class))}"
77
+ >${slot(props.children)}</div>`);
78
+ export const FieldDescription = component((props) => {
79
+ const field = inject(FieldContext);
80
+ return html `<p
81
+ data-slot="field-description"
82
+ id="${field.ids.description}"
83
+ class="${() => cn("text-muted-foreground text-sm leading-normal font-normal group-has-[[data-orientation=horizontal]]/field:text-balance", read(props.class))}"
84
+ >${slot(props.children)}</p>`;
85
+ });
86
+ /**
87
+ * The validation message.
88
+ *
89
+ * Always in the DOM, `empty:hidden` rather than conditionally rendered: the id
90
+ * `describedBy` names has to resolve to something the moment an error appears,
91
+ * and a node added later is announced late or not at all.
92
+ */
93
+ export const FieldError = component((props) => {
94
+ const field = inject(FieldContext);
95
+ return html `<p
96
+ data-slot="field-error"
97
+ id="${field.ids.error}"
98
+ role="alert"
99
+ class="${() => cn("text-destructive text-sm font-normal empty:hidden", read(props.class))}"
100
+ >${() => {
101
+ const many = read(props.errors);
102
+ if (many !== undefined && many.length > 0) {
103
+ return many.length === 1
104
+ ? (many[0] ?? "")
105
+ : html `<ul class="ml-4 flex list-disc flex-col gap-1">${many.map((message) => html `<li>${message}</li>`)}</ul>`;
106
+ }
107
+ return read(props.children) ?? "";
108
+ }}</p>`;
80
109
  });
81
- export const FieldGroup = styledDiv("field-group", "flex w-full flex-col gap-6");
82
- export const FieldSeparator = styledDiv("field-separator", "bg-border h-px w-full");
110
+ /** Several fields stacked, with the spacing upstream gives a form. */
111
+ export const FieldGroup = styledDiv("field-group", "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");
112
+ /** The text half of a horizontal field, beside its control. */
113
+ export const FieldContent = styledDiv("field-content", "group/field-content flex flex-1 flex-col gap-1.5 leading-snug");
114
+ /** A `<fieldset>`, for a group of fields that answer one question together. */
115
+ export const FieldSet = component((props) => html `<fieldset
116
+ data-slot="field-set"
117
+ class="${() => cn("flex flex-col gap-6", read(props.class))}"
118
+ >${slot(props.children)}</fieldset>`);
119
+ export const FieldLegend = component((props) => html `<legend
120
+ data-slot="field-legend"
121
+ data-variant="${props.variant ?? "legend"}"
122
+ class="${() => cn("mb-3 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base", read(props.class))}"
123
+ >${slot(props.children)}</legend>`);
124
+ export const FieldSeparator = component((props) => html `<div
125
+ data-slot="field-separator"
126
+ data-content="${props.children === undefined ? undefined : ""}"
127
+ class="${() => cn("relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2", read(props.class))}"
128
+ ><span class="bg-border absolute inset-x-0 top-1/2 h-px"></span>${props.children === undefined
129
+ ? null
130
+ : html `<span
131
+ data-slot="field-separator-content"
132
+ class="bg-background text-muted-foreground relative mx-auto block w-fit px-2"
133
+ >${slot(props.children)}</span>`}</div>`);