@siiway/ui-react 0.1.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.
Files changed (108) hide show
  1. package/dist/components/AppShell.d.ts +29 -0
  2. package/dist/components/AppShell.d.ts.map +1 -0
  3. package/dist/components/AppShell.js +220 -0
  4. package/dist/components/AppShell.js.map +1 -0
  5. package/dist/components/AuthShell.d.ts +21 -0
  6. package/dist/components/AuthShell.d.ts.map +1 -0
  7. package/dist/components/AuthShell.js +53 -0
  8. package/dist/components/AuthShell.js.map +1 -0
  9. package/dist/components/ButtonLink.d.ts +11 -0
  10. package/dist/components/ButtonLink.d.ts.map +1 -0
  11. package/dist/components/ButtonLink.js +15 -0
  12. package/dist/components/ButtonLink.js.map +1 -0
  13. package/dist/components/CommandPalette.d.ts +23 -11
  14. package/dist/components/CommandPalette.d.ts.map +1 -1
  15. package/dist/components/CommandPalette.js +33 -3
  16. package/dist/components/CommandPalette.js.map +1 -1
  17. package/dist/components/CopyButton.d.ts +25 -6
  18. package/dist/components/CopyButton.d.ts.map +1 -1
  19. package/dist/components/CopyButton.js +11 -8
  20. package/dist/components/CopyButton.js.map +1 -1
  21. package/dist/components/DetailHeader.d.ts +5 -1
  22. package/dist/components/DetailHeader.d.ts.map +1 -1
  23. package/dist/components/DetailHeader.js +6 -5
  24. package/dist/components/DetailHeader.js.map +1 -1
  25. package/dist/components/FormPage.d.ts +1 -1
  26. package/dist/components/FormPage.d.ts.map +1 -1
  27. package/dist/components/LoadingPage.d.ts +6 -0
  28. package/dist/components/LoadingPage.d.ts.map +1 -0
  29. package/dist/components/LoadingPage.js +25 -0
  30. package/dist/components/LoadingPage.js.map +1 -0
  31. package/dist/components/MenuPanel.d.ts +34 -0
  32. package/dist/components/MenuPanel.d.ts.map +1 -0
  33. package/dist/components/MenuPanel.js +98 -0
  34. package/dist/components/MenuPanel.js.map +1 -0
  35. package/dist/components/PageHeader.d.ts +3 -3
  36. package/dist/components/PageHeader.d.ts.map +1 -1
  37. package/dist/components/PageHeader.js +5 -14
  38. package/dist/components/PageHeader.js.map +1 -1
  39. package/dist/components/PasswordInput.d.ts.map +1 -1
  40. package/dist/components/PasswordInput.js +15 -2
  41. package/dist/components/PasswordInput.js.map +1 -1
  42. package/dist/components/SideNav.d.ts +18 -5
  43. package/dist/components/SideNav.d.ts.map +1 -1
  44. package/dist/components/SideNav.js +30 -4
  45. package/dist/components/SideNav.js.map +1 -1
  46. package/dist/components/StatusPage.d.ts +17 -5
  47. package/dist/components/StatusPage.d.ts.map +1 -1
  48. package/dist/components/StatusPage.js +10 -6
  49. package/dist/components/StatusPage.js.map +1 -1
  50. package/dist/components/ThemeMenu.d.ts +6 -4
  51. package/dist/components/ThemeMenu.d.ts.map +1 -1
  52. package/dist/components/ThemeMenu.js +3 -27
  53. package/dist/components/ThemeMenu.js.map +1 -1
  54. package/dist/components/ThemeSwitch.d.ts +7 -4
  55. package/dist/components/ThemeSwitch.d.ts.map +1 -1
  56. package/dist/components/ThemeSwitch.js +12 -4
  57. package/dist/components/ThemeSwitch.js.map +1 -1
  58. package/dist/components/TopBar.d.ts +6 -2
  59. package/dist/components/TopBar.d.ts.map +1 -1
  60. package/dist/components/TopBar.js +10 -9
  61. package/dist/components/TopBar.js.map +1 -1
  62. package/dist/hooks/useCopy.d.ts +5 -2
  63. package/dist/hooks/useCopy.d.ts.map +1 -1
  64. package/dist/hooks/useCopy.js +29 -4
  65. package/dist/hooks/useCopy.js.map +1 -1
  66. package/dist/hooks/useThemeMode.d.ts +13 -1
  67. package/dist/hooks/useThemeMode.d.ts.map +1 -1
  68. package/dist/hooks/useThemeMode.js +59 -10
  69. package/dist/hooks/useThemeMode.js.map +1 -1
  70. package/dist/index.d.ts +8 -3
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +7 -2
  73. package/dist/index.js.map +1 -1
  74. package/dist/provider/UiProvider.d.ts +30 -7
  75. package/dist/provider/UiProvider.d.ts.map +1 -1
  76. package/dist/provider/UiProvider.js +116 -14
  77. package/dist/provider/UiProvider.js.map +1 -1
  78. package/dist/provider/canvas.d.ts +5 -0
  79. package/dist/provider/canvas.d.ts.map +1 -1
  80. package/dist/provider/canvas.js +21 -6
  81. package/dist/provider/canvas.js.map +1 -1
  82. package/dist/provider/context.d.ts +27 -3
  83. package/dist/provider/context.d.ts.map +1 -1
  84. package/dist/provider/context.js +48 -3
  85. package/dist/provider/context.js.map +1 -1
  86. package/package.json +2 -2
  87. package/src/components/AppShell.tsx +337 -0
  88. package/src/components/AuthShell.tsx +104 -0
  89. package/src/components/ButtonLink.tsx +40 -0
  90. package/src/components/CommandPalette.tsx +56 -14
  91. package/src/components/CopyButton.tsx +61 -33
  92. package/src/components/DetailHeader.tsx +12 -6
  93. package/src/components/FormPage.tsx +1 -1
  94. package/src/components/LoadingPage.tsx +41 -0
  95. package/src/components/MenuPanel.tsx +163 -0
  96. package/src/components/PageHeader.tsx +9 -20
  97. package/src/components/PasswordInput.tsx +16 -1
  98. package/src/components/SideNav.tsx +86 -16
  99. package/src/components/StatusPage.tsx +41 -17
  100. package/src/components/ThemeMenu.tsx +12 -44
  101. package/src/components/ThemeSwitch.tsx +20 -10
  102. package/src/components/TopBar.tsx +31 -18
  103. package/src/hooks/useCopy.ts +27 -8
  104. package/src/hooks/useThemeMode.ts +78 -8
  105. package/src/index.ts +17 -1
  106. package/src/provider/UiProvider.tsx +182 -21
  107. package/src/provider/canvas.ts +20 -6
  108. package/src/provider/context.tsx +65 -5
@@ -1,14 +1,15 @@
1
1
  // A full-page message: an icon or code, a title, a line of explanation and
2
- // where to go next. Used for 404, access denied and signed-out pages: Prism's
3
- // StatusPage, with what Vellum's 404 adds (the site above, the missing path,
4
- // pages the reader may have meant).
2
+ // where to go next. Used for 404, error, access denied and signed-out pages:
3
+ // Prism's StatusPage, with what Vellum's 404 adds (the site above, the missing
4
+ // path, pages the reader may have meant).
5
5
 
6
6
  import { Button, Text, Title2, makeStyles, mergeClasses, tokens } from "@fluentui/react-components";
7
7
  import { ChevronRight16Regular } from "@fluentui/react-icons";
8
- import type { ReactNode } from "react";
8
+ import type { ReactElement, ReactNode } from "react";
9
9
  import { useCanvasClass } from "../provider/canvas.js";
10
- import { UiLink, useUiNavigate } from "../provider/context.js";
10
+ import { UiLink } from "../provider/context.js";
11
11
  import { BrandMark } from "./BrandMark.js";
12
+ import { ButtonLink } from "./ButtonLink.js";
12
13
 
13
14
  const useStyles = makeStyles({
14
15
  page: {
@@ -100,11 +101,37 @@ const useStyles = makeStyles({
100
101
  },
101
102
  });
102
103
 
103
- export interface StatusPageAction {
104
+ /** A button on a status page: a link to a page in the app (`to`), or
105
+ * something to do there (`onClick`), such as reloading or signing in again. */
106
+ export type StatusPageAction = {
104
107
  /** The button's text. */
105
108
  label: string;
106
- /** An in-app path, opened through the app's router. */
107
- to: string;
109
+ /** An icon before the label. */
110
+ icon?: ReactElement;
111
+ } & (
112
+ | {
113
+ /** An in-app path, opened through the app's router. */
114
+ to: string;
115
+ onClick?: never;
116
+ }
117
+ | {
118
+ /** What the button does, when it opens no page of its own. */
119
+ onClick: () => void;
120
+ to?: never;
121
+ }
122
+ );
123
+
124
+ function Action({ action, primary }: { action: StatusPageAction; primary?: boolean }) {
125
+ const appearance = primary ? "primary" : undefined;
126
+ return action.to !== undefined ? (
127
+ <ButtonLink appearance={appearance} icon={action.icon} to={action.to}>
128
+ {action.label}
129
+ </ButtonLink>
130
+ ) : (
131
+ <Button appearance={appearance} icon={action.icon} onClick={action.onClick}>
132
+ {action.label}
133
+ </Button>
134
+ );
108
135
  }
109
136
 
110
137
  export interface StatusPageProps {
@@ -120,9 +147,11 @@ export interface StatusPageProps {
120
147
  hint?: ReactNode;
121
148
  /** Label for a button that goes back in the browser's history, when going back makes sense. */
122
149
  back?: string;
123
- /** More ways on, between "go back" and the main one, such as search. */
150
+ /** More ways on, between "go back" and the main one, such as search: each
151
+ * `{ label, to }` or `{ label, onClick }`, with an optional `icon`. */
124
152
  secondary?: StatusPageAction[];
125
- /** The main way on: the primary button, `{ label, to }`. */
153
+ /** The main way on, the primary button: `{ label, to }` to open a page, or
154
+ * `{ label, onClick }`, such as reloading after an error. */
126
155
  primary: StatusPageAction;
127
156
  /** Pages the reader may have meant, listed under the buttons. */
128
157
  links?: { label: ReactNode; to: string }[];
@@ -144,7 +173,6 @@ export function StatusPage({
144
173
  }: StatusPageProps) {
145
174
  const styles = useStyles();
146
175
  const canvas = useCanvasClass();
147
- const navigate = useUiNavigate();
148
176
  return (
149
177
  <div className={mergeClasses(styles.page, canvas)}>
150
178
  <div className={styles.card}>
@@ -163,13 +191,9 @@ export function StatusPage({
163
191
  <div className={styles.actions}>
164
192
  {back && <Button onClick={() => window.history.back()}>{back}</Button>}
165
193
  {secondary?.map((action) => (
166
- <Button key={action.to} onClick={() => navigate(action.to)}>
167
- {action.label}
168
- </Button>
194
+ <Action key={action.to ?? action.label} action={action} />
169
195
  ))}
170
- <Button appearance="primary" onClick={() => navigate(primary.to)}>
171
- {primary.label}
172
- </Button>
196
+ <Action action={primary} primary />
173
197
  </div>
174
198
  {links && links.length > 0 && (
175
199
  <div className={styles.links}>
@@ -2,57 +2,26 @@
2
2
  // scheme drawn now) opening a small panel with the System / Light / Dark
3
3
  // switch, as in Vellum's nav bar.
4
4
 
5
- import {
6
- Button,
7
- Popover,
8
- PopoverSurface,
9
- PopoverTrigger,
10
- Tooltip,
11
- makeStyles,
12
- tokens,
13
- } from "@fluentui/react-components";
5
+ import { Button, Popover, PopoverTrigger, Tooltip } from "@fluentui/react-components";
14
6
  import { WeatherMoon20Regular, WeatherSunny20Regular } from "@fluentui/react-icons";
15
7
  import type { ThemeMode } from "@siiway/ui-core";
16
8
  import { useState } from "react";
17
9
  import { useUiScheme, useUiStrings } from "../provider/context.js";
10
+ import { MenuPanel, MenuPanelRow } from "./MenuPanel.js";
18
11
  import { ThemeSwitch } from "./ThemeSwitch.js";
19
12
 
20
- const useStyles = makeStyles({
21
- // PopoverSurface brings a drop-shadow filter (sized for an arrow this
22
- // panel doesn't have) and a transparent border; this has one soft shadow
23
- // and a hairline edge instead, like the rest of the floating surfaces.
24
- panel: {
25
- padding: "4px",
26
- minWidth: "220px",
27
- borderRadius: tokens.borderRadiusLarge,
28
- border: `1px solid ${tokens.colorNeutralStroke2}`,
29
- filter: "none",
30
- boxShadow: tokens.shadow8,
31
- background: tokens.colorNeutralBackground1,
32
- fontSize: "13px",
33
- },
34
- row: {
35
- display: "flex",
36
- alignItems: "center",
37
- justifyContent: "space-between",
38
- gap: "12px",
39
- minHeight: "36px",
40
- padding: "0 4px 0 8px",
41
- color: tokens.colorNeutralForeground2,
42
- },
43
- });
44
-
45
13
  export interface ThemeMenuProps {
46
- /** The selected mode: `system`, `light` or `dark`. The icon shows the provider's `scheme`, not this. */
47
- value: ThemeMode;
48
- /** Called with the picked mode. */
49
- onChange: (mode: ThemeMode) => void;
14
+ /** The selected mode: `system`, `light` or `dark`. Without `value` and
15
+ * `onChange`, the menu shows and changes the choice `<UiProvider>` keeps.
16
+ * The icon shows the provider's `scheme`, not this. */
17
+ value?: ThemeMode;
18
+ /** Called with the picked mode, as in `ThemeSwitch`. */
19
+ onChange?: (mode: ThemeMode) => void;
50
20
  /** The button's name and the label in its panel. Defaults to the library's "Theme". */
51
21
  label?: string;
52
22
  }
53
23
 
54
24
  export function ThemeMenu({ value, onChange, label }: ThemeMenuProps) {
55
- const styles = useStyles();
56
25
  const strings = useUiStrings();
57
26
  const scheme = useUiScheme();
58
27
  const [open, setOpen] = useState(false);
@@ -72,12 +41,11 @@ export function ThemeMenu({ value, onChange, label }: ThemeMenuProps) {
72
41
  />
73
42
  </Tooltip>
74
43
  </PopoverTrigger>
75
- <PopoverSurface className={styles.panel}>
76
- <div className={styles.row}>
77
- <span>{name}</span>
44
+ <MenuPanel>
45
+ <MenuPanelRow label={name}>
78
46
  <ThemeSwitch value={value} onChange={onChange} label={name} />
79
- </div>
80
- </PopoverSurface>
47
+ </MenuPanelRow>
48
+ </MenuPanel>
81
49
  </Popover>
82
50
  );
83
51
  }
@@ -1,18 +1,21 @@
1
1
  // System / Light / Dark switch: the segmented control in Prism's account menu
2
2
  // and Vellum's theme popover. Icons only; each segment is named for screen
3
- // readers and in its tooltip. Where the app keeps the choice, and how it
4
- // resolves "system", is up to the app.
3
+ // readers and in its tooltip. It shows and changes the choice <UiProvider>
4
+ // keeps, or the app's own, given as value and onChange.
5
5
 
6
6
  import { DesktopRegular, WeatherMoonRegular, WeatherSunnyRegular } from "@fluentui/react-icons";
7
7
  import type { ThemeMode } from "@siiway/ui-core";
8
- import { useUiStrings } from "../provider/context.js";
8
+ import { useUiStrings, useUiThemeMode } from "../provider/context.js";
9
9
  import { Segmented } from "./Segmented.js";
10
10
 
11
11
  export interface ThemeSwitchProps {
12
- /** The selected mode: `system`, `light` or `dark`. */
13
- value: ThemeMode;
14
- /** Called with the picked mode. */
15
- onChange: (mode: ThemeMode) => void;
12
+ /** The selected mode: `system`, `light` or `dark`. Without `value` and
13
+ * `onChange`, the switch shows and changes the choice `<UiProvider>`
14
+ * keeps; given either, it's the app's, and the provider's is left alone. */
15
+ value?: ThemeMode;
16
+ /** Called with the picked mode. Without it, the switch is disabled, unless
17
+ * it uses the provider's choice. */
18
+ onChange?: (mode: ThemeMode) => void;
16
19
  /** Names the group for screen readers; defaults to the library's "Theme". */
17
20
  label?: string;
18
21
  /** Disables all three segments. */
@@ -22,12 +25,19 @@ export interface ThemeSwitchProps {
22
25
 
23
26
  export function ThemeSwitch({ value, onChange, label, disabled, className }: ThemeSwitchProps) {
24
27
  const strings = useUiStrings();
28
+ const kept = useUiThemeMode();
29
+ // The app's choice, given either prop, or else the provider's: never half
30
+ // of each, or picking would change one thing and show another.
31
+ const own = value !== undefined || onChange !== undefined;
32
+ const mode = own ? value : kept?.mode;
33
+ const change = own ? onChange : kept?.setMode;
25
34
  return (
26
35
  <Segmented<ThemeMode>
27
36
  label={label ?? strings.theme}
28
- value={value}
29
- onChange={onChange}
30
- disabled={disabled}
37
+ // With no choice to show, none is selected.
38
+ value={mode ?? null}
39
+ onChange={change ?? (() => {})}
40
+ disabled={disabled || !change}
31
41
  className={className}
32
42
  options={[
33
43
  { value: "system", label: strings.themeSystem, content: <DesktopRegular /> },
@@ -4,8 +4,8 @@
4
4
  // a grey (not brand) selection.
5
5
  //
6
6
  // On a narrow screen it gives way in steps: the menu button that opens the
7
- // app's navigation shows from 960px down, the links go at 720px, the crumb at
8
- // 540px.
7
+ // app's navigation shows from 960px (or 768px) down, at the end or the start,
8
+ // the links go at 720px, the crumb at 540px.
9
9
 
10
10
  import {
11
11
  Button,
@@ -82,11 +82,11 @@ const useStyles = makeStyles({
82
82
  "@media (max-width: 540px)": { display: "none" },
83
83
  },
84
84
  // A wrapper, because Fluent's Button sets its own display at the same
85
- // specificity as a class on it.
86
- menuButton: {
87
- display: "none",
88
- "@media (max-width: 960px)": { display: "inline-flex" },
89
- },
85
+ // specificity as a class on it. Griffel's media queries are static, so the
86
+ // widths it shows from are presets.
87
+ menuButton: { display: "none" },
88
+ menuBelow960: { "@media (max-width: 960px)": { display: "inline-flex" } },
89
+ menuBelow768: { "@media (max-width: 768px)": { display: "inline-flex" } },
90
90
  links: {
91
91
  display: "flex",
92
92
  alignItems: "center",
@@ -160,8 +160,12 @@ export interface TopBarProps {
160
160
  * makes a menu, active when one of its items is. Hidden from 720px down.
161
161
  */
162
162
  links?: TopBarLink[];
163
- /** Shows a menu button from 960px down that calls this, to open the app's navigation. */
163
+ /** Shows a menu button from `menuBelow` down that calls this, to open the app's navigation. */
164
164
  onMenu?: () => void;
165
+ /** Where the menu button sits: `"end"`, before the actions (the default), or `"start"`, before the brand. */
166
+ menuPosition?: "start" | "end";
167
+ /** The width the menu button shows from, and down: `960` (the default) or `768`, to match where the app's navigation becomes a drawer. */
168
+ menuBelow?: 768 | 960;
165
169
  /** The menu button's name. Defaults to the library's "Open navigation". */
166
170
  menuLabel?: string;
167
171
  /** Names the links for screen readers. Defaults to the library's "Primary". */
@@ -180,6 +184,8 @@ export function TopBar({
180
184
  crumb,
181
185
  links,
182
186
  onMenu,
187
+ menuPosition = "end",
188
+ menuBelow = 960,
183
189
  menuLabel,
184
190
  linksLabel,
185
191
  sticky,
@@ -189,8 +195,24 @@ export function TopBar({
189
195
  const styles = useStyles();
190
196
  const canvas = useCanvasClass();
191
197
  const strings = useUiStrings();
198
+ const menu = onMenu && (
199
+ <span
200
+ className={mergeClasses(
201
+ styles.menuButton,
202
+ menuBelow === 768 ? styles.menuBelow768 : styles.menuBelow960,
203
+ )}
204
+ >
205
+ <Button
206
+ appearance="subtle"
207
+ icon={<Navigation20Regular />}
208
+ aria-label={menuLabel ?? strings.openNavigation}
209
+ onClick={onMenu}
210
+ />
211
+ </span>
212
+ );
192
213
  return (
193
214
  <header className={mergeClasses(styles.root, canvas, sticky && styles.sticky, className)}>
215
+ {menuPosition === "start" && menu}
194
216
  <div className={styles.brandGroup}>
195
217
  <UiLink to={brand.to} className={styles.brand}>
196
218
  <BrandMark iconUrl={brand.logo} size={24} />
@@ -217,16 +239,7 @@ export function TopBar({
217
239
  )}
218
240
  <div className={styles.spacer} />
219
241
  <div className={styles.actions}>
220
- {onMenu && (
221
- <span className={styles.menuButton}>
222
- <Button
223
- appearance="subtle"
224
- icon={<Navigation20Regular />}
225
- aria-label={menuLabel ?? strings.openNavigation}
226
- onClick={onMenu}
227
- />
228
- </span>
229
- )}
242
+ {menuPosition !== "start" && menu}
230
243
  {children}
231
244
  </div>
232
245
  </header>
@@ -1,20 +1,39 @@
1
- import { useState } from "react";
1
+ import { useEffect, useRef, useState } from "react";
2
2
 
3
3
  export type CopyState = "idle" | "copied" | "failed";
4
4
 
5
5
  /** Copy text to the clipboard and report how it went for a moment, so the
6
6
  * control can say "Copied" — or "Copy failed" when clipboard access is
7
- * denied (permission, insecure context), rather than claiming success. */
7
+ * denied (permission, insecure context), rather than claiming success.
8
+ * `copy` takes the text, or a function that returns it, called as it copies:
9
+ * for a value only known then, such as what a code block shows. It resolves
10
+ * to how that copy went. */
8
11
  export function useCopy(resetMs = 1500) {
9
12
  const [state, setState] = useState<CopyState>("idle");
10
- const copy = (text: string) =>
11
- navigator.clipboard.writeText(text).then(
12
- () => flash("copied"),
13
- () => flash("failed"),
14
- );
13
+ const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
14
+ // The latest copy: only it reports, so an earlier one that settles later
15
+ // can't replace its outcome.
16
+ const latest = useRef(0);
17
+ useEffect(() => () => clearTimeout(timer.current), []);
15
18
  function flash(next: CopyState) {
19
+ // A second copy starts its own moment, not the rest of the first.
20
+ clearTimeout(timer.current);
16
21
  setState(next);
17
- setTimeout(() => setState("idle"), resetMs);
22
+ timer.current = setTimeout(() => setState("idle"), resetMs);
23
+ }
24
+ // Up to the first await, this runs in the click that called it, as Safari
25
+ // needs. No clipboard (a plain-http page) or a throwing `text` is a failure.
26
+ async function copy(text: string | (() => string)): Promise<"copied" | "failed"> {
27
+ const id = ++latest.current;
28
+ let outcome: "copied" | "failed";
29
+ try {
30
+ await navigator.clipboard.writeText(typeof text === "function" ? text() : text);
31
+ outcome = "copied";
32
+ } catch {
33
+ outcome = "failed";
34
+ }
35
+ if (id === latest.current) flash(outcome);
36
+ return outcome;
18
37
  }
19
38
  return { state, copy };
20
39
  }
@@ -2,25 +2,31 @@
2
2
  // kept in localStorage under the key schemeBootScript reads, and follows
3
3
  // other tabs; `system` follows the OS setting as it changes.
4
4
  //
5
- // On the server, and while hydrating, it gives `defaultMode` drawn light: the
6
- // server can't see the stored choice or the OS. The client then moves to the
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
7
9
  // real scheme. useSyncExternalStore makes that switch without a hydration
8
- // mismatch.
10
+ // mismatch. With `cookie`, it keeps the scheme cookie in step for the next
11
+ // request.
9
12
 
10
13
  import {
11
14
  THEME_STORAGE_KEY,
12
15
  isThemeMode,
13
16
  resolveScheme,
17
+ schemeCookie,
14
18
  type Scheme,
15
19
  type ThemeMode,
16
20
  } from "@siiway/ui-core";
17
- import { useCallback, useSyncExternalStore } from "react";
21
+ import { useCallback, useEffect, useMemo, useSyncExternalStore } from "react";
18
22
 
19
23
  const DARK_QUERY = "(prefers-color-scheme: dark)";
20
24
 
21
25
  // setMode in this tab notifies these; other tabs arrive as `storage` events.
22
26
  const listeners = new Set<() => void>();
23
- // Where the choice lives when the browser blocks localStorage.
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.
24
30
  const memory = new Map<string, string>();
25
31
 
26
32
  function subscribeMode(onChange: () => void) {
@@ -43,13 +49,22 @@ function systemDark() {
43
49
  }
44
50
 
45
51
  function readStored(key: string): string | null {
52
+ const own = memory.get(key);
53
+ if (own !== undefined) return own;
46
54
  try {
47
55
  return window.localStorage.getItem(key);
48
56
  } catch {
49
- return memory.get(key) ?? null;
57
+ return null;
50
58
  }
51
59
  }
52
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
+
53
68
  function serverStored() {
54
69
  return null;
55
70
  }
@@ -58,11 +73,45 @@ function serverDark() {
58
73
  return false;
59
74
  }
60
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
+
61
98
  export interface ThemeModeOptions {
62
99
  /** The `localStorage` key; give `schemeBootScript` the same. Defaults to `"siiway-theme"`. */
63
100
  storageKey?: string;
64
101
  /** Until a choice is stored, and on the server. Defaults to `"system"`. */
65
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;
66
115
  }
67
116
 
68
117
  export interface ThemeModeState {
@@ -77,22 +126,43 @@ export interface ThemeModeState {
77
126
  export function useThemeMode({
78
127
  storageKey = THEME_STORAGE_KEY,
79
128
  defaultMode = "system",
129
+ cookie,
130
+ initialScheme,
80
131
  }: ThemeModeOptions = {}): ThemeModeState {
81
132
  const stored = useSyncExternalStore(subscribeMode, () => readStored(storageKey), serverStored);
82
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
+ );
83
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]);
84
150
 
85
151
  const setMode = useCallback(
86
152
  (next: ThemeMode) => {
87
153
  try {
88
154
  window.localStorage.setItem(storageKey, next);
155
+ memory.delete(storageKey);
89
156
  } catch {
90
157
  memory.set(storageKey, next);
91
158
  }
159
+ // At once, not in the effect: a page load right after must carry it.
160
+ if (cookie) writeCookie(cookie, currentScheme(storageKey, defaultMode));
92
161
  for (const listener of listeners) listener();
93
162
  },
94
- [storageKey],
163
+ [storageKey, cookie, defaultMode],
95
164
  );
96
165
 
97
- return { mode, setMode, scheme: resolveScheme(mode, dark) };
166
+ // The same object until something changes: UiProvider puts it in context.
167
+ return useMemo(() => ({ mode, setMode, scheme }), [mode, setMode, scheme]);
98
168
  }
package/src/index.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  // Theme and provider
2
2
  export { createTheme, type CreateThemeOptions } from "./theme/createTheme.js";
3
3
  export { UiProvider, type UiProviderProps } from "./provider/UiProvider.js";
4
+ export { useCanvasClass } from "./provider/canvas.js";
4
5
  export {
5
6
  DEFAULT_ROUTER,
6
7
  UiLink,
8
+ useUiMatch,
7
9
  useUiNavigate,
8
10
  useUiPageTitle,
9
11
  useUiScheme,
12
+ useUiThemeMode,
10
13
  useUiStrings,
11
14
  type PageTitleHandler,
12
15
  type UiLinkProps,
@@ -31,6 +34,8 @@ export {
31
34
  type StatusPageAction,
32
35
  type StatusPageProps,
33
36
  } from "./components/StatusPage.js";
37
+ export { LoadingPage } from "./components/LoadingPage.js";
38
+ export { AuthShell, type AuthShellProps } from "./components/AuthShell.js";
34
39
  export { EmptyState } from "./components/EmptyState.js";
35
40
  export { GetStartedPanel } from "./components/GetStartedPanel.js";
36
41
  export { ViewBanner } from "./components/ViewBanner.js";
@@ -54,6 +59,7 @@ export {
54
59
  export { Outline, type OutlineItem, type OutlineProps } from "./components/Outline.js";
55
60
  export { PrevNext, type PrevNextLink, type PrevNextProps } from "./components/PrevNext.js";
56
61
  export { LoadingBar } from "./components/LoadingBar.js";
62
+ export { AppShell, AppShellClose, type AppShellProps } from "./components/AppShell.js";
57
63
 
58
64
  // Search
59
65
  export { SearchField, type SearchFieldProps } from "./components/SearchField.js";
@@ -116,6 +122,13 @@ export {
116
122
  export { Segmented, type SegmentedOption } from "./components/Segmented.js";
117
123
  export { ThemeSwitch, type ThemeSwitchProps } from "./components/ThemeSwitch.js";
118
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";
119
132
  export { Select } from "./components/Select.js";
120
133
  export { CheckboxField } from "./components/CheckboxField.js";
121
134
  export { PasswordInput } from "./components/PasswordInput.js";
@@ -123,16 +136,19 @@ export { DurationInput } from "./components/DurationInput.js";
123
136
  export { ColorPickerInput } from "./components/ColorPickerInput.js";
124
137
 
125
138
  // Copy
126
- export { CopyButton } from "./components/CopyButton.js";
139
+ export { CopyButton, type CopyButtonProps } from "./components/CopyButton.js";
127
140
  export { CopyIdText, CopyIdTrigger } from "./components/CopyIdTrigger.js";
128
141
 
129
142
  // Re-exported from @siiway/ui-core for convenience
130
143
  export {
131
144
  CANVAS,
132
145
  DEFAULT_ACCENT,
146
+ SCHEME_COOKIE,
133
147
  THEME_STORAGE_KEY,
134
148
  resolveScheme,
135
149
  schemeBootScript,
150
+ schemeCookie,
151
+ schemeFromRequest,
136
152
  type Scheme,
137
153
  type ThemeMode,
138
154
  type UiStrings,