@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
@@ -20,56 +20,137 @@
20
20
  * keyboard, and this is the whole of what it takes to fix that.
21
21
  */
22
22
 
23
- import { component, html, signal } from "@c9up/aurora";
24
- import { type Slot, slot } from "../lib/children.js";
23
+ import {
24
+ component,
25
+ createContext,
26
+ html,
27
+ inject,
28
+ provide,
29
+ signal,
30
+ } from "@c9up/aurora";
31
+ import type { Parts, Slot } from "../lib/children.js";
32
+ import { slot } from "../lib/children.js";
25
33
  import { cn } from "../lib/cn.js";
26
34
  import { GripVerticalIcon } from "../lib/icons.js";
27
35
  import { uid } from "../lib/id.js";
28
36
  import { type Reactive, read } from "../lib/props.js";
29
37
 
30
- export interface ResizableProps {
31
- first?: Slot;
32
- second?: Slot;
38
+ const KEYBOARD_STEP = 5;
39
+
40
+ interface ResizableApi {
41
+ readonly rootId: string;
42
+ readonly horizontal: boolean;
43
+ readonly min: number;
44
+ readonly max: number;
45
+ readonly size: () => number;
46
+ setSize(percent: number): void;
47
+ /** Claim a position: the FIRST panel is the one the size applies to. */
48
+ claimPanel(): number;
49
+ }
50
+
51
+ const ResizableContext = createContext<ResizableApi>("ResizablePanelGroup");
52
+
53
+ export interface ResizablePanelGroupProps {
33
54
  direction?: "horizontal" | "vertical";
34
55
  /** First pane's share at first render, in percent. Default `50`. */
35
56
  defaultSize?: number;
36
57
  minSize?: number;
37
58
  maxSize?: number;
38
- /** Show the grip dots on the handle. */
39
- withHandle?: boolean;
40
- class?: Reactive<string>;
41
59
  onResize?: (percent: number) => void;
60
+ class?: Reactive<string>;
61
+ children?: Parts;
42
62
  }
43
63
 
44
- const KEYBOARD_STEP = 5;
64
+ export const ResizablePanelGroup = component<ResizablePanelGroupProps>(
65
+ (props) => {
66
+ const horizontal = props.direction !== "vertical";
67
+ const min = props.minSize ?? 10;
68
+ const max = props.maxSize ?? 90;
69
+ const size = signal(clamp(props.defaultSize ?? 50, min, max));
70
+ const rootId = uid("resizable");
71
+ let panels = 0;
45
72
 
46
- export const Resizable = component<ResizableProps>((props) => {
47
- const horizontal = props.direction !== "vertical";
48
- const min = props.minSize ?? 10;
49
- const max = props.maxSize ?? 90;
50
- const size = signal(clamp(props.defaultSize ?? 50, min, max));
51
- const rootId = uid("resizable");
52
-
53
- function setSize(percent: number): void {
54
- const next = clamp(percent, min, max);
55
- size(next);
56
- props.onResize?.(next);
57
- }
73
+ function setSize(percent: number): void {
74
+ const next = clamp(percent, min, max);
75
+ size(next);
76
+ props.onResize?.(next);
77
+ }
78
+
79
+ provide<ResizableApi>(ResizableContext, {
80
+ rootId,
81
+ horizontal,
82
+ min,
83
+ max,
84
+ size: () => size(),
85
+ setSize,
86
+ claimPanel() {
87
+ panels += 1;
88
+ return panels;
89
+ },
90
+ });
91
+
92
+ return html`<div
93
+ data-slot="resizable-panel-group"
94
+ id="${rootId}"
95
+ data-direction="${horizontal ? "horizontal" : "vertical"}"
96
+ class="${() =>
97
+ cn(
98
+ "flex h-full w-full",
99
+ horizontal ? "flex-row" : "flex-col",
100
+ read(props.class),
101
+ )}"
102
+ >${props.children?.()}</div>`;
103
+ },
104
+ );
105
+
106
+ export interface ResizablePanelProps {
107
+ children?: Slot;
108
+ class?: Reactive<string>;
109
+ }
110
+
111
+ /**
112
+ * One pane.
113
+ *
114
+ * NAMED DEVIATION — two panes, not n. Upstream's `react-resizable-panels`
115
+ * distributes a size array across any number of panels and persists it;
116
+ * `ResizablePanelGroup` here holds ONE split, so the first panel takes the
117
+ * share and the second takes the rest. The parts are upstream's so a layout
118
+ * written against them reads the same; a third panel would simply have no
119
+ * share of its own.
120
+ */
121
+ export const ResizablePanel = component<ResizablePanelProps>((props) => {
122
+ const group = inject(ResizableContext);
123
+ const first = group.claimPanel() === 1;
124
+ return html`<div
125
+ data-slot="resizable-panel"
126
+ class="${() => cn("overflow-hidden", first ? "" : "flex-1", read(props.class))}"
127
+ style="${() => (first ? `flex: 0 0 ${group.size()}%` : "")}"
128
+ >${slot(props.children)}</div>`;
129
+ });
130
+
131
+ export interface ResizableHandleProps {
132
+ /** Show the grip dots. */
133
+ withHandle?: boolean;
134
+ class?: Reactive<string>;
135
+ }
136
+
137
+ /**
138
+ * The divider.
139
+ *
140
+ * `role="separator"` with `aria-valuenow`, and it takes focus: the arrows,
141
+ * Home and End move it. A drag handle that only answers to a pointer is not
142
+ * reachable at all for a keyboard user, and there is no other way to resize.
143
+ */
144
+ export const ResizableHandle = component<ResizableHandleProps>((props) => {
145
+ const group = inject(ResizableContext);
58
146
 
59
- /**
60
- * Convert a pointer position into a percentage of the group.
61
- *
62
- * Measured against the group's own rect on every move rather than a rect
63
- * cached at drag start: the window can be resized mid-drag, and a stale rect
64
- * makes the divider drift away from the pointer.
65
- */
66
147
  function percentAt(event: PointerEvent): number | null {
67
- const root = document.getElementById(rootId);
148
+ const root = document.getElementById(group.rootId);
68
149
  if (root === null) return null;
69
150
  const rect = root.getBoundingClientRect();
70
- const span = horizontal ? rect.width : rect.height;
151
+ const span = group.horizontal ? rect.width : rect.height;
71
152
  if (span === 0) return null;
72
- const offset = horizontal
153
+ const offset = group.horizontal
73
154
  ? event.clientX - rect.left
74
155
  : event.clientY - rect.top;
75
156
  return (offset / span) * 100;
@@ -83,7 +164,7 @@ export const Resizable = component<ResizableProps>((props) => {
83
164
 
84
165
  const onMove = (move: PointerEvent): void => {
85
166
  const percent = percentAt(move);
86
- if (percent !== null) setSize(percent);
167
+ if (percent !== null) group.setSize(percent);
87
168
  };
88
169
  const onUp = (): void => {
89
170
  target.releasePointerCapture(event.pointerId);
@@ -98,67 +179,49 @@ export const Resizable = component<ResizableProps>((props) => {
98
179
  }
99
180
 
100
181
  function onKeyDown(event: KeyboardEvent): void {
101
- const back = horizontal ? "ArrowLeft" : "ArrowUp";
102
- const forward = horizontal ? "ArrowRight" : "ArrowDown";
182
+ const back = group.horizontal ? "ArrowLeft" : "ArrowUp";
183
+ const forward = group.horizontal ? "ArrowRight" : "ArrowDown";
103
184
 
104
185
  if (event.key === back) {
105
186
  event.preventDefault();
106
- setSize(size() - KEYBOARD_STEP);
187
+ group.setSize(group.size() - KEYBOARD_STEP);
107
188
  } else if (event.key === forward) {
108
189
  event.preventDefault();
109
- setSize(size() + KEYBOARD_STEP);
190
+ group.setSize(group.size() + KEYBOARD_STEP);
110
191
  } else if (event.key === "Home") {
111
192
  event.preventDefault();
112
- setSize(min);
193
+ group.setSize(group.min);
113
194
  } else if (event.key === "End") {
114
195
  event.preventDefault();
115
- setSize(max);
196
+ group.setSize(group.max);
116
197
  }
117
198
  }
118
199
 
119
200
  return html`<div
120
- data-slot="resizable-group"
121
- id="${rootId}"
122
- data-direction="${horizontal ? "horizontal" : "vertical"}"
201
+ data-slot="resizable-handle"
202
+ role="separator"
203
+ tabindex="0"
204
+ aria-orientation="${group.horizontal ? "vertical" : "horizontal"}"
205
+ aria-valuemin="${group.min}"
206
+ aria-valuemax="${group.max}"
207
+ aria-valuenow="${() => Math.round(group.size())}"
208
+ aria-label="Resize panes"
123
209
  class="${() =>
124
210
  cn(
125
- "flex h-full w-full",
126
- horizontal ? "flex-row" : "flex-col",
127
- read(props.class),
128
- )}"
129
- >
130
- <div
131
- data-slot="resizable-panel"
132
- class="overflow-hidden"
133
- style="${() => `flex: 0 0 ${size()}%`}"
134
- >${slot(props.first)}</div>
135
- <div
136
- data-slot="resizable-handle"
137
- role="separator"
138
- tabindex="0"
139
- aria-orientation="${horizontal ? "vertical" : "horizontal"}"
140
- aria-valuemin="${min}"
141
- aria-valuemax="${max}"
142
- aria-valuenow="${() => Math.round(size())}"
143
- aria-label="Resize panes"
144
- class="${cn(
145
211
  "bg-border relative flex shrink-0 items-center justify-center outline-none",
146
212
  "focus-visible:ring-ring focus-visible:ring-1 focus-visible:ring-offset-1",
147
- horizontal ? "w-px cursor-col-resize" : "h-px cursor-row-resize",
213
+ group.horizontal ? "w-px cursor-col-resize" : "h-px cursor-row-resize",
214
+ read(props.class),
148
215
  )}"
149
- @pointerdown="${onPointerDown}"
150
- @keydown="${onKeyDown}"
151
- >
152
- ${
153
- props.withHandle === true
154
- ? html`<span
155
- class="bg-border z-10 flex h-4 w-3 items-center justify-center rounded-xs border"
156
- >${GripVerticalIcon({ class: "size-2.5" })}</span>`
157
- : null
158
- }
159
- </div>
160
- <div data-slot="resizable-panel" class="flex-1 overflow-hidden">${slot(props.second)}</div>
161
- </div>`;
216
+ @pointerdown="${onPointerDown}"
217
+ @keydown="${onKeyDown}"
218
+ >${
219
+ props.withHandle === true
220
+ ? html`<span
221
+ class="bg-border z-10 flex h-4 w-3 items-center justify-center rounded-xs border"
222
+ >${GripVerticalIcon({ class: "size-2.5" })}</span>`
223
+ : null
224
+ }</div>`;
162
225
  });
163
226
 
164
227
  function clamp(value: number, min: number, max: number): number {
@@ -1,9 +1,21 @@
1
1
  /**
2
2
  * Tabs — one panel visible at a time.
3
3
  *
4
+ * Tabs({ defaultValue: "account", children: () => html`
5
+ * ${TabsList({ children: html`
6
+ * ${TabsTrigger({ value: "account", children: "Account" })}
7
+ * ${TabsTrigger({ value: "password", children: "Password" })}
8
+ * ` })}
9
+ * ${TabsContent({ value: "account", children: "…" })}
10
+ * ${TabsContent({ value: "password", children: "…" })}
11
+ * ` })
12
+ *
4
13
  * The WAI-ARIA tabs pattern in full: the tab list is a single tab stop with
5
14
  * arrows moving between tabs, each tab points at its panel with
6
- * `aria-controls`, and each panel points back with `aria-labelledby`.
15
+ * `aria-controls`, and each panel points back with `aria-labelledby`. A
16
+ * trigger and its panel find each other through the shared `value` — the ids
17
+ * are minted once per value by the context, so neither has to know the other
18
+ * exists.
7
19
  *
8
20
  * Activation is automatic — moving to a tab selects it — which is the right
9
21
  * default when panels are already rendered and switching costs nothing. The
@@ -16,66 +28,166 @@
16
28
  * without also removing them from the DOM.
17
29
  */
18
30
 
19
- import { component, html, onMount, onUnmount, signal } from "@c9up/aurora";
20
- import type { Child, Slot } from "../lib/children.js";
31
+ import {
32
+ component,
33
+ createContext,
34
+ html,
35
+ inject,
36
+ onMount,
37
+ onUnmount,
38
+ provide,
39
+ signal,
40
+ } from "@c9up/aurora";
41
+ import type { Parts, Slot } from "../lib/children.js";
21
42
  import { slot } from "../lib/children.js";
22
43
  import { cn } from "../lib/cn.js";
44
+ import { cva, type VariantProps } from "../lib/cva.js";
23
45
  import { uid } from "../lib/id.js";
24
46
  import { type Reactive, read } from "../lib/props.js";
25
47
  import { type RovingFocus, rovingFocus } from "../primitives/rovingFocus.js";
26
48
 
27
- export interface TabItem {
28
- value: string;
29
- label: Child;
30
- content: Slot;
31
- disabled?: boolean;
49
+ export const tabsListVariants = cva(
50
+ "group/tabs-list text-muted-foreground inline-flex w-fit items-center justify-center rounded-lg p-[3px] group-data-[orientation=horizontal]/tabs:h-9 group-data-[orientation=vertical]/tabs:h-fit group-data-[orientation=vertical]/tabs:flex-col data-[variant=line]:rounded-none",
51
+ {
52
+ variants: {
53
+ variant: {
54
+ default: "bg-muted",
55
+ line: "gap-1 bg-transparent",
56
+ },
57
+ },
58
+ defaultVariants: { variant: "default" },
59
+ },
60
+ );
61
+
62
+ export type TabsListVariants = VariantProps<typeof tabsListVariants>;
63
+
64
+ const triggerClasses =
65
+ "relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap text-foreground/60 transition-all group-data-[orientation=vertical]/tabs:w-full group-data-[orientation=vertical]/tabs:justify-start hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-50 group-data-[variant=default]/tabs-list:data-[state=active]:shadow-sm group-data-[variant=line]/tabs-list:data-[state=active]:shadow-none dark:text-muted-foreground dark:hover:text-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4";
66
+
67
+ /** The filled look: the active tab gets a surface of its own. */
68
+ const triggerSurfaceClasses =
69
+ "data-[state=active]:bg-background data-[state=active]:text-foreground dark:data-[state=active]:border-input dark:data-[state=active]:bg-input/30 dark:data-[state=active]:text-foreground group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-[state=active]:bg-transparent dark:group-data-[variant=line]/tabs-list:data-[state=active]:border-transparent dark:group-data-[variant=line]/tabs-list:data-[state=active]:bg-transparent";
70
+
71
+ /** The underlined look: a rule along the active tab's edge, faded in. */
72
+ const triggerUnderlineClasses =
73
+ "after:absolute after:bg-foreground after:opacity-0 after:transition-opacity group-data-[orientation=horizontal]/tabs:after:inset-x-0 group-data-[orientation=horizontal]/tabs:after:bottom-[-5px] group-data-[orientation=horizontal]/tabs:after:h-0.5 group-data-[orientation=vertical]/tabs:after:inset-y-0 group-data-[orientation=vertical]/tabs:after:-right-1 group-data-[orientation=vertical]/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-[state=active]:after:opacity-100";
74
+
75
+ interface TabIds {
76
+ readonly tab: string;
77
+ readonly panel: string;
32
78
  }
33
79
 
80
+ interface TabsApi {
81
+ readonly orientation: "horizontal" | "vertical";
82
+ readonly activateOnFocus: boolean;
83
+ readonly active: () => string;
84
+ select(value: string): void;
85
+ /**
86
+ * This Tabs' ids for a value.
87
+ *
88
+ * Minted on first ask and kept, so a trigger and its panel — built at
89
+ * different moments and knowing only the value they share — name the same
90
+ * pair. Per instance, because ids must be unique on a page.
91
+ */
92
+ ids(value: string): TabIds;
93
+ /**
94
+ * A trigger announcing itself, so the first ENABLED one becomes the default
95
+ * when the caller named none. A disabled tab selected by default shows a
96
+ * panel nothing can navigate back to.
97
+ */
98
+ registerTrigger(value: string, disabled: boolean): void;
99
+ }
100
+
101
+ const TabsContext = createContext<TabsApi>("Tabs");
102
+
34
103
  export interface TabsProps {
35
- items: readonly TabItem[];
36
- /** Selected at first render. Defaults to the first enabled tab. */
104
+ /** Selected at first render. Defaults to the first trigger built. */
37
105
  defaultValue?: string;
106
+ value?: Reactive<string | undefined>;
38
107
  orientation?: "horizontal" | "vertical";
39
108
  /** Selecting follows focus. Default `true`. */
40
109
  activateOnFocus?: boolean;
41
110
  onValueChange?: (value: string) => void;
42
111
  class?: Reactive<string>;
43
- listClass?: Reactive<string>;
112
+ children?: Parts;
44
113
  }
45
114
 
46
115
  export const Tabs = component<TabsProps>((props) => {
47
116
  const orientation = props.orientation ?? "horizontal";
48
- const first = props.items.find((item) => item.disabled !== true);
49
- const active = signal(props.defaultValue ?? first?.value ?? "");
50
- const listId = uid("tabs-list");
117
+ const internal = signal(props.defaultValue ?? "");
118
+ const ids = new Map<string, TabIds>();
51
119
 
52
- // Ids are minted once per tab, up front, because a tab and its panel each
53
- // need to name the other — one of them has to know both before rendering.
54
- const ids = new Map<string, { tab: string; panel: string }>();
55
- for (const item of props.items) {
56
- ids.set(item.value, { tab: uid("tab"), panel: uid("tab-panel") });
120
+ function active(): string {
121
+ const external = read(props.value);
122
+ return external === undefined ? internal() : external;
57
123
  }
58
124
 
59
125
  function select(value: string): void {
60
126
  if (active() === value) return;
61
- active(value);
127
+ internal(value);
62
128
  props.onValueChange?.(value);
63
129
  }
64
130
 
131
+ provide<TabsApi>(TabsContext, {
132
+ orientation,
133
+ activateOnFocus: props.activateOnFocus !== false,
134
+ active,
135
+ select,
136
+ ids(value) {
137
+ let found = ids.get(value);
138
+ if (found === undefined) {
139
+ found = { tab: uid("tab"), panel: uid("tab-panel") };
140
+ ids.set(value, found);
141
+ }
142
+ return found;
143
+ },
144
+ registerTrigger(value, disabled) {
145
+ if (disabled) return;
146
+ if (props.defaultValue !== undefined || internal() !== "") return;
147
+ internal(value);
148
+ },
149
+ });
150
+
151
+ return html`<div
152
+ data-slot="tabs"
153
+ data-orientation="${orientation}"
154
+ class="${() =>
155
+ cn(
156
+ "group/tabs flex gap-2 data-[orientation=horizontal]:flex-col",
157
+ read(props.class),
158
+ )}"
159
+ >${props.children?.()}</div>`;
160
+ });
161
+
162
+ export interface TabsListProps extends TabsListVariants {
163
+ children?: Slot;
164
+ class?: Reactive<string>;
165
+ }
166
+
167
+ /**
168
+ * The row of triggers, and the single tab stop that holds them.
169
+ *
170
+ * `rovingFocus` is wired here rather than in `Tabs`: it needs the element the
171
+ * tabs are actually in, and only this part has one.
172
+ */
173
+ export const TabsList = component<TabsListProps>((props) => {
174
+ const tabs = inject(TabsContext);
175
+ const listId = uid("tabs-list");
65
176
  let group: RovingFocus | undefined;
177
+
66
178
  onMount(() => {
67
179
  group = rovingFocus({
68
180
  container: () => document.getElementById(listId),
69
181
  itemSelector: "[role='tab']",
70
- orientation,
182
+ orientation: tabs.orientation,
71
183
  onFocusChange: (item) => {
72
- if (props.activateOnFocus === false) return;
184
+ if (!tabs.activateOnFocus) return;
73
185
  const value = item.getAttribute("data-value");
74
- if (value !== null) select(value);
186
+ if (value !== null) tabs.select(value);
75
187
  },
76
188
  onSelect: (item) => {
77
189
  const value = item.getAttribute("data-value");
78
- if (value !== null) select(value);
190
+ if (value !== null) tabs.select(value);
79
191
  },
80
192
  });
81
193
  group.sync();
@@ -83,66 +195,67 @@ export const Tabs = component<TabsProps>((props) => {
83
195
  onUnmount(() => group?.destroy());
84
196
 
85
197
  return html`<div
86
- data-slot="tabs"
87
- data-orientation="${orientation}"
88
- class="${() => cn("flex gap-2", orientation === "vertical" ? "flex-row" : "flex-col", read(props.class))}"
89
- >
90
- <div
91
- role="tablist"
92
- data-slot="tabs-list"
93
- id="${listId}"
94
- aria-orientation="${orientation}"
95
- class="${() =>
96
- cn(
97
- "bg-muted text-muted-foreground inline-flex w-fit items-center justify-center rounded-lg p-[3px]",
98
- orientation === "vertical" ? "flex-col" : "h-9",
99
- read(props.listClass),
100
- )}"
101
- >
102
- ${props.items.map((item) => renderTab(item, ids, active, select))}
103
- </div>
104
- ${props.items.map((item) => renderPanel(item, ids, active))}
105
- </div>`;
198
+ data-slot="tabs-list"
199
+ id="${listId}"
200
+ role="tablist"
201
+ data-variant="${props.variant ?? "default"}"
202
+ aria-orientation="${tabs.orientation}"
203
+ class="${() =>
204
+ cn(tabsListVariants({ variant: props.variant }), read(props.class))}"
205
+ >${slot(props.children)}</div>`;
106
206
  });
107
207
 
108
- function renderTab(
109
- item: TabItem,
110
- ids: ReadonlyMap<string, { tab: string; panel: string }>,
111
- active: () => string,
112
- select: (value: string) => void,
113
- ): Child {
114
- const id = ids.get(item.value);
115
- const selected = (): boolean => active() === item.value;
208
+ export interface TabsTriggerProps {
209
+ value: string;
210
+ children?: Slot;
211
+ disabled?: boolean;
212
+ class?: Reactive<string>;
213
+ }
214
+
215
+ export const TabsTrigger = component<TabsTriggerProps>((props) => {
216
+ const tabs = inject(TabsContext);
217
+ const ids = tabs.ids(props.value);
218
+ tabs.registerTrigger(props.value, props.disabled === true);
219
+ const selected = (): boolean => tabs.active() === props.value;
116
220
 
117
221
  return html`<button
118
222
  type="button"
119
- role="tab"
120
223
  data-slot="tabs-trigger"
121
- data-value="${item.value}"
224
+ id="${ids.tab}"
225
+ role="tab"
226
+ data-value="${props.value}"
122
227
  data-state="${() => (selected() ? "active" : "inactive")}"
123
- data-disabled="${item.disabled === true ? "" : undefined}"
124
- id="${id?.tab}"
228
+ data-disabled="${props.disabled === true ? "" : undefined}"
125
229
  aria-selected="${() => (selected() ? "true" : "false")}"
126
- aria-controls="${id?.panel}"
127
- ?disabled="${item.disabled === true}"
128
- class="data-[state=active]:bg-background data-[state=active]:text-foreground focus-visible:border-ring focus-visible:ring-ring/50 text-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[3px] focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0"
129
- @click="${() => select(item.value)}"
130
- >${item.label}</button>`;
230
+ aria-controls="${ids.panel}"
231
+ ?disabled="${props.disabled === true}"
232
+ class="${() =>
233
+ cn(
234
+ triggerClasses,
235
+ triggerSurfaceClasses,
236
+ triggerUnderlineClasses,
237
+ read(props.class),
238
+ )}"
239
+ @click="${() => tabs.select(props.value)}"
240
+ >${slot(props.children)}</button>`;
241
+ });
242
+
243
+ export interface TabsContentProps {
244
+ value: string;
245
+ children?: Slot;
246
+ class?: Reactive<string>;
131
247
  }
132
248
 
133
- function renderPanel(
134
- item: TabItem,
135
- ids: ReadonlyMap<string, { tab: string; panel: string }>,
136
- active: () => string,
137
- ): Child {
138
- const id = ids.get(item.value);
249
+ export const TabsContent = component<TabsContentProps>((props) => {
250
+ const tabs = inject(TabsContext);
251
+ const ids = tabs.ids(props.value);
139
252
  return html`<div
140
- role="tabpanel"
141
253
  data-slot="tabs-content"
142
- id="${id?.panel}"
143
- aria-labelledby="${id?.tab}"
254
+ id="${ids.panel}"
255
+ role="tabpanel"
256
+ aria-labelledby="${ids.tab}"
144
257
  tabindex="0"
145
- ?hidden="${() => active() !== item.value}"
146
- class="flex-1 outline-none"
147
- >${slot(item.content)}</div>`;
148
- }
258
+ ?hidden="${() => tabs.active() !== props.value}"
259
+ class="${() => cn("flex-1 outline-none", read(props.class))}"
260
+ >${slot(props.children)}</div>`;
261
+ });