@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.
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,405 @@
1
+ import * as React from "react";
2
+ import { Dialog as DialogPrimitive } from "radix-ui";
3
+ import { cva, type VariantProps } from "class-variance-authority";
4
+ import { cn } from "../lib/utils";
5
+ import { Glyph } from "../lib/glyphs";
6
+ import { Button } from "./button";
7
+ import { Heading } from "./heading";
8
+
9
+ export const Dialog = DialogPrimitive.Root;
10
+ export const DialogTrigger = DialogPrimitive.Trigger;
11
+ export const DialogClose = DialogPrimitive.Close;
12
+ export const DialogPortal = DialogPrimitive.Portal;
13
+
14
+ /** Swirl's backdrop: a 20% black wash that fades in. */
15
+ export function DialogOverlay({
16
+ className,
17
+ ...props
18
+ }: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
19
+ return (
20
+ <DialogPrimitive.Overlay
21
+ className={cn(
22
+ "fixed inset-0 z-40 bg-black/20 duration-150 motion-reduce:animate-none",
23
+ "data-[state=open]:animate-in data-[state=open]:fade-in-0",
24
+ "data-[state=closed]:animate-out data-[state=closed]:fade-out-0",
25
+ className,
26
+ )}
27
+ {...props}
28
+ />
29
+ );
30
+ }
31
+
32
+ const contentVariants = cva(
33
+ [
34
+ "fixed z-40 flex flex-row overflow-hidden bg-surface-overlay pt-[env(safe-area-inset-top,0)] text-foreground outline-none",
35
+ "duration-150 motion-reduce:animate-none data-[state=open]:animate-in data-[state=closed]:animate-out",
36
+ ],
37
+ {
38
+ variants: {
39
+ /**
40
+ * `default` is Swirl's modal: full screen on phones, centred from 768px.
41
+ * `drawer` slides in from the right at full height. `sheet` is a bottom
42
+ * sheet at every size.
43
+ */
44
+ variant: {
45
+ default: [
46
+ "inset-0 h-dvh max-h-dvh w-screen max-w-[100vw]",
47
+ "data-[state=open]:zoom-in-0 data-[state=closed]:fade-out-0",
48
+ "md:inset-auto md:top-1/2 md:left-1/2 md:h-auto md:max-h-[90dvh] md:w-[90vw] md:-translate-x-1/2 md:-translate-y-1/2",
49
+ "md:rounded-base md:shadow-level-3",
50
+ ],
51
+ drawer: [
52
+ "inset-y-0 right-0 h-dvh max-w-[100vw] shadow-level-3 md:w-[90vw]",
53
+ "data-[state=open]:slide-in-from-right data-[state=closed]:slide-out-to-right",
54
+ ],
55
+ sheet: [
56
+ "inset-x-0 bottom-0 max-h-[90dvh] rounded-t-l shadow-level-3",
57
+ "data-[state=open]:slide-in-from-bottom-4 data-[state=closed]:slide-out-to-bottom-4",
58
+ ],
59
+ },
60
+ /** Narrower or wider than Swirl's 40rem. */
61
+ size: {
62
+ s: "md:max-w-sm",
63
+ m: "md:max-w-lg",
64
+ l: "md:max-w-2xl",
65
+ xl: "md:max-w-4xl",
66
+ full: "md:top-4 md:left-4 md:h-[calc(100dvh-2rem)] md:max-h-none md:w-[calc(100vw-2rem)] md:max-w-none md:translate-x-0 md:translate-y-0",
67
+ },
68
+ },
69
+ defaultVariants: { variant: "default" },
70
+ },
71
+ );
72
+
73
+ export interface DialogContentProps
74
+ extends Omit<React.ComponentProps<typeof DialogPrimitive.Content>, "title">,
75
+ VariantProps<typeof contentVariants> {
76
+ /** The dialog's heading, in its header bar. Without it, put a `DialogTitle` in the content. */
77
+ label?: string;
78
+ /** Keep `label` as the accessible name but don't show it. */
79
+ hideLabel?: boolean;
80
+ /** Hide the close button. */
81
+ hideCloseButton?: boolean;
82
+ /** @deprecated Use `hideCloseButton`. */
83
+ showClose?: boolean;
84
+ closeButtonLabel?: string;
85
+ /** @deprecated Use `closeButtonLabel`. */
86
+ closeLabel?: string;
87
+ /** Pad the content, as Swirl does by default. */
88
+ padded?: boolean;
89
+ /** Footer buttons: a primary flat button, and a secondary one before it. */
90
+ primaryActionLabel?: string;
91
+ secondaryActionLabel?: string;
92
+ onPrimaryAction?: () => void;
93
+ onSecondaryAction?: () => void;
94
+ /** Tools under the header, such as a search field. */
95
+ headerTools?: React.ReactNode;
96
+ /** A second column beside the content from 992px (Swirl's breakpoint, not Tailwind's `lg`), and under it below. */
97
+ secondaryContent?: React.ReactNode;
98
+ /** Drop the rules Swirl draws around secondary content. */
99
+ hideSecondaryContentBorders?: boolean;
100
+ /** A sidebar on the left, from 768px, with its own heading, content and footer. */
101
+ sidebarLabel?: string;
102
+ sidebarContent?: React.ReactNode;
103
+ sidebarFooter?: React.ReactNode;
104
+ sidebarPadded?: boolean;
105
+ sidebarFooterPadded?: boolean;
106
+ /** CSS max width, overriding Swirl's 40rem. */
107
+ maxWidth?: string;
108
+ }
109
+
110
+ /**
111
+ * Tracks whether a scroll area is scrolled, and whether it can and has
112
+ * scrolled to the end. Pass `setElement` as the ref; it measures whenever the area mounts,
113
+ * which is each time the dialog opens.
114
+ */
115
+ function useScrollState() {
116
+ const [element, setElement] = React.useState<HTMLElement | null>(null);
117
+ const [state, setState] = React.useState({ scrolled: false, scrollable: false, atEnd: true });
118
+ const update = React.useCallback(() => {
119
+ if (!element) return;
120
+ const next = {
121
+ scrolled: element.scrollTop > 0,
122
+ scrollable: element.scrollHeight > element.clientHeight,
123
+ atEnd: element.scrollTop + element.clientHeight >= element.scrollHeight - 1,
124
+ };
125
+ setState((prev) =>
126
+ prev.scrolled === next.scrolled && prev.scrollable === next.scrollable && prev.atEnd === next.atEnd
127
+ ? prev
128
+ : next,
129
+ );
130
+ }, [element]);
131
+ React.useEffect(() => {
132
+ if (!element) return;
133
+ const observer = new ResizeObserver(update);
134
+ observer.observe(element);
135
+ for (const child of element.children) observer.observe(child);
136
+ return () => observer.disconnect();
137
+ }, [element, update]);
138
+ return { ...state, update, setElement };
139
+ }
140
+
141
+ /**
142
+ * Swirl's modal. Full screen on phones and centred from 768px, with a header
143
+ * bar holding the close button and the `label`. Content scrolls under it,
144
+ * and rules appear above and below as it does. `headerTools`,
145
+ * `secondaryContent`, a sidebar and footer actions are all optional.
146
+ *
147
+ * <DialogContent label="Rename" primaryActionLabel="Save" onPrimaryAction={save}>
148
+ * <Input defaultValue="acme" />
149
+ * </DialogContent>
150
+ *
151
+ * The shadcn parts (`DialogHeader`, `DialogTitle`, `DialogFooter`, ...) still
152
+ * work inside it, for a dialog without a `label`.
153
+ */
154
+ export function DialogContent({
155
+ className,
156
+ children,
157
+ variant,
158
+ size,
159
+ label,
160
+ hideLabel = false,
161
+ hideCloseButton,
162
+ showClose,
163
+ closeButtonLabel,
164
+ closeLabel,
165
+ padded = true,
166
+ primaryActionLabel,
167
+ secondaryActionLabel,
168
+ onPrimaryAction,
169
+ onSecondaryAction,
170
+ headerTools,
171
+ secondaryContent,
172
+ hideSecondaryContentBorders = false,
173
+ sidebarLabel,
174
+ sidebarContent,
175
+ sidebarFooter,
176
+ sidebarPadded = true,
177
+ sidebarFooterPadded = true,
178
+ maxWidth,
179
+ style,
180
+ ...props
181
+ }: DialogContentProps) {
182
+ const variantName = variant ?? "default";
183
+ const showCloseButton = !(hideCloseButton ?? (showClose === undefined ? false : !showClose));
184
+ const closeText = closeButtonLabel ?? closeLabel ?? "Close modal";
185
+ const showHeading = Boolean(label) && !hideLabel;
186
+ const hasSecondary = secondaryContent !== undefined;
187
+ const hasSidebar = sidebarContent !== undefined;
188
+ const hasTools = headerTools !== undefined;
189
+ const hasControls = Boolean(primaryActionLabel || secondaryActionLabel);
190
+ const secondaryBorders = hasSecondary && !hideSecondaryContentBorders;
191
+
192
+ const id = React.useId();
193
+ const { setElement: setMainElement, update: updateMain, ...main } = useScrollState();
194
+ const { setElement: setSidebarElement, update: updateSidebar, ...sidebar } = useScrollState();
195
+
196
+ // Swirl widens the body for an extra column: 40rem, plus 25rem for a sidebar
197
+ // or 24rem for secondary content. With both, its sidebar rule wins: 65rem.
198
+ const width = hasSidebar ? "md:max-w-[65rem]" : hasSecondary ? "md:max-w-[64rem]" : "md:max-w-[40rem]";
199
+
200
+ return (
201
+ <DialogPortal>
202
+ <DialogOverlay className={cn(variantName === "drawer" && "bg-transparent")} />
203
+ <DialogPrimitive.Content
204
+ // A labelled modal needs no description; leave Radix's wiring alone otherwise.
205
+ {...(label ? { "aria-describedby": undefined } : {})}
206
+ className={cn(variantName !== "sheet" && width, contentVariants({ variant, size }), className)}
207
+ style={{ ...(maxWidth ? { maxWidth } : {}), ...style }}
208
+ {...props}
209
+ >
210
+ {hasSidebar && (
211
+ <aside className="hidden w-[25rem] shrink-0 flex-col overflow-y-auto border-r border-border md:flex">
212
+ {sidebarLabel && (
213
+ <header
214
+ className={cn(
215
+ "flex items-center gap-2 border-b px-6",
216
+ sidebar.scrolled ? "border-border" : "border-transparent",
217
+ )}
218
+ >
219
+ <Heading as="h3" level={5} className="flex h-15 items-center">
220
+ {sidebarLabel}
221
+ </Heading>
222
+ </header>
223
+ )}
224
+ <div
225
+ ref={setSidebarElement}
226
+ onScroll={updateSidebar}
227
+ className={cn(
228
+ "block grow overflow-y-auto",
229
+ sidebarPadded && "px-6 py-4",
230
+ sidebarPadded && sidebar.scrollable && "pb-0",
231
+ )}
232
+ >
233
+ {sidebarContent}
234
+ </div>
235
+ {sidebarFooter !== undefined && (
236
+ <div
237
+ className={cn(
238
+ sidebarFooterPadded &&
239
+ "px-4 pt-3 pb-[max(env(safe-area-inset-bottom),0.75rem)] md:px-6 md:pt-4 md:pb-[max(env(safe-area-inset-bottom),1rem)]",
240
+ sidebar.scrollable && !sidebar.atEnd && "border-t border-border",
241
+ )}
242
+ >
243
+ {sidebarFooter}
244
+ </div>
245
+ )}
246
+ </aside>
247
+ )}
248
+
249
+ <div className="flex grow flex-col overflow-y-auto">
250
+ {(showHeading || showCloseButton) && (
251
+ <header
252
+ className={cn(
253
+ "border-b border-border md:border-transparent",
254
+ // The header's rule shows once the content scrolls under it, or
255
+ // beside secondary content, unless header tools take it over.
256
+ !hasTools && (main.scrolled || secondaryBorders) && "md:border-border",
257
+ hasTools && "border-transparent",
258
+ )}
259
+ >
260
+ <div
261
+ className={cn(
262
+ "flex h-14 shrink-0 items-center gap-2 px-4 py-2",
263
+ "md:h-15 md:px-6 md:py-3",
264
+ variantName === "drawer" ? "bg-surface-overlay md:flex-row" : "md:flex-row-reverse",
265
+ variantName === "drawer" && hideLabel && "md:h-auto md:py-1",
266
+ )}
267
+ >
268
+ {showCloseButton && (
269
+ <DialogPrimitive.Close asChild>
270
+ <Button
271
+ hideLabel
272
+ label={closeText}
273
+ icon={<Glyph name="close" />}
274
+ className={cn(variantName === "drawer" && "-ml-2")}
275
+ />
276
+ </DialogPrimitive.Close>
277
+ )}
278
+ {label && (
279
+ <DialogPrimitive.Title asChild>
280
+ <Heading
281
+ as="h2"
282
+ level={3}
283
+ className={cn("min-w-0 grow truncate", hideLabel && "sr-only")}
284
+ >
285
+ {label}
286
+ </Heading>
287
+ </DialogPrimitive.Title>
288
+ )}
289
+ </div>
290
+ </header>
291
+ )}
292
+ {label && !showHeading && !showCloseButton && (
293
+ <DialogPrimitive.Title className="sr-only">{label}</DialogPrimitive.Title>
294
+ )}
295
+
296
+ <div className="flex grow flex-col gap-6 overflow-hidden min-[62rem]:flex-row">
297
+ <div
298
+ className={cn(
299
+ "flex min-w-0 grow basis-full flex-col overflow-hidden overflow-y-auto",
300
+ hasSecondary &&
301
+ "max-h-[60%] basis-auto overflow-visible min-[62rem]:max-h-none min-[62rem]:max-w-[calc(100%-24rem)] min-[62rem]:basis-[calc(100%-24rem)]",
302
+ hasSidebar && !hasSecondary && "min-[62rem]:basis-[calc(100%-20rem)]",
303
+ )}
304
+ >
305
+ {hasTools && (
306
+ <div
307
+ className={cn(
308
+ "border-b border-border px-4 md:border-transparent md:px-6",
309
+ (secondaryBorders || main.scrolled) && "md:border-border",
310
+ hasSecondary && "min-[62rem]:pr-0",
311
+ )}
312
+ >
313
+ {headerTools}
314
+ </div>
315
+ )}
316
+ <div
317
+ id={`${id}-content`}
318
+ ref={setMainElement}
319
+ onScroll={updateMain}
320
+ className={cn(
321
+ "h-full overflow-x-hidden overflow-y-auto",
322
+ padded && "px-4 py-6 md:px-6 md:pt-0 md:pb-4",
323
+ padded && main.scrollable && "md:pb-0",
324
+ hasSecondary && "min-[62rem]:pr-0",
325
+ )}
326
+ >
327
+ {children}
328
+ </div>
329
+ </div>
330
+ {hasSecondary && (
331
+ <div
332
+ className={cn(
333
+ "block max-h-[40%] min-w-0 overflow-x-hidden overflow-y-auto px-4 md:pl-6",
334
+ "min-[62rem]:max-h-none min-[62rem]:max-w-[24rem] min-[62rem]:grow min-[62rem]:basis-1/2 min-[62rem]:pt-0 min-[62rem]:pr-6 min-[62rem]:pb-4 min-[62rem]:pl-0",
335
+ )}
336
+ >
337
+ {secondaryContent}
338
+ </div>
339
+ )}
340
+ </div>
341
+
342
+ {hasControls && (
343
+ <footer
344
+ className={cn(
345
+ "flex shrink-0 justify-end",
346
+ "px-4 pt-3 pb-[max(env(safe-area-inset-bottom),0.75rem)] md:px-6 md:pt-4 md:pb-[max(env(safe-area-inset-bottom),1rem)]",
347
+ ((main.scrollable && !main.atEnd) || secondaryBorders) && "border-t border-border",
348
+ )}
349
+ >
350
+ <div className="flex flex-wrap justify-end gap-2">
351
+ {secondaryActionLabel && <Button label={secondaryActionLabel} onClick={onSecondaryAction} />}
352
+ {primaryActionLabel && (
353
+ <Button variant="flat" intent="primary" label={primaryActionLabel} onClick={onPrimaryAction} />
354
+ )}
355
+ </div>
356
+ </footer>
357
+ )}
358
+ </div>
359
+ </DialogPrimitive.Content>
360
+ </DialogPortal>
361
+ );
362
+ }
363
+
364
+ export function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
365
+ return <div className={cn("flex flex-col gap-1", className)} {...props} />;
366
+ }
367
+
368
+ export function DialogTitle({
369
+ className,
370
+ ...props
371
+ }: React.ComponentProps<typeof DialogPrimitive.Title>) {
372
+ return (
373
+ <DialogPrimitive.Title
374
+ className={cn("text-xl leading-7 font-semibold text-foreground", className)}
375
+ {...props}
376
+ />
377
+ );
378
+ }
379
+
380
+ export function DialogDescription({
381
+ className,
382
+ ...props
383
+ }: React.ComponentProps<typeof DialogPrimitive.Description>) {
384
+ return (
385
+ <DialogPrimitive.Description
386
+ className={cn("text-sm leading-5 text-subdued", className)}
387
+ {...props}
388
+ />
389
+ );
390
+ }
391
+
392
+ export function DialogBody({ className, ...props }: React.ComponentProps<"div">) {
393
+ return <div className={cn("flex-1 overflow-y-auto", className)} {...props} />;
394
+ }
395
+
396
+ export function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
397
+ return (
398
+ <div
399
+ className={cn("mt-4 flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)}
400
+ {...props}
401
+ />
402
+ );
403
+ }
404
+
405
+ export { contentVariants as dialogContentVariants };
@@ -0,0 +1,63 @@
1
+ import { cn } from "../lib/utils";
2
+ import { Heading, type HeadingLevel } from "./heading";
3
+ import { Text } from "./text";
4
+
5
+ export interface EmptyStateProps extends React.ComponentProps<"div"> {
6
+ /**
7
+ * An image URL, as in Swirl, or any element such as an `Icon`. Capped at
8
+ * 16rem wide.
9
+ */
10
+ illustration?: React.ReactNode;
11
+ heading?: React.ReactNode;
12
+ /** The heading's element. It is always drawn at level 4's size, as in Swirl. */
13
+ headingLevel?: HeadingLevel;
14
+ /** The message under the heading. Interchangeable with `children`. */
15
+ description?: React.ReactNode;
16
+ /** Buttons under the message, such as a `ButtonGroup`. */
17
+ action?: React.ReactNode;
18
+ }
19
+
20
+ /**
21
+ * Swirl's empty state: the placeholder shown when a list, search or view has
22
+ * nothing to display. A centred column up to 28rem wide; place it where it
23
+ * should sit, for example inside a centred container.
24
+ */
25
+ export function EmptyState({
26
+ className,
27
+ illustration,
28
+ heading,
29
+ headingLevel = 4,
30
+ description,
31
+ action,
32
+ children,
33
+ ...props
34
+ }: EmptyStateProps) {
35
+ const message = description ?? children;
36
+
37
+ return (
38
+ <div className={cn("flex max-w-[28rem] flex-col items-center gap-4", className)} {...props}>
39
+ {typeof illustration === "string" ? (
40
+ <img src={illustration} alt="" className="w-full max-w-64" />
41
+ ) : (
42
+ illustration && <div className="flex max-w-64 justify-center">{illustration}</div>
43
+ )}
44
+ <div className="flex flex-col items-center gap-6">
45
+ {(heading || message) && (
46
+ <div className="flex flex-col items-stretch gap-2">
47
+ {heading && (
48
+ <Heading level={4} as={`h${headingLevel}`} align="center">
49
+ {heading}
50
+ </Heading>
51
+ )}
52
+ {message && (
53
+ <Text as="div" color="subdued" align="center">
54
+ {message}
55
+ </Text>
56
+ )}
57
+ </div>
58
+ )}
59
+ {action && <div>{action}</div>}
60
+ </div>
61
+ </div>
62
+ );
63
+ }
@@ -0,0 +1,206 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+ import { Glyph, type GlyphName } from "../lib/glyphs";
4
+ import { Button } from "./button";
5
+ import { Spinner } from "./spinner";
6
+
7
+ type FileKind = "image" | "video" | "audio" | "pdf" | "compressed" | "document" | "unknown";
8
+
9
+ /** Sorts a MIME type the way Swirl does. */
10
+ function fileKind(type: string): FileKind {
11
+ if (["image/jpeg", "image/png", "image/avif", "image/gif", "image/webp"].includes(type)) return "image";
12
+ if (type.startsWith("video/")) return "video";
13
+ if (type.startsWith("audio/")) return "audio";
14
+ if (type.startsWith("application/pdf")) return "pdf";
15
+ if (
16
+ ["application/zip", "application/x-zip", "application/vnd.rar", "application/x-7z", "application/x-tar", "application/x-gzip"].some(
17
+ (prefix) => type.startsWith(prefix),
18
+ )
19
+ )
20
+ return "compressed";
21
+ if (
22
+ [
23
+ "application/msword",
24
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
25
+ "application/vnd.apple.pages",
26
+ ].some((prefix) => type.startsWith(prefix))
27
+ )
28
+ return "document";
29
+ return "unknown";
30
+ }
31
+
32
+ const kinds: Record<FileKind, { glyph: GlyphName; className: string }> = {
33
+ image: { glyph: "image", className: "bg-kiwi-subdued text-kiwi-text" },
34
+ video: { glyph: "videoPlayer", className: "bg-radish-subdued text-radish-text" },
35
+ audio: { glyph: "audioFile", className: "bg-grape-subdued text-grape-text" },
36
+ pdf: { glyph: "pictureAsPdf", className: "bg-chilli-subdued text-chilli-text" },
37
+ compressed: { glyph: "folder", className: "bg-pumpkin-subdued text-pumpkin-text" },
38
+ document: { glyph: "file", className: "bg-blueberry-subdued text-blueberry-text" },
39
+ unknown: { glyph: "attachment", className: "bg-banana-subdued text-banana-text" },
40
+ };
41
+
42
+ /** Downloads `url` as `name`, without navigating away. */
43
+ async function saveFile(url: string, name: string) {
44
+ const blob = await (await fetch(url)).blob();
45
+ const href = URL.createObjectURL(blob);
46
+ const link = document.createElement("a");
47
+ link.href = href;
48
+ link.download = name;
49
+ link.click();
50
+ URL.revokeObjectURL(href);
51
+ }
52
+
53
+ export interface FileChipProps extends Omit<React.ComponentProps<"span">, "onClick"> {
54
+ /** The file's name. */
55
+ name: string;
56
+ /** Its MIME type, which picks the icon and colour. */
57
+ type: string;
58
+ /** Where to download it from. */
59
+ url?: string;
60
+ /** Such as the file's size. */
61
+ description?: React.ReactNode;
62
+ loading?: boolean;
63
+ hasError?: boolean;
64
+ showPreviewButton?: boolean;
65
+ showDownloadButton?: boolean;
66
+ showDeleteButton?: boolean;
67
+ /** Only call `onDownload`; don't download `url` too. */
68
+ skipNativeDownload?: boolean;
69
+ onPreview?: () => void;
70
+ onDownload?: () => void;
71
+ onDelete?: () => void;
72
+ previewButtonLabel?: string;
73
+ downloadButtonLabel?: string;
74
+ deleteButtonLabel?: string;
75
+ errorLabel?: string;
76
+ loadingLabel?: string;
77
+ }
78
+
79
+ /**
80
+ * Swirl's file chip: an attached file, with an icon coloured by its type,
81
+ * its name and size, and preview, download and delete buttons. On devices
82
+ * that can hover, the buttons replace the size on hover or focus, and a
83
+ * click anywhere previews or downloads the file.
84
+ */
85
+ export function FileChip({
86
+ name,
87
+ type,
88
+ url,
89
+ description,
90
+ loading = false,
91
+ hasError = false,
92
+ showPreviewButton = false,
93
+ showDownloadButton = false,
94
+ showDeleteButton = false,
95
+ skipNativeDownload = false,
96
+ onPreview,
97
+ onDownload,
98
+ onDelete,
99
+ previewButtonLabel = "Preview",
100
+ downloadButtonLabel = "Download",
101
+ deleteButtonLabel = "Delete",
102
+ errorLabel = "Error",
103
+ loadingLabel = "Loading …",
104
+ className,
105
+ ...props
106
+ }: FileChipProps) {
107
+ const kind = kinds[fileKind(type)];
108
+ const hasAction = showPreviewButton || showDownloadButton || showDeleteButton;
109
+ const hasDescription = description !== undefined || loading || hasError;
110
+ const clickable = !loading && (showPreviewButton || showDownloadButton);
111
+ const actionsOnHover = hasAction && !loading;
112
+
113
+ const download = () => {
114
+ onDownload?.();
115
+ if (!skipNativeDownload && url) void saveFile(url, name || url.split("/").pop() || "download");
116
+ };
117
+
118
+ return (
119
+ <span
120
+ className={cn(
121
+ "inline-block max-w-full overflow-hidden rounded-sm bg-background outline-1 -outline-offset-1 outline-border-strong outline-solid",
122
+ className,
123
+ )}
124
+ {...props}
125
+ >
126
+ <span
127
+ role="group"
128
+ aria-label={name}
129
+ onClick={() => {
130
+ if (loading) return;
131
+ if (showPreviewButton) onPreview?.();
132
+ else if (showDownloadButton) download();
133
+ }}
134
+ className={cn(
135
+ "group/chip relative flex max-w-full items-center gap-3 bg-transparent py-2 pr-4 pl-2 text-sm leading-5",
136
+ clickable &&
137
+ "[@media(hover:hover)]:hover:cursor-pointer [@media(hover:hover)]:hover:bg-state-hovered [@media(hover:hover)]:focus-within:bg-state-hovered",
138
+ )}
139
+ >
140
+ <span
141
+ className={cn(
142
+ "flex shrink-0 rounded-full p-1 [&>svg]:size-5",
143
+ hasError ? "bg-surface-critical-subdued text-critical" : loading ? "bg-transparent p-0.5" : kind.className,
144
+ )}
145
+ >
146
+ {hasError ? (
147
+ <Glyph name="error" />
148
+ ) : loading ? (
149
+ <Spinner size="s" label={loadingLabel} />
150
+ ) : (
151
+ <Glyph name={kind.glyph} />
152
+ )}
153
+ </span>
154
+ <span className={cn("flex min-w-0 grow gap-3", hasDescription ? "items-baseline" : "items-center")}>
155
+ <span title={name} className="truncate font-semibold text-foreground">
156
+ {name}
157
+ </span>
158
+ {/* As in Swirl, with no description and no buttons there's no suffix, not even "Loading". */}
159
+ {(description !== undefined || hasAction) && (
160
+ <span
161
+ className={cn(
162
+ "inline-flex min-w-fit grow items-center justify-end gap-3",
163
+ "[@media(hover:hover)]:inline-grid [@media(hover:hover)]:justify-items-end [@media(hover:hover)]:gap-0",
164
+ )}
165
+ >
166
+ {hasDescription && (
167
+ <span
168
+ role={hasError ? "status" : undefined}
169
+ className={cn(
170
+ "truncate text-xs [@media(hover:hover)]:[grid-area:1/1]",
171
+ hasError ? "text-critical" : "text-subdued",
172
+ actionsOnHover &&
173
+ "[@media(hover:hover)]:group-focus-within/chip:pointer-events-none [@media(hover:hover)]:group-focus-within/chip:opacity-0 [@media(hover:hover)]:group-hover/chip:pointer-events-none [@media(hover:hover)]:group-hover/chip:opacity-0",
174
+ )}
175
+ >
176
+ {hasError ? errorLabel : loading ? loadingLabel : description}
177
+ </span>
178
+ )}
179
+ {actionsOnHover && (
180
+ <span
181
+ className={cn(
182
+ "flex items-center gap-2 [@media(hover:hover)]:[grid-area:1/1]",
183
+ "[@media(hover:hover)]:pointer-events-none [@media(hover:hover)]:opacity-0",
184
+ "[@media(hover:hover)]:group-focus-within/chip:pointer-events-auto [@media(hover:hover)]:group-focus-within/chip:opacity-100",
185
+ "[@media(hover:hover)]:group-hover/chip:pointer-events-auto [@media(hover:hover)]:group-hover/chip:opacity-100",
186
+ )}
187
+ onClick={(event) => event.stopPropagation()}
188
+ >
189
+ {showPreviewButton && (
190
+ <Button variant="plain" hideLabel label={previewButtonLabel} icon={<Glyph name="preview" />} onClick={onPreview} />
191
+ )}
192
+ {showDownloadButton && (
193
+ <Button variant="plain" hideLabel label={downloadButtonLabel} icon={<Glyph name="download" />} onClick={download} />
194
+ )}
195
+ {showDeleteButton && (
196
+ <Button variant="plain" hideLabel label={deleteButtonLabel} icon={<Glyph name="close" />} onClick={onDelete} />
197
+ )}
198
+ </span>
199
+ )}
200
+ </span>
201
+ )}
202
+ </span>
203
+ </span>
204
+ </span>
205
+ );
206
+ }