@siiway/ui-react 0.0.0 → 0.2.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 +19 -1
- package/dist/components/AppShell.d.ts +29 -0
- package/dist/components/AppShell.d.ts.map +1 -0
- package/dist/components/AppShell.js +220 -0
- package/dist/components/AppShell.js.map +1 -0
- package/dist/components/AuthShell.d.ts +21 -0
- package/dist/components/AuthShell.d.ts.map +1 -0
- package/dist/components/AuthShell.js +53 -0
- package/dist/components/AuthShell.js.map +1 -0
- package/dist/components/BrandMark.d.ts +11 -0
- package/dist/components/BrandMark.d.ts.map +1 -0
- package/dist/components/BrandMark.js +11 -0
- package/dist/components/BrandMark.js.map +1 -0
- package/dist/components/ButtonLink.d.ts +11 -0
- package/dist/components/ButtonLink.d.ts.map +1 -0
- package/dist/components/ButtonLink.js +15 -0
- package/dist/components/ButtonLink.js.map +1 -0
- package/dist/components/CheckboxField.d.ts +15 -0
- package/dist/components/CheckboxField.d.ts.map +1 -0
- package/dist/components/CheckboxField.js +23 -0
- package/dist/components/CheckboxField.js.map +1 -0
- package/dist/components/ColorPickerInput.d.ts +17 -0
- package/dist/components/ColorPickerInput.d.ts.map +1 -0
- package/dist/components/ColorPickerInput.js +97 -0
- package/dist/components/ColorPickerInput.js.map +1 -0
- package/dist/components/CommandPalette.d.ts +65 -0
- package/dist/components/CommandPalette.d.ts.map +1 -0
- package/dist/components/CommandPalette.js +269 -0
- package/dist/components/CommandPalette.js.map +1 -0
- package/dist/components/CopyButton.d.ts +27 -0
- package/dist/components/CopyButton.d.ts.map +1 -0
- package/dist/components/CopyButton.js +20 -0
- package/dist/components/CopyButton.js.map +1 -0
- package/dist/components/CopyIdTrigger.d.ts +31 -0
- package/dist/components/CopyIdTrigger.d.ts.map +1 -0
- package/dist/components/CopyIdTrigger.js +72 -0
- package/dist/components/CopyIdTrigger.js.map +1 -0
- package/dist/components/DataList.d.ts +78 -0
- package/dist/components/DataList.d.ts.map +1 -0
- package/dist/components/DataList.js +150 -0
- package/dist/components/DataList.js.map +1 -0
- package/dist/components/DetailHeader.d.ts +26 -0
- package/dist/components/DetailHeader.d.ts.map +1 -0
- package/dist/components/DetailHeader.js +49 -0
- package/dist/components/DetailHeader.js.map +1 -0
- package/dist/components/DurationInput.d.ts +25 -0
- package/dist/components/DurationInput.d.ts.map +1 -0
- package/dist/components/DurationInput.js +83 -0
- package/dist/components/DurationInput.js.map +1 -0
- package/dist/components/EmptyState.d.ts +14 -0
- package/dist/components/EmptyState.d.ts.map +1 -0
- package/dist/components/EmptyState.js +41 -0
- package/dist/components/EmptyState.js.map +1 -0
- package/dist/components/FormPage.d.ts +22 -0
- package/dist/components/FormPage.d.ts.map +1 -0
- package/dist/components/FormPage.js +65 -0
- package/dist/components/FormPage.js.map +1 -0
- package/dist/components/GetStartedPanel.d.ts +16 -0
- package/dist/components/GetStartedPanel.d.ts.map +1 -0
- package/dist/components/GetStartedPanel.js +96 -0
- package/dist/components/GetStartedPanel.js.map +1 -0
- package/dist/components/LabeledLine.d.ts +12 -0
- package/dist/components/LabeledLine.d.ts.map +1 -0
- package/dist/components/LabeledLine.js +6 -0
- package/dist/components/LabeledLine.js.map +1 -0
- package/dist/components/ListSearch.d.ts +15 -0
- package/dist/components/ListSearch.d.ts.map +1 -0
- package/dist/components/ListSearch.js +23 -0
- package/dist/components/ListSearch.js.map +1 -0
- package/dist/components/LoadingBar.d.ts +8 -0
- package/dist/components/LoadingBar.d.ts.map +1 -0
- package/dist/components/LoadingBar.js +55 -0
- package/dist/components/LoadingBar.js.map +1 -0
- package/dist/components/LoadingPage.d.ts +6 -0
- package/dist/components/LoadingPage.d.ts.map +1 -0
- package/dist/components/LoadingPage.js +25 -0
- package/dist/components/LoadingPage.js.map +1 -0
- package/dist/components/MenuPanel.d.ts +34 -0
- package/dist/components/MenuPanel.d.ts.map +1 -0
- package/dist/components/MenuPanel.js +98 -0
- package/dist/components/MenuPanel.js.map +1 -0
- package/dist/components/Outline.d.ts +23 -0
- package/dist/components/Outline.d.ts.map +1 -0
- package/dist/components/Outline.js +150 -0
- package/dist/components/Outline.js.map +1 -0
- package/dist/components/Overview.d.ts +103 -0
- package/dist/components/Overview.d.ts.map +1 -0
- package/dist/components/Overview.js +286 -0
- package/dist/components/Overview.js.map +1 -0
- package/dist/components/PageHeader.d.ts +19 -0
- package/dist/components/PageHeader.d.ts.map +1 -0
- package/dist/components/PageHeader.js +41 -0
- package/dist/components/PageHeader.js.map +1 -0
- package/dist/components/Pagination.d.ts +19 -0
- package/dist/components/Pagination.d.ts.map +1 -0
- package/dist/components/Pagination.js +47 -0
- package/dist/components/Pagination.js.map +1 -0
- package/dist/components/PasswordInput.d.ts +3 -0
- package/dist/components/PasswordInput.d.ts.map +1 -0
- package/dist/components/PasswordInput.js +29 -0
- package/dist/components/PasswordInput.js.map +1 -0
- package/dist/components/PrevNext.d.ts +16 -0
- package/dist/components/PrevNext.d.ts.map +1 -0
- package/dist/components/PrevNext.js +61 -0
- package/dist/components/PrevNext.js.map +1 -0
- package/dist/components/SearchButton.d.ts +16 -0
- package/dist/components/SearchButton.d.ts.map +1 -0
- package/dist/components/SearchButton.js +63 -0
- package/dist/components/SearchButton.js.map +1 -0
- package/dist/components/SearchField.d.ts +10 -0
- package/dist/components/SearchField.d.ts.map +1 -0
- package/dist/components/SearchField.js +67 -0
- package/dist/components/SearchField.js.map +1 -0
- package/dist/components/Segmented.d.ts +28 -0
- package/dist/components/Segmented.d.ts.map +1 -0
- package/dist/components/Segmented.js +74 -0
- package/dist/components/Segmented.js.map +1 -0
- package/dist/components/Select.d.ts +22 -0
- package/dist/components/Select.d.ts.map +1 -0
- package/dist/components/Select.js +50 -0
- package/dist/components/Select.js.map +1 -0
- package/dist/components/Settings.d.ts +103 -0
- package/dist/components/Settings.d.ts.map +1 -0
- package/dist/components/Settings.js +246 -0
- package/dist/components/Settings.js.map +1 -0
- package/dist/components/SideNav.d.ts +47 -0
- package/dist/components/SideNav.d.ts.map +1 -0
- package/dist/components/SideNav.js +125 -0
- package/dist/components/SideNav.js.map +1 -0
- package/dist/components/Skeletons.d.ts +37 -0
- package/dist/components/Skeletons.d.ts.map +1 -0
- package/dist/components/Skeletons.js +109 -0
- package/dist/components/Skeletons.js.map +1 -0
- package/dist/components/StatusPage.d.ts +49 -0
- package/dist/components/StatusPage.d.ts.map +1 -0
- package/dist/components/StatusPage.js +110 -0
- package/dist/components/StatusPage.js.map +1 -0
- package/dist/components/ThemeMenu.d.ts +13 -0
- package/dist/components/ThemeMenu.d.ts.map +1 -0
- package/dist/components/ThemeMenu.js +18 -0
- package/dist/components/ThemeMenu.js.map +1 -0
- package/dist/components/ThemeSwitch.d.ts +17 -0
- package/dist/components/ThemeSwitch.d.ts.map +1 -0
- package/dist/components/ThemeSwitch.js +25 -0
- package/dist/components/ThemeSwitch.js.map +1 -0
- package/dist/components/TopBar.d.ts +49 -0
- package/dist/components/TopBar.d.ts.map +1 -0
- package/dist/components/TopBar.js +146 -0
- package/dist/components/TopBar.js.map +1 -0
- package/dist/components/ViewBanner.d.ts +17 -0
- package/dist/components/ViewBanner.d.ts.map +1 -0
- package/dist/components/ViewBanner.js +14 -0
- package/dist/components/ViewBanner.js.map +1 -0
- package/dist/hooks/platform.d.ts +7 -0
- package/dist/hooks/platform.d.ts.map +1 -0
- package/dist/hooks/platform.js +19 -0
- package/dist/hooks/platform.js.map +1 -0
- package/dist/hooks/useConfirm.d.ts +23 -0
- package/dist/hooks/useConfirm.d.ts.map +1 -0
- package/dist/hooks/useConfirm.js +55 -0
- package/dist/hooks/useConfirm.js.map +1 -0
- package/dist/hooks/useCopy.d.ts +12 -0
- package/dist/hooks/useCopy.d.ts.map +1 -0
- package/dist/hooks/useCopy.js +39 -0
- package/dist/hooks/useCopy.js.map +1 -0
- package/dist/hooks/useDebouncedSearch.d.ts +8 -0
- package/dist/hooks/useDebouncedSearch.d.ts.map +1 -0
- package/dist/hooks/useDebouncedSearch.js +19 -0
- package/dist/hooks/useDebouncedSearch.js.map +1 -0
- package/dist/hooks/usePageClamp.d.ts +4 -0
- package/dist/hooks/usePageClamp.d.ts.map +1 -0
- package/dist/hooks/usePageClamp.js +17 -0
- package/dist/hooks/usePageClamp.js.map +1 -0
- package/dist/hooks/useSearchShortcut.d.ts +8 -0
- package/dist/hooks/useSearchShortcut.d.ts.map +1 -0
- package/dist/hooks/useSearchShortcut.js +38 -0
- package/dist/hooks/useSearchShortcut.js.map +1 -0
- package/dist/hooks/useThemeMode.d.ts +29 -0
- package/dist/hooks/useThemeMode.d.ts.map +1 -0
- package/dist/hooks/useThemeMode.js +111 -0
- package/dist/hooks/useThemeMode.js.map +1 -0
- package/dist/hooks/useToastMessage.d.ts +22 -0
- package/dist/hooks/useToastMessage.d.ts.map +1 -0
- package/dist/hooks/useToastMessage.js +26 -0
- package/dist/hooks/useToastMessage.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/provider/UiProvider.d.ts +60 -0
- package/dist/provider/UiProvider.d.ts.map +1 -0
- package/dist/provider/UiProvider.js +144 -0
- package/dist/provider/UiProvider.js.map +1 -0
- package/dist/provider/canvas.d.ts +7 -0
- package/dist/provider/canvas.d.ts.map +1 -0
- package/dist/provider/canvas.js +30 -0
- package/dist/provider/canvas.js.map +1 -0
- package/dist/provider/context.d.ts +69 -0
- package/dist/provider/context.d.ts.map +1 -0
- package/dist/provider/context.js +84 -0
- package/dist/provider/context.js.map +1 -0
- package/dist/theme/createTheme.d.ts +13 -0
- package/dist/theme/createTheme.d.ts.map +1 -0
- package/dist/theme/createTheme.js +18 -0
- package/dist/theme/createTheme.js.map +1 -0
- package/package.json +42 -2
- package/src/components/AppShell.tsx +337 -0
- package/src/components/AuthShell.tsx +104 -0
- package/src/components/BrandMark.tsx +28 -0
- package/src/components/ButtonLink.tsx +40 -0
- package/src/components/CheckboxField.tsx +50 -0
- package/src/components/ColorPickerInput.tsx +190 -0
- package/src/components/CommandPalette.tsx +479 -0
- package/src/components/CopyButton.tsx +76 -0
- package/src/components/CopyIdTrigger.tsx +127 -0
- package/src/components/DataList.tsx +332 -0
- package/src/components/DetailHeader.tsx +108 -0
- package/src/components/DurationInput.tsx +133 -0
- package/src/components/EmptyState.tsx +67 -0
- package/src/components/FormPage.tsx +109 -0
- package/src/components/GetStartedPanel.tsx +132 -0
- package/src/components/LabeledLine.tsx +28 -0
- package/src/components/ListSearch.tsx +64 -0
- package/src/components/LoadingBar.tsx +81 -0
- package/src/components/LoadingPage.tsx +41 -0
- package/src/components/MenuPanel.tsx +163 -0
- package/src/components/Outline.tsx +208 -0
- package/src/components/Overview.tsx +448 -0
- package/src/components/PageHeader.tsx +69 -0
- package/src/components/Pagination.tsx +105 -0
- package/src/components/PasswordInput.tsx +50 -0
- package/src/components/PrevNext.tsx +98 -0
- package/src/components/SearchButton.tsx +90 -0
- package/src/components/SearchField.tsx +97 -0
- package/src/components/Segmented.tsx +136 -0
- package/src/components/Select.tsx +110 -0
- package/src/components/Settings.tsx +432 -0
- package/src/components/SideNav.tsx +258 -0
- package/src/components/Skeletons.tsx +233 -0
- package/src/components/StatusPage.tsx +212 -0
- package/src/components/ThemeMenu.tsx +51 -0
- package/src/components/ThemeSwitch.tsx +49 -0
- package/src/components/TopBar.tsx +293 -0
- package/src/components/ViewBanner.tsx +51 -0
- package/src/hooks/platform.ts +22 -0
- package/src/hooks/useConfirm.tsx +97 -0
- package/src/hooks/useCopy.ts +39 -0
- package/src/hooks/useDebouncedSearch.ts +19 -0
- package/src/hooks/usePageClamp.ts +20 -0
- package/src/hooks/useSearchShortcut.ts +49 -0
- package/src/hooks/useThemeMode.ts +168 -0
- package/src/hooks/useToastMessage.ts +38 -0
- package/src/index.ts +156 -0
- package/src/provider/UiProvider.tsx +256 -0
- package/src/provider/canvas.ts +31 -0
- package/src/provider/context.tsx +145 -0
- package/src/theme/createTheme.ts +29 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// The person's light/dark choice and the scheme it draws in now. The choice is
|
|
2
|
+
// kept in localStorage under the key schemeBootScript reads, and follows
|
|
3
|
+
// other tabs; `system` follows the OS setting as it changes.
|
|
4
|
+
//
|
|
5
|
+
// On the server, and while hydrating, it gives `defaultMode`, drawn in
|
|
6
|
+
// `initialScheme` (what the server read from the scheme cookie), or else as
|
|
7
|
+
// `defaultMode` draws without the OS (`system` light): the server can't see
|
|
8
|
+
// the stored choice or the OS. The client then moves to the
|
|
9
|
+
// real scheme. useSyncExternalStore makes that switch without a hydration
|
|
10
|
+
// mismatch. With `cookie`, it keeps the scheme cookie in step for the next
|
|
11
|
+
// request.
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
THEME_STORAGE_KEY,
|
|
15
|
+
isThemeMode,
|
|
16
|
+
resolveScheme,
|
|
17
|
+
schemeCookie,
|
|
18
|
+
type Scheme,
|
|
19
|
+
type ThemeMode,
|
|
20
|
+
} from "@siiway/ui-core";
|
|
21
|
+
import { useCallback, useEffect, useMemo, useSyncExternalStore } from "react";
|
|
22
|
+
|
|
23
|
+
const DARK_QUERY = "(prefers-color-scheme: dark)";
|
|
24
|
+
|
|
25
|
+
// setMode in this tab notifies these; other tabs arrive as `storage` events.
|
|
26
|
+
const listeners = new Set<() => void>();
|
|
27
|
+
// This tab's choice when localStorage wouldn't take it (blocked, or full). It
|
|
28
|
+
// wins over what's stored, other tabs' choices included, until a write gets
|
|
29
|
+
// through.
|
|
30
|
+
const memory = new Map<string, string>();
|
|
31
|
+
|
|
32
|
+
function subscribeMode(onChange: () => void) {
|
|
33
|
+
listeners.add(onChange);
|
|
34
|
+
window.addEventListener("storage", onChange);
|
|
35
|
+
return () => {
|
|
36
|
+
listeners.delete(onChange);
|
|
37
|
+
window.removeEventListener("storage", onChange);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function subscribeSystem(onChange: () => void) {
|
|
42
|
+
const query = window.matchMedia?.(DARK_QUERY);
|
|
43
|
+
query?.addEventListener("change", onChange);
|
|
44
|
+
return () => query?.removeEventListener("change", onChange);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function systemDark() {
|
|
48
|
+
return window.matchMedia?.(DARK_QUERY).matches ?? false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function readStored(key: string): string | null {
|
|
52
|
+
const own = memory.get(key);
|
|
53
|
+
if (own !== undefined) return own;
|
|
54
|
+
try {
|
|
55
|
+
return window.localStorage.getItem(key);
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// The scheme the page draws, read afresh: while hydrating, the hook's own
|
|
62
|
+
// values are still the server's.
|
|
63
|
+
function currentScheme(storageKey: string, defaultMode: ThemeMode): Scheme {
|
|
64
|
+
const stored = readStored(storageKey);
|
|
65
|
+
return resolveScheme(isThemeMode(stored) ? stored : defaultMode, systemDark());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function serverStored() {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function serverDark() {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function writeCookie(name: string, scheme: Scheme) {
|
|
77
|
+
try {
|
|
78
|
+
document.cookie = schemeCookie(name, scheme, window.isSecureContext);
|
|
79
|
+
} catch {
|
|
80
|
+
// A sandboxed frame has no cookies.
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// True once the page is the client's: false on the server and while
|
|
85
|
+
// hydrating, where initialScheme must be drawn.
|
|
86
|
+
function subscribeNothing() {
|
|
87
|
+
return () => {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function clientTrue() {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function serverFalse() {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface ThemeModeOptions {
|
|
99
|
+
/** The `localStorage` key; give `schemeBootScript` the same. Defaults to `"siiway-theme"`. */
|
|
100
|
+
storageKey?: string;
|
|
101
|
+
/** Until a choice is stored, and on the server. Defaults to `"system"`. */
|
|
102
|
+
defaultMode?: ThemeMode;
|
|
103
|
+
/**
|
|
104
|
+
* A cookie to keep the scheme in, for the server to render the next
|
|
105
|
+
* request in (`schemeFromRequest`), such as `SCHEME_COOKIE`. Give
|
|
106
|
+
* `schemeBootScript` the same.
|
|
107
|
+
*/
|
|
108
|
+
cookie?: string;
|
|
109
|
+
/**
|
|
110
|
+
* The scheme the server rendered in, on the server and again on the client:
|
|
111
|
+
* drawn until hydration ends. `schemeFromRequest`'s answer; with null,
|
|
112
|
+
* `defaultMode`'s scheme, light for `system`.
|
|
113
|
+
*/
|
|
114
|
+
initialScheme?: Scheme | null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface ThemeModeState {
|
|
118
|
+
/** The choice, for `<ThemeSwitch value>`. */
|
|
119
|
+
mode: ThemeMode;
|
|
120
|
+
/** Stores a new choice, for `<ThemeSwitch onChange>`. */
|
|
121
|
+
setMode: (mode: ThemeMode) => void;
|
|
122
|
+
/** What to draw in now, for `<UiProvider scheme>`. */
|
|
123
|
+
scheme: Scheme;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function useThemeMode({
|
|
127
|
+
storageKey = THEME_STORAGE_KEY,
|
|
128
|
+
defaultMode = "system",
|
|
129
|
+
cookie,
|
|
130
|
+
initialScheme,
|
|
131
|
+
}: ThemeModeOptions = {}): ThemeModeState {
|
|
132
|
+
const stored = useSyncExternalStore(subscribeMode, () => readStored(storageKey), serverStored);
|
|
133
|
+
const dark = useSyncExternalStore(subscribeSystem, systemDark, serverDark);
|
|
134
|
+
// Without initialScheme there's nothing to hold until hydration ends, so the
|
|
135
|
+
// server's answer is the client's, and hydrating needs no second render.
|
|
136
|
+
const hydrated = useSyncExternalStore(
|
|
137
|
+
subscribeNothing,
|
|
138
|
+
clientTrue,
|
|
139
|
+
initialScheme ? serverFalse : clientTrue,
|
|
140
|
+
);
|
|
141
|
+
const mode = isThemeMode(stored) ? stored : defaultMode;
|
|
142
|
+
const live = resolveScheme(mode, dark);
|
|
143
|
+
const scheme = hydrated || !initialScheme ? live : initialScheme;
|
|
144
|
+
|
|
145
|
+
// On load, and whenever the scheme changes: another tab's choice, or the
|
|
146
|
+
// OS setting.
|
|
147
|
+
useEffect(() => {
|
|
148
|
+
if (cookie) writeCookie(cookie, currentScheme(storageKey, defaultMode));
|
|
149
|
+
}, [cookie, storageKey, defaultMode, live]);
|
|
150
|
+
|
|
151
|
+
const setMode = useCallback(
|
|
152
|
+
(next: ThemeMode) => {
|
|
153
|
+
try {
|
|
154
|
+
window.localStorage.setItem(storageKey, next);
|
|
155
|
+
memory.delete(storageKey);
|
|
156
|
+
} catch {
|
|
157
|
+
memory.set(storageKey, next);
|
|
158
|
+
}
|
|
159
|
+
// At once, not in the effect: a page load right after must carry it.
|
|
160
|
+
if (cookie) writeCookie(cookie, currentScheme(storageKey, defaultMode));
|
|
161
|
+
for (const listener of listeners) listener();
|
|
162
|
+
},
|
|
163
|
+
[storageKey, cookie, defaultMode],
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
// The same object until something changes: UiProvider puts it in context.
|
|
167
|
+
return useMemo(() => ({ mode, setMode, scheme }), [mode, setMode, scheme]);
|
|
168
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/** A message for `SettingsToast`. */
|
|
4
|
+
export interface ToastMessage {
|
|
5
|
+
/** Shows it as a success or an error. */
|
|
6
|
+
type: "success" | "error";
|
|
7
|
+
/** The message, already translated. */
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A message that clears itself, for `SettingsToast`.
|
|
13
|
+
*
|
|
14
|
+
* @param timeoutMs How long a message shows, in milliseconds.
|
|
15
|
+
*/
|
|
16
|
+
export function useToastMessage(timeoutMs = 5000) {
|
|
17
|
+
const [message, setMessage] = useState<ToastMessage | null>(null);
|
|
18
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
19
|
+
|
|
20
|
+
const showMsg = useCallback(
|
|
21
|
+
(type: ToastMessage["type"], text: string) => {
|
|
22
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
23
|
+
setMessage({ type, text });
|
|
24
|
+
timerRef.current = setTimeout(() => setMessage(null), timeoutMs);
|
|
25
|
+
},
|
|
26
|
+
[timeoutMs],
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
/** The message to show, or `null`. Pass it to `SettingsToast`. */
|
|
31
|
+
message,
|
|
32
|
+
/** Shows a message, which clears after `timeoutMs`. A new one replaces
|
|
33
|
+
* the one showing and starts its own timeout. */
|
|
34
|
+
showMsg,
|
|
35
|
+
/** To clear or set the message yourself. */
|
|
36
|
+
setMessage,
|
|
37
|
+
};
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Theme and provider
|
|
2
|
+
export { createTheme, type CreateThemeOptions } from "./theme/createTheme.js";
|
|
3
|
+
export { UiProvider, type UiProviderProps } from "./provider/UiProvider.js";
|
|
4
|
+
export { useCanvasClass } from "./provider/canvas.js";
|
|
5
|
+
export {
|
|
6
|
+
DEFAULT_ROUTER,
|
|
7
|
+
UiLink,
|
|
8
|
+
useUiMatch,
|
|
9
|
+
useUiNavigate,
|
|
10
|
+
useUiPageTitle,
|
|
11
|
+
useUiScheme,
|
|
12
|
+
useUiThemeMode,
|
|
13
|
+
useUiStrings,
|
|
14
|
+
type PageTitleHandler,
|
|
15
|
+
type UiLinkProps,
|
|
16
|
+
type UiRouter,
|
|
17
|
+
} from "./provider/context.js";
|
|
18
|
+
|
|
19
|
+
// Hooks
|
|
20
|
+
export { useConfirm, type ConfirmOptions } from "./hooks/useConfirm.js";
|
|
21
|
+
export { useCopy, type CopyState } from "./hooks/useCopy.js";
|
|
22
|
+
export { useDebouncedSearch } from "./hooks/useDebouncedSearch.js";
|
|
23
|
+
export { usePageClamp } from "./hooks/usePageClamp.js";
|
|
24
|
+
export { useSearchShortcut, type SearchShortcutOptions } from "./hooks/useSearchShortcut.js";
|
|
25
|
+
export { useThemeMode, type ThemeModeOptions, type ThemeModeState } from "./hooks/useThemeMode.js";
|
|
26
|
+
export { useToastMessage, type ToastMessage } from "./hooks/useToastMessage.js";
|
|
27
|
+
|
|
28
|
+
// Page structure
|
|
29
|
+
export { PageHeader } from "./components/PageHeader.js";
|
|
30
|
+
export { BackButton, DetailHeader } from "./components/DetailHeader.js";
|
|
31
|
+
export { FormPage } from "./components/FormPage.js";
|
|
32
|
+
export {
|
|
33
|
+
StatusPage,
|
|
34
|
+
type StatusPageAction,
|
|
35
|
+
type StatusPageProps,
|
|
36
|
+
} from "./components/StatusPage.js";
|
|
37
|
+
export { LoadingPage } from "./components/LoadingPage.js";
|
|
38
|
+
export { AuthShell, type AuthShellProps } from "./components/AuthShell.js";
|
|
39
|
+
export { EmptyState } from "./components/EmptyState.js";
|
|
40
|
+
export { GetStartedPanel } from "./components/GetStartedPanel.js";
|
|
41
|
+
export { ViewBanner } from "./components/ViewBanner.js";
|
|
42
|
+
export { LabeledLine } from "./components/LabeledLine.js";
|
|
43
|
+
export { BrandMark } from "./components/BrandMark.js";
|
|
44
|
+
|
|
45
|
+
// Navigation
|
|
46
|
+
export {
|
|
47
|
+
TOP_BAR_HEIGHT,
|
|
48
|
+
TopBar,
|
|
49
|
+
type TopBarBrand,
|
|
50
|
+
type TopBarLink,
|
|
51
|
+
type TopBarProps,
|
|
52
|
+
} from "./components/TopBar.js";
|
|
53
|
+
export {
|
|
54
|
+
SideNav,
|
|
55
|
+
type SideNavGroup,
|
|
56
|
+
type SideNavItem,
|
|
57
|
+
type SideNavProps,
|
|
58
|
+
} from "./components/SideNav.js";
|
|
59
|
+
export { Outline, type OutlineItem, type OutlineProps } from "./components/Outline.js";
|
|
60
|
+
export { PrevNext, type PrevNextLink, type PrevNextProps } from "./components/PrevNext.js";
|
|
61
|
+
export { LoadingBar } from "./components/LoadingBar.js";
|
|
62
|
+
export { AppShell, AppShellClose, type AppShellProps } from "./components/AppShell.js";
|
|
63
|
+
|
|
64
|
+
// Search
|
|
65
|
+
export { SearchField, type SearchFieldProps } from "./components/SearchField.js";
|
|
66
|
+
export { SearchButton, type SearchButtonProps } from "./components/SearchButton.js";
|
|
67
|
+
export {
|
|
68
|
+
CommandPalette,
|
|
69
|
+
type CommandGroup,
|
|
70
|
+
type CommandItem,
|
|
71
|
+
type CommandPaletteProps,
|
|
72
|
+
} from "./components/CommandPalette.js";
|
|
73
|
+
|
|
74
|
+
// Settings
|
|
75
|
+
export {
|
|
76
|
+
SaveFooter,
|
|
77
|
+
SettingsBody,
|
|
78
|
+
SettingsCard,
|
|
79
|
+
SettingsEmpty,
|
|
80
|
+
SettingsFooter,
|
|
81
|
+
SettingsLinkRow,
|
|
82
|
+
SettingsPage,
|
|
83
|
+
SettingsRow,
|
|
84
|
+
SettingsSubhead,
|
|
85
|
+
SettingsTable,
|
|
86
|
+
SettingsToast,
|
|
87
|
+
} from "./components/Settings.js";
|
|
88
|
+
|
|
89
|
+
// Lists and data
|
|
90
|
+
export {
|
|
91
|
+
DataList,
|
|
92
|
+
DataListLink,
|
|
93
|
+
DataListName,
|
|
94
|
+
DataListRow,
|
|
95
|
+
DataListSkeleton,
|
|
96
|
+
type DataListColumn,
|
|
97
|
+
} from "./components/DataList.js";
|
|
98
|
+
export { Pagination } from "./components/Pagination.js";
|
|
99
|
+
export { ListSearch } from "./components/ListSearch.js";
|
|
100
|
+
export {
|
|
101
|
+
Callout,
|
|
102
|
+
Callouts,
|
|
103
|
+
ListColumn,
|
|
104
|
+
ListGrid,
|
|
105
|
+
ListRow,
|
|
106
|
+
Metric,
|
|
107
|
+
MetricGrid,
|
|
108
|
+
MetricSuffix,
|
|
109
|
+
SectionTitle,
|
|
110
|
+
SummaryList,
|
|
111
|
+
SummaryRow,
|
|
112
|
+
} from "./components/Overview.js";
|
|
113
|
+
export {
|
|
114
|
+
SkeletonSettingsCard,
|
|
115
|
+
SkeletonSettingsPage,
|
|
116
|
+
SkeletonStatCards,
|
|
117
|
+
SkeletonTableBodyRows,
|
|
118
|
+
SkeletonTableRows,
|
|
119
|
+
} from "./components/Skeletons.js";
|
|
120
|
+
|
|
121
|
+
// Inputs
|
|
122
|
+
export { Segmented, type SegmentedOption } from "./components/Segmented.js";
|
|
123
|
+
export { ThemeSwitch, type ThemeSwitchProps } from "./components/ThemeSwitch.js";
|
|
124
|
+
export { ThemeMenu, type ThemeMenuProps } from "./components/ThemeMenu.js";
|
|
125
|
+
export {
|
|
126
|
+
MenuPanel,
|
|
127
|
+
MenuPanelButton,
|
|
128
|
+
MenuPanelDivider,
|
|
129
|
+
MenuPanelHeader,
|
|
130
|
+
MenuPanelRow,
|
|
131
|
+
} from "./components/MenuPanel.js";
|
|
132
|
+
export { Select } from "./components/Select.js";
|
|
133
|
+
export { CheckboxField } from "./components/CheckboxField.js";
|
|
134
|
+
export { PasswordInput } from "./components/PasswordInput.js";
|
|
135
|
+
export { DurationInput } from "./components/DurationInput.js";
|
|
136
|
+
export { ColorPickerInput } from "./components/ColorPickerInput.js";
|
|
137
|
+
|
|
138
|
+
// Copy
|
|
139
|
+
export { CopyButton, type CopyButtonProps } from "./components/CopyButton.js";
|
|
140
|
+
export { CopyIdText, CopyIdTrigger } from "./components/CopyIdTrigger.js";
|
|
141
|
+
|
|
142
|
+
// Re-exported from @siiway/ui-core for convenience
|
|
143
|
+
export {
|
|
144
|
+
CANVAS,
|
|
145
|
+
DEFAULT_ACCENT,
|
|
146
|
+
SCHEME_COOKIE,
|
|
147
|
+
THEME_STORAGE_KEY,
|
|
148
|
+
resolveScheme,
|
|
149
|
+
schemeBootScript,
|
|
150
|
+
schemeCookie,
|
|
151
|
+
schemeFromRequest,
|
|
152
|
+
type Scheme,
|
|
153
|
+
type ThemeMode,
|
|
154
|
+
type UiStrings,
|
|
155
|
+
type DurationUnit,
|
|
156
|
+
} from "@siiway/ui-core";
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FluentProvider,
|
|
3
|
+
createCSSRuleFromTheme,
|
|
4
|
+
makeStaticStyles,
|
|
5
|
+
mergeClasses,
|
|
6
|
+
tokens,
|
|
7
|
+
useIsomorphicLayoutEffect,
|
|
8
|
+
type FluentProviderProps,
|
|
9
|
+
} from "@fluentui/react-components";
|
|
10
|
+
import { CANVAS, DEFAULT_ACCENT, pickStrings, type Scheme, type UiStrings } from "@siiway/ui-core";
|
|
11
|
+
import { useContext, useMemo, useSyncExternalStore, type CSSProperties } from "react";
|
|
12
|
+
import { useThemeMode, type ThemeModeOptions, type ThemeModeState } from "../hooks/useThemeMode.js";
|
|
13
|
+
import { createTheme } from "../theme/createTheme.js";
|
|
14
|
+
import {
|
|
15
|
+
DEFAULT_ROUTER,
|
|
16
|
+
NO_PROVIDER,
|
|
17
|
+
UiContext,
|
|
18
|
+
type PageTitleHandler,
|
|
19
|
+
type UiRouter,
|
|
20
|
+
} from "./context.js";
|
|
21
|
+
|
|
22
|
+
export interface UiProviderProps extends Omit<FluentProviderProps, "theme"> {
|
|
23
|
+
/**
|
|
24
|
+
* The scheme to render in, when the app keeps the light/dark choice itself
|
|
25
|
+
* or draws a fixed one. Leave it out and the provider keeps the person's
|
|
26
|
+
* choice (`themeMode`), or a provider inside another follows the outer one.
|
|
27
|
+
*/
|
|
28
|
+
scheme?: Scheme;
|
|
29
|
+
/**
|
|
30
|
+
* How the provider keeps the person's choice when it has no `scheme`:
|
|
31
|
+
* `useThemeMode`'s options (`storageKey`, `defaultMode`, `cookie`,
|
|
32
|
+
* `initialScheme`). `ThemeSwitch` and `ThemeMenu` show and change it, and
|
|
33
|
+
* `useUiThemeMode()` gives it to your own components.
|
|
34
|
+
*/
|
|
35
|
+
themeMode?: ThemeModeOptions;
|
|
36
|
+
/**
|
|
37
|
+
* This provider is the page. It takes the canvas colour, removes the
|
|
38
|
+
* body's margin, and draws `<html>` in the scheme (`data-scheme`,
|
|
39
|
+
* `color-scheme`, the canvas background, `--siiway-canvas`) and each
|
|
40
|
+
* `<meta name="theme-color">` in the canvas, as `schemeBootScript` does
|
|
41
|
+
* before the app loads. Selected text takes a tint of the accent, and
|
|
42
|
+
* scrollbars are thin. Set it on the app's outermost provider only.
|
|
43
|
+
*/
|
|
44
|
+
page?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* With `page`, for a page rendered once for everyone, such as a static
|
|
47
|
+
* site: it carries both schemes' colours, keyed on `<html data-scheme>`,
|
|
48
|
+
* so the scheme `schemeBootScript` picks shows before the app loads, not
|
|
49
|
+
* the one the page was rendered in. It adds about as much CSS again as the
|
|
50
|
+
* theme takes. Without `page` it does nothing.
|
|
51
|
+
*/
|
|
52
|
+
bothSchemes?: boolean;
|
|
53
|
+
/** Brand colour; missing or invalid gives Fluent's blue. */
|
|
54
|
+
accent?: string | null;
|
|
55
|
+
/** BCP-47 tag for the library's own strings: `zh*` is Chinese, anything else English. */
|
|
56
|
+
locale?: string | null;
|
|
57
|
+
/** Replaces individual library strings. */
|
|
58
|
+
strings?: Partial<UiStrings>;
|
|
59
|
+
/**
|
|
60
|
+
* The app's router. Without one, links are `<a href>` and navigation
|
|
61
|
+
* reloads the page. Pass a stable object (define it at module level).
|
|
62
|
+
*/
|
|
63
|
+
router?: UiRouter;
|
|
64
|
+
/**
|
|
65
|
+
* Called with a page's title when its PageHeader or DetailHeader mounts
|
|
66
|
+
* (`tabTitle`, or the title if it's plain text), and with null when the
|
|
67
|
+
* page goes, so the app can set the browser tab's title. Pass a stable
|
|
68
|
+
* function.
|
|
69
|
+
*/
|
|
70
|
+
onPageTitle?: PageTitleHandler;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The look's rules for Fluent's own parts, wherever they render. Badges keep
|
|
74
|
+
// their label on one line: in a narrow flex row, CJK text otherwise wraps a
|
|
75
|
+
// character at a time. Surfaces that float (in a portal, outside the page's
|
|
76
|
+
// containers) get a hairline on top of their shadow, which barely shows on the
|
|
77
|
+
// dark canvas.
|
|
78
|
+
const useLookStyles = makeStaticStyles({
|
|
79
|
+
".fui-Badge": { whiteSpace: "nowrap", flexShrink: "0" },
|
|
80
|
+
".fui-DialogSurface, .fui-MenuPopover, .fui-PopoverSurface, .fui-Listbox": {
|
|
81
|
+
border: `1px solid ${tokens.colorNeutralStroke2}`,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
function LookStyles() {
|
|
86
|
+
useLookStyles();
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Selected text takes a tint of the accent, and scrollbars are thin, with a
|
|
91
|
+
// neutral thumb on a clear track in either scheme.
|
|
92
|
+
const usePageStyles = makeStaticStyles({
|
|
93
|
+
body: { margin: "0" },
|
|
94
|
+
"::selection": {
|
|
95
|
+
backgroundColor: `color-mix(in srgb, var(--colorBrandBackground, ${DEFAULT_ACCENT}) 22%, transparent)`,
|
|
96
|
+
},
|
|
97
|
+
"::-webkit-scrollbar": { width: "8px", height: "8px" },
|
|
98
|
+
"::-webkit-scrollbar-track": { backgroundColor: "transparent" },
|
|
99
|
+
"::-webkit-scrollbar-thumb": {
|
|
100
|
+
backgroundColor: "rgba(0, 0, 0, 0.16)",
|
|
101
|
+
backgroundClip: "padding-box",
|
|
102
|
+
border: "2px solid transparent",
|
|
103
|
+
borderRadius: "4px",
|
|
104
|
+
},
|
|
105
|
+
"::-webkit-scrollbar-thumb:hover": { backgroundColor: "rgba(0, 0, 0, 0.32)" },
|
|
106
|
+
'html[data-scheme="dark"] ::-webkit-scrollbar-thumb': {
|
|
107
|
+
backgroundColor: "rgba(255, 255, 255, 0.14)",
|
|
108
|
+
},
|
|
109
|
+
'html[data-scheme="dark"] ::-webkit-scrollbar-thumb:hover': {
|
|
110
|
+
backgroundColor: "rgba(255, 255, 255, 0.28)",
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const subscribeNothing = () => () => {};
|
|
115
|
+
|
|
116
|
+
// False while hydrating, true from the render after (and at once in a render
|
|
117
|
+
// that doesn't hydrate).
|
|
118
|
+
function useHydrated(): boolean {
|
|
119
|
+
return useSyncExternalStore(
|
|
120
|
+
subscribeNothing,
|
|
121
|
+
() => true,
|
|
122
|
+
() => false,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// <html> in the page's scheme, for what lies outside the provider: the
|
|
127
|
+
// overscroll area, the scrollbars, form controls' native parts, the app's CSS
|
|
128
|
+
// (--siiway-canvas) and the browser's own bar (theme-color).
|
|
129
|
+
function PageScheme({ scheme, both }: { scheme: Scheme; both: boolean }) {
|
|
130
|
+
usePageStyles();
|
|
131
|
+
// Hydrating a page with both schemes, `scheme` is the one the server drew,
|
|
132
|
+
// and <html> has the one the boot script picked, which the page shows. Left
|
|
133
|
+
// as it is until the render after, which brings the app's own scheme.
|
|
134
|
+
const hydrated = useHydrated();
|
|
135
|
+
const hold = both && !hydrated;
|
|
136
|
+
useIsomorphicLayoutEffect(() => {
|
|
137
|
+
if (hold) return;
|
|
138
|
+
const root = document.documentElement;
|
|
139
|
+
const canvas = CANVAS[scheme];
|
|
140
|
+
root.setAttribute("data-scheme", scheme);
|
|
141
|
+
root.style.colorScheme = scheme;
|
|
142
|
+
root.style.backgroundColor = canvas;
|
|
143
|
+
root.style.setProperty("--siiway-canvas", canvas);
|
|
144
|
+
const metas = Array.from(
|
|
145
|
+
document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]'),
|
|
146
|
+
);
|
|
147
|
+
const colors = metas.map((meta) => meta.content);
|
|
148
|
+
for (const meta of metas) meta.content = canvas;
|
|
149
|
+
return () => {
|
|
150
|
+
root.removeAttribute("data-scheme");
|
|
151
|
+
root.style.removeProperty("color-scheme");
|
|
152
|
+
root.style.removeProperty("background-color");
|
|
153
|
+
root.style.removeProperty("--siiway-canvas");
|
|
154
|
+
metas.forEach((meta, i) => (meta.content = colors[i]!));
|
|
155
|
+
};
|
|
156
|
+
}, [scheme, hold]);
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// For bothSchemes: each scheme's tokens, keyed on <html data-scheme>, which the
|
|
161
|
+
// boot script sets before the page paints. They outrank FluentProvider's own
|
|
162
|
+
// rule for its class, and agree with it once the provider draws the page's
|
|
163
|
+
// scheme.
|
|
164
|
+
const BOTH_SCHEMES = "siiway-both-schemes";
|
|
165
|
+
|
|
166
|
+
// Through Griffel, as static styles: the renderer gives them its attributes
|
|
167
|
+
// (a CSP nonce) and, on the server, puts them in the head with the rest. One
|
|
168
|
+
// hook per accent, made once.
|
|
169
|
+
const bothSchemesStyles = new Map<string, () => void>();
|
|
170
|
+
|
|
171
|
+
function BothSchemes({ accent }: { accent?: string | null }) {
|
|
172
|
+
const key = accent ?? "";
|
|
173
|
+
let useRules = bothSchemesStyles.get(key);
|
|
174
|
+
if (!useRules) {
|
|
175
|
+
useRules = makeStaticStyles(
|
|
176
|
+
(["light", "dark"] as const).map((scheme) =>
|
|
177
|
+
createCSSRuleFromTheme(
|
|
178
|
+
`html[data-scheme="${scheme}"] .${BOTH_SCHEMES}`,
|
|
179
|
+
createTheme({ scheme, accent }),
|
|
180
|
+
),
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
bothSchemesStyles.set(key, useRules);
|
|
184
|
+
}
|
|
185
|
+
useRules();
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Fluent's provider with the SiiWay theme, plus the library's strings and
|
|
191
|
+
* router. Wrap the app in it once, where FluentProvider used to be.
|
|
192
|
+
*/
|
|
193
|
+
export function UiProvider(props: UiProviderProps) {
|
|
194
|
+
const outer = useContext(UiContext);
|
|
195
|
+
const kept = outer.themeMode;
|
|
196
|
+
if (props.scheme !== undefined) return <Provider {...props} scheme={props.scheme} kept={kept} />;
|
|
197
|
+
// Inside another provider: draw what it draws, whether it keeps the choice
|
|
198
|
+
// or was given a scheme.
|
|
199
|
+
if (outer !== NO_PROVIDER) return <Provider {...props} scheme={outer.scheme} kept={kept} />;
|
|
200
|
+
return <KeepingProvider {...props} />;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// The outermost provider without a scheme keeps the person's choice.
|
|
204
|
+
function KeepingProvider({ themeMode, ...props }: UiProviderProps) {
|
|
205
|
+
const kept = useThemeMode(themeMode);
|
|
206
|
+
return <Provider {...props} scheme={kept.scheme} kept={kept} />;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function Provider({
|
|
210
|
+
scheme,
|
|
211
|
+
kept,
|
|
212
|
+
themeMode: _options,
|
|
213
|
+
page,
|
|
214
|
+
bothSchemes,
|
|
215
|
+
accent,
|
|
216
|
+
locale,
|
|
217
|
+
strings,
|
|
218
|
+
router,
|
|
219
|
+
onPageTitle,
|
|
220
|
+
children,
|
|
221
|
+
...rest
|
|
222
|
+
}: UiProviderProps & { scheme: Scheme; kept: ThemeModeState | null }) {
|
|
223
|
+
const both = !!(page && bothSchemes);
|
|
224
|
+
const theme = useMemo(() => createTheme({ scheme, accent }), [scheme, accent]);
|
|
225
|
+
const resolved = useMemo(() => pickStrings(locale, strings), [locale, strings]);
|
|
226
|
+
const value = useMemo(
|
|
227
|
+
() => ({
|
|
228
|
+
scheme,
|
|
229
|
+
strings: resolved,
|
|
230
|
+
router: router ?? DEFAULT_ROUTER,
|
|
231
|
+
onPageTitle: onPageTitle ?? null,
|
|
232
|
+
themeMode: kept,
|
|
233
|
+
bothSchemes: both,
|
|
234
|
+
}),
|
|
235
|
+
[scheme, resolved, router, onPageTitle, kept, both],
|
|
236
|
+
);
|
|
237
|
+
// Fluent paints its provider colorNeutralBackground1; the page is the canvas,
|
|
238
|
+
// with bothSchemes the one <html> has.
|
|
239
|
+
const canvas = both ? `var(--siiway-canvas, ${CANVAS[scheme]})` : CANVAS[scheme];
|
|
240
|
+
const style: CSSProperties | undefined = page
|
|
241
|
+
? { backgroundColor: canvas, ...rest.style }
|
|
242
|
+
: rest.style;
|
|
243
|
+
return (
|
|
244
|
+
<FluentProvider
|
|
245
|
+
theme={theme}
|
|
246
|
+
{...rest}
|
|
247
|
+
className={both ? mergeClasses(BOTH_SCHEMES, rest.className) : rest.className}
|
|
248
|
+
style={style}
|
|
249
|
+
>
|
|
250
|
+
<LookStyles />
|
|
251
|
+
{both && <BothSchemes accent={accent} />}
|
|
252
|
+
{page && <PageScheme scheme={scheme} both={both} />}
|
|
253
|
+
<UiContext.Provider value={value}>{children}</UiContext.Provider>
|
|
254
|
+
</FluentProvider>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Tokens have no canvas (it's darker than colorNeutralBackground1 in dark),
|
|
2
|
+
// so this picks CANVAS by the provider's scheme. Under a page provider with
|
|
3
|
+
// bothSchemes, whose colours follow <html data-scheme>, it reads the canvas
|
|
4
|
+
// there instead (--siiway-canvas, set by schemeBootScript before the page
|
|
5
|
+
// paints): the one the boot script picked, not the one the page was rendered
|
|
6
|
+
// in. Without bothSchemes the rest of the page is drawn in the rendered
|
|
7
|
+
// scheme until the app loads, so the canvas stays with it.
|
|
8
|
+
|
|
9
|
+
import { makeStyles } from "@fluentui/react-components";
|
|
10
|
+
import { CANVAS } from "@siiway/ui-core";
|
|
11
|
+
import { useContext } from "react";
|
|
12
|
+
import { UiContext } from "./context.js";
|
|
13
|
+
|
|
14
|
+
const useStyles = makeStyles({
|
|
15
|
+
light: { backgroundColor: CANVAS.light },
|
|
16
|
+
dark: { backgroundColor: CANVAS.dark },
|
|
17
|
+
pageLight: { backgroundColor: `var(--siiway-canvas, ${CANVAS.light})` },
|
|
18
|
+
pageDark: { backgroundColor: `var(--siiway-canvas, ${CANVAS.dark})` },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A class with the canvas colour, in the provider's scheme (or the page's,
|
|
23
|
+
* with `bothSchemes`), for parts of the page drawn on it that must be opaque:
|
|
24
|
+
* a sticky bar, a drawer, a full-page message.
|
|
25
|
+
*/
|
|
26
|
+
export function useCanvasClass(): string {
|
|
27
|
+
const styles = useStyles();
|
|
28
|
+
const { scheme, bothSchemes } = useContext(UiContext);
|
|
29
|
+
if (!bothSchemes) return styles[scheme];
|
|
30
|
+
return scheme === "dark" ? styles.pageDark : styles.pageLight;
|
|
31
|
+
}
|