@enablo/swirlcn-ui 0.0.0-stage → 0.5.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/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-3DJWDNQL.js +77 -0
- package/dist/chunk-7XUCETA6.js +447 -0
- package/dist/chunk-KZESIYUN.js +18 -0
- package/dist/flip.d.ts +98 -0
- package/dist/flip.js +158 -0
- package/dist/fonts/Inter-italic.var.woff2 +0 -0
- package/dist/fonts/Inter.var.woff2 +0 -0
- package/dist/fonts/OFL.txt +96 -0
- package/dist/index.d.ts +2459 -0
- package/dist/index.js +8439 -0
- package/dist/server.d.ts +144 -0
- package/dist/server.js +88 -0
- package/dist/spacing-BQaEPbUv.d.ts +59 -0
- package/dist/theme.css +146 -0
- package/package.json +84 -3
- package/src/components/accordion.tsx +102 -0
- package/src/components/action-list.tsx +294 -0
- package/src/components/alert-dialog.tsx +155 -0
- package/src/components/autocomplete.tsx +313 -0
- package/src/components/avatar.tsx +258 -0
- package/src/components/badge.tsx +100 -0
- package/src/components/banner.tsx +112 -0
- package/src/components/button-group.tsx +87 -0
- package/src/components/button.tsx +362 -0
- package/src/components/card.tsx +186 -0
- package/src/components/carousel.tsx +245 -0
- package/src/components/checkbox.tsx +125 -0
- package/src/components/chip.tsx +153 -0
- package/src/components/color-input.tsx +98 -0
- package/src/components/data-cell.tsx +230 -0
- package/src/components/date-input.tsx +223 -0
- package/src/components/date-picker.tsx +276 -0
- package/src/components/description-list.tsx +97 -0
- package/src/components/dialog.tsx +405 -0
- package/src/components/empty-state.tsx +63 -0
- package/src/components/file-chip.tsx +206 -0
- package/src/components/file-uploader.tsx +148 -0
- package/src/components/flip-app-header.tsx +104 -0
- package/src/components/flip-brand.tsx +77 -0
- package/src/components/flip-theme.tsx +95 -0
- package/src/components/form-control.tsx +397 -0
- package/src/components/form-group.tsx +26 -0
- package/src/components/heading.tsx +70 -0
- package/src/components/icon.tsx +119 -0
- package/src/components/image-grid.tsx +285 -0
- package/src/components/inline-notification.tsx +167 -0
- package/src/components/inline-status.tsx +100 -0
- package/src/components/input.tsx +390 -0
- package/src/components/label.tsx +28 -0
- package/src/components/lightbox.tsx +314 -0
- package/src/components/link.tsx +46 -0
- package/src/components/list.tsx +40 -0
- package/src/components/menu.tsx +530 -0
- package/src/components/option-list.tsx +582 -0
- package/src/components/pagination.tsx +336 -0
- package/src/components/popover.tsx +194 -0
- package/src/components/progress.tsx +158 -0
- package/src/components/radio-group.tsx +136 -0
- package/src/components/resource-list.tsx +686 -0
- package/src/components/search.tsx +127 -0
- package/src/components/select.tsx +456 -0
- package/src/components/separator.tsx +93 -0
- package/src/components/skeleton-box.tsx +63 -0
- package/src/components/skeleton-text.tsx +66 -0
- package/src/components/spinner.tsx +83 -0
- package/src/components/stack.tsx +86 -0
- package/src/components/status-indicator.tsx +31 -0
- package/src/components/switch.tsx +90 -0
- package/src/components/table.tsx +242 -0
- package/src/components/tabs.tsx +147 -0
- package/src/components/tag.tsx +169 -0
- package/src/components/text.tsx +126 -0
- package/src/components/textarea.tsx +130 -0
- package/src/components/theme-provider.tsx +115 -0
- package/src/components/thumbnail.tsx +250 -0
- package/src/components/time-input.tsx +123 -0
- package/src/components/toast.tsx +217 -0
- package/src/components/toggle-group.tsx +124 -0
- package/src/components/toolbar.tsx +105 -0
- package/src/components/tooltip.tsx +97 -0
- package/src/components/tree-navigation.tsx +194 -0
- package/src/components/tree-view.tsx +292 -0
- package/src/flip.ts +12 -0
- package/src/index.ts +317 -0
- package/src/lib/date-mask.ts +176 -0
- package/src/lib/field-context.ts +86 -0
- package/src/lib/flip-app-script.ts +38 -0
- package/src/lib/flip-host.ts +84 -0
- package/src/lib/flip-theme-css.ts +90 -0
- package/src/lib/focus.ts +18 -0
- package/src/lib/glyphs.ts +137 -0
- package/src/lib/spacing.ts +93 -0
- package/src/lib/theme-context.ts +27 -0
- package/src/lib/theme-script.ts +49 -0
- package/src/lib/use-mobile.ts +17 -0
- package/src/lib/utils.ts +28 -0
- package/src/server.ts +20 -0
- package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
- package/src/styles/fonts/Inter.var.woff2 +0 -0
- package/src/styles/fonts/OFL.txt +96 -0
- package/src/styles/theme.css +146 -0
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
import { ActionListSection, type ActionListSectionProps } from "./action-list";
|
|
4
|
+
|
|
5
|
+
/* ---------------------------------------------------------------- icons */
|
|
6
|
+
|
|
7
|
+
const CheckSmallIcon = () => (
|
|
8
|
+
<svg viewBox="0 0 24 24" aria-hidden focusable={false}>
|
|
9
|
+
<path
|
|
10
|
+
d="M18.6213 7.37879C19.2071 7.96458 19.2071 8.91433 18.6213 9.50011L10.6213 17.5C10.0355 18.0858 9.08577 18.0858 8.49998 17.5L5.56068 14.5605C4.97489 13.9748 4.97489 13.025 5.56068 12.4392C6.14646 11.8534 7.09621 11.8534 7.682 12.4392L9.56064 14.318L16.5 7.37879C17.0858 6.79301 18.0355 6.79301 18.6213 7.37879Z"
|
|
11
|
+
fill="currentColor"
|
|
12
|
+
/>
|
|
13
|
+
</svg>
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const CheckStrongIcon = () => (
|
|
17
|
+
<svg viewBox="0 0 16 16" className="size-4 shrink-0" aria-hidden focusable={false}>
|
|
18
|
+
<path
|
|
19
|
+
fillRule="evenodd"
|
|
20
|
+
clipRule="evenodd"
|
|
21
|
+
d="M13.0404 4.29289C13.431 4.68342 13.431 5.31658 13.0404 5.70711L6.7071 12.0404C6.31657 12.431 5.68341 12.431 5.29288 12.0404L2.95955 9.70711C2.56903 9.31658 2.56903 8.68342 2.95955 8.29289C3.35007 7.90237 3.98324 7.90237 4.37376 8.29289L5.99999 9.91912L11.6262 4.29289C12.0167 3.90237 12.6499 3.90237 13.0404 4.29289Z"
|
|
22
|
+
fill="currentColor"
|
|
23
|
+
/>
|
|
24
|
+
</svg>
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const DragHandleIcon = () => (
|
|
28
|
+
<svg viewBox="0 0 24 24" aria-hidden focusable={false}>
|
|
29
|
+
{[4.5, 10.5, 16.5].flatMap((y) =>
|
|
30
|
+
[7.5, 13.5].map((x) => (
|
|
31
|
+
<rect key={`${x}-${y}`} x={x} y={y} width="3" height="3" rx="1.5" fill="currentColor" />
|
|
32
|
+
)),
|
|
33
|
+
)}
|
|
34
|
+
</svg>
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
/* -------------------------------------------------------------- context */
|
|
38
|
+
|
|
39
|
+
const SELECT_ALL = "\u0000select-all";
|
|
40
|
+
|
|
41
|
+
interface Drag {
|
|
42
|
+
value: string;
|
|
43
|
+
from: number;
|
|
44
|
+
/** The item values in their previewed order, shown with CSS `order`. */
|
|
45
|
+
order: string[];
|
|
46
|
+
pointer: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface OptionListContextValue {
|
|
50
|
+
multiSelect: boolean;
|
|
51
|
+
disabled: boolean;
|
|
52
|
+
allowDrag: boolean;
|
|
53
|
+
isSelected: (value: string) => boolean;
|
|
54
|
+
toggle: (value: string) => void;
|
|
55
|
+
tabStop: string | undefined;
|
|
56
|
+
setActive: (value: string) => void;
|
|
57
|
+
register: (value: string) => () => void;
|
|
58
|
+
drag: Drag | null;
|
|
59
|
+
startDrag: (value: string, pointer: boolean) => void;
|
|
60
|
+
moveDrag: (step: -1 | 1) => void;
|
|
61
|
+
pointerDrag: (clientY: number) => void;
|
|
62
|
+
endDrag: (cancel?: boolean) => void;
|
|
63
|
+
dragHandleLabel: string;
|
|
64
|
+
dragHandleDescription: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const OptionListContext = React.createContext<OptionListContextValue | null>(null);
|
|
68
|
+
|
|
69
|
+
function useOptionList() {
|
|
70
|
+
const context = React.useContext(OptionListContext);
|
|
71
|
+
if (!context) throw new Error("OptionListItem must be used inside an OptionList.");
|
|
72
|
+
return context;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* ----------------------------------------------------------------- list */
|
|
76
|
+
|
|
77
|
+
export interface OptionListItemDrop {
|
|
78
|
+
/** The moved item's value. */
|
|
79
|
+
value: string;
|
|
80
|
+
oldIndex: number;
|
|
81
|
+
newIndex: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface OptionListProps
|
|
85
|
+
extends Omit<React.ComponentProps<"div">, "defaultValue" | "onChange"> {
|
|
86
|
+
/** Accessible name of the listbox. */
|
|
87
|
+
label?: string;
|
|
88
|
+
/** Let several options be chosen; each shows a checkbox instead of its icon. */
|
|
89
|
+
multiSelect?: boolean;
|
|
90
|
+
/** The chosen values, controlled. Swirl's value is always an array. */
|
|
91
|
+
value?: string[];
|
|
92
|
+
defaultValue?: string[];
|
|
93
|
+
onValueChange?: (value: string[]) => void;
|
|
94
|
+
/** Whether choosing a selected option again deselects it. Defaults to true. */
|
|
95
|
+
allowDeselect?: boolean;
|
|
96
|
+
disabled?: boolean;
|
|
97
|
+
/** Add a "Select all" option above the others. Multi-select only. */
|
|
98
|
+
showSelectAll?: boolean;
|
|
99
|
+
selectAllLabel?: string;
|
|
100
|
+
/**
|
|
101
|
+
* Show a drag handle on each item, to reorder by pointer or keyboard
|
|
102
|
+
* (Space to grab, arrows to move, Space to drop, Escape to cancel). Only for
|
|
103
|
+
* multi-select lists without sections, as in Swirl. The list does not
|
|
104
|
+
* reorder its children itself: apply `onItemDrop` to your data.
|
|
105
|
+
*/
|
|
106
|
+
allowDrag?: boolean;
|
|
107
|
+
onItemDrop?: (drop: OptionListItemDrop) => void;
|
|
108
|
+
dragHandleLabel?: string;
|
|
109
|
+
dragHandleDescription?: string;
|
|
110
|
+
assistiveTextItemGrabbed?: string;
|
|
111
|
+
assistiveTextItemMoving?: string;
|
|
112
|
+
assistiveTextItemMoved?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Swirl's option list: a listbox for choosing one or several options, used
|
|
117
|
+
* on its own, in a popover or as the menu of a select.
|
|
118
|
+
*
|
|
119
|
+
* One tab stop; arrow keys move between options, Home and End jump to the
|
|
120
|
+
* ends, and Space or Enter chooses. Cmd/Ctrl+A selects all in a multi-select
|
|
121
|
+
* list.
|
|
122
|
+
*/
|
|
123
|
+
export function OptionList({
|
|
124
|
+
className,
|
|
125
|
+
label,
|
|
126
|
+
multiSelect = false,
|
|
127
|
+
value: valueProp,
|
|
128
|
+
defaultValue = [],
|
|
129
|
+
onValueChange,
|
|
130
|
+
allowDeselect = true,
|
|
131
|
+
disabled = false,
|
|
132
|
+
showSelectAll = false,
|
|
133
|
+
selectAllLabel = "Select all",
|
|
134
|
+
allowDrag = false,
|
|
135
|
+
onItemDrop,
|
|
136
|
+
dragHandleLabel = "Move option",
|
|
137
|
+
dragHandleDescription = "Press spacebar to toggle grab",
|
|
138
|
+
assistiveTextItemGrabbed = "Item grabbed. Use arrow keys to move item up or down. Use spacebar to save position.",
|
|
139
|
+
assistiveTextItemMoving = "Current position:",
|
|
140
|
+
assistiveTextItemMoved = "Item moved. New position:",
|
|
141
|
+
onKeyDown,
|
|
142
|
+
children,
|
|
143
|
+
...props
|
|
144
|
+
}: OptionListProps) {
|
|
145
|
+
const ref = React.useRef<HTMLDivElement>(null);
|
|
146
|
+
const [uncontrolled, setUncontrolled] = React.useState(defaultValue);
|
|
147
|
+
const value = valueProp ?? uncontrolled;
|
|
148
|
+
const [items, setItems] = React.useState<string[]>([]);
|
|
149
|
+
const [active, setActive] = React.useState<string>();
|
|
150
|
+
const [drag, setDrag] = React.useState<Drag | null>(null);
|
|
151
|
+
const [assistiveText, setAssistiveText] = React.useState("");
|
|
152
|
+
|
|
153
|
+
const selectAll = multiSelect && showSelectAll;
|
|
154
|
+
const canDrag = allowDrag && multiSelect;
|
|
155
|
+
|
|
156
|
+
React.useEffect(() => {
|
|
157
|
+
if (allowDrag && !multiSelect) {
|
|
158
|
+
console.error("[OptionList] Drag can only be allowed for multi select lists.");
|
|
159
|
+
} else if (allowDrag && ref.current?.querySelector('[role="group"]')) {
|
|
160
|
+
console.error("[OptionList] Drag can only be allowed for lists without sections.");
|
|
161
|
+
}
|
|
162
|
+
}, [allowDrag, multiSelect]);
|
|
163
|
+
|
|
164
|
+
const update = (next: string[]) => {
|
|
165
|
+
if (valueProp === undefined) setUncontrolled(next);
|
|
166
|
+
onValueChange?.(next);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/** Item values in document order. */
|
|
170
|
+
const itemValues = () =>
|
|
171
|
+
Array.from(ref.current?.querySelectorAll<HTMLElement>("[data-option-item]") ?? []).map(
|
|
172
|
+
(el) => el.dataset.optionItem!,
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
const toggleAll = () => {
|
|
176
|
+
if (disabled) return;
|
|
177
|
+
const all = itemValues();
|
|
178
|
+
update(all.length > 0 && all.every((v) => value.includes(v)) ? [] : all);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const toggle = (item: string) => {
|
|
182
|
+
if (disabled) return;
|
|
183
|
+
if (item === SELECT_ALL) return toggleAll();
|
|
184
|
+
const selected = value.includes(item);
|
|
185
|
+
if (selected && !allowDeselect) return;
|
|
186
|
+
if (!multiSelect) return update(selected ? [] : [item]);
|
|
187
|
+
update(selected ? value.filter((v) => v !== item) : [...value, item]);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const focusOption = (item: string) => {
|
|
191
|
+
const selector =
|
|
192
|
+
item === SELECT_ALL ? "[data-select-all]" : `[data-option-item="${CSS.escape(item)}"]`;
|
|
193
|
+
ref.current?.querySelector<HTMLElement>(`${selector} [role="option"]`)?.focus();
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const endDrag = (cancel = false) => {
|
|
197
|
+
if (!drag) return;
|
|
198
|
+
setDrag(null);
|
|
199
|
+
const newIndex = drag.order.indexOf(drag.value);
|
|
200
|
+
if (cancel) {
|
|
201
|
+
setAssistiveText("");
|
|
202
|
+
} else {
|
|
203
|
+
setAssistiveText(`${assistiveTextItemMoved} ${newIndex + 1}`);
|
|
204
|
+
onItemDrop?.({ value: drag.value, oldIndex: drag.from, newIndex });
|
|
205
|
+
}
|
|
206
|
+
// The drop may reorder the DOM, which can drop focus; put it back.
|
|
207
|
+
requestAnimationFrame(() => focusOption(drag.value));
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const context: OptionListContextValue = {
|
|
211
|
+
multiSelect,
|
|
212
|
+
disabled,
|
|
213
|
+
allowDrag: canDrag,
|
|
214
|
+
isSelected: (item) => value.includes(item),
|
|
215
|
+
toggle,
|
|
216
|
+
tabStop:
|
|
217
|
+
active !== undefined && (active === SELECT_ALL ? selectAll : items.includes(active))
|
|
218
|
+
? active
|
|
219
|
+
: selectAll
|
|
220
|
+
? SELECT_ALL
|
|
221
|
+
: items[0],
|
|
222
|
+
setActive,
|
|
223
|
+
register: (item) => {
|
|
224
|
+
setItems((prev) => [...prev, item]);
|
|
225
|
+
return () => setItems((prev) => prev.filter((v) => v !== item));
|
|
226
|
+
},
|
|
227
|
+
drag,
|
|
228
|
+
startDrag: (item, pointer) => {
|
|
229
|
+
if (disabled) return;
|
|
230
|
+
const order = itemValues();
|
|
231
|
+
setDrag({ value: item, from: order.indexOf(item), order, pointer });
|
|
232
|
+
if (!pointer) setAssistiveText(assistiveTextItemGrabbed);
|
|
233
|
+
},
|
|
234
|
+
moveDrag: (step) => {
|
|
235
|
+
if (!drag) return;
|
|
236
|
+
const index = drag.order.indexOf(drag.value);
|
|
237
|
+
const to = index + step;
|
|
238
|
+
if (to < 0 || to >= drag.order.length) return;
|
|
239
|
+
const order = drag.order.filter((v) => v !== drag.value);
|
|
240
|
+
order.splice(to, 0, drag.value);
|
|
241
|
+
setDrag({ ...drag, order });
|
|
242
|
+
setAssistiveText(`${assistiveTextItemMoving} ${to + 1}`);
|
|
243
|
+
},
|
|
244
|
+
pointerDrag: (clientY) => {
|
|
245
|
+
if (!drag || !ref.current) return;
|
|
246
|
+
const others = drag.order.filter((v) => v !== drag.value);
|
|
247
|
+
// Slot the item after every other item whose middle is above the pointer.
|
|
248
|
+
const to = others.filter((v) => {
|
|
249
|
+
const el = ref.current!.querySelector(`[data-option-item="${CSS.escape(v)}"]`);
|
|
250
|
+
if (!el) return false;
|
|
251
|
+
const rect = el.getBoundingClientRect();
|
|
252
|
+
return rect.top + rect.height / 2 < clientY;
|
|
253
|
+
}).length;
|
|
254
|
+
if (drag.order.indexOf(drag.value) === to) return;
|
|
255
|
+
others.splice(to, 0, drag.value);
|
|
256
|
+
setDrag({ ...drag, order: others });
|
|
257
|
+
},
|
|
258
|
+
endDrag,
|
|
259
|
+
dragHandleLabel,
|
|
260
|
+
dragHandleDescription,
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const selectAllState =
|
|
264
|
+
items.length === 0 || value.length === 0
|
|
265
|
+
? false
|
|
266
|
+
: items.every((v) => value.includes(v))
|
|
267
|
+
? true
|
|
268
|
+
: "indeterminate";
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<OptionListContext.Provider value={context}>
|
|
272
|
+
<span className="sr-only" role="alert">
|
|
273
|
+
{assistiveText}
|
|
274
|
+
</span>
|
|
275
|
+
<div
|
|
276
|
+
ref={ref}
|
|
277
|
+
role="listbox"
|
|
278
|
+
aria-label={label}
|
|
279
|
+
aria-multiselectable={multiSelect || undefined}
|
|
280
|
+
className={cn("flex w-full flex-col", className)}
|
|
281
|
+
onKeyDown={(event) => {
|
|
282
|
+
onKeyDown?.(event);
|
|
283
|
+
if (event.defaultPrevented || drag || disabled) return;
|
|
284
|
+
const list = ref.current;
|
|
285
|
+
if (!list) return;
|
|
286
|
+
const options = Array.from(list.querySelectorAll<HTMLElement>('[role="option"]'));
|
|
287
|
+
const row = (document.activeElement as HTMLElement | null)?.closest(
|
|
288
|
+
"[data-option-item],[data-select-all]",
|
|
289
|
+
);
|
|
290
|
+
const current = row ? options.indexOf(row.querySelector('[role="option"]')!) : -1;
|
|
291
|
+
const target = {
|
|
292
|
+
ArrowDown: Math.min(current + 1, options.length - 1),
|
|
293
|
+
ArrowUp: Math.max(current - 1, 0),
|
|
294
|
+
Home: 0,
|
|
295
|
+
End: options.length - 1,
|
|
296
|
+
}[event.key];
|
|
297
|
+
if (target !== undefined) {
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
options[target]?.focus();
|
|
300
|
+
} else if (event.key.toLowerCase() === "a" && (event.metaKey || event.ctrlKey) && multiSelect) {
|
|
301
|
+
event.preventDefault();
|
|
302
|
+
toggleAll();
|
|
303
|
+
}
|
|
304
|
+
}}
|
|
305
|
+
{...props}
|
|
306
|
+
>
|
|
307
|
+
{selectAll && (
|
|
308
|
+
<OptionRow
|
|
309
|
+
value={SELECT_ALL}
|
|
310
|
+
label={selectAllLabel}
|
|
311
|
+
selected={selectAllState === true}
|
|
312
|
+
indeterminate={selectAllState === "indeterminate"}
|
|
313
|
+
disabled={disabled}
|
|
314
|
+
dragEnabled={false}
|
|
315
|
+
/>
|
|
316
|
+
)}
|
|
317
|
+
{children}
|
|
318
|
+
</div>
|
|
319
|
+
</OptionListContext.Provider>
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/* ----------------------------------------------------------------- item */
|
|
324
|
+
|
|
325
|
+
export interface OptionListItemProps
|
|
326
|
+
extends Omit<React.ComponentProps<"div">, "children" | "onSelect"> {
|
|
327
|
+
/** The option's name. Interchangeable with `children`. */
|
|
328
|
+
label?: React.ReactNode;
|
|
329
|
+
children?: React.ReactNode;
|
|
330
|
+
value: string;
|
|
331
|
+
/** Secondary text under the label. */
|
|
332
|
+
description?: React.ReactNode;
|
|
333
|
+
/** Leading icon, shown in single-select lists. 24px, or 20px with the desktop layout. */
|
|
334
|
+
icon?: React.ReactNode;
|
|
335
|
+
/** Small marker over the icon's corner. */
|
|
336
|
+
iconBadge?: React.ReactNode;
|
|
337
|
+
/** Leading avatar. */
|
|
338
|
+
avatar?: React.ReactNode;
|
|
339
|
+
disabled?: boolean;
|
|
340
|
+
/** Let a long label wrap instead of truncating. */
|
|
341
|
+
truncateLabel?: boolean;
|
|
342
|
+
/** Defaults to 15rem, as in Swirl. */
|
|
343
|
+
minWidth?: string;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** One option. Selected options take the highlight colour and a check. */
|
|
347
|
+
export function OptionListItem({ value, ...props }: OptionListItemProps) {
|
|
348
|
+
const { register, isSelected, allowDrag } = useOptionList();
|
|
349
|
+
|
|
350
|
+
React.useLayoutEffect(() => register(value), [value]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
351
|
+
|
|
352
|
+
return <OptionRow value={value} selected={isSelected(value)} dragEnabled={allowDrag} {...props} />;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
interface OptionRowProps extends Omit<OptionListItemProps, "value"> {
|
|
356
|
+
value: string;
|
|
357
|
+
selected: boolean;
|
|
358
|
+
indeterminate?: boolean;
|
|
359
|
+
dragEnabled: boolean;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function OptionRow({
|
|
363
|
+
className,
|
|
364
|
+
style,
|
|
365
|
+
value,
|
|
366
|
+
label,
|
|
367
|
+
children,
|
|
368
|
+
description,
|
|
369
|
+
icon,
|
|
370
|
+
iconBadge,
|
|
371
|
+
avatar,
|
|
372
|
+
disabled: disabledProp,
|
|
373
|
+
truncateLabel = true,
|
|
374
|
+
minWidth = "15rem",
|
|
375
|
+
selected,
|
|
376
|
+
indeterminate = false,
|
|
377
|
+
dragEnabled,
|
|
378
|
+
onClick,
|
|
379
|
+
onKeyDown,
|
|
380
|
+
onFocus,
|
|
381
|
+
...props
|
|
382
|
+
}: OptionRowProps) {
|
|
383
|
+
const list = useOptionList();
|
|
384
|
+
const id = React.useId();
|
|
385
|
+
const [focusWithin, setFocusWithin] = React.useState(false);
|
|
386
|
+
const isSelectAll = value === SELECT_ALL;
|
|
387
|
+
const disabled = disabledProp || list.disabled;
|
|
388
|
+
const context = isSelectAll || list.multiSelect ? "multi" : "single";
|
|
389
|
+
const dragging = list.drag?.value === value;
|
|
390
|
+
const order = list.drag && !isSelectAll ? list.drag.order.indexOf(value) : undefined;
|
|
391
|
+
|
|
392
|
+
return (
|
|
393
|
+
<div
|
|
394
|
+
data-option-item={isSelectAll ? undefined : value}
|
|
395
|
+
data-select-all={isSelectAll || undefined}
|
|
396
|
+
className="relative block w-full"
|
|
397
|
+
style={{ order }}
|
|
398
|
+
onFocus={() => setFocusWithin(true)}
|
|
399
|
+
onBlur={(event) => {
|
|
400
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setFocusWithin(false);
|
|
401
|
+
}}
|
|
402
|
+
>
|
|
403
|
+
<div
|
|
404
|
+
role="option"
|
|
405
|
+
id={`${id}-option`}
|
|
406
|
+
aria-selected={indeterminate ? undefined : selected}
|
|
407
|
+
aria-disabled={disabled || undefined}
|
|
408
|
+
aria-labelledby={`${id}-label`}
|
|
409
|
+
aria-describedby={description ? `${id}-description` : undefined}
|
|
410
|
+
tabIndex={list.tabStop === value ? 0 : -1}
|
|
411
|
+
className={cn(
|
|
412
|
+
"relative inline-flex min-h-12 w-full cursor-pointer items-center gap-3 rounded-sm bg-transparent px-4 py-3",
|
|
413
|
+
"desktop:min-h-0 desktop:gap-2 desktop:px-3 desktop:py-2",
|
|
414
|
+
!disabled && "hover:bg-state-hovered active:bg-translucent-low-pressed",
|
|
415
|
+
"outline-none focus-visible:z-1 focus-visible:bg-state-hovered focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-focus",
|
|
416
|
+
dragging &&
|
|
417
|
+
"z-1 rounded-(--radius-s) bg-translucent-low-pressed shadow-[0_0.0625rem_0.125rem_rgba(25,26,28,0.08),0_0.25rem_2rem_rgba(25,26,28,0.16)] hover:bg-translucent-low-pressed",
|
|
418
|
+
disabled && "cursor-default",
|
|
419
|
+
className,
|
|
420
|
+
)}
|
|
421
|
+
style={{ minWidth, ...style }}
|
|
422
|
+
onClick={(event) => {
|
|
423
|
+
onClick?.(event);
|
|
424
|
+
if (event.defaultPrevented) return;
|
|
425
|
+
event.currentTarget.focus();
|
|
426
|
+
if (!disabled) list.toggle(value);
|
|
427
|
+
}}
|
|
428
|
+
onKeyDown={(event) => {
|
|
429
|
+
onKeyDown?.(event);
|
|
430
|
+
if (event.defaultPrevented) return;
|
|
431
|
+
if (event.key === " " || event.key === "Enter") {
|
|
432
|
+
event.preventDefault();
|
|
433
|
+
if (!disabled) list.toggle(value);
|
|
434
|
+
}
|
|
435
|
+
}}
|
|
436
|
+
onFocus={(event) => {
|
|
437
|
+
onFocus?.(event);
|
|
438
|
+
list.setActive(value);
|
|
439
|
+
}}
|
|
440
|
+
{...props}
|
|
441
|
+
>
|
|
442
|
+
{context === "single" && icon && (
|
|
443
|
+
<span
|
|
444
|
+
className={cn(
|
|
445
|
+
"relative inline-flex size-6 shrink-0 desktop:size-5 [&>svg]:size-full",
|
|
446
|
+
disabled ? "text-disabled" : selected ? "text-highlight" : "text-icon-default",
|
|
447
|
+
)}
|
|
448
|
+
>
|
|
449
|
+
{icon}
|
|
450
|
+
{iconBadge && <span className="absolute right-0 bottom-0 grid">{iconBadge}</span>}
|
|
451
|
+
</span>
|
|
452
|
+
)}
|
|
453
|
+
{context === "multi" && (
|
|
454
|
+
<span className="inline-flex size-6 shrink-0 p-0.5 desktop:size-5.5">
|
|
455
|
+
<span
|
|
456
|
+
className={cn(
|
|
457
|
+
"inline-flex size-full items-center justify-center rounded-(--radius-s) border-2",
|
|
458
|
+
disabled
|
|
459
|
+
? selected || indeterminate
|
|
460
|
+
? "border-disabled bg-disabled text-on-status"
|
|
461
|
+
: "border-disabled bg-surface-overlay"
|
|
462
|
+
: selected || indeterminate
|
|
463
|
+
? "border-highlight bg-highlight text-on-status"
|
|
464
|
+
: "border-icon-default",
|
|
465
|
+
)}
|
|
466
|
+
>
|
|
467
|
+
{selected ? (
|
|
468
|
+
<CheckStrongIcon />
|
|
469
|
+
) : (
|
|
470
|
+
indeterminate && <span className="h-0.5 w-2.5 rounded-[1px] bg-current" />
|
|
471
|
+
)}
|
|
472
|
+
</span>
|
|
473
|
+
</span>
|
|
474
|
+
)}
|
|
475
|
+
{avatar && (
|
|
476
|
+
<span className={cn("inline-flex shrink-0", disabled && "grayscale")}>{avatar}</span>
|
|
477
|
+
)}
|
|
478
|
+
<span
|
|
479
|
+
className={cn(
|
|
480
|
+
"flex min-w-0 grow flex-col overflow-hidden text-left text-base leading-6 font-medium",
|
|
481
|
+
"desktop:text-sm desktop:leading-5",
|
|
482
|
+
dragEnabled && "pr-10 desktop:pr-8",
|
|
483
|
+
)}
|
|
484
|
+
>
|
|
485
|
+
<span
|
|
486
|
+
id={`${id}-label`}
|
|
487
|
+
className={cn(
|
|
488
|
+
"block overflow-hidden text-ellipsis",
|
|
489
|
+
truncateLabel ? "whitespace-nowrap" : "whitespace-normal",
|
|
490
|
+
disabled ? "text-disabled" : selected ? "text-highlight" : "text-foreground",
|
|
491
|
+
)}
|
|
492
|
+
>
|
|
493
|
+
{label ?? children}
|
|
494
|
+
</span>
|
|
495
|
+
{description && (
|
|
496
|
+
<span
|
|
497
|
+
id={`${id}-description`}
|
|
498
|
+
className={cn(
|
|
499
|
+
"block truncate",
|
|
500
|
+
disabled ? "text-disabled" : "text-subdued",
|
|
501
|
+
)}
|
|
502
|
+
>
|
|
503
|
+
{description}
|
|
504
|
+
</span>
|
|
505
|
+
)}
|
|
506
|
+
</span>
|
|
507
|
+
{context === "single" && selected && (
|
|
508
|
+
<span
|
|
509
|
+
className={cn(
|
|
510
|
+
"inline-flex shrink-0 [&>svg]:size-6 desktop:[&>svg]:size-5",
|
|
511
|
+
disabled ? "text-disabled" : "text-highlight",
|
|
512
|
+
)}
|
|
513
|
+
>
|
|
514
|
+
<CheckSmallIcon />
|
|
515
|
+
</span>
|
|
516
|
+
)}
|
|
517
|
+
</div>
|
|
518
|
+
{dragEnabled && (
|
|
519
|
+
<button
|
|
520
|
+
type="button"
|
|
521
|
+
aria-label={`${list.dragHandleLabel} "${typeof label === "string" ? label : ""}"`}
|
|
522
|
+
aria-description={list.dragHandleDescription}
|
|
523
|
+
tabIndex={focusWithin ? 0 : -1}
|
|
524
|
+
className={cn(
|
|
525
|
+
"absolute top-1/2 right-1 z-1 inline-flex -translate-y-1/2 border-none bg-transparent p-2",
|
|
526
|
+
"cursor-grab text-icon-default active:cursor-grabbing [&>svg]:size-6 desktop:[&>svg]:size-5",
|
|
527
|
+
"touch-none outline-focus focus:not-focus-visible:outline-none",
|
|
528
|
+
list.drag?.pointer && dragging && "cursor-grabbing",
|
|
529
|
+
)}
|
|
530
|
+
onPointerDown={(event) => {
|
|
531
|
+
if (event.button !== 0 || disabled) return;
|
|
532
|
+
event.preventDefault();
|
|
533
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
534
|
+
list.startDrag(value, true);
|
|
535
|
+
}}
|
|
536
|
+
onPointerMove={(event) => {
|
|
537
|
+
if (list.drag?.pointer && dragging) list.pointerDrag(event.clientY);
|
|
538
|
+
}}
|
|
539
|
+
onPointerUp={() => {
|
|
540
|
+
if (list.drag?.pointer && dragging) list.endDrag();
|
|
541
|
+
}}
|
|
542
|
+
onPointerCancel={() => {
|
|
543
|
+
if (list.drag?.pointer && dragging) list.endDrag(true);
|
|
544
|
+
}}
|
|
545
|
+
onKeyDown={(event) => {
|
|
546
|
+
const grabbed = dragging && !list.drag?.pointer;
|
|
547
|
+
if (event.key === " " || event.key === "Enter") {
|
|
548
|
+
event.preventDefault();
|
|
549
|
+
event.stopPropagation();
|
|
550
|
+
if (grabbed) list.endDrag();
|
|
551
|
+
else list.startDrag(value, false);
|
|
552
|
+
} else if (grabbed && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
|
|
553
|
+
event.preventDefault();
|
|
554
|
+
event.stopPropagation();
|
|
555
|
+
list.moveDrag(event.key === "ArrowUp" ? -1 : 1);
|
|
556
|
+
} else if (grabbed && event.key === "Escape") {
|
|
557
|
+
event.preventDefault();
|
|
558
|
+
event.stopPropagation();
|
|
559
|
+
list.endDrag(true);
|
|
560
|
+
} else if (grabbed && event.key === "Tab") {
|
|
561
|
+
event.preventDefault();
|
|
562
|
+
}
|
|
563
|
+
}}
|
|
564
|
+
>
|
|
565
|
+
<DragHandleIcon />
|
|
566
|
+
</button>
|
|
567
|
+
)}
|
|
568
|
+
</div>
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/* -------------------------------------------------------------- section */
|
|
573
|
+
|
|
574
|
+
export interface OptionListSectionProps extends ActionListSectionProps {}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* A labelled group of options. `hasSeparator` draws a line above it, except
|
|
578
|
+
* when it opens the list.
|
|
579
|
+
*/
|
|
580
|
+
export function OptionListSection(props: OptionListSectionProps) {
|
|
581
|
+
return <ActionListSection {...props} />;
|
|
582
|
+
}
|