@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,147 @@
1
+ import * as React from "react";
2
+ import { Tabs as TabsPrimitive } from "radix-ui";
3
+ import { cn } from "../lib/utils";
4
+ import { Badge, type BadgeIntent } from "./badge";
5
+ import { Tooltip } from "./tooltip";
6
+
7
+ export const Tabs = TabsPrimitive.Root;
8
+
9
+ type TabsVariant = "underline" | "pill" | "segmented";
10
+
11
+ const VariantContext = React.createContext<TabsVariant>("underline");
12
+
13
+ export interface TabsListProps extends React.ComponentProps<typeof TabsPrimitive.List> {
14
+ /**
15
+ * `underline` is Swirl's default tab bar and `pill` its pill bar.
16
+ * `segmented`, a filled control, is not in Swirl.
17
+ */
18
+ variant?: TabsVariant;
19
+ /** Share the row evenly between the tabs. Swirl's `justify="evenly"`. */
20
+ stretch?: boolean;
21
+ }
22
+
23
+ /** The row of tabs. It scrolls sideways, without a scrollbar, when the tabs overflow. */
24
+ export function TabsList({
25
+ className,
26
+ variant = "underline",
27
+ stretch = false,
28
+ ...props
29
+ }: TabsListProps) {
30
+ return (
31
+ <VariantContext.Provider value={variant}>
32
+ <TabsPrimitive.List
33
+ data-variant={variant}
34
+ className={cn(
35
+ "relative flex w-full justify-start overflow-x-auto [scrollbar-width:none]",
36
+ variant === "underline" && "border-b border-border",
37
+ variant === "pill" && "gap-2",
38
+ variant === "segmented" && "w-auto items-center gap-1 rounded-base bg-surface-sunken p-1",
39
+ stretch && "*:flex-1",
40
+ className,
41
+ )}
42
+ {...props}
43
+ />
44
+ </VariantContext.Provider>
45
+ );
46
+ }
47
+
48
+ const triggerVariants = {
49
+ underline: [
50
+ "bg-transparent px-2 py-1 text-interactive-neutral desktop:py-1.5",
51
+ // Swirl's 3px rounded bar, 4px in from each side: grey on hover, highlight when active.
52
+ "after:pointer-events-none after:absolute after:bottom-0 after:left-1/2 after:h-[0.1875rem] after:w-[calc(100%-0.5rem)] after:-translate-x-1/2 after:rounded-[0.125rem]",
53
+ "not-aria-selected:not-disabled:hover:text-interactive-neutral-hovered not-aria-selected:not-disabled:hover:after:bg-border",
54
+ "not-aria-selected:not-disabled:active:text-interactive-neutral-pressed",
55
+ "aria-selected:text-highlight aria-selected:after:bg-border-highlight",
56
+ ],
57
+ pill: [
58
+ "gap-2 rounded-full bg-neutral px-4 py-2 text-foreground",
59
+ "not-disabled:hover:bg-neutral-hovered not-disabled:active:bg-neutral-pressed",
60
+ "aria-selected:bg-primary aria-selected:text-on-primary",
61
+ "aria-selected:hover:bg-primary aria-selected:active:bg-primary",
62
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid focus-visible:outline-focus",
63
+ ],
64
+ segmented: [
65
+ "gap-2 rounded-sm px-3 py-1.5 text-subdued hover:text-foreground",
66
+ "aria-selected:bg-surface aria-selected:text-foreground aria-selected:shadow-level-1",
67
+ "focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-focus",
68
+ ],
69
+ } as const satisfies Record<TabsVariant, string[]>;
70
+
71
+ const suffixVariants = {
72
+ underline: "text-interactive-neutral group-aria-selected/tab:text-highlight",
73
+ pill: "text-subdued group-aria-selected/tab:text-on-primary",
74
+ segmented: "text-subdued",
75
+ } as const satisfies Record<TabsVariant, string>;
76
+
77
+ export interface TabsTriggerProps extends React.ComponentProps<typeof TabsPrimitive.Trigger> {
78
+ /** Leading icon, 20px. */
79
+ icon?: React.ReactNode;
80
+ /** Quiet text after the label, such as a count. */
81
+ suffix?: React.ReactNode;
82
+ /** A status dot after the label. `label` names it for screen readers. */
83
+ badge?: { label: string; intent?: BadgeIntent };
84
+ /** Help text shown in a tooltip on hover and focus. */
85
+ tooltip?: React.ReactNode;
86
+ }
87
+
88
+ /*
89
+ * Tabs are styled on aria-selected rather than data-state: a Tooltip wrapped
90
+ * around a tab sets its own data-state on the same element.
91
+ */
92
+ export function TabsTrigger({
93
+ className,
94
+ icon,
95
+ suffix,
96
+ badge,
97
+ tooltip,
98
+ children,
99
+ ...props
100
+ }: TabsTriggerProps) {
101
+ const variant = React.useContext(VariantContext);
102
+
103
+ const trigger = (
104
+ <TabsPrimitive.Trigger
105
+ className={cn(
106
+ "group/tab relative flex min-w-fit shrink-0 cursor-pointer items-center justify-center",
107
+ "border-none leading-6 font-medium outline-none desktop:text-sm desktop:leading-5",
108
+ "disabled:cursor-default disabled:text-disabled",
109
+ triggerVariants[variant],
110
+ className,
111
+ )}
112
+ {...props}
113
+ >
114
+ {icon && <span className="inline-flex [&>svg]:size-5">{icon}</span>}
115
+ <span
116
+ className={cn(
117
+ "inline-flex items-center gap-1 rounded-(--radius-s) text-center whitespace-nowrap",
118
+ // Swirl pads the label, not the tab, and rings it on keyboard focus.
119
+ variant === "underline" &&
120
+ "p-2 group-focus-visible/tab:shadow-[inset_0_0_0_0.125rem_var(--color-focus)]",
121
+ )}
122
+ >
123
+ {children}
124
+ {suffix && <span className={cn("font-normal", suffixVariants[variant])}>{suffix}</span>}
125
+ {badge && (
126
+ <span className="inline-flex pl-1.5">
127
+ <Badge
128
+ variant="dot"
129
+ size="xs"
130
+ intent={badge.intent ?? "info"}
131
+ label={badge.label}
132
+ // On a pill the dot sits on the pill's own colour, so it needs no ring.
133
+ className={variant === "pill" ? "ring-transparent" : undefined}
134
+ />
135
+ </span>
136
+ )}
137
+ </span>
138
+ </TabsPrimitive.Trigger>
139
+ );
140
+
141
+ return tooltip ? <Tooltip content={tooltip}>{trigger}</Tooltip> : trigger;
142
+ }
143
+
144
+ /** A tab's panel, padded 8px as in Swirl. */
145
+ export function TabsContent({ className, ...props }: React.ComponentProps<typeof TabsPrimitive.Content>) {
146
+ return <TabsPrimitive.Content className={cn("p-2 outline-none", className)} {...props} />;
147
+ }
@@ -0,0 +1,169 @@
1
+ import { cva, type VariantProps } from "class-variance-authority";
2
+ import { cn } from "../lib/utils";
3
+
4
+ /*
5
+ * Swirl's tag. It always carries a 1px transparent border, so `outline`
6
+ * recolours it rather than shifting the layout. The remove button takes the
7
+ * tag's icon colour, set here as `--tag-icon`.
8
+ */
9
+ const tagVariants = cva(
10
+ "relative inline-flex max-w-full shrink-0 items-center rounded-(--radius-s) border border-transparent text-sm leading-5 font-medium whitespace-nowrap [&_svg]:shrink-0",
11
+ {
12
+ variants: {
13
+ intent: {
14
+ default: "bg-surface-raised text-foreground [--tag-icon:var(--color-icon-strong)]",
15
+ critical: "bg-surface-critical-subdued text-critical [--tag-icon:var(--color-critical)]",
16
+ warning: "bg-surface-warning-subdued text-warning [--tag-icon:var(--color-warning)]",
17
+ success: "bg-surface-success-subdued text-success [--tag-icon:var(--color-success)]",
18
+ info: "bg-surface-info-subdued text-info [--tag-icon:var(--color-info)]",
19
+ special: "bg-grape-subdued text-grape-text [--tag-icon:var(--color-grape-text)]",
20
+ translucent:
21
+ "bg-[var(--s-surface-on-image-default)] text-on-image [--tag-icon:var(--color-on-image)]",
22
+ },
23
+ variant: {
24
+ default: "",
25
+ outline: "border-border-strong",
26
+ strong: "",
27
+ },
28
+ size: {
29
+ s: "px-1",
30
+ m: "px-2 py-0.5",
31
+ },
32
+ interactive: {
33
+ // A state layer over the whole tag, whatever its colour.
34
+ true: [
35
+ "cursor-pointer after:pointer-events-none after:absolute after:-inset-px after:rounded-[inherit]",
36
+ "hover:after:bg-state-hovered active:after:bg-state-pressed",
37
+ "outline-focus focus:not-focus-visible:outline-none",
38
+ ],
39
+ false: "",
40
+ },
41
+ bordered: { true: "border-border-strong", false: "" },
42
+ },
43
+ compoundVariants: [
44
+ // `strong` inverts the tint into a filled surface.
45
+ { variant: "strong", intent: "default", class: "bg-surface-neutral text-on-status [--tag-icon:var(--color-on-status)]" },
46
+ { variant: "strong", intent: "critical", class: "bg-surface-critical text-on-status [--tag-icon:var(--color-on-status)]" },
47
+ { variant: "strong", intent: "warning", class: "bg-surface-warning text-dark [--tag-icon:var(--color-dark)]" },
48
+ { variant: "strong", intent: "success", class: "bg-surface-success text-on-status [--tag-icon:var(--color-on-status)]" },
49
+ { variant: "strong", intent: "info", class: "bg-surface-info text-on-status [--tag-icon:var(--color-on-status)]" },
50
+ { variant: "strong", intent: "special", class: "bg-grape text-on-status [--tag-icon:var(--color-on-status)]" },
51
+ // `outline` borders the tag in its intent colour.
52
+ { variant: "outline", intent: "critical", class: "border-border-critical" },
53
+ { variant: "outline", intent: "success", class: "border-border-success" },
54
+ { variant: "outline", intent: "warning", class: "border-border-warning" },
55
+ { variant: "outline", intent: "info", class: "border-border-info" },
56
+ { variant: "outline", intent: "special", class: "border-grape-text" },
57
+ { variant: "outline", intent: "translucent", class: "border-on-image" },
58
+ ],
59
+ defaultVariants: { intent: "default", variant: "default", size: "m", interactive: false },
60
+ },
61
+ );
62
+
63
+ export interface TagProps
64
+ extends Omit<React.ComponentProps<"span">, "onClick">,
65
+ VariantProps<typeof tagVariants> {
66
+ /** Tag text. Interchangeable with `children`. */
67
+ label?: string;
68
+ /** Render icon-only, keeping `label` as the accessible name. */
69
+ hideLabel?: boolean;
70
+ icon?: React.ReactNode;
71
+ iconPosition?: "start" | "end";
72
+ /** Show a remove button and call `onRemove` when it is pressed. */
73
+ removable?: boolean;
74
+ removalButtonLabel?: string;
75
+ onRemove?: (event: React.MouseEvent<HTMLButtonElement>) => void;
76
+ onClick?: (event: React.MouseEvent<HTMLElement>) => void;
77
+ }
78
+
79
+ /**
80
+ * A labelled descriptor, optionally removable.
81
+ *
82
+ * When `removable` is set the tag renders its own nested button, so an
83
+ * `interactive` tag stays a `<button>` only for its own activation - the remove
84
+ * control stops propagation rather than triggering both.
85
+ */
86
+ export function Tag({
87
+ className,
88
+ intent,
89
+ variant,
90
+ size,
91
+ interactive,
92
+ bordered,
93
+ label,
94
+ hideLabel = false,
95
+ icon,
96
+ iconPosition = "start",
97
+ removable = false,
98
+ removalButtonLabel = "Remove",
99
+ onRemove,
100
+ onClick,
101
+ children,
102
+ ...props
103
+ }: TagProps) {
104
+ const content = children ?? label;
105
+ const isButton = Boolean(interactive || onClick);
106
+ const Component = (isButton ? "button" : "span") as React.ElementType;
107
+ const small = size === "s";
108
+
109
+ return (
110
+ <Component
111
+ type={isButton ? "button" : undefined}
112
+ onClick={onClick}
113
+ className={cn(
114
+ tagVariants({ intent, variant, size, interactive: isButton, bordered }),
115
+ hideLabel && "px-0",
116
+ className,
117
+ )}
118
+ {...props}
119
+ >
120
+ {icon && (
121
+ // Swirl tucks the icon into the padding: 4px (2px when small) on its
122
+ // outer side, 4px of space towards the label.
123
+ <span
124
+ className={cn(
125
+ "inline-flex shrink-0",
126
+ hideLabel
127
+ ? small
128
+ ? "mx-0"
129
+ : "mx-0.5"
130
+ : iconPosition === "end"
131
+ ? cn("order-2 ml-1", small ? "-mr-0.5" : "-mr-1")
132
+ : cn("mr-1", small ? "-ml-0.5" : "-ml-1"),
133
+ )}
134
+ >
135
+ {icon}
136
+ </span>
137
+ )}
138
+ {hideLabel ? (
139
+ <span className="sr-only">{label}</span>
140
+ ) : (
141
+ <span className="order-1 min-w-0 truncate">{content}</span>
142
+ )}
143
+ {removable && (
144
+ <button
145
+ type="button"
146
+ aria-label={`${removalButtonLabel}${label ? `: ${label}` : ""}`}
147
+ onClick={(event) => {
148
+ // Keep a removable, interactive tag from firing both handlers.
149
+ event.stopPropagation();
150
+ onRemove?.(event);
151
+ }}
152
+ className={cn(
153
+ "relative z-1 order-3 -mr-1 ml-1 inline-flex shrink-0 cursor-pointer border-none bg-transparent p-0",
154
+ "text-[var(--tag-icon)] outline-focus focus:not-focus-visible:outline-none",
155
+ )}
156
+ >
157
+ <svg viewBox="0 0 16 16" className="size-4" aria-hidden focusable={false}>
158
+ <path
159
+ d="M8 9.06672L4.8 12.2667C4.65555 12.4112 4.47777 12.4834 4.26666 12.4834C4.05555 12.4834 3.87777 12.4112 3.73333 12.2667C3.58888 12.1223 3.51666 11.9445 3.51666 11.7334C3.51666 11.5223 3.58888 11.3445 3.73333 11.2001L6.93333 8.00006L3.73333 4.80006C3.58888 4.65561 3.51666 4.47783 3.51666 4.26672C3.51666 4.05561 3.58888 3.87783 3.73333 3.73339C3.87777 3.58895 4.05555 3.51672 4.26666 3.51672C4.47777 3.51672 4.65555 3.58895 4.8 3.73339L8 6.93339L11.2 3.73339C11.3444 3.58895 11.5222 3.51672 11.7333 3.51672C11.9444 3.51672 12.1222 3.58895 12.2667 3.73339C12.4111 3.87783 12.4833 4.05561 12.4833 4.26672C12.4833 4.47783 12.4111 4.65561 12.2667 4.80006L9.06666 8.00006L12.2667 11.2001C12.4111 11.3445 12.4833 11.5223 12.4833 11.7334C12.4833 11.9445 12.4111 12.1223 12.2667 12.2667C12.1222 12.4112 11.9444 12.4834 11.7333 12.4834C11.5222 12.4834 11.3444 12.4112 11.2 12.2667L8 9.06672Z"
160
+ fill="currentColor"
161
+ />
162
+ </svg>
163
+ </button>
164
+ )}
165
+ </Component>
166
+ );
167
+ }
168
+
169
+ export { tagVariants };
@@ -0,0 +1,126 @@
1
+ import { cva, type VariantProps } from "class-variance-authority";
2
+ import { cn } from "../lib/utils";
3
+
4
+ const textVariants = cva("", {
5
+ variants: {
6
+ size: {
7
+ xs: "text-xs",
8
+ sm: "text-sm",
9
+ base: "text-base",
10
+ lg: "text-lg",
11
+ xl: "text-xl",
12
+ "2xl": "text-2xl",
13
+ },
14
+ color: {
15
+ default: "text-foreground",
16
+ subdued: "text-subdued",
17
+ disabled: "text-disabled",
18
+ critical: "text-critical",
19
+ success: "text-success",
20
+ warning: "text-warning",
21
+ highlight: "text-highlight",
22
+ "on-action-primary": "text-on-primary",
23
+ "on-status": "text-on-status",
24
+ "on-image": "text-on-image",
25
+ "on-surface-highlight": "text-on-highlight",
26
+ "on-surface-highlight-subdued": "text-on-highlight-subdued",
27
+ },
28
+ weight: {
29
+ normal: "font-normal",
30
+ medium: "font-medium",
31
+ semibold: "font-semibold",
32
+ bold: "font-bold",
33
+ },
34
+ align: { start: "text-start", center: "text-center", end: "text-end" },
35
+ fontFamily: { text: "font-sans", code: "font-mono" },
36
+ fontStyle: { normal: "not-italic", italic: "italic" },
37
+ balance: { true: "text-balance", false: "" },
38
+ /**
39
+ * Step the size down one notch with the desktop layout, as Swirl does by
40
+ * default: body text is 16px on touch screens and 14px on desktop, which
41
+ * keeps it in step with the other components.
42
+ */
43
+ responsive: { true: "", false: "" },
44
+ },
45
+ compoundVariants: [
46
+ { responsive: true, size: "base", class: "desktop:text-sm" },
47
+ { responsive: true, size: "lg", class: "desktop:text-base" },
48
+ { responsive: true, size: "xl", class: "desktop:text-lg" },
49
+ { responsive: true, size: "2xl", class: "desktop:text-xl" },
50
+ ],
51
+ defaultVariants: {
52
+ size: "base",
53
+ color: "default",
54
+ weight: "normal",
55
+ fontFamily: "text",
56
+ responsive: true,
57
+ },
58
+ });
59
+
60
+ export type TextSize = NonNullable<VariantProps<typeof textVariants>["size"]>;
61
+ export type TextColor = NonNullable<VariantProps<typeof textVariants>["color"]>;
62
+ export type TextWeight = NonNullable<VariantProps<typeof textVariants>["weight"]>;
63
+
64
+ export interface TextProps
65
+ extends Omit<React.ComponentProps<"span">, "color">,
66
+ VariantProps<typeof textVariants> {
67
+ /** Element to render. Defaults to `span`. */
68
+ as?: React.ElementType;
69
+ /** Truncate to a single line with an ellipsis. */
70
+ truncate?: boolean;
71
+ /**
72
+ * Which end `truncate` cuts. `start` keeps the end visible, for file paths
73
+ * and the like. Like Swirl it reverses the line's direction, but it keeps
74
+ * the text itself left-to-right, so a leading "/" stays at the front.
75
+ */
76
+ truncateDirection?: "start" | "end";
77
+ /** Clamp to N lines. Takes precedence over `truncate`. */
78
+ lines?: number;
79
+ whiteSpace?: React.CSSProperties["whiteSpace"];
80
+ }
81
+
82
+ /** Swirl's text primitive: the type scale and semantic text colours as props. */
83
+ export function Text({
84
+ as: Component = "span",
85
+ className,
86
+ size,
87
+ color,
88
+ weight,
89
+ align,
90
+ fontFamily,
91
+ fontStyle,
92
+ balance,
93
+ responsive,
94
+ truncate,
95
+ truncateDirection = "end",
96
+ lines,
97
+ whiteSpace,
98
+ style,
99
+ children,
100
+ ...props
101
+ }: TextProps) {
102
+ const fromStart = truncate && !lines && truncateDirection === "start";
103
+ return (
104
+ <Component
105
+ className={cn(
106
+ textVariants({ size, color, weight, align, fontFamily, fontStyle, balance, responsive }),
107
+ truncate && !lines && "truncate",
108
+ fromStart && "text-left [direction:rtl]",
109
+ lines && "overflow-hidden",
110
+ className,
111
+ )}
112
+ style={{
113
+ ...(lines
114
+ ? { display: "-webkit-box", WebkitBoxOrient: "vertical", WebkitLineClamp: lines }
115
+ : null),
116
+ ...(whiteSpace ? { whiteSpace } : null),
117
+ ...style,
118
+ }}
119
+ {...props}
120
+ >
121
+ {fromStart ? <bdi dir="ltr">{children}</bdi> : children}
122
+ </Component>
123
+ );
124
+ }
125
+
126
+ export { textVariants };
@@ -0,0 +1,130 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+ import { useFieldText } from "../lib/field-context";
4
+ import { bareFieldControl, inputVariants } from "./input";
5
+
6
+ export interface TextareaProps extends React.ComponentProps<"textarea"> {
7
+ /** Grow with the content without limit, instead of stopping at 13.75rem. */
8
+ autoResize?: boolean;
9
+ /** Show "used / max" characters. Use with `maxLength`. */
10
+ showCharacterCounter?: boolean;
11
+ /** Only show the counter from 80% of `maxLength`. */
12
+ showCharacterCounterNearLimit?: boolean;
13
+ characterCounterLabel?: string;
14
+ }
15
+
16
+ /**
17
+ * A multi-line text field.
18
+ *
19
+ * Inside a `FormControl` it renders bare and grows with its text, as Swirl's
20
+ * does: at least `rows` lines, up to 13.75rem (unlimited with `autoResize`).
21
+ * On its own it has the Input's box and a resize handle. Growing uses
22
+ * `field-sizing: content`, so nothing is measured on each keystroke; browsers
23
+ * without it keep the fixed height and scroll.
24
+ */
25
+ export function Textarea({
26
+ className,
27
+ autoResize = false,
28
+ rows = 3,
29
+ showCharacterCounter = false,
30
+ showCharacterCounterNearLimit = false,
31
+ characterCounterLabel = "Used characters",
32
+ value,
33
+ defaultValue,
34
+ onChange,
35
+ placeholder,
36
+ maxLength,
37
+ disabled,
38
+ readOnly,
39
+ id,
40
+ style,
41
+ ...props
42
+ }: TextareaProps) {
43
+ const { field, text, handleChange } = useFieldText<HTMLTextAreaElement>(value, defaultValue, onChange);
44
+
45
+ const setHasPlaceholder = field?.setHasPlaceholder;
46
+ const setHasCounter = field?.setHasCounter;
47
+ React.useEffect(() => {
48
+ setHasPlaceholder?.(Boolean(placeholder));
49
+ }, [setHasPlaceholder, placeholder]);
50
+ React.useEffect(() => {
51
+ setHasCounter?.(showCharacterCounter);
52
+ }, [setHasCounter, showCharacterCounter]);
53
+
54
+ const showCounter =
55
+ showCharacterCounter &&
56
+ (!showCharacterCounterNearLimit || (maxLength != null && text.length >= maxLength * 0.8));
57
+
58
+ const counter = showCounter && (
59
+ <span aria-live="polite" className="text-sm leading-5 text-subdued tabular-nums">
60
+ <span className="sr-only">{characterCounterLabel}</span>
61
+ {text.length}
62
+ {maxLength != null ? ` / ${maxLength}` : ""}
63
+ </span>
64
+ );
65
+
66
+ const shared = {
67
+ ...props,
68
+ id: id ?? field?.id,
69
+ rows,
70
+ value,
71
+ defaultValue,
72
+ placeholder,
73
+ maxLength,
74
+ disabled: disabled ?? field?.disabled,
75
+ readOnly: readOnly ?? field?.readOnly,
76
+ required: props.required ?? (field?.required || undefined),
77
+ "aria-describedby": props["aria-describedby"] ?? field?.describedBy,
78
+ "aria-invalid": props["aria-invalid"] ?? (field?.invalid || undefined),
79
+ onChange: handleChange,
80
+ };
81
+
82
+ if (field) {
83
+ const compact = field.inline || field.labelPosition === "outside";
84
+ return (
85
+ <>
86
+ <textarea
87
+ {...shared}
88
+ // At least `rows` lines tall, whatever field-sizing does.
89
+ style={{ minHeight: `${rows}lh`, ...style }}
90
+ className={cn(
91
+ bareFieldControl(field),
92
+ "block resize-none field-sizing-content",
93
+ !autoResize && "max-h-[13.75rem] overflow-y-auto",
94
+ className,
95
+ )}
96
+ />
97
+ {counter && (
98
+ <span
99
+ className={cn(
100
+ "absolute flex",
101
+ compact ? "top-full right-0 mt-1" : "top-3 right-4",
102
+ // While the label sits in the empty box, Swirl hides the counter.
103
+ !compact && field.labelAsPlaceholder && "invisible",
104
+ )}
105
+ >
106
+ {counter}
107
+ </span>
108
+ )}
109
+ </>
110
+ );
111
+ }
112
+
113
+ return (
114
+ <div className={cn("relative w-full", counter && "pb-6")}>
115
+ <textarea
116
+ {...shared}
117
+ style={style}
118
+ className={cn(
119
+ inputVariants({ size: "m" }),
120
+ // Input's size m sets desktop:min-h-[2.375rem], which outranks a plain
121
+ // min-h-20 from 992px up and collapsed the textarea to one line.
122
+ "min-h-20 resize-y desktop:min-h-20",
123
+ autoResize && "field-sizing-content resize-none",
124
+ className,
125
+ )}
126
+ />
127
+ {counter && <span className="absolute right-0 bottom-0 flex">{counter}</span>}
128
+ </div>
129
+ );
130
+ }
@@ -0,0 +1,115 @@
1
+ import * as React from "react";
2
+ import { ThemeContext, type Theme } from "../lib/theme-context";
3
+
4
+ export type { Theme, ThemeContextValue } from "../lib/theme-context";
5
+ export { useOptionalTheme } from "../lib/theme-context";
6
+
7
+ export function useTheme() {
8
+ const context = React.useContext(ThemeContext);
9
+ if (!context) throw new Error("useTheme must be used within a <ThemeProvider>");
10
+ return context;
11
+ }
12
+
13
+ /* Fired in this tab when the stored theme changes; `storage` covers others. */
14
+ const STORED_THEME_EVENT = "swirlcn-theme-change";
15
+
16
+ function subscribeToStoredTheme(onChange: () => void) {
17
+ window.addEventListener("storage", onChange);
18
+ window.addEventListener(STORED_THEME_EVENT, onChange);
19
+ return () => {
20
+ window.removeEventListener("storage", onChange);
21
+ window.removeEventListener(STORED_THEME_EVENT, onChange);
22
+ };
23
+ }
24
+
25
+ function readStoredTheme(storageKey: string | null): Theme | null {
26
+ if (!storageKey) return null;
27
+ try {
28
+ const stored = localStorage.getItem(storageKey);
29
+ return stored === "light" || stored === "dark" || stored === "system" ? stored : null;
30
+ } catch {
31
+ // Private mode or blocked storage: nothing is stored.
32
+ return null;
33
+ }
34
+ }
35
+
36
+ const DARK_QUERY = "(prefers-color-scheme: dark)";
37
+
38
+ function subscribeToSystemTheme(onChange: () => void) {
39
+ const query = window.matchMedia(DARK_QUERY);
40
+ query.addEventListener("change", onChange);
41
+ return () => query.removeEventListener("change", onChange);
42
+ }
43
+
44
+ export interface ThemeProviderProps {
45
+ children: React.ReactNode;
46
+ /** Initial theme when nothing is stored. */
47
+ defaultTheme?: Theme;
48
+ /** localStorage key. Pass `null` to skip persistence entirely. */
49
+ storageKey?: string | null;
50
+ /** Element the theme class is applied to. Defaults to `<html>`. */
51
+ target?: () => HTMLElement;
52
+ }
53
+
54
+ /**
55
+ * Applies Swirl's `theme-light` / `theme-dark` class to the document root.
56
+ *
57
+ * With `theme="system"` neither class is set, which is what lets the generated
58
+ * token stylesheet fall through to its `prefers-color-scheme` block - so the OS
59
+ * is followed with no JavaScript involved in the common case.
60
+ */
61
+ export function ThemeProvider({
62
+ children,
63
+ defaultTheme = "system",
64
+ storageKey = "swirlcn-theme",
65
+ target,
66
+ }: ThemeProviderProps) {
67
+ // The saved choice and the OS preference are both outside React, so they
68
+ // are read with useSyncExternalStore. On the server, and while hydrating,
69
+ // nothing is stored and the OS is light, so both renders agree.
70
+ const stored = React.useSyncExternalStore(
71
+ subscribeToStoredTheme,
72
+ () => readStoredTheme(storageKey),
73
+ () => null,
74
+ );
75
+ const systemTheme = React.useSyncExternalStore(
76
+ subscribeToSystemTheme,
77
+ () => (window.matchMedia(DARK_QUERY).matches ? "dark" : "light"),
78
+ () => "light" as const,
79
+ );
80
+ // A theme set without saving it, by `{ persist: false }` or because storage
81
+ // is blocked. It wins over the stored one until a saved choice replaces it.
82
+ const [chosen, setChosen] = React.useState<Theme | null>(null);
83
+ const theme = chosen ?? stored ?? defaultTheme;
84
+
85
+ React.useEffect(() => {
86
+ const element = target ? target() : document.documentElement;
87
+ element.classList.remove("theme-light", "theme-dark");
88
+ if (theme !== "system") element.classList.add(`theme-${theme}`);
89
+ }, [theme, target]);
90
+
91
+ const setTheme = React.useCallback(
92
+ (next: Theme, options?: { persist?: boolean }) => {
93
+ if (storageKey && options?.persist !== false) {
94
+ try {
95
+ localStorage.setItem(storageKey, next);
96
+ // Saved: let the stored value lead, so other tabs stay in step.
97
+ setChosen(null);
98
+ window.dispatchEvent(new Event(STORED_THEME_EVENT));
99
+ return;
100
+ } catch {
101
+ // Storage is blocked: keep the choice for this session instead.
102
+ }
103
+ }
104
+ setChosen(next);
105
+ },
106
+ [storageKey],
107
+ );
108
+
109
+ const value = React.useMemo(
110
+ () => ({ theme, resolvedTheme: theme === "system" ? systemTheme : theme, setTheme }),
111
+ [theme, systemTheme, setTheme],
112
+ );
113
+
114
+ return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
115
+ }