@c9up/nebula 0.1.8 → 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 +5 -4
  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
@@ -15,7 +15,7 @@
15
15
  * jump to the top of the page after choosing.
16
16
  */
17
17
 
18
- import { component, html, signal } from "@c9up/aurora";
18
+ import { component, html, signal, type TemplateResult } from "@c9up/aurora";
19
19
  import { cn } from "../lib/cn.js";
20
20
  import { ChevronsUpDownIcon } from "../lib/icons.js";
21
21
  import { uid } from "../lib/id.js";
@@ -24,7 +24,14 @@ import { type Reactive, read, readOr } from "../lib/props.js";
24
24
  import { controllable } from "../primitives/controllable.js";
25
25
  import { floatingSurface } from "../primitives/floatingSurface.js";
26
26
  import { focusSilently } from "../primitives/focusable.js";
27
- import { Command, type CommandItem } from "./Command.js";
27
+ import {
28
+ Command,
29
+ CommandEmpty,
30
+ CommandGroup,
31
+ CommandInput,
32
+ CommandItem,
33
+ CommandList,
34
+ } from "./Command.js";
28
35
  import { selectTriggerClasses } from "./Select.js";
29
36
 
30
37
  export interface ComboboxOption {
@@ -69,21 +76,64 @@ export const Combobox = component<ComboboxProps>((props) => {
69
76
  return props.options.find((option) => option.value === value)?.label;
70
77
  }
71
78
 
72
- const items = (): readonly CommandItem[] =>
73
- props.options.map((option) => ({
74
- value: option.value,
75
- label: option.label,
76
- keywords: option.keywords,
77
- disabled: option.disabled,
78
- group: option.group,
79
- }));
80
-
81
- function choose(item: CommandItem): void {
82
- selection.set(item.value);
79
+ function choose(value: string): void {
80
+ selection.set(value);
83
81
  open(false);
84
82
  focusSilently(document.getElementById(triggerId));
85
83
  }
86
84
 
85
+ /**
86
+ * The palette inside the popover.
87
+ *
88
+ * Built fresh on each open, so the options prop is read at the moment the
89
+ * surface appears rather than captured once — a combobox whose options load
90
+ * asynchronously is the common case.
91
+ *
92
+ * Consecutive options sharing a `group` are gathered under one heading, in
93
+ * the order they were given. A group that repeats after an interruption
94
+ * gets a second heading, because reordering the caller's list to merge them
95
+ * would change which option the arrows reach next.
96
+ */
97
+ function renderPalette(): TemplateResult {
98
+ const groups: Array<{
99
+ heading: string | undefined;
100
+ options: ComboboxOption[];
101
+ }> = [];
102
+ for (const option of props.options) {
103
+ const last = groups[groups.length - 1];
104
+ if (last !== undefined && last.heading === option.group) {
105
+ last.options.push(option);
106
+ } else {
107
+ groups.push({ heading: option.group, options: [option] });
108
+ }
109
+ }
110
+ return Command({
111
+ onSelect: choose,
112
+ children: () =>
113
+ html`${CommandInput({
114
+ placeholder: props.searchPlaceholder ?? "Search…",
115
+ })}${CommandList({
116
+ children: () =>
117
+ html`${CommandEmpty({
118
+ children: props.emptyMessage ?? "No results found.",
119
+ })}${groups.map((group) =>
120
+ CommandGroup({
121
+ heading: group.heading,
122
+ children: () =>
123
+ group.options.map((option) =>
124
+ CommandItem({
125
+ value: option.value,
126
+ children: option.label,
127
+ keywords: option.keywords,
128
+ disabled: option.disabled,
129
+ }),
130
+ ),
131
+ }),
132
+ )}`,
133
+ })}`,
134
+ });
135
+ }
136
+
87
137
  floatingSurface({
88
138
  anchor: () => document.getElementById(triggerId),
89
139
  open: () => open(),
@@ -106,12 +156,7 @@ export const Combobox = component<ComboboxProps>((props) => {
106
156
  read(props.contentClass),
107
157
  )}"
108
158
  >
109
- ${Command({
110
- items: items(),
111
- placeholder: props.searchPlaceholder ?? "Search…",
112
- emptyMessage: props.emptyMessage ?? "No results found.",
113
- onSelect: choose,
114
- })}
159
+ ${renderPalette()}
115
160
  </div>`,
116
161
  });
117
162
 
@@ -1,84 +1,160 @@
1
1
  /**
2
- * Command — a searchable list of actions.
2
+ * Command — the palette: type to filter, arrows to move, Enter to run.
3
3
  *
4
- * The palette pattern: type to filter, arrows to move, Enter to run. Used on
5
- * its own — inside a Popover, a Sheet, or a page — while `CommandDialog` wraps
6
- * this same component in a modal for the full-screen palette.
4
+ * Command({ children: () => html`
5
+ * ${CommandInput({ placeholder: "Search…" })}
6
+ * ${CommandList({ children: () => html`
7
+ * ${CommandEmpty({ children: "Nothing found." })}
8
+ * ${CommandGroup({ heading: "Actions", children: () => html`
9
+ * ${CommandItem({ value: "new", children: "New file", onSelect: create })}
10
+ * ` })}
11
+ * ` })}
12
+ * ` })
7
13
  *
8
- * The filter is the interesting decision. `cmdk`, which shadcn wraps, scores
9
- * matches with a fuzzy algorithm; nebula matches on substring, plus an
10
- * optional `keywords` list per item. Fuzzy matching is impressive on a demo
11
- * and frustrating in use — it surfaces items the user did not type anything
12
- * resembling, and the ranking is impossible to predict. Substring plus
13
- * keywords is predictable, and "s" reaching "Settings" is a keyword, not an
14
- * algorithm.
14
+ * Filtering happens WITHOUT re-rendering. Each item registers itself as it is
15
+ * built and then hides itself when the query stops matching — Aurora never
16
+ * re-runs a component's setup, so a palette that rebuilt its list on every
17
+ * keystroke would lose the DOM the highlight and the scroll position live in.
18
+ * A group hides itself the same way, once none of its own items are showing,
19
+ * so an empty heading never sits over nothing.
15
20
  *
16
- * Focus stays in the search input the whole time; the highlighted item is
17
- * named with `aria-activedescendant`. Moving DOM focus onto the list would
18
- * take it out of the input and the next keystroke would go nowhere.
21
+ * `role="combobox"` on the input with `aria-activedescendant`, and focus never
22
+ * leaves it. The list is a `listbox` whose options are named rather than
23
+ * focused, which is what lets a screen reader read each result as the arrows
24
+ * pass over it while typing continues to work.
19
25
  */
20
26
 
21
- import { component, html, signal } from "@c9up/aurora";
22
- import type { Child } from "../lib/children.js";
27
+ import {
28
+ component,
29
+ createContext,
30
+ html,
31
+ inject,
32
+ provide,
33
+ signal,
34
+ } from "@c9up/aurora";
35
+ import type { Parts, Slot } from "../lib/children.js";
36
+ import { slot } from "../lib/children.js";
23
37
  import { cn } from "../lib/cn.js";
24
38
  import { SearchIcon } from "../lib/icons.js";
25
39
  import { uid } from "../lib/id.js";
26
40
  import { type Reactive, read } from "../lib/props.js";
27
41
 
28
- export interface CommandItem {
29
- /** Stable key. */
30
- value: string;
31
- label: string;
32
- icon?: Child;
42
+ /** What the filter is given. */
43
+ export interface CommandCandidate {
44
+ readonly value: string;
45
+ /** The visible text, or `textValue` when the children are markup. */
46
+ readonly textValue: string;
33
47
  /** Extra terms that should match this item. */
34
- keywords?: readonly string[];
35
- shortcut?: string;
36
- disabled?: boolean;
37
- group?: string;
38
- onSelect?: () => void;
48
+ readonly keywords: readonly string[];
39
49
  }
40
50
 
41
- export interface CommandProps {
42
- items: readonly CommandItem[];
43
- placeholder?: string;
44
- /** Shown when the filter matches nothing. */
45
- emptyMessage?: Child;
46
- /** Replace the built-in substring filter. */
47
- filter?: (item: CommandItem, query: string) => boolean;
48
- class?: Reactive<string>;
49
- onSelect?: (item: CommandItem) => void;
50
- }
51
-
52
- /** Substring over the label, then over each keyword. */
53
- export function defaultFilter(item: CommandItem, query: string): boolean {
51
+ export function defaultFilter(
52
+ candidate: CommandCandidate,
53
+ query: string,
54
+ ): boolean {
54
55
  if (query === "") return true;
55
56
  const needle = query.toLowerCase();
56
- if (item.label.toLowerCase().includes(needle)) return true;
57
- return (item.keywords ?? []).some((keyword) =>
57
+ if (candidate.textValue.toLowerCase().includes(needle)) return true;
58
+ return candidate.keywords.some((keyword) =>
58
59
  keyword.toLowerCase().includes(needle),
59
60
  );
60
61
  }
61
62
 
63
+ interface RegisteredItem extends CommandCandidate {
64
+ readonly id: string;
65
+ readonly disabled: boolean;
66
+ readonly groupId: string | undefined;
67
+ run(): void;
68
+ }
69
+
70
+ interface CommandApi {
71
+ readonly inputId: string;
72
+ readonly listId: string;
73
+ readonly query: () => string;
74
+ readonly active: () => string | undefined;
75
+ setActive(value: string | undefined): void;
76
+ register(item: Omit<RegisteredItem, "id">): void;
77
+ /** This palette's element id for a value. Per instance: ids are page-wide. */
78
+ itemId(value: string): string;
79
+ /** Does this item survive the current query? */
80
+ isVisible(value: string): boolean;
81
+ /** Does this group have anything left to show? */
82
+ groupHasMatches(groupId: string): boolean;
83
+ readonly hasMatches: () => boolean;
84
+ run(value: string): void;
85
+ onInput(event: Event): void;
86
+ onKeyDown(event: KeyboardEvent): void;
87
+ }
88
+
89
+ const CommandContext = createContext<CommandApi>("Command");
90
+
91
+ interface CommandGroupApi {
92
+ readonly groupId: string;
93
+ }
94
+
95
+ /**
96
+ * Which group an item belongs to.
97
+ *
98
+ * Defaulted rather than required: an item outside any group is ordinary, so
99
+ * there is nothing to raise about.
100
+ */
101
+ const CommandGroupContext = createContext<CommandGroupApi | undefined>(
102
+ "CommandGroup",
103
+ undefined,
104
+ );
105
+
106
+ export interface CommandProps {
107
+ /** Replace the built-in substring filter. */
108
+ filter?: (candidate: CommandCandidate, query: string) => boolean;
109
+ onSelect?: (value: string) => void;
110
+ class?: Reactive<string>;
111
+ children?: Parts;
112
+ }
113
+
62
114
  export const Command = component<CommandProps>((props) => {
63
115
  const inputId = uid("command-input");
64
116
  const listId = uid("command-list");
117
+ const items: RegisteredItem[] = [];
118
+ /** Per instance, not per module: two palettes may offer the same value. */
65
119
  const itemIds = new Map<string, string>();
66
- for (const item of props.items) itemIds.set(item.value, uid("command-item"));
67
-
68
120
  const query = signal("");
69
- const active = signal<string | undefined>(undefined);
70
121
 
71
- function matches(): readonly CommandItem[] {
122
+ function itemId(value: string): string {
123
+ let id = itemIds.get(value);
124
+ if (id === undefined) {
125
+ id = uid("command-item");
126
+ itemIds.set(value, id);
127
+ }
128
+ return id;
129
+ }
130
+
131
+ /**
132
+ * The highlighted value, or `undefined` for "the first match".
133
+ *
134
+ * DERIVED rather than seeded at registration. Writing it while the items
135
+ * registered would be a signal write during the render that is building
136
+ * them, and a surface rebuilds its content inside an effect — the write
137
+ * re-entered that effect and the palette recursed until the stack gave out.
138
+ * Nothing needs the value stored before the user moves.
139
+ */
140
+ const explicitActive = signal<string | undefined>(undefined);
141
+
142
+ function active(): string | undefined {
143
+ const chosen = explicitActive();
144
+ if (chosen !== undefined) return chosen;
145
+ return matches()[0]?.value;
146
+ }
147
+
148
+ function matches(): readonly RegisteredItem[] {
72
149
  const filter = props.filter ?? defaultFilter;
73
- return props.items.filter(
74
- (item) => item.disabled !== true && filter(item, query()),
75
- );
150
+ return items.filter((item) => !item.disabled && filter(item, query()));
76
151
  }
77
152
 
78
- function run(item: CommandItem): void {
79
- if (item.disabled === true) return;
80
- item.onSelect?.();
81
- props.onSelect?.(item);
153
+ function run(value: string): void {
154
+ const item = items.find((entry) => entry.value === value);
155
+ if (item === undefined || item.disabled) return;
156
+ item.run();
157
+ props.onSelect?.(value);
82
158
  }
83
159
 
84
160
  function move(delta: number): void {
@@ -89,40 +165,52 @@ export const Command = component<CommandProps>((props) => {
89
165
  // Wrapping is right here, unlike in a menu: a palette is a search result
90
166
  // list and the user is scanning it, not navigating a fixed structure.
91
167
  const target = list[(next + list.length) % list.length];
92
- if (target !== undefined) {
93
- active(target.value);
94
- scrollIntoView(target.value);
95
- }
96
- }
97
-
98
- function scrollIntoView(value: string): void {
99
- const id = itemIds.get(value);
100
- if (id === undefined) return;
101
- document.getElementById(id)?.scrollIntoView({ block: "nearest" });
102
- }
103
-
104
- function onKeyDown(event: KeyboardEvent): void {
105
- if (event.key === "ArrowDown") {
106
- event.preventDefault();
107
- move(1);
108
- } else if (event.key === "ArrowUp") {
109
- event.preventDefault();
110
- move(-1);
111
- } else if (event.key === "Enter") {
112
- event.preventDefault();
113
- const current = matches().find((item) => item.value === active());
114
- if (current !== undefined) run(current);
115
- }
168
+ if (target === undefined) return;
169
+ explicitActive(target.value);
170
+ document.getElementById(target.id)?.scrollIntoView({ block: "nearest" });
116
171
  }
117
172
 
118
- function onInput(event: Event): void {
119
- const target = event.target;
120
- if (!(target instanceof HTMLInputElement)) return;
121
- query(target.value);
122
- // Re-point the highlight at the first result: leaving it on an item the
123
- // new query filtered out means Enter runs something invisible.
124
- active(matches()[0]?.value);
125
- }
173
+ provide<CommandApi>(CommandContext, {
174
+ inputId,
175
+ listId,
176
+ query: () => query(),
177
+ active,
178
+ setActive: (value) => explicitActive(value),
179
+ itemId,
180
+ register(item) {
181
+ items.push({ ...item, id: itemId(item.value) });
182
+ },
183
+ isVisible(value) {
184
+ return matches().some((item) => item.value === value);
185
+ },
186
+ groupHasMatches(groupId) {
187
+ return matches().some((item) => item.groupId === groupId);
188
+ },
189
+ hasMatches: () => matches().length > 0,
190
+ run,
191
+ onInput(event) {
192
+ const target = event.target;
193
+ if (!(target instanceof HTMLInputElement)) return;
194
+ query(target.value);
195
+ // Back to "the first match": leaving the highlight on an item the new
196
+ // query filtered out means Enter runs something invisible.
197
+ explicitActive(undefined);
198
+ },
199
+ onKeyDown(event) {
200
+ if (event.key === "ArrowDown") {
201
+ event.preventDefault();
202
+ move(1);
203
+ } else if (event.key === "ArrowUp") {
204
+ event.preventDefault();
205
+ move(-1);
206
+ } else if (event.key === "Enter") {
207
+ event.preventDefault();
208
+ const current = active();
209
+ if (current !== undefined && matches().some((i) => i.value === current))
210
+ run(current);
211
+ }
212
+ },
213
+ });
126
214
 
127
215
  return html`<div
128
216
  data-slot="command"
@@ -131,81 +219,176 @@ export const Command = component<CommandProps>((props) => {
131
219
  "bg-popover text-popover-foreground flex size-full flex-col overflow-hidden rounded-md",
132
220
  read(props.class),
133
221
  )}"
134
- >
135
- <div data-slot="command-input-wrapper" class="flex h-9 items-center gap-2 border-b px-3">
136
- ${SearchIcon({ class: "size-4 shrink-0 opacity-50" })}
137
- <input
138
- data-slot="command-input"
139
- id="${inputId}"
140
- type="text"
141
- role="combobox"
142
- autocomplete="off"
143
- aria-expanded="true"
144
- aria-controls="${listId}"
145
- aria-activedescendant="${() => {
146
- const current = active();
147
- return current === undefined ? undefined : itemIds.get(current);
148
- }}"
149
- placeholder="${props.placeholder ?? "Type a command or search…"}"
150
- class="placeholder:text-muted-foreground flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-none disabled:cursor-not-allowed disabled:opacity-50"
151
- @input="${onInput}"
152
- @keydown="${onKeyDown}"
153
- />
154
- </div>
155
- <div
156
- data-slot="command-list"
157
- id="${listId}"
158
- role="listbox"
159
- class="max-h-[300px] scroll-py-1 overflow-x-hidden overflow-y-auto p-1"
160
- >
161
- ${() => renderResults()}
162
- </div>
163
- </div>`;
164
-
165
- function renderResults(): Child {
166
- const list = matches();
167
- if (list.length === 0) {
168
- return html`<div
169
- data-slot="command-empty"
170
- role="presentation"
171
- class="py-6 text-center text-sm"
172
- >${props.emptyMessage ?? "No results found."}</div>`;
173
- }
222
+ >${props.children?.()}</div>`;
223
+ });
174
224
 
175
- let lastGroup: string | undefined;
176
- return list.map((item) => {
177
- const heading =
178
- item.group !== undefined && item.group !== lastGroup
179
- ? html`<div
180
- role="presentation"
181
- class="text-muted-foreground px-2 py-1.5 text-xs font-medium"
182
- >${item.group}</div>`
183
- : null;
184
- lastGroup = item.group;
185
- return [heading, renderItem(item)];
186
- });
187
- }
225
+ export interface CommandInputProps {
226
+ placeholder?: string;
227
+ class?: Reactive<string>;
228
+ }
188
229
 
189
- function renderItem(item: CommandItem): Child {
190
- return html`<div
191
- role="option"
192
- data-slot="command-item"
193
- id="${itemIds.get(item.value)}"
194
- aria-selected="${() => (active() === item.value ? "true" : "false")}"
195
- data-active="${() => (active() === item.value ? "" : undefined)}"
196
- class="relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none data-[active]:bg-accent data-[active]:text-accent-foreground [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4"
197
- @click="${() => run(item)}"
198
- @pointerenter="${() => active(item.value)}"
199
- >
200
- ${item.icon}
201
- <span class="flex-1 truncate">${item.label}</span>
202
- ${
203
- item.shortcut === undefined
204
- ? null
205
- : html`<span class="text-muted-foreground ml-auto text-xs tracking-widest"
206
- >${item.shortcut}</span
207
- >`
208
- }
209
- </div>`;
210
- }
230
+ export const CommandInput = component<CommandInputProps>((props) => {
231
+ const command = inject(CommandContext);
232
+ return html`<div
233
+ data-slot="command-input-wrapper"
234
+ class="flex h-9 items-center gap-2 border-b px-3"
235
+ >${SearchIcon({ class: "size-4 shrink-0 opacity-50" })}<input
236
+ data-slot="command-input"
237
+ id="${command.inputId}"
238
+ type="text"
239
+ role="combobox"
240
+ autocomplete="off"
241
+ aria-expanded="true"
242
+ aria-controls="${command.listId}"
243
+ aria-activedescendant="${() => {
244
+ const current = command.active();
245
+ return current === undefined ? undefined : command.itemId(current);
246
+ }}"
247
+ placeholder="${props.placeholder ?? "Type a command or search…"}"
248
+ class="${() =>
249
+ cn(
250
+ "placeholder:text-muted-foreground flex h-10 w-full rounded-md bg-transparent py-3 text-sm outline-hidden disabled:cursor-not-allowed disabled:opacity-50",
251
+ read(props.class),
252
+ )}"
253
+ @input="${(event: Event) => command.onInput(event)}"
254
+ @keydown="${(event: KeyboardEvent) => command.onKeyDown(event)}"
255
+ /></div>`;
211
256
  });
257
+
258
+ export interface CommandListProps {
259
+ children?: Parts;
260
+ class?: Reactive<string>;
261
+ }
262
+
263
+ export const CommandList = component<CommandListProps>((props) => {
264
+ const command = inject(CommandContext);
265
+ return html`<div
266
+ data-slot="command-list"
267
+ id="${command.listId}"
268
+ role="listbox"
269
+ class="${() =>
270
+ cn(
271
+ "max-h-[300px] scroll-py-1 overflow-x-hidden overflow-y-auto p-1",
272
+ read(props.class),
273
+ )}"
274
+ >${props.children?.()}</div>`;
275
+ });
276
+
277
+ export interface CommandEmptyProps {
278
+ children?: Slot;
279
+ class?: Reactive<string>;
280
+ }
281
+
282
+ /** Shown when the query matches nothing. Hidden, not removed — see `Command`. */
283
+ export const CommandEmpty = component<CommandEmptyProps>((props) => {
284
+ const command = inject(CommandContext);
285
+ return html`<div
286
+ data-slot="command-empty"
287
+ role="presentation"
288
+ ?hidden="${() => command.hasMatches()}"
289
+ class="${() => cn("py-6 text-center text-sm", read(props.class))}"
290
+ >${slot(props.children)}</div>`;
291
+ });
292
+
293
+ export interface CommandGroupProps {
294
+ heading?: Slot;
295
+ children?: Parts;
296
+ class?: Reactive<string>;
297
+ }
298
+
299
+ export const CommandGroup = component<CommandGroupProps>((props) => {
300
+ const command = inject(CommandContext);
301
+ const groupId = uid("command-group");
302
+ provide<CommandGroupApi | undefined>(CommandGroupContext, { groupId });
303
+ const rows = props.children?.();
304
+ return html`<div
305
+ data-slot="command-group"
306
+ role="group"
307
+ ?hidden="${() => !command.groupHasMatches(groupId)}"
308
+ class="${() => cn("text-foreground overflow-hidden p-1", read(props.class))}"
309
+ >${
310
+ props.heading === undefined
311
+ ? null
312
+ : html`<div
313
+ role="presentation"
314
+ class="text-muted-foreground px-2 py-1.5 text-xs font-medium"
315
+ >${slot(props.heading)}</div>`
316
+ }${rows}</div>`;
317
+ });
318
+
319
+ export interface CommandItemProps {
320
+ value: string;
321
+ children?: Slot;
322
+ /** What the filter matches, when the children are markup. */
323
+ textValue?: string;
324
+ /** Extra terms that should match this item. */
325
+ keywords?: readonly string[];
326
+ disabled?: boolean;
327
+ onSelect?: () => void;
328
+ class?: Reactive<string>;
329
+ }
330
+
331
+ export const CommandItem = component<CommandItemProps>((props) => {
332
+ const command = inject(CommandContext);
333
+ const group = inject(CommandGroupContext);
334
+ const children = props.children;
335
+
336
+ const id = command.itemId(props.value);
337
+ command.register({
338
+ value: props.value,
339
+ textValue:
340
+ props.textValue ??
341
+ (typeof children === "string" ? children : props.value),
342
+ keywords: props.keywords ?? [],
343
+ disabled: props.disabled === true,
344
+ groupId: group?.groupId,
345
+ run: () => props.onSelect?.(),
346
+ });
347
+
348
+ const chosen = (): boolean => command.active() === props.value;
349
+ return html`<div
350
+ data-slot="command-item"
351
+ id="${id}"
352
+ role="option"
353
+ ?hidden="${() => !command.isVisible(props.value)}"
354
+ aria-selected="${() => (chosen() ? "true" : "false")}"
355
+ aria-disabled="${props.disabled === true ? "true" : undefined}"
356
+ data-active="${() => (chosen() ? "" : undefined)}"
357
+ data-disabled="${props.disabled === true ? "" : undefined}"
358
+ class="${() =>
359
+ cn(
360
+ "relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[active]:bg-accent data-[active]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",
361
+ read(props.class),
362
+ )}"
363
+ @click="${() => command.run(props.value)}"
364
+ @pointerenter="${() => command.setActive(props.value)}"
365
+ >${slot(children)}</div>`;
366
+ });
367
+
368
+ export interface CommandSeparatorProps {
369
+ class?: Reactive<string>;
370
+ }
371
+
372
+ export const CommandSeparator = component<CommandSeparatorProps>(
373
+ (props) => html`<div
374
+ data-slot="command-separator"
375
+ role="separator"
376
+ class="${() => cn("bg-border -mx-1 h-px", read(props.class))}"
377
+ ></div>`,
378
+ );
379
+
380
+ export interface CommandShortcutProps {
381
+ children?: Slot;
382
+ class?: Reactive<string>;
383
+ }
384
+
385
+ export const CommandShortcut = component<CommandShortcutProps>(
386
+ (props) => html`<span
387
+ data-slot="command-shortcut"
388
+ class="${() =>
389
+ cn(
390
+ "text-muted-foreground ml-auto text-xs tracking-widest",
391
+ read(props.class),
392
+ )}"
393
+ >${slot(props.children)}</span>`,
394
+ );