@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,63 @@
1
+ import { cn } from "../lib/utils";
2
+
3
+ const radii = {
4
+ none: "rounded-none",
5
+ s: "rounded-(--radius-s)",
6
+ sm: "rounded-sm",
7
+ base: "rounded-base",
8
+ pill: "rounded-full",
9
+ } as const;
10
+
11
+ export interface SkeletonBoxProps extends React.ComponentProps<"div"> {
12
+ /** CSS width, such as `"50%"` or `"12rem"`. Full width when neither this nor `aspectRatio` is set. */
13
+ width?: string;
14
+ /** CSS height. At least 0.875rem. */
15
+ height?: string;
16
+ /** CSS aspect ratio, such as `"1/0.5"`, to size the box from its width. */
17
+ aspectRatio?: string;
18
+ borderRadius?: keyof typeof radii;
19
+ /** Sweep a shimmer across the box. Defaults to true. */
20
+ animated?: boolean;
21
+ }
22
+
23
+ /**
24
+ * Swirl's skeleton box: a placeholder for an image, avatar, card or other
25
+ * block that is still loading. Use `borderRadius="pill"` with a square
26
+ * `aspectRatio` for a circle.
27
+ *
28
+ * Hidden from assistive tech; announce the wait once on the container (a
29
+ * `Spinner`, or `aria-busy`) rather than once per placeholder.
30
+ */
31
+ export function SkeletonBox({
32
+ className,
33
+ style,
34
+ width,
35
+ height,
36
+ aspectRatio,
37
+ borderRadius = "base",
38
+ animated = true,
39
+ ...props
40
+ }: SkeletonBoxProps) {
41
+ return (
42
+ <div
43
+ aria-hidden
44
+ className={cn(
45
+ "relative min-h-3.5 overflow-hidden bg-surface-raised-hovered",
46
+ !width && !aspectRatio && "w-full",
47
+ radii[borderRadius],
48
+ className,
49
+ )}
50
+ style={{ width, height, aspectRatio, ...style }}
51
+ {...props}
52
+ >
53
+ {animated && (
54
+ <div
55
+ className={cn(
56
+ "absolute inset-0 -translate-x-full animate-skeleton-shimmer motion-reduce:hidden",
57
+ "bg-[linear-gradient(90deg,transparent_0,transparent_12%,var(--color-surface-raised)_50%,transparent_88%,transparent_100%)]",
58
+ )}
59
+ />
60
+ )}
61
+ </div>
62
+ );
63
+ }
@@ -0,0 +1,66 @@
1
+ import { cn } from "../lib/utils";
2
+
3
+ export type SkeletonTextSize = "sm" | "base" | "lg" | "xl" | "2xl";
4
+
5
+ /*
6
+ * Each line is as tall as a line of text at that size, with a bar as tall as
7
+ * the font size centred in it, so the placeholder takes the space the text
8
+ * will. `base` and `lg` step down a size with the desktop layout, as Swirl's
9
+ * text does.
10
+ */
11
+ const lineSizes = {
12
+ sm: "h-5 py-[0.1875rem]",
13
+ base: "h-6 py-1 desktop:h-5 desktop:py-[0.1875rem]",
14
+ lg: "h-6 py-[0.1875rem] desktop:py-1",
15
+ xl: "h-7 py-1",
16
+ "2xl": "h-9 py-1",
17
+ } as const satisfies Record<SkeletonTextSize, string>;
18
+
19
+ export interface SkeletonTextProps extends React.ComponentProps<"div"> {
20
+ /** How many lines to draw. The last of several is a quarter wide, as in Swirl. */
21
+ lines?: number;
22
+ /** The text size the lines stand in for. */
23
+ size?: SkeletonTextSize;
24
+ /** Sweep a shimmer across the lines. Defaults to true. */
25
+ animated?: boolean;
26
+ }
27
+
28
+ /**
29
+ * Swirl's skeleton text: placeholder lines for text that is still loading.
30
+ *
31
+ * Hidden from assistive tech; announce the wait once on the container (a
32
+ * `Spinner`, or `aria-busy`) rather than once per placeholder.
33
+ */
34
+ export function SkeletonText({
35
+ className,
36
+ lines = 1,
37
+ size = "base",
38
+ animated = true,
39
+ ...props
40
+ }: SkeletonTextProps) {
41
+ return (
42
+ <div aria-hidden className={cn("relative block w-full", className)} {...props}>
43
+ {Array.from({ length: lines }, (_, index) => (
44
+ <div
45
+ key={index}
46
+ className={cn(
47
+ "w-full",
48
+ lineSizes[size],
49
+ lines > 1 && index === lines - 1 && "w-1/4",
50
+ )}
51
+ >
52
+ <div className="relative size-full overflow-hidden rounded-full bg-surface-raised-hovered">
53
+ {animated && (
54
+ <div
55
+ className={cn(
56
+ "absolute inset-0 -translate-x-full animate-skeleton-shimmer motion-reduce:hidden",
57
+ "bg-[linear-gradient(90deg,transparent_0,var(--color-surface-raised)_33%,transparent_66%)]",
58
+ )}
59
+ />
60
+ )}
61
+ </div>
62
+ </div>
63
+ ))}
64
+ </div>
65
+ );
66
+ }
@@ -0,0 +1,83 @@
1
+ import { cn } from "../lib/utils";
2
+
3
+ /*
4
+ * Swirl's sizes, with its numeric aliases. The two small sizes draw a
5
+ * slightly thinner arc, as Swirl does, so it stays crisp.
6
+ */
7
+ const sizes = {
8
+ xs: { box: "size-5", stroke: 3 },
9
+ s: { box: "size-6", stroke: 3 },
10
+ m: { box: "size-9", stroke: 4 },
11
+ 20: { box: "size-5", stroke: 3 },
12
+ 24: { box: "size-6", stroke: 3 },
13
+ 36: { box: "size-9", stroke: 4 },
14
+ } as const;
15
+
16
+ export interface SpinnerProps extends React.ComponentProps<"div"> {
17
+ /** 20, 24 or 36px wide (`xs`, `s`, `m`). Defaults to `m`, as in Swirl. */
18
+ size?: keyof typeof sizes;
19
+ /** Announced to screen readers while busy. Pass `null` to stay silent. */
20
+ label?: string | null;
21
+ /**
22
+ * Draw the arc in the current text colour instead of Swirl's primary blue,
23
+ * for use on coloured surfaces such as a primary button.
24
+ */
25
+ inheritColor?: boolean;
26
+ }
27
+
28
+ /**
29
+ * Swirl's spinner: an arc that grows and shrinks as it turns, over a faint
30
+ * track.
31
+ *
32
+ * Renders as a `status` live region so assistive tech announces the wait, which
33
+ * is why `label` defaults to real text rather than being decorative.
34
+ */
35
+ export function Spinner({
36
+ className,
37
+ size = "m",
38
+ label = "Loading",
39
+ inheritColor = false,
40
+ ...props
41
+ }: SpinnerProps) {
42
+ const { box, stroke } = sizes[size];
43
+
44
+ return (
45
+ <div
46
+ role={label ? "status" : undefined}
47
+ aria-live={label ? "polite" : undefined}
48
+ className={cn("relative inline-flex shrink-0 overflow-hidden", box, className)}
49
+ {...props}
50
+ >
51
+ <svg
52
+ viewBox="0 0 50 50"
53
+ aria-hidden
54
+ focusable={false}
55
+ className="absolute inset-0 size-full animate-spinner-rotate"
56
+ >
57
+ <circle
58
+ cx="25"
59
+ cy="25"
60
+ r="20"
61
+ fill="none"
62
+ strokeWidth={stroke}
63
+ strokeLinecap="round"
64
+ className={inheritColor ? "stroke-current opacity-20" : "stroke-border"}
65
+ />
66
+ <circle
67
+ cx="25"
68
+ cy="25"
69
+ r="20"
70
+ fill="none"
71
+ strokeWidth={stroke}
72
+ strokeLinecap="round"
73
+ strokeDasharray="1, 200"
74
+ className={cn(
75
+ "animate-spinner-dash",
76
+ inheritColor ? "stroke-current" : "stroke-interactive-primary",
77
+ )}
78
+ />
79
+ </svg>
80
+ {label && <span className="sr-only">{label}</span>}
81
+ </div>
82
+ );
83
+ }
@@ -0,0 +1,86 @@
1
+ import { cn } from "../lib/utils";
2
+ import { gap, gapX, gapY, type SwirlSpacing } from "../lib/spacing";
3
+
4
+ export type StackOrientation =
5
+ | "horizontal"
6
+ | "horizontal-reverse"
7
+ | "vertical"
8
+ | "vertical-reverse";
9
+
10
+ const orientations = {
11
+ vertical: "flex-col",
12
+ "vertical-reverse": "flex-col-reverse",
13
+ horizontal: "flex-row",
14
+ "horizontal-reverse": "flex-row-reverse",
15
+ } as const satisfies Record<StackOrientation, string>;
16
+
17
+ const aligns = {
18
+ start: "items-start",
19
+ center: "items-center",
20
+ end: "items-end",
21
+ stretch: "items-stretch",
22
+ } as const;
23
+
24
+ const justifies = {
25
+ start: "justify-start",
26
+ center: "justify-center",
27
+ end: "justify-end",
28
+ "space-between": "justify-between",
29
+ "space-around": "justify-around",
30
+ "space-evenly": "justify-evenly",
31
+ stretch: "justify-stretch",
32
+ } as const;
33
+
34
+ export interface StackProps extends React.ComponentProps<"div"> {
35
+ as?: React.ElementType;
36
+ orientation?: StackOrientation;
37
+ align?: keyof typeof aligns;
38
+ justify?: keyof typeof justifies;
39
+ /** Swirl spacing step in px, e.g. `"16"`. Sets both axes. */
40
+ spacing?: SwirlSpacing;
41
+ columnSpacing?: SwirlSpacing;
42
+ rowSpacing?: SwirlSpacing;
43
+ wrap?: boolean;
44
+ /** CSS height, such as `"100%"`. */
45
+ height?: string;
46
+ }
47
+
48
+ /**
49
+ * Swirl's flex layout primitive, with spacing named in Swirl's px steps.
50
+ *
51
+ * As in Swirl, it is full width, children align to the start, and they may
52
+ * shrink but not grow or overflow; `justify="stretch"` lets them grow.
53
+ */
54
+ export function Stack({
55
+ as: Component = "div",
56
+ className,
57
+ style,
58
+ orientation = "vertical",
59
+ align = "start",
60
+ justify = "start",
61
+ spacing,
62
+ columnSpacing,
63
+ rowSpacing,
64
+ wrap = false,
65
+ height,
66
+ ...props
67
+ }: StackProps) {
68
+ return (
69
+ <Component
70
+ className={cn(
71
+ "m-0 flex w-full max-w-full list-none p-0 *:min-w-0 *:max-w-full",
72
+ orientations[orientation],
73
+ aligns[align],
74
+ justifies[justify],
75
+ justify === "stretch" && "*:grow",
76
+ spacing && gap(spacing),
77
+ columnSpacing && gapX(columnSpacing),
78
+ rowSpacing && gapY(rowSpacing),
79
+ wrap && "flex-wrap",
80
+ className,
81
+ )}
82
+ style={height ? { height, ...style } : style}
83
+ {...props}
84
+ />
85
+ );
86
+ }
@@ -0,0 +1,31 @@
1
+ import { cn } from "../lib/utils";
2
+
3
+ const dotColors = {
4
+ critical: "bg-critical",
5
+ info: "bg-info",
6
+ success: "bg-success",
7
+ warning: "bg-warning",
8
+ } as const;
9
+
10
+ export interface StatusIndicatorProps extends Omit<React.ComponentProps<"div">, "children"> {
11
+ /** The status, such as "Active" or "Offline". */
12
+ label: React.ReactNode;
13
+ intent: keyof typeof dotColors;
14
+ }
15
+
16
+ /**
17
+ * Swirl's status indicator: a coloured dot and a short label, such as a
18
+ * person's or a system's state in a table cell. The label truncates rather
19
+ * than wrapping. The colour only reinforces the label, so the label must
20
+ * say the status on its own.
21
+ */
22
+ export function StatusIndicator({ className, label, intent, ...props }: StatusIndicatorProps) {
23
+ return (
24
+ <div className={cn("flex items-center gap-1", className)} {...props}>
25
+ <span aria-hidden className="inline-flex size-5 shrink-0 items-center justify-center">
26
+ <span className={cn("size-2 rounded-full", dotColors[intent])} />
27
+ </span>
28
+ <span className="min-w-0 truncate text-sm">{label}</span>
29
+ </div>
30
+ );
31
+ }
@@ -0,0 +1,90 @@
1
+ import { Switch as SwitchPrimitive } from "radix-ui";
2
+ import { cn } from "../lib/utils";
3
+
4
+ export interface SwitchProps extends React.ComponentProps<typeof SwitchPrimitive.Root> {
5
+ label?: React.ReactNode;
6
+ description?: React.ReactNode;
7
+ /** Put the label before the switch instead of after it. */
8
+ labelPosition?: "start" | "end";
9
+ /** Keep the label for screen readers only. */
10
+ hideLabel?: boolean;
11
+ /** Keep the description for screen readers only. */
12
+ hideDescription?: boolean;
13
+ }
14
+
15
+ /**
16
+ * Swirl's switch: an immediate on/off toggle, 44x24px on touch screens and
17
+ * 30x18px with the desktop layout. Use a Checkbox when the change needs
18
+ * submitting.
19
+ */
20
+ export function Switch({
21
+ className,
22
+ label,
23
+ description,
24
+ labelPosition = "end",
25
+ hideLabel = false,
26
+ hideDescription = false,
27
+ id,
28
+ ...props
29
+ }: SwitchProps) {
30
+ const control = (
31
+ <SwitchPrimitive.Root
32
+ id={id}
33
+ className={cn(
34
+ "group/switch relative h-6 w-11 shrink-0 cursor-pointer rounded-full outline-none",
35
+ "desktop:h-4.5 desktop:w-7.5",
36
+ "bg-interactive-primary-disabled transition-colors duration-200 motion-reduce:transition-none",
37
+ "data-[state=checked]:bg-interactive-primary",
38
+ "disabled:cursor-default disabled:data-[state=checked]:bg-interactive-primary-disabled",
39
+ // Swirl's focus ring: a 1px gap in the page colour, then 2px of focus.
40
+ "focus-visible:shadow-[0_0_0_1px_var(--color-background),0_0_0_3px_var(--color-focus)]",
41
+ className,
42
+ )}
43
+ {...props}
44
+ >
45
+ <SwitchPrimitive.Thumb
46
+ className={cn(
47
+ "pointer-events-none absolute top-0.5 left-0.5 block size-5 rounded-full bg-surface",
48
+ "desktop:size-3.5 group-disabled/switch:bg-interactive-neutral",
49
+ "transition-transform duration-200 motion-reduce:transition-none",
50
+ "data-[state=checked]:translate-x-5 desktop:data-[state=checked]:translate-x-3",
51
+ )}
52
+ />
53
+ </SwitchPrimitive.Root>
54
+ );
55
+
56
+ if (!label && !description) return control;
57
+
58
+ const text = (
59
+ <span className={cn("relative flex min-w-0 flex-1 flex-col", labelPosition === "start" ? "mr-3" : "ml-3", hideLabel && (!description || hideDescription) && "m-0")}>
60
+ {label && (
61
+ <span
62
+ className={cn(
63
+ hideLabel
64
+ ? "sr-only"
65
+ : "truncate text-sm leading-6 font-medium text-foreground group-has-disabled/label:text-disabled",
66
+ )}
67
+ >
68
+ {label}
69
+ </span>
70
+ )}
71
+ {description && (
72
+ <span className={hideDescription ? "sr-only" : "text-sm leading-5 text-subdued"}>
73
+ {description}
74
+ </span>
75
+ )}
76
+ </span>
77
+ );
78
+
79
+ return (
80
+ <label
81
+ className={cn(
82
+ "group/label inline-flex max-w-full cursor-pointer touch-none items-center has-disabled:cursor-default",
83
+ labelPosition === "start" && "flex-row-reverse",
84
+ )}
85
+ >
86
+ {control}
87
+ {text}
88
+ </label>
89
+ );
90
+ }
@@ -0,0 +1,242 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+
4
+ const ArrowIcon = ({ direction }: { direction: "up" | "down" }) => (
5
+ <svg viewBox="0 0 24 24" className="size-5" aria-hidden focusable={false}>
6
+ <path
7
+ d={
8
+ direction === "up"
9
+ ? "M12 20C11.7167 20 11.4793 19.904 11.288 19.712C11.096 19.5207 11 19.2834 11 19V7.82505L6.125 12.7C5.925 12.9 5.68733 13 5.412 13C5.13733 13 4.9 12.9 4.7 12.7C4.5 12.5 4.4 12.2667 4.4 12C4.4 11.7334 4.5 11.5 4.7 11.3L11.3 4.70005C11.4 4.60005 11.5083 4.52905 11.625 4.48705C11.7417 4.44572 11.8667 4.42505 12 4.42505C12.1333 4.42505 12.2627 4.44572 12.388 4.48705C12.5127 4.52905 12.6167 4.60005 12.7 4.70005L19.3 11.3C19.5 11.5 19.6 11.7334 19.6 12C19.6 12.2667 19.5 12.5 19.3 12.7C19.1 12.9 18.8623 13 18.587 13C18.3123 13 18.075 12.9 17.875 12.7L13 7.82505V19C13 19.2834 12.9043 19.5207 12.713 19.712C12.521 19.904 12.2833 20 12 20Z"
10
+ : "M12 19.575C11.8667 19.575 11.7417 19.554 11.625 19.512C11.5083 19.4707 11.4 19.4 11.3 19.3L4.7 12.7C4.5 12.5 4.4 12.2623 4.4 11.987C4.4 11.7123 4.5 11.475 4.7 11.275C4.9 11.075 5.13333 10.975 5.4 10.975C5.66667 10.975 5.9 11.075 6.1 11.275L11 16.175V4.975C11 4.69167 11.096 4.45833 11.288 4.275C11.4793 4.09167 11.7167 4 12 4C12.2833 4 12.521 4.09567 12.713 4.287C12.9043 4.479 13 4.71667 13 5V16.175L17.9 11.275C18.1 11.075 18.3333 10.975 18.6 10.975C18.8667 10.975 19.1 11.075 19.3 11.275C19.5 11.475 19.6 11.7123 19.6 11.987C19.6 12.2623 19.5 12.5 19.3 12.7L12.7 19.3C12.6 19.4 12.4917 19.4707 12.375 19.512C12.2583 19.554 12.1333 19.575 12 19.575Z"
11
+ }
12
+ fill="currentColor"
13
+ />
14
+ </svg>
15
+ );
16
+
17
+ export interface TableProps extends React.ComponentProps<"table"> {
18
+ /** Describes the table's contents to screen readers. Hidden on screen, as in Swirl. */
19
+ caption?: React.ReactNode;
20
+ /** Keep the header visible while the body scrolls. */
21
+ stickyHeader?: boolean;
22
+ /** Wrapper class for the scroll container. */
23
+ wrapperClassName?: string;
24
+ }
25
+
26
+ /**
27
+ * Swirl's table: at least as wide as its container, and wider when its
28
+ * columns need it, inside its own scroll container so a wide table never
29
+ * scrolls the page sideways. Columns marked `sticky` stay pinned while it
30
+ * scrolls, with a shadow along the edge content passes under.
31
+ */
32
+ export function Table({
33
+ className,
34
+ wrapperClassName,
35
+ caption,
36
+ stickyHeader = false,
37
+ children,
38
+ ...props
39
+ }: TableProps) {
40
+ const ref = React.useRef<HTMLDivElement>(null);
41
+ const [scroll, setScroll] = React.useState({ scrolled: false, scrollable: false, atEnd: true });
42
+
43
+ const measure = React.useCallback(() => {
44
+ const el = ref.current;
45
+ if (!el) return;
46
+ const max = el.scrollWidth - el.clientWidth;
47
+ const next = { scrolled: el.scrollLeft > 0, scrollable: max > 0, atEnd: el.scrollLeft >= max - 1 };
48
+ setScroll((prev) =>
49
+ prev.scrolled === next.scrolled && prev.scrollable === next.scrollable && prev.atEnd === next.atEnd
50
+ ? prev
51
+ : next,
52
+ );
53
+ }, []);
54
+
55
+ React.useEffect(() => {
56
+ const el = ref.current;
57
+ if (!el) return;
58
+ measure();
59
+ const observer = new ResizeObserver(measure);
60
+ observer.observe(el);
61
+ if (el.firstElementChild) observer.observe(el.firstElementChild);
62
+ return () => observer.disconnect();
63
+ }, [measure]);
64
+
65
+ return (
66
+ <div
67
+ ref={ref}
68
+ data-scrolled={scroll.scrolled || undefined}
69
+ data-scroll-more={(scroll.scrollable && !scroll.atEnd) || undefined}
70
+ className={cn("group/table relative w-full overflow-auto", wrapperClassName)}
71
+ onScroll={measure}
72
+ >
73
+ <table
74
+ data-sticky-header={stickyHeader || undefined}
75
+ className={cn(
76
+ "w-max min-w-[max(20rem,100%)] border-separate border-spacing-0 text-sm leading-5",
77
+ className,
78
+ )}
79
+ {...props}
80
+ >
81
+ {caption && <caption className="sr-only">{caption}</caption>}
82
+ {children}
83
+ </table>
84
+ </div>
85
+ );
86
+ }
87
+
88
+ export function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
89
+ return (
90
+ <thead
91
+ className={cn(
92
+ // Honours `stickyHeader` on the parent table.
93
+ "[[data-sticky-header]_&]:sticky [[data-sticky-header]_&]:top-0 [[data-sticky-header]_&]:z-20",
94
+ className,
95
+ )}
96
+ {...props}
97
+ />
98
+ );
99
+ }
100
+
101
+ export function TableBody(props: React.ComponentProps<"tbody">) {
102
+ return <tbody {...props} />;
103
+ }
104
+
105
+ export function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
106
+ return (
107
+ <tfoot
108
+ className={cn("font-medium [&_td]:bg-surface-sunken [&_td]:border-b-0", className)}
109
+ {...props}
110
+ />
111
+ );
112
+ }
113
+
114
+ export interface TableRowProps extends React.ComponentProps<"tr"> {
115
+ /** Highlight the row, such as the current selection. Swirl's `highlighted`. */
116
+ selected?: boolean;
117
+ /** Add hover feedback for clickable rows. */
118
+ interactive?: boolean;
119
+ }
120
+
121
+ /** A row. Its colour fills the cells, so pinned cells cover what scrolls under them. */
122
+ export function TableRow({ className, selected, interactive, ...props }: TableRowProps) {
123
+ return (
124
+ <tr
125
+ data-state={selected ? "selected" : undefined}
126
+ className={cn(
127
+ "bg-surface transition-colors",
128
+ interactive && "cursor-pointer hover:bg-surface-hovered",
129
+ selected && "bg-surface-highlight",
130
+ className,
131
+ )}
132
+ {...props}
133
+ />
134
+ );
135
+ }
136
+
137
+ /*
138
+ * A pinned cell inherits the row's colour so content scrolling under it is
139
+ * hidden. Swirl's shadows: one to the right of start columns once the table
140
+ * has scrolled, one beside end columns while there is more to scroll to.
141
+ */
142
+ const stickyClasses = (sticky: "start" | "end" | undefined) =>
143
+ cn(
144
+ sticky && "sticky z-10 bg-inherit transition-shadow duration-150",
145
+ sticky === "start" &&
146
+ "left-0 group-data-scrolled/table:shadow-[4px_0_16px_-4px_rgba(23,23,23,0.2),2px_0_4px_-2px_rgba(23,23,23,0.2)]",
147
+ sticky === "end" &&
148
+ "right-0 group-data-scroll-more/table:shadow-[0_4px_16px_0_rgba(23,23,23,0.2),0_1px_4px_0_rgba(23,23,23,0.2)]",
149
+ );
150
+
151
+ export interface TableHeadProps extends Omit<React.ComponentProps<"th">, "align"> {
152
+ align?: "start" | "center" | "end";
153
+ /** Show a sort arrow slot, as Swirl's `sortable` columns do. */
154
+ sortable?: boolean;
155
+ /** The current sort: shows an arrow and sets `aria-sort`. */
156
+ sorted?: "ascending" | "descending";
157
+ /**
158
+ * Pin the column while the table scrolls. Give later pinned columns a
159
+ * `style={{ left }}` (or `right`) offset past the ones before them.
160
+ */
161
+ sticky?: "start" | "end";
162
+ }
163
+
164
+ /** A column header: 14px medium, subdued, on one line. */
165
+ export function TableHead({
166
+ className,
167
+ align = "start",
168
+ sortable,
169
+ sorted,
170
+ sticky,
171
+ children,
172
+ ...props
173
+ }: TableHeadProps) {
174
+ return (
175
+ <th
176
+ scope="col"
177
+ aria-sort={sorted}
178
+ className={cn(
179
+ "border-b border-border bg-surface px-4 py-3 align-middle font-medium whitespace-nowrap text-subdued",
180
+ align === "start" && "text-left",
181
+ align === "center" && "text-center",
182
+ align === "end" && "text-right",
183
+ stickyClasses(sticky),
184
+ className,
185
+ )}
186
+ {...props}
187
+ >
188
+ {sortable || sorted ? (
189
+ <span className="inline-flex items-center gap-2 align-middle">
190
+ <span>{children}</span>
191
+ <span className="inline-flex size-5 shrink-0">
192
+ {sorted && <ArrowIcon direction={sorted === "ascending" ? "up" : "down"} />}
193
+ </span>
194
+ </span>
195
+ ) : (
196
+ children
197
+ )}
198
+ </th>
199
+ );
200
+ }
201
+
202
+ export interface TableCellProps extends Omit<React.ComponentProps<"td">, "align"> {
203
+ align?: "start" | "center" | "end";
204
+ /** Pin the cell while the table scrolls; match its column's `sticky`. */
205
+ sticky?: "start" | "end";
206
+ }
207
+
208
+ export function TableCell({ className, align = "start", sticky, ...props }: TableCellProps) {
209
+ return (
210
+ <td
211
+ className={cn(
212
+ "border-b border-border px-4 py-2 align-middle text-foreground [overflow-wrap:anywhere] hyphens-auto",
213
+ align === "start" && "text-left",
214
+ align === "center" && "text-center",
215
+ align === "end" && "text-right",
216
+ stickyClasses(sticky),
217
+ className,
218
+ )}
219
+ {...props}
220
+ />
221
+ );
222
+ }
223
+
224
+ export interface TableEmptyProps extends React.ComponentProps<"td"> {
225
+ /** How many columns the message spans. */
226
+ colSpan: number;
227
+ }
228
+
229
+ /** Swirl's empty state: one row across every column, "No results found." unless you pass content. */
230
+ export function TableEmpty({ className, colSpan, children, ...props }: TableEmptyProps) {
231
+ return (
232
+ <tr className="bg-surface">
233
+ <td
234
+ colSpan={colSpan}
235
+ className={cn("border-b border-border px-4 py-2 text-center", className)}
236
+ {...props}
237
+ >
238
+ {children ?? "No results found."}
239
+ </td>
240
+ </tr>
241
+ );
242
+ }