@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,176 @@
1
+ /**
2
+ * Masks typed dates and times to a date-fns pattern such as "yyyy-MM-dd" or
3
+ * "hh:mm aa", the way Swirl's IMask set-up does:
4
+ *
5
+ * - Each field takes only digits in its range: a day 01–31, a month 01–12,
6
+ * hours 00–23 (01–12 for `hh`), minutes and seconds 00–59, a four-digit
7
+ * year from 1000.
8
+ * - A first digit that can't start a two-digit value is padded: "4" in a
9
+ * day becomes "04". Typing a separator pads a part-typed field too.
10
+ * - A separator is added as soon as the field before it is full, except
11
+ * while deleting, so Backspace can remove it.
12
+ * - `aa` takes "A" or "P" and fills in "AM" or "PM".
13
+ */
14
+
15
+ interface Block {
16
+ kind: "block";
17
+ token: string;
18
+ length: number;
19
+ from: number;
20
+ to: number;
21
+ }
22
+
23
+ interface Separator {
24
+ kind: "separator";
25
+ text: string;
26
+ }
27
+
28
+ interface Meridiem {
29
+ kind: "meridiem";
30
+ }
31
+
32
+ type Part = Block | Separator | Meridiem;
33
+
34
+ const RANGES: Record<string, [number, number, number]> = {
35
+ dd: [2, 1, 31],
36
+ MM: [2, 1, 12],
37
+ yy: [2, 0, 99],
38
+ yyyy: [4, 1000, 9999],
39
+ HH: [2, 0, 23],
40
+ hh: [2, 1, 12],
41
+ mm: [2, 0, 59],
42
+ ss: [2, 0, 59],
43
+ };
44
+
45
+ /** Widens single-letter tokens to their padded form, as Swirl does: d → dd, y → yyyy. */
46
+ export function normalizePattern(pattern: string): string {
47
+ return pattern
48
+ .replace(/(?<!d)d(?!d)/g, "dd")
49
+ .replace(/(?<!M)M(?!M)/g, "MM")
50
+ .replace(/(?<!y)y(?!y)/g, "yyyy")
51
+ .replace(/(?<!y)yyy(?!y)/g, "yyyy")
52
+ .replace(/(?<!H)H(?!H)/g, "HH")
53
+ .replace(/(?<!h)h(?!h)/g, "hh")
54
+ .replace(/(?<!m)m(?!m)/g, "mm")
55
+ .replace(/(?<!s)s(?!s)/g, "ss")
56
+ .replace(/(?<!a)a(?!a)/g, "aa");
57
+ }
58
+
59
+ function parsePattern(pattern: string): Part[] {
60
+ const parts: Part[] = [];
61
+ const re = /yyyy|yy|MM|dd|HH|hh|mm|ss|aa/g;
62
+ let last = 0;
63
+ for (const match of pattern.matchAll(re)) {
64
+ if (match.index > last) parts.push({ kind: "separator", text: pattern.slice(last, match.index) });
65
+ if (match[0] === "aa") parts.push({ kind: "meridiem" });
66
+ else {
67
+ const [length, from, to] = RANGES[match[0]]!;
68
+ parts.push({ kind: "block", token: match[0], length, from, to });
69
+ }
70
+ last = match.index + match[0].length;
71
+ }
72
+ if (last < pattern.length) parts.push({ kind: "separator", text: pattern.slice(last) });
73
+ return parts;
74
+ }
75
+
76
+ /** Whether `digits` can still grow into a value in range. */
77
+ function canComplete(digits: string, block: Block): boolean {
78
+ const missing = block.length - digits.length;
79
+ const low = Number(digits.padEnd(block.length, "0"));
80
+ const high = Number(digits.padEnd(block.length, "9"));
81
+ if (missing === 0) return low >= block.from && low <= block.to;
82
+ return high >= block.from && low <= block.to;
83
+ }
84
+
85
+ /** Pads a part-typed field with leading zeros, if that gives a value in range. */
86
+ function pad(digits: string, block: Block): string | null {
87
+ const padded = digits.padStart(block.length, "0");
88
+ const value = Number(padded);
89
+ return value >= block.from && value <= block.to ? padded : null;
90
+ }
91
+
92
+ export interface MaskOptions {
93
+ /** Add the next separator as soon as a field is full. Off while deleting. */
94
+ eager?: boolean;
95
+ }
96
+
97
+ /** Masks `input` to `pattern`, dropping anything that doesn't fit. */
98
+ export function maskDate(input: string, pattern: string, { eager = true }: MaskOptions = {}): string {
99
+ const parts = parsePattern(normalizePattern(pattern));
100
+ const chars = [...input.toUpperCase()];
101
+ let i = 0;
102
+ let out = "";
103
+
104
+ for (let p = 0; p < parts.length; p++) {
105
+ const part = parts[p]!;
106
+ if (part.kind === "separator") {
107
+ // Written out when the field before it is full and input follows.
108
+ if (i >= chars.length && !eager) break;
109
+ out += part.text;
110
+ // Skip the separator if the user typed it.
111
+ for (const ch of part.text) if (chars[i] === ch) i++;
112
+ continue;
113
+ }
114
+
115
+ if (part.kind === "meridiem") {
116
+ while (i < chars.length && chars[i] !== "A" && chars[i] !== "P") i++;
117
+ if (i >= chars.length) break;
118
+ out += chars[i] + "M";
119
+ i++;
120
+ if (chars[i] === "M") i++;
121
+ continue;
122
+ }
123
+
124
+ let digits = "";
125
+ let done = false;
126
+ while (i < chars.length && !done) {
127
+ const ch = chars[i]!;
128
+ if (/\d/.test(ch)) {
129
+ const next = digits + ch;
130
+ if (next.length === part.length) {
131
+ if (canComplete(next, part)) {
132
+ digits = next;
133
+ done = true;
134
+ }
135
+ } else if (canComplete(next, part)) {
136
+ digits = next;
137
+ } else if (digits === "") {
138
+ // A first digit that can't start the field: "4" in a day → "04".
139
+ const padded = pad(ch, part);
140
+ if (padded) {
141
+ digits = padded;
142
+ done = true;
143
+ }
144
+ }
145
+ i++;
146
+ } else {
147
+ // Typing the next separator pads a part-typed field: "4-" → "04-".
148
+ const nextPart = parts[p + 1];
149
+ if (digits && nextPart?.kind === "separator" && nextPart.text.startsWith(ch)) {
150
+ const padded = pad(digits, part);
151
+ if (padded) {
152
+ digits = padded;
153
+ done = true;
154
+ break;
155
+ }
156
+ }
157
+ i++;
158
+ }
159
+ }
160
+ out += digits;
161
+ if (!done) break;
162
+ }
163
+
164
+ // While deleting, never end on a separator the user just removed.
165
+ if (!eager) {
166
+ let end = out.length;
167
+ while (end > 0 && !/[\dM]/.test(out[end - 1]!)) end--;
168
+ out = out.slice(0, end);
169
+ }
170
+ return out;
171
+ }
172
+
173
+ /** Whether `text` fills every field of `pattern`. */
174
+ export function isComplete(text: string, pattern: string): boolean {
175
+ return text.length === normalizePattern(pattern).length && maskDate(text, pattern) === text;
176
+ }
@@ -0,0 +1,86 @@
1
+ import * as React from "react";
2
+
3
+ /*
4
+ * What a FormControl tells the control inside it, and how the control reports
5
+ * back. A control that finds this context renders bare - no border, padding
6
+ * or background - because the FormControl draws the field box around it.
7
+ */
8
+
9
+ export type FieldLabelPosition = "inside" | "outside";
10
+ export type FieldFontSize = "default" | "base" | "sm";
11
+
12
+ export interface FieldContextValue {
13
+ /** The control's id; the label points at it. */
14
+ id: string;
15
+ /** The description or error, whichever is shown. */
16
+ describedBy: string | undefined;
17
+ invalid: boolean;
18
+ disabled: boolean;
19
+ readOnly: boolean;
20
+ required: boolean;
21
+ labelPosition: FieldLabelPosition;
22
+ inline: boolean;
23
+ fontSize: FieldFontSize;
24
+ /**
25
+ * True while the label stands in for the placeholder: the field is empty
26
+ * and unfocused. The control hides its own placeholder meanwhile.
27
+ */
28
+ labelAsPlaceholder: boolean;
29
+ /** The control reports whether it holds a value, so the label can float. */
30
+ setFilled: (filled: boolean) => void;
31
+ /** The control reports whether it shows a placeholder of its own. */
32
+ setHasPlaceholder: (hasPlaceholder: boolean) => void;
33
+ /** The control reports that it shows a character counter. */
34
+ setHasCounter: (hasCounter: boolean) => void;
35
+ }
36
+
37
+ export const FieldContext = React.createContext<FieldContextValue | null>(null);
38
+
39
+ /** The surrounding FormControl's field, or `null` when the control stands alone. */
40
+ export function useField() {
41
+ return React.useContext(FieldContext);
42
+ }
43
+
44
+ /**
45
+ * Tracks whether a text control holds a value, for both controlled and
46
+ * uncontrolled use, and reports it to the surrounding field.
47
+ *
48
+ * Returns the current text, for counters, and an `onChange` wrapper that keeps
49
+ * the uncontrolled copy up to date.
50
+ */
51
+ export function useFieldText<E extends HTMLInputElement | HTMLTextAreaElement>(
52
+ value: unknown,
53
+ defaultValue: unknown,
54
+ onChange: React.ChangeEventHandler<E> | undefined,
55
+ ) {
56
+ const field = useField();
57
+ const controlled = value !== undefined;
58
+ const [uncontrolled, setUncontrolled] = React.useState(() =>
59
+ defaultValue == null ? "" : String(defaultValue),
60
+ );
61
+ const text = controlled ? (value == null ? "" : String(value)) : uncontrolled;
62
+ const filled = text !== "";
63
+
64
+ const setFilled = field?.setFilled;
65
+ React.useEffect(() => {
66
+ setFilled?.(filled);
67
+ }, [setFilled, filled]);
68
+
69
+ const handleChange: React.ChangeEventHandler<E> = (event) => {
70
+ if (!controlled) setUncontrolled(event.target.value);
71
+ onChange?.(event);
72
+ };
73
+
74
+ return { field, text, handleChange };
75
+ }
76
+
77
+ /**
78
+ * Sets a native input or textarea's value the way a user would, so React's
79
+ * `onChange` fires for both controlled and uncontrolled use.
80
+ */
81
+ export function setNativeValue(element: HTMLInputElement | HTMLTextAreaElement, value: string) {
82
+ const proto =
83
+ element instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
84
+ Object.getOwnPropertyDescriptor(proto, "value")?.set?.call(element, value);
85
+ element.dispatchEvent(new Event("input", { bubbles: true }));
86
+ }
@@ -0,0 +1,38 @@
1
+ import { createElement } from "react";
2
+
3
+ /**
4
+ * Inline script that marks `<html>` with `flip-app` inside Flip's mobile app,
5
+ * a Flutter webview that injects `window.FlipFlutter`, before first paint.
6
+ * `flip-app:` styles then apply from the start, so an app header hidden with
7
+ * `flip-app:hidden` never flashes; a React effect would run too late.
8
+ *
9
+ * Like `themeScript`, this is re-exported from `@enablo/swirlcn-ui/server` so
10
+ * it can render from a server component in the document `<head>`.
11
+ */
12
+ export function flipAppScript() {
13
+ return 'if(window.FlipFlutter)document.documentElement.classList.add("flip-app")';
14
+ }
15
+
16
+ export interface FlipAppScriptProps {
17
+ /** CSP nonce for this request, when inline scripts need one. */
18
+ nonce?: string;
19
+ }
20
+
21
+ /**
22
+ * Renders `flipAppScript` as a `<script>` element. Place it in `<head>`, and
23
+ * add `suppressHydrationWarning` to `<html>`: the class is there before React
24
+ * hydrates.
25
+ *
26
+ * import { FlipAppScript } from "@enablo/swirlcn-ui/server";
27
+ * <html lang="en" suppressHydrationWarning>
28
+ * <head><FlipAppScript /></head>
29
+ */
30
+ export function FlipAppScript({ nonce }: FlipAppScriptProps) {
31
+ return createElement("script", {
32
+ nonce,
33
+ // Browsers hide a script's nonce from the DOM once it is parsed, so the
34
+ // attribute never matches during hydration.
35
+ suppressHydrationWarning: true,
36
+ dangerouslySetInnerHTML: { __html: flipAppScript() },
37
+ });
38
+ }
@@ -0,0 +1,84 @@
1
+ import * as React from "react";
2
+ import { close, initFlipBridge } from "@getflip/bridge";
3
+
4
+ /**
5
+ * Where an app embedded in Flip is running:
6
+ * - `"web"`: in an iframe in Flip's web app, at one of the given origins;
7
+ * - `"app"`: in Flip's mobile app, a Flutter webview. The bridge talks to it
8
+ * through `window.FlipFlutter.postMessage` rather than `postMessage` on a
9
+ * parent window, and its own header can't be turned off.
10
+ */
11
+ export interface FlipHost {
12
+ origin: string;
13
+ kind: "web" | "app";
14
+ }
15
+
16
+ const normalize = (origin: string) => {
17
+ try {
18
+ return new URL(origin).origin;
19
+ } catch {
20
+ return origin;
21
+ }
22
+ };
23
+
24
+ /** Whether Flip's mobile app has injected its message channel into this page. */
25
+ function inFlipApp(): boolean {
26
+ // Read through a cast: `"FlipFlutter" in window` would narrow `window` to `never`
27
+ // where the bridge's own types declare the property.
28
+ return Boolean((window as { FlipFlutter?: unknown }).FlipFlutter);
29
+ }
30
+
31
+ /**
32
+ * Detects Flip around this page, or `null` outside it. Browser only.
33
+ *
34
+ * In Flip's mobile app the host is `origins[0]`, since a webview has no parent
35
+ * origin to read. In an iframe, the parent's origin must be one of `origins`:
36
+ * `ancestorOrigins` is exact where supported (Chromium, Safari), and the
37
+ * referrer is the best signal elsewhere.
38
+ */
39
+ export function flipHost(origins: readonly string[]): FlipHost | null {
40
+ if (typeof window === "undefined") return null;
41
+ const allowed = origins.map(normalize);
42
+ if (inFlipApp()) return allowed[0] ? { origin: allowed[0], kind: "app" } : null;
43
+ if (window.top === window) return null;
44
+
45
+ let parent: string | undefined = window.location.ancestorOrigins?.[0];
46
+ if (!parent && document.referrer) {
47
+ try {
48
+ parent = new URL(document.referrer).origin;
49
+ } catch {
50
+ parent = undefined;
51
+ }
52
+ }
53
+ return parent && allowed.includes(parent) ? { origin: parent, kind: "web" } : null;
54
+ }
55
+
56
+ /** Starts the Flip bridge for `host`, once: a second `initFlipBridge` only logs a warning. */
57
+ export function ensureFlipBridge(host: FlipHost): void {
58
+ if ((window as { flipBridgeOptions?: unknown }).flipBridgeOptions) return;
59
+ initFlipBridge({ hostAppOrigin: host.origin });
60
+ }
61
+
62
+ /**
63
+ * Asks Flip to close this app, as its own header's back arrow would. For an
64
+ * app that draws its own header; does nothing outside Flip.
65
+ */
66
+ export function closeFlipApp(origins: readonly string[]): void {
67
+ const host = flipHost(origins);
68
+ if (!host) return;
69
+ ensureFlipBridge(host);
70
+ void close().catch(() => {});
71
+ }
72
+
73
+ const subscribe = () => () => {};
74
+
75
+ /**
76
+ * `"web"`, `"app"` or `null` (outside Flip, and always on the server and
77
+ * during hydration, so the server's HTML and the first render agree).
78
+ */
79
+ export function useFlipHost(origins: readonly string[]): FlipHost["kind"] | null {
80
+ const key = origins.join(" ");
81
+ // A string snapshot, not the object, so it is stable between calls.
82
+ const getSnapshot = React.useCallback(() => flipHost(key.split(" "))?.kind ?? null, [key]);
83
+ return React.useSyncExternalStore(subscribe, getSnapshot, () => null);
84
+ }
@@ -0,0 +1,90 @@
1
+ /*
2
+ * Turns a Flip tenant's Swirl themes into CSS that overrides swirlcn's
3
+ * default token values. Pure and server-safe: no React, no DOM, no fetch.
4
+ */
5
+
6
+ /** One RGBA colour as Flip's branding API returns it. `a` runs 0-255. */
7
+ export interface FlipColor {
8
+ r: number;
9
+ g: number;
10
+ b: number;
11
+ a: number;
12
+ }
13
+
14
+ export interface FlipDesignToken {
15
+ /** A Swirl token name, such as `action-primary-default`. */
16
+ id: string;
17
+ color: FlipColor;
18
+ }
19
+
20
+ export interface FlipTheme {
21
+ /** False when the tenant has not set up this theme. */
22
+ active?: boolean;
23
+ design_tokens?: FlipDesignToken[];
24
+ company_logo?: unknown;
25
+ company_icon?: unknown;
26
+ favicon?: unknown;
27
+ }
28
+
29
+ /**
30
+ * The response of `GET /api/branding/v4/bootstrap/web/themes`: the tenant's
31
+ * light and dark themes.
32
+ */
33
+ export interface FlipThemes {
34
+ light?: FlipTheme;
35
+ dark?: FlipTheme;
36
+ }
37
+
38
+ /** Token ids become CSS custom property names, so only these characters pass. */
39
+ const TOKEN_ID = /^[a-z0-9-]+$/;
40
+
41
+ const channel = (value: unknown) => {
42
+ const n = Math.round(Number(value));
43
+ return Number.isFinite(n) ? Math.min(255, Math.max(0, n)) : 0;
44
+ };
45
+
46
+ /** `rgba()` with Flip's 0-255 alpha turned into CSS's 0-1, to three places. */
47
+ const rgba = ({ r, g, b, a }: FlipColor) =>
48
+ `rgba(${channel(r)},${channel(g)},${channel(b)},${Number((channel(a) / 255).toFixed(3))})`;
49
+
50
+ /** `--s-<id>:rgba(...)` declarations for a theme's valid tokens. */
51
+ function declarations(theme: FlipTheme | undefined) {
52
+ return (theme?.design_tokens ?? [])
53
+ .filter(
54
+ (token) =>
55
+ token != null &&
56
+ typeof token.id === "string" &&
57
+ TOKEN_ID.test(token.id) &&
58
+ token.color != null &&
59
+ typeof token.color === "object",
60
+ )
61
+ .map((token) => `--s-${token.id}:${rgba(token.color)};`)
62
+ .join("");
63
+ }
64
+
65
+ /**
66
+ * CSS that applies a tenant's brand colours on top of swirlcn's theme.
67
+ *
68
+ * The selectors mirror the generated swirl-tokens stylesheet, so each block
69
+ * overrides its counterpart: light on `:root` and `.theme-light`, dark on
70
+ * `.theme-dark` and, when no class is set, under `prefers-color-scheme: dark`.
71
+ * A dark theme marked `active: false` is left out, so dark mode keeps Swirl's
72
+ * defaults.
73
+ *
74
+ * The output is meant to go into a `<style>` element as-is, so token ids are
75
+ * checked against `[a-z0-9-]` and every colour channel is coerced to a number.
76
+ * Returns an empty string when there is nothing to apply.
77
+ */
78
+ export function flipThemeCss(themes: FlipThemes | null | undefined): string {
79
+ if (!themes) return "";
80
+ const light = themes.light?.active === false ? "" : declarations(themes.light);
81
+ const dark = themes.dark?.active === false ? "" : declarations(themes.dark);
82
+
83
+ let css = "";
84
+ if (light) css += `:root,.theme-light{${light}}`;
85
+ if (dark) {
86
+ css += `.theme-dark{${dark}}`;
87
+ css += `@media (prefers-color-scheme:dark){:root:not(.theme-light){${dark}}}`;
88
+ }
89
+ return css;
90
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Swirl's focus ring: a 2px outline in the focus token, offset by 2px, drawn
3
+ * only for keyboard focus. Shared by every interactive component so the ring is
4
+ * identical everywhere and can be changed in one place.
5
+ *
6
+ * Deliberately no `outline-none`: in Tailwind v4 that sets
7
+ * `--tw-outline-style: none` on the element, which also disables any resting
8
+ * `outline-*` the component uses as its border - that is what made unchecked
9
+ * radios and spaced toggle buttons render with no outline at all. Browsers
10
+ * only paint the UA ring on `:focus-visible` anyway, and these rules override
11
+ * it there.
12
+ */
13
+ export const focusRing =
14
+ "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus";
15
+
16
+ /** For controls that sit flush against an edge, where an offset ring would clip. */
17
+ export const focusRingInset =
18
+ "focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus";