@enablo/swirlcn-ui 0.0.0-stage → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -2
  3. package/dist/chunk-3DJWDNQL.js +77 -0
  4. package/dist/chunk-7XUCETA6.js +447 -0
  5. package/dist/chunk-KZESIYUN.js +18 -0
  6. package/dist/flip.d.ts +98 -0
  7. package/dist/flip.js +158 -0
  8. package/dist/fonts/Inter-italic.var.woff2 +0 -0
  9. package/dist/fonts/Inter.var.woff2 +0 -0
  10. package/dist/fonts/OFL.txt +96 -0
  11. package/dist/index.d.ts +2459 -0
  12. package/dist/index.js +8439 -0
  13. package/dist/server.d.ts +144 -0
  14. package/dist/server.js +88 -0
  15. package/dist/spacing-BQaEPbUv.d.ts +59 -0
  16. package/dist/theme.css +146 -0
  17. package/package.json +84 -3
  18. package/src/components/accordion.tsx +102 -0
  19. package/src/components/action-list.tsx +294 -0
  20. package/src/components/alert-dialog.tsx +155 -0
  21. package/src/components/autocomplete.tsx +313 -0
  22. package/src/components/avatar.tsx +258 -0
  23. package/src/components/badge.tsx +100 -0
  24. package/src/components/banner.tsx +112 -0
  25. package/src/components/button-group.tsx +87 -0
  26. package/src/components/button.tsx +362 -0
  27. package/src/components/card.tsx +186 -0
  28. package/src/components/carousel.tsx +245 -0
  29. package/src/components/checkbox.tsx +125 -0
  30. package/src/components/chip.tsx +153 -0
  31. package/src/components/color-input.tsx +98 -0
  32. package/src/components/data-cell.tsx +230 -0
  33. package/src/components/date-input.tsx +223 -0
  34. package/src/components/date-picker.tsx +276 -0
  35. package/src/components/description-list.tsx +97 -0
  36. package/src/components/dialog.tsx +405 -0
  37. package/src/components/empty-state.tsx +63 -0
  38. package/src/components/file-chip.tsx +206 -0
  39. package/src/components/file-uploader.tsx +148 -0
  40. package/src/components/flip-app-header.tsx +104 -0
  41. package/src/components/flip-brand.tsx +77 -0
  42. package/src/components/flip-theme.tsx +95 -0
  43. package/src/components/form-control.tsx +397 -0
  44. package/src/components/form-group.tsx +26 -0
  45. package/src/components/heading.tsx +70 -0
  46. package/src/components/icon.tsx +119 -0
  47. package/src/components/image-grid.tsx +285 -0
  48. package/src/components/inline-notification.tsx +167 -0
  49. package/src/components/inline-status.tsx +100 -0
  50. package/src/components/input.tsx +390 -0
  51. package/src/components/label.tsx +28 -0
  52. package/src/components/lightbox.tsx +314 -0
  53. package/src/components/link.tsx +46 -0
  54. package/src/components/list.tsx +40 -0
  55. package/src/components/menu.tsx +530 -0
  56. package/src/components/option-list.tsx +582 -0
  57. package/src/components/pagination.tsx +336 -0
  58. package/src/components/popover.tsx +194 -0
  59. package/src/components/progress.tsx +158 -0
  60. package/src/components/radio-group.tsx +136 -0
  61. package/src/components/resource-list.tsx +686 -0
  62. package/src/components/search.tsx +127 -0
  63. package/src/components/select.tsx +456 -0
  64. package/src/components/separator.tsx +93 -0
  65. package/src/components/skeleton-box.tsx +63 -0
  66. package/src/components/skeleton-text.tsx +66 -0
  67. package/src/components/spinner.tsx +83 -0
  68. package/src/components/stack.tsx +86 -0
  69. package/src/components/status-indicator.tsx +31 -0
  70. package/src/components/switch.tsx +90 -0
  71. package/src/components/table.tsx +242 -0
  72. package/src/components/tabs.tsx +147 -0
  73. package/src/components/tag.tsx +169 -0
  74. package/src/components/text.tsx +126 -0
  75. package/src/components/textarea.tsx +130 -0
  76. package/src/components/theme-provider.tsx +115 -0
  77. package/src/components/thumbnail.tsx +250 -0
  78. package/src/components/time-input.tsx +123 -0
  79. package/src/components/toast.tsx +217 -0
  80. package/src/components/toggle-group.tsx +124 -0
  81. package/src/components/toolbar.tsx +105 -0
  82. package/src/components/tooltip.tsx +97 -0
  83. package/src/components/tree-navigation.tsx +194 -0
  84. package/src/components/tree-view.tsx +292 -0
  85. package/src/flip.ts +12 -0
  86. package/src/index.ts +317 -0
  87. package/src/lib/date-mask.ts +176 -0
  88. package/src/lib/field-context.ts +86 -0
  89. package/src/lib/flip-app-script.ts +38 -0
  90. package/src/lib/flip-host.ts +84 -0
  91. package/src/lib/flip-theme-css.ts +90 -0
  92. package/src/lib/focus.ts +18 -0
  93. package/src/lib/glyphs.ts +137 -0
  94. package/src/lib/spacing.ts +93 -0
  95. package/src/lib/theme-context.ts +27 -0
  96. package/src/lib/theme-script.ts +49 -0
  97. package/src/lib/use-mobile.ts +17 -0
  98. package/src/lib/utils.ts +28 -0
  99. package/src/server.ts +20 -0
  100. package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
  101. package/src/styles/fonts/Inter.var.woff2 +0 -0
  102. package/src/styles/fonts/OFL.txt +96 -0
  103. package/src/styles/theme.css +146 -0
@@ -0,0 +1,127 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+ import { setNativeValue, useFieldText } from "../lib/field-context";
4
+ import { Glyph } from "../lib/glyphs";
5
+
6
+ export interface SearchProps extends Omit<React.ComponentProps<"input">, "type" | "size"> {
7
+ /** Accessible name of the field. */
8
+ label?: string;
9
+ /** `filled` (default) sits on a raised surface; `outline` has a border; `ghost` has neither. */
10
+ variant?: "filled" | "outline" | "ghost";
11
+ /** Show a button that clears the term. Defaults to true. */
12
+ clearable?: boolean;
13
+ clearButtonLabel?: string;
14
+ /** Called with the term on every change, and with "" when cleared. */
15
+ onValueChange?: (value: string) => void;
16
+ onClear?: () => void;
17
+ /** Focus the field with Cmd/Ctrl+K or Cmd/Ctrl+/. Defaults to true, as in Swirl. */
18
+ shortcut?: boolean;
19
+ /** Class for the wrapper around the input, icon and clear button. */
20
+ wrapperClassName?: string;
21
+ }
22
+
23
+ /**
24
+ * Swirl's search field: a rounded input with a search icon and a clear
25
+ * button. It stands alone rather than in a `FormControl`, so give it a
26
+ * `label`. Cmd/Ctrl+K or Cmd/Ctrl+/ focuses it from anywhere on the page.
27
+ */
28
+ export function Search({
29
+ className,
30
+ wrapperClassName,
31
+ label,
32
+ variant = "filled",
33
+ clearable = true,
34
+ clearButtonLabel = "Clear search term",
35
+ placeholder = "Search …",
36
+ shortcut = true,
37
+ value,
38
+ defaultValue,
39
+ onChange,
40
+ onValueChange,
41
+ onClear,
42
+ disabled,
43
+ id,
44
+ ...props
45
+ }: SearchProps) {
46
+ const { text, handleChange } = useFieldText<HTMLInputElement>(value, defaultValue, (event) => {
47
+ onChange?.(event);
48
+ onValueChange?.(event.target.value);
49
+ });
50
+ const generatedId = React.useId();
51
+ const inputId = id ?? generatedId;
52
+ const getInput = () => document.getElementById(inputId) as HTMLInputElement | null;
53
+
54
+ React.useEffect(() => {
55
+ if (!shortcut || disabled) return;
56
+ const onKeyDown = (event: KeyboardEvent) => {
57
+ if ((event.code === "KeyK" || event.code === "Slash") && (event.metaKey || event.ctrlKey)) {
58
+ event.preventDefault();
59
+ (document.getElementById(inputId) as HTMLInputElement | null)?.focus();
60
+ }
61
+ };
62
+ window.addEventListener("keydown", onKeyDown);
63
+ return () => window.removeEventListener("keydown", onKeyDown);
64
+ }, [shortcut, disabled, inputId]);
65
+
66
+ const clear = () => {
67
+ const input = getInput();
68
+ if (!input) return;
69
+ setNativeValue(input, "");
70
+ input.focus();
71
+ onClear?.();
72
+ };
73
+
74
+ return (
75
+ <span className={cn("relative flex w-full", wrapperClassName)}>
76
+ <span
77
+ aria-hidden
78
+ className={cn(
79
+ "pointer-events-none absolute top-1/2 left-3 inline-flex size-6 -translate-y-1/2 text-icon-default",
80
+ "desktop:size-5 [&>svg]:size-full",
81
+ disabled && "text-disabled",
82
+ )}
83
+ >
84
+ <Glyph name="search" />
85
+ </span>
86
+ <input
87
+ {...props}
88
+ id={inputId}
89
+ type="search"
90
+ inputMode="search"
91
+ autoComplete="off"
92
+ aria-label={props["aria-label"] ?? label}
93
+ value={value}
94
+ defaultValue={defaultValue}
95
+ placeholder={placeholder}
96
+ disabled={disabled}
97
+ onChange={handleChange}
98
+ className={cn(
99
+ "h-10 w-full rounded-base border-0 py-2 pr-11 pl-11 text-base leading-6 text-foreground",
100
+ "caret-border-highlight placeholder:text-subdued",
101
+ "desktop:h-9 desktop:pl-10 desktop:text-sm desktop:leading-5",
102
+ variant === "filled" && "bg-surface-raised",
103
+ variant === "outline" && "bg-transparent shadow-[inset_0_0_0_1px_var(--color-border-strong)]",
104
+ variant === "ghost" && "bg-transparent",
105
+ "outline-focus focus:not-focus-visible:outline-none focus-visible:outline-2 focus-visible:outline-solid",
106
+ "disabled:text-disabled disabled:placeholder:text-disabled",
107
+ // The browser's own search decorations and clear button.
108
+ "[&::-webkit-search-cancel-button]:appearance-none [&::-webkit-search-decoration]:appearance-none",
109
+ className,
110
+ )}
111
+ />
112
+ {clearable && !disabled && text !== "" && (
113
+ <button
114
+ type="button"
115
+ aria-label={clearButtonLabel}
116
+ onClick={clear}
117
+ className={cn(
118
+ "absolute top-1/2 right-3 inline-flex size-6 -translate-y-1/2 cursor-pointer border-0 bg-transparent p-0",
119
+ "text-icon-default outline-focus focus:not-focus-visible:outline-none focus-visible:outline-2 [&>svg]:size-full",
120
+ )}
121
+ >
122
+ <Glyph name="cancel" />
123
+ </button>
124
+ )}
125
+ </span>
126
+ );
127
+ }
@@ -0,0 +1,456 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+ import { useField } from "../lib/field-context";
4
+ import { Glyph } from "../lib/glyphs";
5
+ import { useMobile } from "../lib/use-mobile";
6
+ import { fieldTextSize } from "./input";
7
+ import { OptionList, OptionListItem, OptionListSection } from "./option-list";
8
+ import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "./popover";
9
+ import { Spinner } from "./spinner";
10
+ import { Tag } from "./tag";
11
+
12
+ /* ------------------------------------------------------------- options */
13
+
14
+ export interface SelectOptionProps {
15
+ value: string;
16
+ /** The option's text, shown in the list and as the selected value. */
17
+ label: string;
18
+ description?: React.ReactNode;
19
+ /** Leading icon, shown in single-select lists. */
20
+ icon?: React.ReactNode;
21
+ avatar?: React.ReactNode;
22
+ disabled?: boolean;
23
+ }
24
+
25
+ /** One option of a `Select`. Describes the option; the Select renders it. */
26
+ export function SelectOption(_props: SelectOptionProps): React.ReactNode {
27
+ return null;
28
+ }
29
+
30
+ export interface SelectSectionProps {
31
+ label: string;
32
+ children?: React.ReactNode;
33
+ }
34
+
35
+ /** A labelled group of `SelectOption`s. */
36
+ export function SelectSection(_props: SelectSectionProps): React.ReactNode {
37
+ return null;
38
+ }
39
+
40
+ interface OptionData extends SelectOptionProps {
41
+ section?: string;
42
+ }
43
+
44
+ /** Reads the options out of the children, keeping their order and sections. */
45
+ function collectOptions(children: React.ReactNode, section?: string): OptionData[] {
46
+ const options: OptionData[] = [];
47
+ React.Children.forEach(children, (child) => {
48
+ if (!React.isValidElement(child)) return;
49
+ if (child.type === React.Fragment) {
50
+ options.push(...collectOptions((child.props as { children?: React.ReactNode }).children, section));
51
+ } else if (child.type === SelectSection) {
52
+ const props = child.props as SelectSectionProps;
53
+ options.push(...collectOptions(props.children, props.label));
54
+ } else if (child.type === SelectOption) {
55
+ options.push({ ...(child.props as SelectOptionProps), section });
56
+ }
57
+ });
58
+ return options;
59
+ }
60
+
61
+ /* -------------------------------------------------------------- select */
62
+
63
+ interface SelectBaseProps {
64
+ /** Accessible name of the list of options. Swirl requires one. */
65
+ label: string;
66
+ /** `SelectOption`s, optionally grouped in `SelectSection`s. */
67
+ children: React.ReactNode;
68
+ /** Text shown while nothing is selected. Inside a `FormControl` the label usually does this. */
69
+ placeholder?: string;
70
+ /** Whether choosing the selected option again clears it. Defaults to true, as in Swirl. */
71
+ allowDeselect?: boolean;
72
+ disabled?: boolean;
73
+ /** Add a search box above the options. */
74
+ withSearch?: boolean;
75
+ searchPlaceholder?: string;
76
+ searchInputLabel?: string;
77
+ /** Called with the search term as it changes. */
78
+ onSearchChange?: (term: string) => void;
79
+ /** Show a spinner in the search box, while you load matching options. */
80
+ searchLoading?: boolean;
81
+ /**
82
+ * Filter the options by label as the user searches. Defaults to true; set
83
+ * false when you filter the options yourself in `onSearchChange`, as Swirl does.
84
+ */
85
+ filter?: boolean;
86
+ emptyListLabel?: string;
87
+ /**
88
+ * A compact trigger that fits its value, for use outside a form, as in
89
+ * Swirl. Outside a `FormControl` the select is always standalone.
90
+ */
91
+ standalone?: boolean;
92
+ /** Submits the value with a form: one hidden input per selected value. */
93
+ name?: string;
94
+ className?: string;
95
+ id?: string;
96
+ }
97
+
98
+ export interface SingleSelectProps extends SelectBaseProps {
99
+ multiSelect?: false;
100
+ value?: string;
101
+ defaultValue?: string;
102
+ onValueChange?: (value: string) => void;
103
+ }
104
+
105
+ export interface MultiSelectProps extends SelectBaseProps {
106
+ /** Let several options be chosen; they show as tags. */
107
+ multiSelect: true;
108
+ value?: string[];
109
+ defaultValue?: string[];
110
+ onValueChange?: (value: string[]) => void;
111
+ }
112
+
113
+ export type SelectProps = SingleSelectProps | MultiSelectProps;
114
+
115
+ const toArray = (value: string | string[] | undefined) =>
116
+ value === undefined ? undefined : Array.isArray(value) ? value : value === "" ? [] : [value];
117
+
118
+ /**
119
+ * Swirl's select: picks one option, or several with `multiSelect`, from a
120
+ * list that opens under the field (a bottom sheet on phones). Inside a
121
+ * `FormControl` it fills the field box; on its own it is a compact trigger.
122
+ *
123
+ * <FormControl label="Fruit">
124
+ * <Select label="Fruit" withSearch>
125
+ * <SelectOption value="apple" label="Apple" />
126
+ * <SelectOption value="pear" label="Pear" />
127
+ * </Select>
128
+ * </FormControl>
129
+ *
130
+ * Space, Enter or Arrow Down opens it. In the list, typing jumps to the next
131
+ * option starting with those letters.
132
+ */
133
+ export function Select(props: SelectProps) {
134
+ const {
135
+ label,
136
+ children,
137
+ placeholder,
138
+ allowDeselect = true,
139
+ disabled: disabledProp,
140
+ withSearch = false,
141
+ searchPlaceholder,
142
+ searchInputLabel = "Search options",
143
+ onSearchChange,
144
+ searchLoading = false,
145
+ filter = true,
146
+ emptyListLabel = "No results found.",
147
+ standalone: standaloneProp = false,
148
+ name,
149
+ className,
150
+ id,
151
+ } = props;
152
+ const multiSelect = props.multiSelect === true;
153
+ const field = useField();
154
+ const mobile = useMobile();
155
+ const standalone = standaloneProp || !field;
156
+ const disabled = disabledProp ?? field?.disabled ?? false;
157
+ const readOnly = field?.readOnly ?? false;
158
+
159
+ const [uncontrolled, setUncontrolled] = React.useState<string[]>(
160
+ () => toArray(props.defaultValue as string | string[] | undefined) ?? [],
161
+ );
162
+ const value = toArray(props.value as string | string[] | undefined) ?? uncontrolled;
163
+ const [open, setOpen] = React.useState(false);
164
+ const [term, setTerm] = React.useState("");
165
+
166
+ const options = React.useMemo(() => collectOptions(children), [children]);
167
+ const selected = value
168
+ .map((v) => options.find((o) => o.value === v))
169
+ .filter((o): o is OptionData => Boolean(o));
170
+
171
+ const filled = value.length > 0;
172
+ const setFilled = field?.setFilled;
173
+ const setHasPlaceholder = field?.setHasPlaceholder;
174
+ React.useEffect(() => {
175
+ setFilled?.(filled);
176
+ }, [setFilled, filled]);
177
+ React.useEffect(() => {
178
+ setHasPlaceholder?.(Boolean(placeholder));
179
+ }, [setHasPlaceholder, placeholder]);
180
+
181
+ const update = (next: string[]) => {
182
+ if (props.value === undefined) setUncontrolled(next);
183
+ if (multiSelect) (props.onValueChange as ((v: string[]) => void) | undefined)?.(next);
184
+ else (props.onValueChange as ((v: string) => void) | undefined)?.(next[0] ?? "");
185
+ };
186
+
187
+ const generatedId = React.useId();
188
+ const triggerId = id ?? field?.id ?? generatedId;
189
+ const listId = `${triggerId}-options`;
190
+ const contentRef = React.useRef<HTMLDivElement>(null);
191
+ // The list opens under the field box, as wide as it, as Swirl's does.
192
+ // Held in state, not a ref, because the anchor is rendered from it.
193
+ const [anchor, setAnchor] = React.useState<{ current: HTMLElement } | null>(null);
194
+ const [boxWidth, setBoxWidth] = React.useState<number>();
195
+
196
+ const openChange = (next: boolean) => {
197
+ if (next && (disabled || readOnly)) return;
198
+ if (next) {
199
+ const trigger = document.getElementById(triggerId);
200
+ const box = trigger?.closest<HTMLElement>("[data-field-box]") ?? null;
201
+ setAnchor(box && !standalone ? { current: box } : null);
202
+ setBoxWidth(box && !standalone ? box.getBoundingClientRect().width : undefined);
203
+ } else {
204
+ setTerm("");
205
+ onSearchChange?.("");
206
+ }
207
+ setOpen(next);
208
+ };
209
+
210
+ const shown =
211
+ filter && term
212
+ ? options.filter((o) => o.label.toLowerCase().includes(term.toLowerCase()))
213
+ : options;
214
+
215
+ /* Typeahead: letters typed in the list jump to the next matching option. */
216
+ const typeahead = React.useRef({ buffer: "", timer: undefined as ReturnType<typeof setTimeout> | undefined });
217
+ const onListKeyDown = (event: React.KeyboardEvent) => {
218
+ if (event.key.length !== 1 || event.metaKey || event.ctrlKey || event.altKey || event.key === " ") return;
219
+ if ((event.target as HTMLElement).tagName === "INPUT") return;
220
+ const state = typeahead.current;
221
+ clearTimeout(state.timer);
222
+ state.timer = setTimeout(() => (state.buffer = ""), 500);
223
+ const char = event.key.toLowerCase();
224
+ // Repeating one letter cycles through the options starting with it.
225
+ state.buffer = state.buffer === char ? char : state.buffer + char;
226
+ const enabled = shown.filter((o) => !o.disabled);
227
+ const focused = (document.activeElement as HTMLElement | null)?.closest<HTMLElement>("[data-option-item]")
228
+ ?.dataset.optionItem;
229
+ const start = Math.max(0, enabled.findIndex((o) => o.value === focused));
230
+ const ordered = [...enabled.slice(start + 1), ...enabled.slice(0, start + 1)];
231
+ const match = ordered.find((o) => o.label.toLowerCase().startsWith(state.buffer));
232
+ if (!match) return;
233
+ event.preventDefault();
234
+ contentRef.current
235
+ ?.querySelector<HTMLElement>(`[data-option-item="${CSS.escape(match.value)}"] [role="option"]`)
236
+ ?.focus();
237
+ };
238
+
239
+ const selectedText = selected.map((o) => o.label).join(", ");
240
+ const showValue = !multiSelect || selected.length === 0;
241
+ const compact = field ? field.inline || field.labelPosition === "outside" : false;
242
+
243
+ const list = (
244
+ <OptionList
245
+ id={listId}
246
+ label={label}
247
+ multiSelect={multiSelect}
248
+ allowDeselect={allowDeselect}
249
+ value={value}
250
+ onValueChange={(next) => {
251
+ update(next);
252
+ if (!multiSelect) openChange(false);
253
+ }}
254
+ className="p-1"
255
+ >
256
+ {groupBySection(shown).map(([section, items], index) => {
257
+ const rendered = items.map((option) => (
258
+ <OptionListItem
259
+ key={option.value}
260
+ value={option.value}
261
+ label={option.label}
262
+ description={option.description}
263
+ icon={option.icon}
264
+ avatar={option.avatar}
265
+ disabled={option.disabled}
266
+ minWidth="0"
267
+ />
268
+ ));
269
+ return section ? (
270
+ <OptionListSection key={section} label={section}>
271
+ {rendered}
272
+ </OptionListSection>
273
+ ) : (
274
+ <React.Fragment key={`items-${index}`}>{rendered}</React.Fragment>
275
+ );
276
+ })}
277
+ </OptionList>
278
+ );
279
+
280
+ return (
281
+ <Popover open={open} onOpenChange={openChange}>
282
+ {!mobile && anchor && <PopoverAnchor virtualRef={anchor} />}
283
+ {/* Inside a field the indicator centres on the field box, as Swirl's does. */}
284
+ <div className={cn("flex w-full items-center", standalone && "relative w-fit", className)}>
285
+ <PopoverTrigger asChild>
286
+ <button
287
+ type="button"
288
+ id={triggerId}
289
+ role="combobox"
290
+ aria-haspopup="listbox"
291
+ aria-expanded={open}
292
+ aria-controls={open ? listId : undefined}
293
+ aria-describedby={field?.describedBy}
294
+ aria-invalid={field?.invalid || undefined}
295
+ aria-required={field?.required || undefined}
296
+ aria-readonly={readOnly || undefined}
297
+ disabled={disabled}
298
+ onKeyDown={(event) => {
299
+ if (event.key === "ArrowDown" && !open) {
300
+ event.preventDefault();
301
+ openChange(true);
302
+ }
303
+ }}
304
+ className={cn(
305
+ "flex min-w-0 cursor-pointer items-center border-0 bg-transparent p-0 text-left text-foreground",
306
+ "outline-none disabled:cursor-default disabled:text-disabled",
307
+ standalone
308
+ ? cn(
309
+ "gap-2 rounded-xs font-medium",
310
+ "outline-focus focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid",
311
+ "text-base leading-6 desktop:text-sm desktop:leading-5",
312
+ )
313
+ : cn("w-full pr-8", field && fieldTextSize(field)),
314
+ // With tags, the trigger turns invisible behind them, as in Swirl:
315
+ // it keeps focus and names the choices, and the tags take its place.
316
+ !showValue && "pointer-events-none absolute inset-0 opacity-0",
317
+ )}
318
+ >
319
+ <span
320
+ className={cn(
321
+ "min-w-0 truncate",
322
+ selected.length === 0 && "text-subdued",
323
+ // While the field's label stands in for it, the placeholder hides.
324
+ selected.length === 0 && field?.labelAsPlaceholder && "text-transparent",
325
+ )}
326
+ >
327
+ {selectedText || placeholder || " "}
328
+ </span>
329
+ {standalone && (
330
+ <span aria-hidden className="inline-flex size-5 shrink-0 text-icon-default [&>svg]:size-full">
331
+ <Glyph name={open ? "expandLess" : "expandMore"} />
332
+ </span>
333
+ )}
334
+ </button>
335
+ </PopoverTrigger>
336
+ {!showValue && (
337
+ // Clicks between the tags reach the field box, which opens the list.
338
+ <span className="flex w-full min-w-0 flex-wrap gap-2 pr-8">
339
+ {selected.map((option) => (
340
+ <Tag
341
+ key={option.value}
342
+ size="s"
343
+ label={option.label}
344
+ className="h-5 px-1 py-0"
345
+ removable={!disabled && !readOnly && allowDeselect && !option.disabled}
346
+ onRemove={() => update(value.filter((v) => v !== option.value))}
347
+ />
348
+ ))}
349
+ </span>
350
+ )}
351
+ {!standalone && (
352
+ <span
353
+ aria-hidden
354
+ className={cn(
355
+ "pointer-events-none absolute top-1/2 inline-flex size-6 text-icon-default [&>svg]:size-full",
356
+ // Swirl centres it on the value 8px above, which lands 2px below the box's middle.
357
+ compact ? "right-3 -translate-y-1/2" : "right-4 -translate-y-[calc(50%-2px)]",
358
+ disabled && "text-disabled",
359
+ )}
360
+ >
361
+ <Glyph name={open ? "expandLess" : "expandMore"} />
362
+ </span>
363
+ )}
364
+ {name &&
365
+ (value.length > 0 ? value : [""]).map((v, index) => (
366
+ <input key={`${v}-${index}`} type="hidden" name={name} value={v} />
367
+ ))}
368
+ </div>
369
+
370
+ <PopoverContent
371
+ ref={contentRef}
372
+ label={label}
373
+ padded={false}
374
+ align="start"
375
+ sideOffset={1}
376
+ style={boxWidth && !mobile ? { width: boxWidth, maxWidth: "none" } : undefined}
377
+ className="flex flex-col p-0"
378
+ onKeyDown={onListKeyDown}
379
+ onOpenAutoFocus={(event) => {
380
+ event.preventDefault();
381
+ // Focus the search box, else the selected option, else the first.
382
+ requestAnimationFrame(() => {
383
+ const root = contentRef.current;
384
+ if (!root) return;
385
+ const search = root.querySelector<HTMLElement>("input[type=search]");
386
+ const current = value[0]
387
+ ? root.querySelector<HTMLElement>(`[data-option-item="${CSS.escape(value[0])}"] [role="option"]`)
388
+ : null;
389
+ (search ?? current ?? root.querySelector<HTMLElement>('[role="option"]'))?.focus();
390
+ });
391
+ }}
392
+ >
393
+ {withSearch && (
394
+ <div className="sticky top-0 z-1 flex w-full shrink-0 border-b border-border px-2 py-px md:px-px md:pt-2">
395
+ <span
396
+ aria-hidden
397
+ className="pointer-events-none absolute top-1/2 left-4 inline-flex size-5 -translate-y-1/2 text-icon-default md:left-3 md:mt-1 [&>svg]:size-full"
398
+ >
399
+ <Glyph name="search" />
400
+ </span>
401
+ <input
402
+ type="search"
403
+ aria-label={searchInputLabel}
404
+ placeholder={searchPlaceholder}
405
+ value={term}
406
+ onChange={(event) => {
407
+ setTerm(event.target.value);
408
+ onSearchChange?.(event.target.value);
409
+ }}
410
+ onKeyDown={(event) => {
411
+ if (event.key === "ArrowDown") {
412
+ event.preventDefault();
413
+ contentRef.current?.querySelector<HTMLElement>('[role="option"]')?.focus();
414
+ }
415
+ }}
416
+ className={cn(
417
+ "h-10 w-full border-0 bg-transparent pr-3 pl-10 text-base leading-6 text-foreground",
418
+ "caret-border-highlight placeholder:text-subdued desktop:text-sm desktop:leading-5",
419
+ "outline-focus focus:not-focus-visible:outline-none focus-visible:outline-2",
420
+ "[&::-webkit-search-cancel-button]:appearance-none",
421
+ searchLoading && "pr-11",
422
+ )}
423
+ />
424
+ {searchLoading && (
425
+ <span className="absolute top-1/2 right-3 inline-flex -translate-y-1/2 md:mt-1">
426
+ <Spinner size="xs" label="Loading" />
427
+ </span>
428
+ )}
429
+ </div>
430
+ )}
431
+ <div className="min-h-0 overflow-auto">
432
+ {shown.length > 0 ? (
433
+ list
434
+ ) : (
435
+ !searchLoading && (
436
+ <div role="status" className="px-3 pt-3 pb-2 text-sm leading-5 font-medium text-subdued">
437
+ {emptyListLabel}
438
+ </div>
439
+ )
440
+ )}
441
+ </div>
442
+ </PopoverContent>
443
+ </Popover>
444
+ );
445
+ }
446
+
447
+ /** Groups consecutive options by section, keeping order. */
448
+ function groupBySection(options: OptionData[]): [string | undefined, OptionData[]][] {
449
+ const groups: [string | undefined, OptionData[]][] = [];
450
+ for (const option of options) {
451
+ const last = groups[groups.length - 1];
452
+ if (last && last[0] === option.section) last[1].push(option);
453
+ else groups.push([option.section, [option]]);
454
+ }
455
+ return groups;
456
+ }
@@ -0,0 +1,93 @@
1
+ import { cn } from "../lib/utils";
2
+
3
+ export type SeparatorSpacing = "0" | "2" | "4" | "8" | "12" | "16" | "24" | "32";
4
+
5
+ /* Space before and after the line, per Swirl spacing step. */
6
+ const paddingsY = {
7
+ "0": "py-0",
8
+ "2": "py-0.5",
9
+ "4": "py-1",
10
+ "8": "py-2",
11
+ "12": "py-3",
12
+ "16": "py-4",
13
+ "24": "py-6",
14
+ "32": "py-8",
15
+ } as const satisfies Record<SeparatorSpacing, string>;
16
+
17
+ const paddingsX = {
18
+ "0": "px-0",
19
+ "2": "px-0.5",
20
+ "4": "px-1",
21
+ "8": "px-2",
22
+ "12": "px-3",
23
+ "16": "px-4",
24
+ "24": "px-6",
25
+ "32": "px-8",
26
+ } as const satisfies Record<SeparatorSpacing, string>;
27
+
28
+ const colors = {
29
+ default: { line: "bg-[var(--s-border-on-translucent)]", label: "text-disabled" },
30
+ strong: { line: "bg-border-strong", label: "text-subdued" },
31
+ highlight: { line: "bg-border-highlight", label: "text-highlight" },
32
+ critical: { line: "bg-border-critical", label: "text-critical" },
33
+ } as const;
34
+
35
+ export interface SeparatorProps extends React.ComponentProps<"div"> {
36
+ orientation?: "horizontal" | "vertical";
37
+ /** Space on both sides of the line, in Swirl's px steps. Defaults to 8. */
38
+ spacing?: SeparatorSpacing;
39
+ color?: keyof typeof colors;
40
+ /** Text in the middle of a horizontal line, such as "or". */
41
+ label?: React.ReactNode;
42
+ /**
43
+ * Hide the separator from screen readers. Defaults to false: as in Swirl,
44
+ * it is announced as a separator.
45
+ */
46
+ decorative?: boolean;
47
+ }
48
+
49
+ /**
50
+ * Swirl's separator: a hairline with space on both sides, optionally split
51
+ * by a label.
52
+ */
53
+ export function Separator({
54
+ className,
55
+ orientation = "horizontal",
56
+ spacing = "8",
57
+ color = "default",
58
+ label,
59
+ decorative = false,
60
+ ...props
61
+ }: SeparatorProps) {
62
+ const vertical = orientation === "vertical";
63
+ const line = (
64
+ <span
65
+ className={cn(
66
+ "block shrink",
67
+ vertical ? "h-full min-h-[1.125rem] w-px" : "h-px w-full",
68
+ colors[color].line,
69
+ )}
70
+ />
71
+ );
72
+
73
+ return (
74
+ <div
75
+ role={decorative ? "none" : "separator"}
76
+ aria-orientation={decorative ? undefined : orientation}
77
+ className={cn(
78
+ "shrink-0 items-center gap-2",
79
+ vertical ? ["inline-flex flex-col", paddingsX[spacing]] : ["flex w-full", paddingsY[spacing]],
80
+ className,
81
+ )}
82
+ {...props}
83
+ >
84
+ {line}
85
+ {label && (
86
+ <>
87
+ <span className={cn("w-fit shrink-0 text-sm leading-[normal]", colors[color].label)}>{label}</span>
88
+ {line}
89
+ </>
90
+ )}
91
+ </div>
92
+ );
93
+ }