@enablo/swirlcn-ui 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-3DJWDNQL.js +77 -0
- package/dist/chunk-7XUCETA6.js +447 -0
- package/dist/chunk-KZESIYUN.js +18 -0
- package/dist/flip.d.ts +98 -0
- package/dist/flip.js +158 -0
- package/dist/fonts/Inter-italic.var.woff2 +0 -0
- package/dist/fonts/Inter.var.woff2 +0 -0
- package/dist/fonts/OFL.txt +96 -0
- package/dist/index.d.ts +2459 -0
- package/dist/index.js +8439 -0
- package/dist/server.d.ts +144 -0
- package/dist/server.js +88 -0
- package/dist/spacing-BQaEPbUv.d.ts +59 -0
- package/dist/theme.css +146 -0
- package/package.json +84 -3
- package/src/components/accordion.tsx +102 -0
- package/src/components/action-list.tsx +294 -0
- package/src/components/alert-dialog.tsx +155 -0
- package/src/components/autocomplete.tsx +313 -0
- package/src/components/avatar.tsx +258 -0
- package/src/components/badge.tsx +100 -0
- package/src/components/banner.tsx +112 -0
- package/src/components/button-group.tsx +87 -0
- package/src/components/button.tsx +362 -0
- package/src/components/card.tsx +186 -0
- package/src/components/carousel.tsx +245 -0
- package/src/components/checkbox.tsx +125 -0
- package/src/components/chip.tsx +153 -0
- package/src/components/color-input.tsx +98 -0
- package/src/components/data-cell.tsx +230 -0
- package/src/components/date-input.tsx +223 -0
- package/src/components/date-picker.tsx +276 -0
- package/src/components/description-list.tsx +97 -0
- package/src/components/dialog.tsx +405 -0
- package/src/components/empty-state.tsx +63 -0
- package/src/components/file-chip.tsx +206 -0
- package/src/components/file-uploader.tsx +148 -0
- package/src/components/flip-app-header.tsx +104 -0
- package/src/components/flip-brand.tsx +77 -0
- package/src/components/flip-theme.tsx +95 -0
- package/src/components/form-control.tsx +397 -0
- package/src/components/form-group.tsx +26 -0
- package/src/components/heading.tsx +70 -0
- package/src/components/icon.tsx +119 -0
- package/src/components/image-grid.tsx +285 -0
- package/src/components/inline-notification.tsx +167 -0
- package/src/components/inline-status.tsx +100 -0
- package/src/components/input.tsx +390 -0
- package/src/components/label.tsx +28 -0
- package/src/components/lightbox.tsx +314 -0
- package/src/components/link.tsx +46 -0
- package/src/components/list.tsx +40 -0
- package/src/components/menu.tsx +530 -0
- package/src/components/option-list.tsx +582 -0
- package/src/components/pagination.tsx +336 -0
- package/src/components/popover.tsx +194 -0
- package/src/components/progress.tsx +158 -0
- package/src/components/radio-group.tsx +136 -0
- package/src/components/resource-list.tsx +686 -0
- package/src/components/search.tsx +127 -0
- package/src/components/select.tsx +456 -0
- package/src/components/separator.tsx +93 -0
- package/src/components/skeleton-box.tsx +63 -0
- package/src/components/skeleton-text.tsx +66 -0
- package/src/components/spinner.tsx +83 -0
- package/src/components/stack.tsx +86 -0
- package/src/components/status-indicator.tsx +31 -0
- package/src/components/switch.tsx +90 -0
- package/src/components/table.tsx +242 -0
- package/src/components/tabs.tsx +147 -0
- package/src/components/tag.tsx +169 -0
- package/src/components/text.tsx +126 -0
- package/src/components/textarea.tsx +130 -0
- package/src/components/theme-provider.tsx +115 -0
- package/src/components/thumbnail.tsx +250 -0
- package/src/components/time-input.tsx +123 -0
- package/src/components/toast.tsx +217 -0
- package/src/components/toggle-group.tsx +124 -0
- package/src/components/toolbar.tsx +105 -0
- package/src/components/tooltip.tsx +97 -0
- package/src/components/tree-navigation.tsx +194 -0
- package/src/components/tree-view.tsx +292 -0
- package/src/flip.ts +12 -0
- package/src/index.ts +317 -0
- package/src/lib/date-mask.ts +176 -0
- package/src/lib/field-context.ts +86 -0
- package/src/lib/flip-app-script.ts +38 -0
- package/src/lib/flip-host.ts +84 -0
- package/src/lib/flip-theme-css.ts +90 -0
- package/src/lib/focus.ts +18 -0
- package/src/lib/glyphs.ts +137 -0
- package/src/lib/spacing.ts +93 -0
- package/src/lib/theme-context.ts +27 -0
- package/src/lib/theme-script.ts +49 -0
- package/src/lib/use-mobile.ts +17 -0
- package/src/lib/utils.ts +28 -0
- package/src/server.ts +20 -0
- package/src/styles/fonts/Inter-italic.var.woff2 +0 -0
- package/src/styles/fonts/Inter.var.woff2 +0 -0
- package/src/styles/fonts/OFL.txt +96 -0
- package/src/styles/theme.css +146 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cn } from "../lib/utils";
|
|
3
|
+
import { Glyph } from "../lib/glyphs";
|
|
4
|
+
import { Button, type ButtonProps } from "./button";
|
|
5
|
+
|
|
6
|
+
export interface FileUploaderHandle {
|
|
7
|
+
/** Clears the chosen files. */
|
|
8
|
+
reset: () => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface FileUploaderProps
|
|
12
|
+
extends Omit<React.ComponentProps<"input">, "type" | "ref" | "onChange" | "value" | "defaultValue"> {
|
|
13
|
+
label: string;
|
|
14
|
+
description?: React.ReactNode;
|
|
15
|
+
hideLabel?: boolean;
|
|
16
|
+
/** Called with the chosen files, whether picked or dropped. */
|
|
17
|
+
onValueChange?: (files: FileList) => void;
|
|
18
|
+
/** Show a drop zone. Defaults to true; false shows a button instead. */
|
|
19
|
+
showDropzone?: boolean;
|
|
20
|
+
ctaLabel?: string;
|
|
21
|
+
dragDropLabel?: string;
|
|
22
|
+
uploadButtonLabel?: string;
|
|
23
|
+
uploadButtonVariant?: ButtonProps["variant"];
|
|
24
|
+
uploadButtonIcon?: React.ReactNode;
|
|
25
|
+
/** Gives access to `reset()`, which clears the chosen files. */
|
|
26
|
+
ref?: React.Ref<FileUploaderHandle>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Swirl's file uploader: a label, a description and a drop zone to click or
|
|
31
|
+
* drop files on, or a button with `showDropzone={false}`. The drop zone is a
|
|
32
|
+
* native file input stretched over it, so picking and dropping both go
|
|
33
|
+
* through the browser's own file handling.
|
|
34
|
+
*/
|
|
35
|
+
export function FileUploader({
|
|
36
|
+
label,
|
|
37
|
+
description,
|
|
38
|
+
hideLabel = false,
|
|
39
|
+
onValueChange,
|
|
40
|
+
showDropzone = true,
|
|
41
|
+
ctaLabel = "Click to upload",
|
|
42
|
+
dragDropLabel = "or drag and drop.",
|
|
43
|
+
uploadButtonLabel = "Select file",
|
|
44
|
+
uploadButtonVariant = "flat",
|
|
45
|
+
uploadButtonIcon,
|
|
46
|
+
id,
|
|
47
|
+
disabled,
|
|
48
|
+
className,
|
|
49
|
+
ref,
|
|
50
|
+
...props
|
|
51
|
+
}: FileUploaderProps) {
|
|
52
|
+
const generatedId = React.useId();
|
|
53
|
+
const inputId = id ?? generatedId;
|
|
54
|
+
const getInput = () => document.getElementById(inputId) as HTMLInputElement | null;
|
|
55
|
+
|
|
56
|
+
React.useImperativeHandle(ref, () => ({
|
|
57
|
+
reset: () => {
|
|
58
|
+
const input = getInput();
|
|
59
|
+
if (input) input.value = "";
|
|
60
|
+
},
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
const describedBy = cn(description && `${inputId}-description`, `${inputId}-additional-label`);
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div className={cn("w-full", className)}>
|
|
67
|
+
<label
|
|
68
|
+
htmlFor={inputId}
|
|
69
|
+
id={`${inputId}-label`}
|
|
70
|
+
className={cn(
|
|
71
|
+
"block text-base leading-6 font-semibold text-foreground desktop:text-sm desktop:leading-5",
|
|
72
|
+
hideLabel && "sr-only",
|
|
73
|
+
)}
|
|
74
|
+
>
|
|
75
|
+
{label}
|
|
76
|
+
</label>
|
|
77
|
+
{description && (
|
|
78
|
+
<span
|
|
79
|
+
id={`${inputId}-description`}
|
|
80
|
+
className="mt-1 mb-2 block text-sm leading-5 text-subdued desktop:mt-0.5"
|
|
81
|
+
>
|
|
82
|
+
{description}
|
|
83
|
+
</span>
|
|
84
|
+
)}
|
|
85
|
+
<div
|
|
86
|
+
className={cn(
|
|
87
|
+
showDropzone &&
|
|
88
|
+
cn(
|
|
89
|
+
"relative mt-1 flex h-40 w-full flex-col items-center justify-center rounded-s border border-dashed border-border-strong",
|
|
90
|
+
"focus-within:border-2 focus-within:border-focus",
|
|
91
|
+
),
|
|
92
|
+
)}
|
|
93
|
+
>
|
|
94
|
+
{showDropzone ? (
|
|
95
|
+
<>
|
|
96
|
+
<span
|
|
97
|
+
aria-hidden
|
|
98
|
+
className={cn(
|
|
99
|
+
"inline-flex size-6 text-[var(--s-icon-highlight)] [&>svg]:size-full",
|
|
100
|
+
disabled && "text-disabled",
|
|
101
|
+
)}
|
|
102
|
+
>
|
|
103
|
+
<Glyph name="cloudUpload" />
|
|
104
|
+
</span>
|
|
105
|
+
<div
|
|
106
|
+
id={`${inputId}-additional-label`}
|
|
107
|
+
className="mt-4 text-base leading-6 desktop:mt-2 desktop:text-sm desktop:leading-5"
|
|
108
|
+
>
|
|
109
|
+
<span className={cn("font-medium text-interactive-primary", disabled && "text-disabled")}>
|
|
110
|
+
{ctaLabel}
|
|
111
|
+
</span>{" "}
|
|
112
|
+
<span className={cn(disabled && "text-disabled")}>{dragDropLabel}</span>
|
|
113
|
+
</div>
|
|
114
|
+
</>
|
|
115
|
+
) : (
|
|
116
|
+
<Button
|
|
117
|
+
variant={uploadButtonVariant}
|
|
118
|
+
intent="primary"
|
|
119
|
+
label={uploadButtonLabel}
|
|
120
|
+
icon={uploadButtonIcon}
|
|
121
|
+
disabled={disabled}
|
|
122
|
+
aria-describedby={describedBy}
|
|
123
|
+
onClick={() => getInput()?.click()}
|
|
124
|
+
className="mt-4 desktop:mt-3"
|
|
125
|
+
/>
|
|
126
|
+
)}
|
|
127
|
+
<input
|
|
128
|
+
{...props}
|
|
129
|
+
type="file"
|
|
130
|
+
id={inputId}
|
|
131
|
+
disabled={disabled}
|
|
132
|
+
autoComplete="off"
|
|
133
|
+
aria-describedby={describedBy}
|
|
134
|
+
tabIndex={showDropzone ? 0 : -1}
|
|
135
|
+
onChange={(event) => {
|
|
136
|
+
if (event.target.files) onValueChange?.(event.target.files);
|
|
137
|
+
}}
|
|
138
|
+
className={cn(
|
|
139
|
+
showDropzone
|
|
140
|
+
? "absolute inset-0 z-1 block w-full cursor-pointer opacity-0 disabled:cursor-default"
|
|
141
|
+
: "hidden",
|
|
142
|
+
"file:cursor-pointer",
|
|
143
|
+
)}
|
|
144
|
+
/>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cn } from "../lib/utils";
|
|
5
|
+
import { Glyph } from "../lib/glyphs";
|
|
6
|
+
import { closeFlipApp, useFlipHost } from "../lib/flip-host";
|
|
7
|
+
import { Button } from "./button";
|
|
8
|
+
|
|
9
|
+
export interface FlipAppHeaderProps extends Omit<React.ComponentProps<"header">, "title"> {
|
|
10
|
+
/** The Flip origins this app may be embedded in, as for `FlipThemeSync`. */
|
|
11
|
+
origins: readonly string[];
|
|
12
|
+
/** The page title, shown as the page's `<h1>`. */
|
|
13
|
+
title: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* A link back, such as `<a href="/" />` or your router's `<Link href="/" />`.
|
|
16
|
+
* The header styles it as Flip's back arrow and fills it with the icon and
|
|
17
|
+
* `backLabel`, so its own children are replaced.
|
|
18
|
+
*/
|
|
19
|
+
back?: React.ReactElement<{ children?: React.ReactNode }>;
|
|
20
|
+
/** Accessible name of the back link. */
|
|
21
|
+
backLabel?: string;
|
|
22
|
+
/** Accessible name of the button that closes the app, shown in Flip's web app on phones. */
|
|
23
|
+
closeLabel?: string;
|
|
24
|
+
/** Trailing content, such as an avatar or a button. */
|
|
25
|
+
actions?: React.ReactNode;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A header for an app embedded in Flip, drawn like Flip's own: a centred
|
|
30
|
+
* title between a back arrow and actions on phones, a left-aligned larger
|
|
31
|
+
* title on desktop.
|
|
32
|
+
*
|
|
33
|
+
* - **Flip's web app** can turn off its own header for an app (do so, to
|
|
34
|
+
* avoid two headers). On phones, with no `back` link, the header then shows
|
|
35
|
+
* an arrow that closes the app through the Flip bridge.
|
|
36
|
+
* - **Flip's mobile app** can't turn its header off, so this one hides there
|
|
37
|
+
* (`flip-app:hidden`; needs `<FlipAppScript>` in `<head>`). Flip's header
|
|
38
|
+
* shows the menu item's name, which the app can't change.
|
|
39
|
+
*
|
|
40
|
+
* <FlipAppHeader origins={FLIP_ORIGINS} title="Weekly Calls"
|
|
41
|
+
* back={<Link href="/" />} backLabel="Back to Weekly Calls" />
|
|
42
|
+
*/
|
|
43
|
+
export function FlipAppHeader({
|
|
44
|
+
origins,
|
|
45
|
+
title,
|
|
46
|
+
back,
|
|
47
|
+
backLabel = "Back",
|
|
48
|
+
closeLabel = "Close",
|
|
49
|
+
actions,
|
|
50
|
+
className,
|
|
51
|
+
...props
|
|
52
|
+
}: FlipAppHeaderProps) {
|
|
53
|
+
const host = useFlipHost(origins);
|
|
54
|
+
// Flip draws a back arrow with a shaft: Material's arrow_back, Swirl's ArrowLeft.
|
|
55
|
+
const arrow = <Glyph name="arrowLeft" />;
|
|
56
|
+
// Flip's arrow is a 20px icon whose glyph starts about 22.5px from the
|
|
57
|
+
// edge; a 40px button pulled 6px left centres it 20px in.
|
|
58
|
+
const arrowButton = "-ml-1.5 [&>svg]:size-5!";
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<header
|
|
62
|
+
className={cn(
|
|
63
|
+
"sticky top-0 z-10 min-h-14 bg-background px-4",
|
|
64
|
+
// Phones: back or close, a centred title, actions; ruled underneath.
|
|
65
|
+
"grid grid-cols-[1fr_minmax(0,auto)_1fr] items-center gap-2 border-b border-border",
|
|
66
|
+
// Desktop: one row with a left-aligned title, and no rule, as in Flip's web app.
|
|
67
|
+
"desktop:flex desktop:gap-0 desktop:border-b-0",
|
|
68
|
+
// Flip's mobile app keeps its own header, so this one steps aside.
|
|
69
|
+
"flip-app:hidden",
|
|
70
|
+
className,
|
|
71
|
+
)}
|
|
72
|
+
{...props}
|
|
73
|
+
>
|
|
74
|
+
{/* `contents` on desktop puts the buttons straight into the row, so a
|
|
75
|
+
close button hidden there leaves no gap behind. */}
|
|
76
|
+
<div className="flex items-center desktop:contents">
|
|
77
|
+
{back && (
|
|
78
|
+
<Button asChild hideLabel className={cn(arrowButton, "desktop:mr-2")}>
|
|
79
|
+
{React.cloneElement(
|
|
80
|
+
back,
|
|
81
|
+
undefined,
|
|
82
|
+
arrow,
|
|
83
|
+
<span className="sr-only">{backLabel}</span>,
|
|
84
|
+
)}
|
|
85
|
+
</Button>
|
|
86
|
+
)}
|
|
87
|
+
{!back && host === "web" && (
|
|
88
|
+
// Flip's web app shows no arrow for a menu item on desktop.
|
|
89
|
+
<Button
|
|
90
|
+
hideLabel
|
|
91
|
+
icon={arrow}
|
|
92
|
+
label={closeLabel}
|
|
93
|
+
onClick={() => closeFlipApp(origins)}
|
|
94
|
+
className={cn(arrowButton, "desktop:hidden")}
|
|
95
|
+
/>
|
|
96
|
+
)}
|
|
97
|
+
</div>
|
|
98
|
+
<h1 className="m-0 min-w-0 truncate text-center text-base font-semibold desktop:flex-1 desktop:text-left desktop:text-lg">
|
|
99
|
+
{title}
|
|
100
|
+
</h1>
|
|
101
|
+
<div className="flex items-center justify-end desktop:ml-2">{actions}</div>
|
|
102
|
+
</header>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { flipThemeCss, type FlipThemes } from "../lib/flip-theme-css";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Flip publishes each tenant's Swirl theme without auth, and its own web app
|
|
5
|
+
* loads it before sign-in. The sibling `/generic/themes` endpoint returns
|
|
6
|
+
* ready-made CSS, but repeats `:root` in its dark block, so dark always wins;
|
|
7
|
+
* this builds the CSS from the token data instead.
|
|
8
|
+
*/
|
|
9
|
+
const THEMES_PATH = "/api/branding/v4/bootstrap/web/themes";
|
|
10
|
+
|
|
11
|
+
/** Flip serves the themes with `max-age=300`; keep them as long. */
|
|
12
|
+
const CACHE_MS = 5 * 60 * 1000;
|
|
13
|
+
const TIMEOUT_MS = 2000;
|
|
14
|
+
|
|
15
|
+
interface CacheEntry {
|
|
16
|
+
css: string;
|
|
17
|
+
fetchedAt: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Last good CSS per Flip origin, kept for the life of the server process. */
|
|
21
|
+
const cache = new Map<string, CacheEntry>();
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Fetches a tenant's themes and returns the CSS for them, or `null` when it
|
|
25
|
+
* cannot. Serves the cached copy for 5 minutes, and the last good copy when
|
|
26
|
+
* Flip is slow or down.
|
|
27
|
+
*/
|
|
28
|
+
export async function fetchFlipBrandCss(origin: string): Promise<string | null> {
|
|
29
|
+
let base: string;
|
|
30
|
+
try {
|
|
31
|
+
base = new URL(origin).origin;
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const cached = cache.get(base);
|
|
37
|
+
if (cached && Date.now() - cached.fetchedAt < CACHE_MS) return cached.css;
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const response = await fetch(`${base}${THEMES_PATH}`, {
|
|
41
|
+
headers: { accept: "application/json" },
|
|
42
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
43
|
+
});
|
|
44
|
+
if (!response.ok) throw new Error(`Flip themes: HTTP ${response.status}`);
|
|
45
|
+
const css = flipThemeCss((await response.json()) as FlipThemes);
|
|
46
|
+
cache.set(base, { css, fetchedAt: Date.now() });
|
|
47
|
+
return css;
|
|
48
|
+
} catch {
|
|
49
|
+
return cached?.css ?? null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface FlipBrandStyleProps {
|
|
54
|
+
/** The Flip tenant's origin, such as `https://acme.flip-app.com`. */
|
|
55
|
+
origin: string;
|
|
56
|
+
/** CSP nonce for the `<style>` element. */
|
|
57
|
+
nonce?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A server component that renders a tenant's brand colours as
|
|
62
|
+
* `<style id="flip-brand">`, for the document `<head>`. Place it after the
|
|
63
|
+
* app's stylesheet so its values win.
|
|
64
|
+
*
|
|
65
|
+
* Renders nothing when the themes cannot be fetched and none are cached,
|
|
66
|
+
* which leaves Swirl's default colours.
|
|
67
|
+
*
|
|
68
|
+
* <head>
|
|
69
|
+
* <FlipBrandStyle origin="https://acme.flip-app.com" />
|
|
70
|
+
* </head>
|
|
71
|
+
*/
|
|
72
|
+
export async function FlipBrandStyle({ origin, nonce }: FlipBrandStyleProps) {
|
|
73
|
+
const css = await fetchFlipBrandCss(origin);
|
|
74
|
+
if (!css) return null;
|
|
75
|
+
// The CSS is built from validated token ids and numeric channels only.
|
|
76
|
+
return <style id="flip-brand" nonce={nonce} dangerouslySetInnerHTML={{ __html: css }} />;
|
|
77
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { BridgeEventType, getTheme, subscribe } from "@getflip/bridge";
|
|
5
|
+
import { ensureFlipBridge, flipHost } from "../lib/flip-host";
|
|
6
|
+
import { useOptionalTheme } from "../lib/theme-context";
|
|
7
|
+
|
|
8
|
+
type FlipTheme = "light" | "dark";
|
|
9
|
+
|
|
10
|
+
const activeThemeOf = (result: unknown): FlipTheme | null => {
|
|
11
|
+
if (result && typeof result === "object" && "activeTheme" in result) {
|
|
12
|
+
const theme = (result as { activeTheme: unknown }).activeTheme;
|
|
13
|
+
if (theme === "light" || theme === "dark") return theme;
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export interface FlipThemeSyncProps {
|
|
19
|
+
/**
|
|
20
|
+
* The Flip origins this app may be embedded in, such as
|
|
21
|
+
* `["https://acme.flip-app.com"]`. Outside them it does nothing. In Flip's
|
|
22
|
+
* mobile app the first one is used.
|
|
23
|
+
*/
|
|
24
|
+
origins: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Follows the user's light or dark setting in Flip while the app is embedded
|
|
29
|
+
* there. Flip owns the theme while embedded: the app's own theme choice is
|
|
30
|
+
* overridden for as long as it runs inside one of `origins`, and left alone
|
|
31
|
+
* everywhere else.
|
|
32
|
+
*
|
|
33
|
+
* Inside a `ThemeProvider` it sets the provider's theme without saving it;
|
|
34
|
+
* without one it sets the `theme-light` / `theme-dark` class on `<html>`.
|
|
35
|
+
* Renders nothing.
|
|
36
|
+
*
|
|
37
|
+
* <body>
|
|
38
|
+
* <FlipThemeSync origins={["https://acme.flip-app.com"]} />
|
|
39
|
+
* {children}
|
|
40
|
+
* </body>
|
|
41
|
+
*/
|
|
42
|
+
export function FlipThemeSync({ origins }: FlipThemeSyncProps) {
|
|
43
|
+
const provider = useOptionalTheme();
|
|
44
|
+
// Read through a ref so the subscription doesn't restart whenever the
|
|
45
|
+
// provider re-renders. It is updated in an effect, not during render; this
|
|
46
|
+
// effect runs before the subscription effect below, and Flip only answers
|
|
47
|
+
// after both have run.
|
|
48
|
+
const setThemeRef = React.useRef(provider?.setTheme);
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
setThemeRef.current = provider?.setTheme;
|
|
51
|
+
});
|
|
52
|
+
const originsKey = origins.join(" ");
|
|
53
|
+
|
|
54
|
+
React.useEffect(() => {
|
|
55
|
+
// Outside Flip the bridge's requests never resolve, so don't start it.
|
|
56
|
+
// Inside, this covers Flip's mobile app as well as its web app.
|
|
57
|
+
const host = flipHost(originsKey.split(" "));
|
|
58
|
+
if (!host) return;
|
|
59
|
+
|
|
60
|
+
let cancelled = false;
|
|
61
|
+
let unsubscribe: (() => Promise<void>) | undefined;
|
|
62
|
+
|
|
63
|
+
const apply = (theme: FlipTheme | null) => {
|
|
64
|
+
if (cancelled || !theme) return;
|
|
65
|
+
const setTheme = setThemeRef.current;
|
|
66
|
+
if (setTheme) {
|
|
67
|
+
setTheme(theme, { persist: false });
|
|
68
|
+
} else {
|
|
69
|
+
const root = document.documentElement;
|
|
70
|
+
root.classList.remove("theme-light", "theme-dark");
|
|
71
|
+
root.classList.add(`theme-${theme}`);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
ensureFlipBridge(host);
|
|
76
|
+
getTheme().then(
|
|
77
|
+
(result) => apply(activeThemeOf(result)),
|
|
78
|
+
() => {},
|
|
79
|
+
);
|
|
80
|
+
subscribe(BridgeEventType.THEME_CHANGE, (event) => apply(activeThemeOf(event?.data))).then(
|
|
81
|
+
(stop) => {
|
|
82
|
+
if (cancelled) void stop();
|
|
83
|
+
else unsubscribe = stop;
|
|
84
|
+
},
|
|
85
|
+
() => {},
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return () => {
|
|
89
|
+
cancelled = true;
|
|
90
|
+
void unsubscribe?.();
|
|
91
|
+
};
|
|
92
|
+
}, [originsKey]);
|
|
93
|
+
|
|
94
|
+
return null;
|
|
95
|
+
}
|