@snap-rail/ui 0.2.0 → 0.4.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.
package/lib/settings.js CHANGED
@@ -2,6 +2,10 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
2
  import { Component, createContext, useContext, useEffect, useState, useSyncExternalStore } from "react";
3
3
  import "@radix-ui/react-separator";
4
4
  import { Slot } from "@radix-ui/react-slot";
5
+ import "@radix-ui/react-dropdown-menu";
6
+ import "@radix-ui/react-popover";
7
+ import "@radix-ui/react-tooltip";
8
+ import "sonner";
5
9
  import "@radix-ui/react-checkbox";
6
10
  import "@radix-ui/react-label";
7
11
  import "@radix-ui/react-radio-group";
@@ -12,10 +16,6 @@ import * as DialogPrimitive from "@radix-ui/react-dialog";
12
16
  import "react-hook-form";
13
17
  import "@hookform/resolvers/zod";
14
18
  import "@radix-ui/react-scroll-area";
15
- import "@radix-ui/react-dropdown-menu";
16
- import "@radix-ui/react-popover";
17
- import "@radix-ui/react-toast";
18
- import "@radix-ui/react-tooltip";
19
19
  import "@radix-ui/react-slider";
20
20
  import { rpcErrorText } from "@snap-rail/connection";
21
21
  import "@snap-rail/client-kernel";
@@ -36,7 +36,13 @@ import { PLUGINS_FEED_URLS_KEY } from "@snap-rail/app-boot/contract";
36
36
  function cn(...values) {
37
37
  return values.filter((value) => typeof value === "string" && value.length > 0).join(" ");
38
38
  }
39
- /** The anatomy attributes every semantic component renders. */
39
+ /**
40
+ * The anatomy attributes every semantic component renders. The identity is
41
+ * component-owned: components spread these AFTER caller props so a composite
42
+ * can never mask a host part (a masked `button.root` loses every skin rule
43
+ * and falls back to the UA default look). Composites tag their own wrappers
44
+ * instead of pushing `part()` into a child component's props.
45
+ */
40
46
  function part(component, name) {
41
47
  return {
42
48
  "data-snap-component": component,
@@ -52,11 +58,11 @@ function space(token) {
52
58
  /** An inline status banner for results and failures. */
53
59
  function Alert({ intent = "neutral", className, children, ...props }) {
54
60
  return jsx("div", {
55
- ...part("alert", "root"),
56
61
  role: "alert",
57
62
  className: cn("snap-alert", className),
58
63
  "data-intent": intent,
59
64
  ...props,
65
+ ...part("alert", "root"),
60
66
  children
61
67
  });
62
68
  }
@@ -112,7 +118,6 @@ function Button({ intent = "neutral", emphasis = "solid", size = "md", loading =
112
118
  loading && jsx(Spinner, {})
113
119
  ] });
114
120
  return jsx(Component, {
115
- ...part("button", "root"),
116
121
  ...type !== void 0 ? { type } : !asChild ? { type: "button" } : {},
117
122
  className: cn("snap-button", className),
118
123
  "data-intent": intent,
@@ -123,13 +128,13 @@ function Button({ intent = "neutral", emphasis = "solid", size = "md", loading =
123
128
  "aria-busy": loading || void 0,
124
129
  disabled: !asChild ? disabled === true || loading : void 0,
125
130
  ...props,
131
+ ...part("button", "root"),
126
132
  children: content
127
133
  });
128
134
  }
129
135
  /** A square glyph button with a mandatory accessible label. */
130
136
  function IconButton({ icon, label, active = false, intent = "neutral", emphasis = "ghost", size = "md", className, ...props }) {
131
137
  return jsx("button", {
132
- ...part("icon-button", "root"),
133
138
  type: "button",
134
139
  className: cn("snap-icon-button", className),
135
140
  "data-intent": intent,
@@ -139,6 +144,7 @@ function IconButton({ icon, label, active = false, intent = "neutral", emphasis
139
144
  "aria-label": label,
140
145
  "aria-pressed": active || void 0,
141
146
  ...props,
147
+ ...part("icon-button", "root"),
142
148
  children: jsx("span", {
143
149
  ...part("icon-button", "icon"),
144
150
  "aria-hidden": "true",
@@ -147,6 +153,79 @@ function IconButton({ icon, label, active = false, intent = "neutral", emphasis
147
153
  });
148
154
  }
149
155
  //#endregion
156
+ //#region lib/types/components/input.js
157
+ /** A single-line text field with semantic prefix/suffix slots. Layout
158
+ * `style`/`className` dress the wrapper — the visible control; the native
159
+ * field already stretches inside it. */
160
+ function Input({ size = "md", invalid = false, prefix, suffix, onClear, fieldClassName, componentName = "input", className, style, disabled, ...props }) {
161
+ return jsxs("div", {
162
+ ...part(componentName, "root"),
163
+ className: cn("snap-input", className),
164
+ style,
165
+ "data-size": size,
166
+ "data-invalid": invalid || void 0,
167
+ "data-disabled": disabled === true || void 0,
168
+ children: [
169
+ prefix !== void 0 && jsx("span", {
170
+ ...part(componentName, "prefix"),
171
+ children: prefix
172
+ }),
173
+ jsx("input", {
174
+ className: cn("snap-input-field", fieldClassName),
175
+ disabled,
176
+ "aria-invalid": invalid || void 0,
177
+ ...props,
178
+ ...part(componentName, "field")
179
+ }),
180
+ onClear !== void 0 && typeof props.value === "string" && props.value !== "" && jsx("button", {
181
+ ...part(componentName, "clear"),
182
+ type: "button",
183
+ className: "snap-input-clear",
184
+ "aria-label": "清除",
185
+ onClick: onClear,
186
+ children: "×"
187
+ }),
188
+ suffix !== void 0 && jsx("span", {
189
+ ...part(componentName, "suffix"),
190
+ children: suffix
191
+ })
192
+ ]
193
+ });
194
+ }
195
+ jsxs(Fragment, { children: [
196
+ jsx("circle", {
197
+ cx: "12",
198
+ cy: "12",
199
+ r: "10"
200
+ }),
201
+ jsx("path", { d: "M12 16v-4" }),
202
+ jsx("path", { d: "M12 8h.01" })
203
+ ] }), jsxs(Fragment, { children: [
204
+ jsx("circle", {
205
+ cx: "12",
206
+ cy: "12",
207
+ r: "10"
208
+ }),
209
+ jsx("path", { d: "M12 16v-4" }),
210
+ jsx("path", { d: "M12 8h.01" })
211
+ ] }), jsxs(Fragment, { children: [jsx("circle", {
212
+ cx: "12",
213
+ cy: "12",
214
+ r: "10"
215
+ }), jsx("path", { d: "m9 12 2 2 4-4" })] }), jsxs(Fragment, { children: [
216
+ jsx("path", { d: "m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z" }),
217
+ jsx("path", { d: "M12 9v4" }),
218
+ jsx("path", { d: "M12 17h.01" })
219
+ ] }), jsxs(Fragment, { children: [
220
+ jsx("circle", {
221
+ cx: "12",
222
+ cy: "12",
223
+ r: "10"
224
+ }),
225
+ jsx("path", { d: "m15 9-6 6" }),
226
+ jsx("path", { d: "m9 9 6 6" })
227
+ ] });
228
+ //#endregion
150
229
  //#region lib/types/components/dialog.js
151
230
  /**
152
231
  * Dialog/sheet semantics over Radix's focus trap and portal. The skin chooses
@@ -164,11 +243,11 @@ function DialogContent({ children, className, presentation = "auto", size = "md"
164
243
  ...part("dialog", "overlay"),
165
244
  className: "snap-dialog-overlay"
166
245
  }), jsxs(DialogPrimitive.Content, {
167
- ...part("dialog", "panel"),
168
246
  className: cn("snap-dialog-panel", className),
169
247
  "data-presentation": resolved,
170
248
  "data-size": size,
171
249
  ...rest,
250
+ ...part("dialog", "panel"),
172
251
  children: [children, showClose && jsx(DialogPrimitive.Close, {
173
252
  ...part("dialog", "close"),
174
253
  className: "snap-dialog-close",
@@ -222,27 +301,27 @@ function DialogFooter({ children, className }) {
222
301
  /** The tabs root. */
223
302
  function Tabs({ className, children, ...props }) {
224
303
  return jsx(TabsPrimitive.Root, {
225
- ...part("tabs", "root"),
226
304
  className: cn("snap-tabs", className),
227
305
  ...props,
306
+ ...part("tabs", "root"),
228
307
  children
229
308
  });
230
309
  }
231
310
  /** The tabs list. */
232
311
  function TabsList({ className, children, ...props }) {
233
312
  return jsx(TabsPrimitive.List, {
234
- ...part("tabs", "list"),
235
313
  className: cn("snap-tabs-list", className),
236
314
  ...props,
315
+ ...part("tabs", "list"),
237
316
  children
238
317
  });
239
318
  }
240
319
  /** A tab trigger. */
241
320
  function TabsTrigger({ className, children, ...props }) {
242
321
  return jsx(TabsPrimitive.Trigger, {
243
- ...part("tabs", "trigger"),
244
322
  className: cn("snap-tabs-trigger", className),
245
323
  ...props,
324
+ ...part("tabs", "trigger"),
246
325
  children
247
326
  });
248
327
  }
@@ -257,9 +336,9 @@ function Switch({ className, label, labelPosition = "end", description, containe
257
336
  children: [
258
337
  labelPosition === "start" && labelNode,
259
338
  jsx(SwitchPrimitive.Root, {
260
- ...part("switch", "root"),
261
339
  className: cn("snap-switch", className),
262
340
  ...props,
341
+ ...part("switch", "root"),
263
342
  children: jsx(SwitchPrimitive.Thumb, {
264
343
  ...part("switch", "thumb"),
265
344
  className: "snap-switch-thumb"
@@ -318,46 +397,6 @@ function EmptyState({ title, description, icon, action, className }) {
318
397
  createContext(null);
319
398
  createContext(null);
320
399
  //#endregion
321
- //#region lib/types/components/input.js
322
- /** A single-line text field with semantic prefix/suffix slots. Layout
323
- * `style`/`className` dress the wrapper — the visible control; the native
324
- * field already stretches inside it. */
325
- function Input({ size = "md", invalid = false, prefix, suffix, onClear, fieldClassName, componentName = "input", className, style, disabled, ...props }) {
326
- return jsxs("div", {
327
- ...part(componentName, "root"),
328
- className: cn("snap-input", className),
329
- style,
330
- "data-size": size,
331
- "data-invalid": invalid || void 0,
332
- "data-disabled": disabled === true || void 0,
333
- children: [
334
- prefix !== void 0 && jsx("span", {
335
- ...part(componentName, "prefix"),
336
- children: prefix
337
- }),
338
- jsx("input", {
339
- ...part(componentName, "field"),
340
- className: cn("snap-input-field", fieldClassName),
341
- disabled,
342
- "aria-invalid": invalid || void 0,
343
- ...props
344
- }),
345
- onClear !== void 0 && typeof props.value === "string" && props.value !== "" && jsx("button", {
346
- ...part(componentName, "clear"),
347
- type: "button",
348
- className: "snap-input-clear",
349
- "aria-label": "清除",
350
- onClick: onClear,
351
- children: "×"
352
- }),
353
- suffix !== void 0 && jsx("span", {
354
- ...part(componentName, "suffix"),
355
- children: suffix
356
- })
357
- ]
358
- });
359
- }
360
- //#endregion
361
400
  //#region lib/types/components/layout.js
362
401
  /** A vertical flow. */
363
402
  function Stack({ gap = "md", padding, align, className, style, children }) {
@@ -504,45 +543,45 @@ function Table({ className, children, ...props }) {
504
543
  /** The table head. */
505
544
  function TableHeader({ className, children, ...props }) {
506
545
  return jsx("thead", {
507
- ...part("table", "header"),
508
546
  className: cn("snap-table-header", className),
509
547
  ...props,
548
+ ...part("table", "header"),
510
549
  children
511
550
  });
512
551
  }
513
552
  /** The table body. */
514
553
  function TableBody({ className, children, ...props }) {
515
554
  return jsx("tbody", {
516
- ...part("table", "body"),
517
555
  className: cn("snap-table-body", className),
518
556
  ...props,
557
+ ...part("table", "body"),
519
558
  children
520
559
  });
521
560
  }
522
561
  /** A table row. */
523
562
  function TableRow({ className, children, ...props }) {
524
563
  return jsx("tr", {
525
- ...part("table", "row"),
526
564
  className: cn("snap-table-row", className),
527
565
  ...props,
566
+ ...part("table", "row"),
528
567
  children
529
568
  });
530
569
  }
531
570
  /** A table header cell. */
532
571
  function TableHead({ className, children, ...props }) {
533
572
  return jsx("th", {
534
- ...part("table", "head"),
535
573
  className: cn("snap-table-head", className),
536
574
  ...props,
575
+ ...part("table", "head"),
537
576
  children
538
577
  });
539
578
  }
540
579
  /** A table body cell. */
541
580
  function TableCell({ className, children, ...props }) {
542
581
  return jsx("td", {
543
- ...part("table", "cell"),
544
582
  className: cn("snap-table-cell", className),
545
583
  ...props,
584
+ ...part("table", "cell"),
546
585
  children
547
586
  });
548
587
  }
@@ -10,27 +10,27 @@ import * as SeparatorPrimitive from '@radix-ui/react-separator';
10
10
  import { cn, part } from "../utils.js";
11
11
  /** An inline status banner for results and failures. */
12
12
  export function Alert({ intent = 'neutral', className, children, ...props }) {
13
- return (_jsx("div", { ...part('alert', 'root'), role: "alert", className: cn('snap-alert', className), "data-intent": intent, ...props, children: children }));
13
+ return (_jsx("div", { role: "alert", className: cn('snap-alert', className), "data-intent": intent, ...props, ...part('alert', 'root'), children: children }));
14
14
  }
15
15
  /** The alert heading line. */
16
16
  export function AlertTitle({ className, children, ...props }) {
17
- return _jsx("h5", { ...part('alert', 'title'), className: cn('snap-alert-title', className), ...props, children: children });
17
+ return _jsx("h5", { className: cn('snap-alert-title', className), ...props, ...part('alert', 'title'), children: children });
18
18
  }
19
19
  /** The alert body text. */
20
20
  export function AlertDescription({ className, children, ...props }) {
21
- return _jsx("div", { ...part('alert', 'description'), className: cn('snap-alert-description', className), ...props, children: children });
21
+ return _jsx("div", { className: cn('snap-alert-description', className), ...props, ...part('alert', 'description'), children: children });
22
22
  }
23
23
  /** A themed rule between sections. */
24
24
  export function Separator({ orientation = 'horizontal', className, ...props }) {
25
- return (_jsx(SeparatorPrimitive.Root, { ...part('separator', 'root'), orientation: orientation, className: cn('snap-separator', className), ...props }));
25
+ return (_jsx(SeparatorPrimitive.Root, { orientation: orientation, className: cn('snap-separator', className), ...props, ...part('separator', 'root') }));
26
26
  }
27
27
  /** A neutral loading placeholder shaped by the skin. */
28
28
  export function Skeleton({ variant = 'block', className, ...props }) {
29
- return _jsx("div", { ...part('skeleton', 'root'), className: cn('snap-skeleton', className), "data-variant": variant, "aria-hidden": "true", ...props });
29
+ return _jsx("div", { className: cn('snap-skeleton', className), "data-variant": variant, "aria-hidden": "true", ...props, ...part('skeleton', 'root') });
30
30
  }
31
31
  /** A vertical collection of {@link ListItem} rows. */
32
32
  export function List({ divided = false, className, children, ...props }) {
33
- return (_jsx("ul", { ...part('list', 'root'), className: cn('snap-list', className), "data-divided": divided || undefined, ...props, children: children }));
33
+ return (_jsx("ul", { className: cn('snap-list', className), "data-divided": divided || undefined, ...props, ...part('list', 'root'), children: children }));
34
34
  }
35
35
  /** One list row with leading/trailing slots. */
36
36
  export function ListItem({ leading, trailing, selected = false, disabled = false, onClick, className, children, ...props }) {
@@ -41,6 +41,6 @@ export function ListItem({ leading, trailing, selected = false, disabled = false
41
41
  onClick?.();
42
42
  }
43
43
  };
44
- return (_jsxs("li", { ...part('list', 'item'), className: cn('snap-list-item', className), "data-selected": selected || undefined, "data-interactive": interactive || undefined, "data-disabled": disabled || undefined, "aria-disabled": disabled || undefined, ...interactive ? { role: 'button', tabIndex: 0, onClick, onKeyDown: activate } : {}, ...props, children: [leading !== undefined && _jsx("span", { ...part('list', 'leading'), children: leading }), _jsx("span", { ...part('list', 'content'), style: { flex: 1, minWidth: 0 }, children: children }), trailing !== undefined && _jsx("span", { ...part('list', 'trailing'), children: trailing })] }));
44
+ return (_jsxs("li", { className: cn('snap-list-item', className), "data-selected": selected || undefined, "data-interactive": interactive || undefined, "data-disabled": disabled || undefined, "aria-disabled": disabled || undefined, ...interactive ? { role: 'button', tabIndex: 0, onClick, onKeyDown: activate } : {}, ...props, ...part('list', 'item'), children: [leading !== undefined && _jsx("span", { ...part('list', 'leading'), children: leading }), _jsx("span", { ...part('list', 'content'), style: { flex: 1, minWidth: 0 }, children: children }), trailing !== undefined && _jsx("span", { ...part('list', 'trailing'), children: trailing })] }));
45
45
  }
46
46
  //# sourceMappingURL=alert.js.map
@@ -18,10 +18,10 @@ export function Button({ intent = 'neutral', emphasis = 'solid', size = 'md', lo
18
18
  const content = asChild
19
19
  ? children
20
20
  : (_jsxs(_Fragment, { children: [icon !== undefined && iconPosition === 'start' && _jsx("span", { ...part('button', 'icon'), children: icon }), children !== undefined && _jsx("span", { ...part('button', 'label'), children: children }), icon !== undefined && iconPosition === 'end' && _jsx("span", { ...part('button', 'icon'), children: icon }), loading && _jsx(Spinner, {})] }));
21
- return (_jsx(Component, { ...part('button', 'root'), ...type !== undefined ? { type } : !asChild ? { type: 'button' } : {}, className: cn('snap-button', className), "data-intent": intent, "data-emphasis": emphasis, "data-size": size, "data-loading": loading || undefined, "data-disabled": disabled === true || undefined, "aria-busy": loading || undefined, disabled: !asChild ? disabled === true || loading : undefined, ...props, children: content }));
21
+ return (_jsx(Component, { ...type !== undefined ? { type } : !asChild ? { type: 'button' } : {}, className: cn('snap-button', className), "data-intent": intent, "data-emphasis": emphasis, "data-size": size, "data-loading": loading || undefined, "data-disabled": disabled === true || undefined, "aria-busy": loading || undefined, disabled: !asChild ? disabled === true || loading : undefined, ...props, ...part('button', 'root'), children: content }));
22
22
  }
23
23
  /** A square glyph button with a mandatory accessible label. */
24
24
  export function IconButton({ icon, label, active = false, intent = 'neutral', emphasis = 'ghost', size = 'md', className, ...props }) {
25
- return (_jsx("button", { ...part('icon-button', 'root'), type: "button", className: cn('snap-icon-button', className), "data-intent": intent, "data-emphasis": emphasis, "data-size": size, "data-active": active || undefined, "aria-label": label, "aria-pressed": active || undefined, ...props, children: _jsx("span", { ...part('icon-button', 'icon'), "aria-hidden": "true", children: icon }) }));
25
+ return (_jsx("button", { type: "button", className: cn('snap-icon-button', className), "data-intent": intent, "data-emphasis": emphasis, "data-size": size, "data-active": active || undefined, "aria-label": label, "aria-pressed": active || undefined, ...props, ...part('icon-button', 'root'), children: _jsx("span", { ...part('icon-button', 'icon'), "aria-hidden": "true", children: icon }) }));
26
26
  }
27
27
  //# sourceMappingURL=button.js.map
@@ -0,0 +1,45 @@
1
+ /**
2
+ * A text field with an anchored suggestion list: search pickers, filtered
3
+ * choosers, async candidate feeds (operator lookups, assignee search). The
4
+ * focus never leaves the field — keyboard navigation rides
5
+ * `aria-activedescendant`, pointer picks suppress the field's blur, and the
6
+ * panel opens without taking the focus — so typing keeps working through
7
+ * open, navigate, and select. The field is a regular {@link Input}; skins
8
+ * style the `combobox.*` parts around it.
9
+ *
10
+ * @module @snap-rail/ui/components/combobox
11
+ */
12
+ import { type ReactNode } from 'react';
13
+ import type { SelectOption } from './controls.tsx';
14
+ import { type InputSize } from './input.tsx';
15
+ /** Props of {@link Combobox}. */
16
+ export interface ComboboxProps {
17
+ /** Controlled selected value. */
18
+ value?: string;
19
+ /** Uncontrolled initial value. */
20
+ defaultValue?: string;
21
+ onValueChange?: (value: string) => void;
22
+ /** The field text changed. When set, `options` are taken as already
23
+ * matched (an async search feeds them); otherwise the list filters by
24
+ * label locally. */
25
+ onInputValueChange?: (text: string) => void;
26
+ options: readonly SelectOption[];
27
+ placeholder?: string;
28
+ disabled?: boolean;
29
+ invalid?: boolean;
30
+ size?: InputSize;
31
+ /** Candidates are being fetched (async search). */
32
+ loading?: boolean;
33
+ /** Shown when no option matches (default `无匹配项`). */
34
+ emptyText?: ReactNode;
35
+ /** The field's id; also wires a label's `htmlFor`. */
36
+ id?: string;
37
+ /** Dresses the field wrapper. */
38
+ className?: string;
39
+ /** Dresses the suggestion panel. */
40
+ contentClassName?: string;
41
+ 'aria-label'?: string;
42
+ }
43
+ /** A field with an anchored, keyboard-navigable suggestion list. */
44
+ export declare function Combobox({ value, defaultValue, onValueChange, onInputValueChange, options, placeholder, disabled, invalid, size, loading, emptyText, id, className, contentClassName, 'aria-label': ariaLabel, }: ComboboxProps): ReactNode;
45
+ //# sourceMappingURL=combobox.d.ts.map
@@ -0,0 +1,108 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * A text field with an anchored suggestion list: search pickers, filtered
4
+ * choosers, async candidate feeds (operator lookups, assignee search). The
5
+ * focus never leaves the field — keyboard navigation rides
6
+ * `aria-activedescendant`, pointer picks suppress the field's blur, and the
7
+ * panel opens without taking the focus — so typing keeps working through
8
+ * open, navigate, and select. The field is a regular {@link Input}; skins
9
+ * style the `combobox.*` parts around it.
10
+ *
11
+ * @module @snap-rail/ui/components/combobox
12
+ */
13
+ import { useEffect, useId, useRef, useState } from 'react';
14
+ import { part } from "../utils.js";
15
+ import { Input } from "./input.js";
16
+ import { Popover, PopoverAnchor, PopoverContent } from "./overlay.js";
17
+ /** The text an option contributes to filtering and to the field once picked. */
18
+ function textOf(option) {
19
+ return typeof option.label === 'string' || typeof option.label === 'number' ? String(option.label) : option.value;
20
+ }
21
+ /** The next enabled row index from `from`, wrapping; -1 when none is enabled. */
22
+ function nextEnabled(rows, from, step) {
23
+ if (rows.length === 0)
24
+ return -1;
25
+ let index = from < 0 ? (step > 0 ? -1 : 0) : from;
26
+ for (let tries = 0; tries < rows.length; tries++) {
27
+ index = (index + step + rows.length) % rows.length;
28
+ if (rows[index]?.disabled !== true)
29
+ return index;
30
+ }
31
+ return -1;
32
+ }
33
+ /** A field with an anchored, keyboard-navigable suggestion list. */
34
+ export function Combobox({ value, defaultValue, onValueChange, onInputValueChange, options, placeholder, disabled = false, invalid = false, size = 'md', loading = false, emptyText = '无匹配项', id, className, contentClassName, 'aria-label': ariaLabel, }) {
35
+ const listId = useId();
36
+ const anchorRef = useRef(null);
37
+ const [uncontrolled, setUncontrolled] = useState(defaultValue);
38
+ const current = value !== undefined ? value : uncontrolled;
39
+ const selected = options.find(option => option.value === current);
40
+ const selectedText = selected !== undefined ? textOf(selected) : undefined;
41
+ const [text, setText] = useState(selectedText ?? '');
42
+ const [open, setOpen] = useState(false);
43
+ const [active, setActive] = useState(-1);
44
+ // An external value change (or async options delivering the selected row)
45
+ // re-dresses the field with the selected label.
46
+ useEffect(() => {
47
+ if (selectedText !== undefined)
48
+ setText(selectedText);
49
+ }, [selectedText]);
50
+ const query = text.trim().toLowerCase();
51
+ const rows = onInputValueChange !== undefined || query === '' || text === selectedText
52
+ ? options
53
+ : options.filter(option => textOf(option).toLowerCase().includes(query));
54
+ const optionId = (index) => `${listId}-${index}`;
55
+ useEffect(() => {
56
+ if (active < 0)
57
+ return;
58
+ document.getElementById(optionId(active))?.scrollIntoView?.({ block: 'nearest' });
59
+ });
60
+ const close = () => {
61
+ setOpen(false);
62
+ setActive(-1);
63
+ };
64
+ const choose = (option) => {
65
+ if (option.disabled === true)
66
+ return;
67
+ if (value === undefined)
68
+ setUncontrolled(option.value);
69
+ setText(textOf(option));
70
+ onValueChange?.(option.value);
71
+ close();
72
+ };
73
+ const onKeyDown = (event) => {
74
+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
75
+ event.preventDefault();
76
+ if (!open) {
77
+ setOpen(true);
78
+ return;
79
+ }
80
+ setActive(index => nextEnabled(rows, index, event.key === 'ArrowDown' ? 1 : -1));
81
+ return;
82
+ }
83
+ if (event.key === 'Enter' && open && active >= 0) {
84
+ const option = rows[active];
85
+ if (option !== undefined) {
86
+ event.preventDefault();
87
+ choose(option);
88
+ }
89
+ return;
90
+ }
91
+ if (event.key === 'Escape' && open) {
92
+ event.preventDefault();
93
+ close();
94
+ }
95
+ };
96
+ return (_jsx("span", { ...part('combobox', 'root'), className: "snap-combobox", children: _jsxs(Popover, { open: open && !disabled, onOpenChange: next => { if (!next)
97
+ close(); }, children: [_jsx(PopoverAnchor, { asChild: true, children: _jsx("span", { ref: anchorRef, ...part('combobox', 'anchor'), children: _jsx(Input, { id: id, size: size, invalid: invalid, disabled: disabled, placeholder: placeholder, className: className, value: text, role: "combobox", autoComplete: "off", "aria-label": ariaLabel, "aria-expanded": open, "aria-controls": listId, "aria-autocomplete": "list", "aria-activedescendant": open && active >= 0 ? optionId(active) : undefined, onChange: event => {
98
+ setText(event.target.value);
99
+ setActive(-1);
100
+ setOpen(true);
101
+ onInputValueChange?.(event.target.value);
102
+ }, onFocus: () => setOpen(true), onBlur: close, onKeyDown: onKeyDown }) }) }), _jsx(PopoverContent, { align: "start", sideOffset: 4, autoFocusOnOpen: false, className: contentClassName, onInteractOutside: event => {
103
+ // A press on the field itself is not "outside": keep the list open.
104
+ if (anchorRef.current?.contains(event.target) === true)
105
+ event.preventDefault();
106
+ }, children: _jsxs("div", { ...part('combobox', 'list'), role: "listbox", id: listId, children: [rows.map((option, index) => (_jsx("div", { ...part('combobox', 'option'), id: optionId(index), role: "option", "aria-selected": option.value === current, "aria-disabled": option.disabled === true || undefined, "data-active": index === active || undefined, "data-disabled": option.disabled === true || undefined, onMouseDown: event => event.preventDefault(), onClick: () => choose(option), children: option.label }, option.value))), loading && _jsx("div", { ...part('combobox', 'empty'), children: "\u641C\u7D22\u4E2D\u2026" }), !loading && rows.length === 0 && _jsx("div", { ...part('combobox', 'empty'), children: emptyText })] }) })] }) }));
107
+ }
108
+ //# sourceMappingURL=combobox.js.map
@@ -22,24 +22,24 @@ export function Label({ className, children, ...props }) {
22
22
  }
23
23
  /** The tabs root. */
24
24
  export function Tabs({ className, children, ...props }) {
25
- return _jsx(TabsPrimitive.Root, { ...part('tabs', 'root'), className: cn('snap-tabs', className), ...props, children: children });
25
+ return _jsx(TabsPrimitive.Root, { className: cn('snap-tabs', className), ...props, ...part('tabs', 'root'), children: children });
26
26
  }
27
27
  /** The tabs list. */
28
28
  export function TabsList({ className, children, ...props }) {
29
- return _jsx(TabsPrimitive.List, { ...part('tabs', 'list'), className: cn('snap-tabs-list', className), ...props, children: children });
29
+ return _jsx(TabsPrimitive.List, { className: cn('snap-tabs-list', className), ...props, ...part('tabs', 'list'), children: children });
30
30
  }
31
31
  /** A tab trigger. */
32
32
  export function TabsTrigger({ className, children, ...props }) {
33
- return _jsx(TabsPrimitive.Trigger, { ...part('tabs', 'trigger'), className: cn('snap-tabs-trigger', className), ...props, children: children });
33
+ return _jsx(TabsPrimitive.Trigger, { className: cn('snap-tabs-trigger', className), ...props, ...part('tabs', 'trigger'), children: children });
34
34
  }
35
35
  /** A tab panel. */
36
36
  export function TabsContent({ className, children, ...props }) {
37
- return _jsx(TabsPrimitive.Content, { ...part('tabs', 'content'), className: cn('snap-tabs-content', className), ...props, children: children });
37
+ return _jsx(TabsPrimitive.Content, { className: cn('snap-tabs-content', className), ...props, ...part('tabs', 'content'), children: children });
38
38
  }
39
39
  /** A boolean switch with an optional label on either side. */
40
40
  export function Switch({ className, label, labelPosition = 'end', description, containerClassName, ...props }) {
41
41
  const labelNode = label !== undefined && _jsx("span", { ...part('switch', 'label'), children: label });
42
- const row = (_jsxs("label", { className: "snap-switch-label", children: [labelPosition === 'start' && labelNode, _jsx(SwitchPrimitive.Root, { ...part('switch', 'root'), className: cn('snap-switch', className), ...props, children: _jsx(SwitchPrimitive.Thumb, { ...part('switch', 'thumb'), className: "snap-switch-thumb" }) }), labelPosition === 'end' && labelNode] }));
42
+ const row = (_jsxs("label", { className: "snap-switch-label", children: [labelPosition === 'start' && labelNode, _jsx(SwitchPrimitive.Root, { className: cn('snap-switch', className), ...props, ...part('switch', 'root'), children: _jsx(SwitchPrimitive.Thumb, { ...part('switch', 'thumb'), className: "snap-switch-thumb" }) }), labelPosition === 'end' && labelNode] }));
43
43
  if (description === undefined)
44
44
  return row;
45
45
  return (_jsxs("span", { ...part('switch', 'field'), className: cn('snap-switch-field', containerClassName), children: [row, _jsx("span", { ...part('switch', 'description'), className: "snap-field-help", children: description })] }));
@@ -47,7 +47,7 @@ export function Switch({ className, label, labelPosition = 'end', description, c
47
47
  /** A tri-state checkbox with an optional label on either side. */
48
48
  export function Checkbox({ className, label, labelPosition = 'end', description, containerClassName, ...props }) {
49
49
  const labelNode = label !== undefined && _jsx("span", { ...part('checkbox', 'label'), children: label });
50
- const row = (_jsxs("label", { className: "snap-checkbox-label", children: [labelPosition === 'start' && labelNode, _jsx(CheckboxPrimitive.Root, { ...part('checkbox', 'root'), className: cn('snap-checkbox', className), ...props, children: _jsx(CheckboxPrimitive.Indicator, { ...part('checkbox', 'indicator'), className: "snap-checkbox-indicator", children: "\u2713" }) }), labelPosition === 'end' && labelNode] }));
50
+ const row = (_jsxs("label", { className: "snap-checkbox-label", children: [labelPosition === 'start' && labelNode, _jsx(CheckboxPrimitive.Root, { className: cn('snap-checkbox', className), ...props, ...part('checkbox', 'root'), children: _jsx(CheckboxPrimitive.Indicator, { ...part('checkbox', 'indicator'), className: "snap-checkbox-indicator", children: "\u2713" }) }), labelPosition === 'end' && labelNode] }));
51
51
  if (description === undefined)
52
52
  return row;
53
53
  return (_jsxs("span", { ...part('checkbox', 'field'), className: cn('snap-checkbox-field', containerClassName), children: [row, _jsx("span", { ...part('checkbox', 'description'), className: "snap-field-help", children: description })] }));
@@ -55,39 +55,39 @@ export function Checkbox({ className, label, labelPosition = 'end', description,
55
55
  /** A labelled group of radio items. */
56
56
  export function RadioGroup({ className, label, description, children, ...props }) {
57
57
  const labelId = useId();
58
- const group = (_jsx(RadioGroupPrimitive.Root, { ...part('radio-group', 'root'), className: cn('snap-radio-group', className), ...label !== undefined ? { 'aria-labelledby': labelId } : {}, ...props, children: children }));
58
+ const group = (_jsx(RadioGroupPrimitive.Root, { className: cn('snap-radio-group', className), ...label !== undefined ? { 'aria-labelledby': labelId } : {}, ...props, ...part('radio-group', 'root'), children: children }));
59
59
  if (label === undefined && description === undefined)
60
60
  return group;
61
61
  return (_jsxs("div", { ...part('radio-group', 'block'), className: "snap-radio-group-block", children: [label !== undefined && _jsx(Label, { id: labelId, ...part('radio-group', 'label'), children: label }), group, description !== undefined && _jsx("div", { ...part('radio-group', 'description'), className: "snap-field-help", children: description })] }));
62
62
  }
63
63
  /** One radio item. */
64
64
  export function RadioGroupItem({ className, ...props }) {
65
- return (_jsx(RadioGroupPrimitive.Item, { ...part('radio-group', 'item'), className: cn('snap-radio-item', className), ...props, children: _jsx(RadioGroupPrimitive.Indicator, { ...part('radio-group', 'indicator'), className: "snap-radio-indicator" }) }));
65
+ return (_jsx(RadioGroupPrimitive.Item, { className: cn('snap-radio-item', className), ...props, ...part('radio-group', 'item'), children: _jsx(RadioGroupPrimitive.Indicator, { ...part('radio-group', 'indicator'), className: "snap-radio-indicator" }) }));
66
66
  }
67
67
  /** A modal option list; use on touch terminals in place of the popover select. */
68
68
  export function TouchSelect({ value, onValueChange, options, placeholder = '请选择', disabled = false, invalid = false, size = 'md', className, 'aria-label': ariaLabel, title, }) {
69
69
  const [open, setOpen] = useState(false);
70
70
  const selected = options.find(option => option.value === value);
71
- return (_jsxs("span", { ...part('touch-select', 'root'), className: "snap-touch-select", children: [_jsxs(Button, { type: "button", size: size, emphasis: "outline", className: className, disabled: disabled, "aria-label": ariaLabel, "aria-invalid": invalid || undefined, "aria-haspopup": "dialog", onClick: () => setOpen(true), ...part('touch-select', 'trigger'), children: [_jsx("span", { style: { flex: 1, textAlign: 'left' }, children: selected?.label ?? placeholder }), _jsx("span", { "aria-hidden": "true", children: "\u25BE" })] }), _jsx(Dialog, { open: open, onOpenChange: setOpen, children: _jsxs(DialogContent, { presentation: "modal", size: "sm", ...part('touch-select', 'sheet'), children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: title ?? ariaLabel ?? '请选择' }) }), _jsxs("div", { ...part('touch-select', 'options'), style: { display: 'flex', flexDirection: 'column', gap: 'var(--snap-space-sm)' }, children: [options.map(option => (_jsx(Button, { type: "button", size: "lg", emphasis: option.value === value ? 'solid' : 'outline', intent: option.value === value ? 'primary' : 'neutral', disabled: option.disabled === true, onClick: () => {
72
- onValueChange?.(option.value);
73
- setOpen(false);
74
- }, ...part('touch-select', 'option'), children: option.label }, option.value))), options.length === 0 && _jsx("span", { style: { color: 'var(--snap-color-muted)' }, children: "\u65E0\u53EF\u9009\u9879" })] })] }) })] }));
71
+ return (_jsxs("span", { ...part('touch-select', 'root'), className: "snap-touch-select", children: [_jsx("span", { ...part('touch-select', 'trigger'), className: "snap-touch-select-trigger", children: _jsxs(Button, { type: "button", size: size, emphasis: "outline", className: className, disabled: disabled, "aria-label": ariaLabel, "aria-invalid": invalid || undefined, "aria-haspopup": "dialog", onClick: () => setOpen(true), children: [_jsx("span", { style: { flex: 1, textAlign: 'left' }, children: selected?.label ?? placeholder }), _jsx("span", { "aria-hidden": "true", children: "\u25BE" })] }) }), _jsx(Dialog, { open: open, onOpenChange: setOpen, children: _jsx(DialogContent, { presentation: "modal", size: "sm", children: _jsxs("div", { ...part('touch-select', 'sheet'), className: "snap-touch-select-sheet", children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: title ?? ariaLabel ?? '请选择' }) }), _jsxs("div", { ...part('touch-select', 'options'), style: { display: 'flex', flexDirection: 'column', gap: 'var(--snap-space-sm)' }, children: [options.map(option => (_jsx(Button, { type: "button", size: "lg", emphasis: option.value === value ? 'solid' : 'outline', intent: option.value === value ? 'primary' : 'neutral', disabled: option.disabled === true, onClick: () => {
72
+ onValueChange?.(option.value);
73
+ setOpen(false);
74
+ }, children: option.label }, option.value))), options.length === 0 && _jsx("span", { style: { color: 'var(--snap-color-muted)' }, children: "\u65E0\u53EF\u9009\u9879" })] })] }) }) })] }));
75
75
  }
76
76
  /** The select trigger; composes a {@link SelectValue} inside. */
77
77
  export function SelectTrigger({ className, size = 'md', invalid = false, disabled = false, children, ...props }) {
78
- return (_jsxs(SelectPrimitive.Trigger, { ...part('select', 'trigger'), className: cn('snap-select-trigger', className), "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled || undefined, "aria-invalid": invalid || undefined, ...props, children: [children, _jsx(SelectPrimitive.Icon, { ...part('select', 'chevron'), className: "snap-select-chevron", children: "\u25BE" })] }));
78
+ return (_jsxs(SelectPrimitive.Trigger, { className: cn('snap-select-trigger', className), "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled || undefined, "aria-invalid": invalid || undefined, ...props, ...part('select', 'trigger'), children: [children, _jsx(SelectPrimitive.Icon, { ...part('select', 'chevron'), className: "snap-select-chevron", children: "\u25BE" })] }));
79
79
  }
80
80
  /** The selected-value slot of a trigger. */
81
81
  export function SelectValue(props) {
82
- return _jsx(SelectPrimitive.Value, { ...part('select', 'value'), ...props });
82
+ return _jsx(SelectPrimitive.Value, { ...props, ...part('select', 'value') });
83
83
  }
84
84
  /** The portalled option list. */
85
85
  export function SelectContent({ side = 'bottom', align = 'start', sideOffset = 4, className, children, ...props }) {
86
- return (_jsx(SelectPrimitive.Portal, { children: _jsx(SelectPrimitive.Content, { ...part('select', 'content'), className: cn('snap-select-content', className), position: "popper", side: side, align: align, sideOffset: sideOffset, ...props, children: _jsx(SelectPrimitive.Viewport, { children: children }) }) }));
86
+ return (_jsx(SelectPrimitive.Portal, { children: _jsx(SelectPrimitive.Content, { className: cn('snap-select-content', className), position: "popper", side: side, align: align, sideOffset: sideOffset, ...props, ...part('select', 'content'), children: _jsx(SelectPrimitive.Viewport, { children: children }) }) }));
87
87
  }
88
88
  /** One option row. */
89
89
  export function SelectItem({ className, children, ...props }) {
90
- return (_jsxs(SelectPrimitive.Item, { ...part('select', 'item'), className: cn('snap-select-item', className), ...props, children: [_jsx(SelectPrimitive.ItemText, { children: children }), _jsx(SelectPrimitive.ItemIndicator, { ...part('select', 'item-indicator'), className: "snap-select-item-indicator", children: "\u2713" })] }));
90
+ return (_jsxs(SelectPrimitive.Item, { className: cn('snap-select-item', className), ...props, ...part('select', 'item'), children: [_jsx(SelectPrimitive.ItemText, { children: children }), _jsx(SelectPrimitive.ItemIndicator, { ...part('select', 'item-indicator'), className: "snap-select-item-indicator", children: "\u2713" })] }));
91
91
  }
92
92
  /** A labelled group of options. */
93
93
  export function SelectGroup(props) {
@@ -17,7 +17,7 @@ export const DialogClose = DialogPrimitive.Close;
17
17
  /** The portalled dialog content. */
18
18
  export function DialogContent({ children, className, presentation = 'auto', size = 'md', showClose = true, ...rest }) {
19
19
  const resolved = presentation === 'auto' ? 'modal' : presentation;
20
- return (_jsxs(DialogPrimitive.Portal, { children: [_jsx(DialogPrimitive.Overlay, { ...part('dialog', 'overlay'), className: "snap-dialog-overlay" }), _jsxs(DialogPrimitive.Content, { ...part('dialog', 'panel'), className: cn('snap-dialog-panel', className), "data-presentation": resolved, "data-size": size, ...rest, children: [children, showClose && (_jsx(DialogPrimitive.Close, { ...part('dialog', 'close'), className: "snap-dialog-close", "aria-label": "\u5173\u95ED", children: "\u00D7" }))] })] }));
20
+ return (_jsxs(DialogPrimitive.Portal, { children: [_jsx(DialogPrimitive.Overlay, { ...part('dialog', 'overlay'), className: "snap-dialog-overlay" }), _jsxs(DialogPrimitive.Content, { className: cn('snap-dialog-panel', className), "data-presentation": resolved, "data-size": size, ...rest, ...part('dialog', 'panel'), children: [children, showClose && (_jsx(DialogPrimitive.Close, { ...part('dialog', 'close'), className: "snap-dialog-close", "aria-label": "\u5173\u95ED", children: "\u00D7" }))] })] }));
21
21
  }
22
22
  /** The dialog header. */
23
23
  export function DialogHeader({ children, className }) {