@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,24 +1,55 @@
1
1
  import {
2
2
  FluentProvider,
3
+ createCSSRuleFromTheme,
3
4
  makeStaticStyles,
5
+ mergeClasses,
6
+ tokens,
4
7
  useIsomorphicLayoutEffect,
5
8
  type FluentProviderProps,
6
9
  } from "@fluentui/react-components";
7
- import { CANVAS, pickStrings, type Scheme, type UiStrings } from "@siiway/ui-core";
8
- import { useMemo } from "react";
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";
9
13
  import { createTheme } from "../theme/createTheme.js";
10
- import { DEFAULT_ROUTER, UiContext, type PageTitleHandler, type UiRouter } from "./context.js";
14
+ import {
15
+ DEFAULT_ROUTER,
16
+ NO_PROVIDER,
17
+ UiContext,
18
+ type PageTitleHandler,
19
+ type UiRouter,
20
+ } from "./context.js";
11
21
 
12
22
  export interface UiProviderProps extends Omit<FluentProviderProps, "theme"> {
13
- /** The scheme to render in: `useThemeMode().scheme`, or the app's own. */
14
- scheme: Scheme;
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;
15
36
  /**
16
37
  * This provider is the page. It takes the canvas colour, removes the
17
38
  * body's margin, and draws `<html>` in the scheme (`data-scheme`,
18
- * `color-scheme`, the canvas background), as `schemeBootScript` does
19
- * before the app loads. Set it on the app's outermost provider only.
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.
20
43
  */
21
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;
22
53
  /** Brand colour; missing or invalid gives Fluent's blue. */
23
54
  accent?: string | null;
24
55
  /** BCP-47 tag for the library's own strings: `zh*` is Chinese, anything else English. */
@@ -31,30 +62,127 @@ export interface UiProviderProps extends Omit<FluentProviderProps, "theme"> {
31
62
  */
32
63
  router?: UiRouter;
33
64
  /**
34
- * Called with a page's title when a PageHeader mounts, and with null when
35
- * it unmounts, so the app can set the browser tab's title. Pass a stable
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
36
68
  * function.
37
69
  */
38
70
  onPageTitle?: PageTitleHandler;
39
71
  }
40
72
 
41
- const usePageStyles = makeStaticStyles({ body: { margin: "0" } });
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
+ }
42
125
 
43
126
  // <html> in the page's scheme, for what lies outside the provider: the
44
- // overscroll area, the scrollbars, form controls' native parts.
45
- function PageScheme({ scheme }: { scheme: Scheme }) {
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 }) {
46
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;
47
136
  useIsomorphicLayoutEffect(() => {
137
+ if (hold) return;
48
138
  const root = document.documentElement;
139
+ const canvas = CANVAS[scheme];
49
140
  root.setAttribute("data-scheme", scheme);
50
141
  root.style.colorScheme = scheme;
51
- root.style.backgroundColor = CANVAS[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;
52
149
  return () => {
53
150
  root.removeAttribute("data-scheme");
54
151
  root.style.removeProperty("color-scheme");
55
152
  root.style.removeProperty("background-color");
153
+ root.style.removeProperty("--siiway-canvas");
154
+ metas.forEach((meta, i) => (meta.content = colors[i]!));
56
155
  };
57
- }, [scheme]);
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();
58
186
  return null;
59
187
  }
60
188
 
@@ -62,9 +190,28 @@ function PageScheme({ scheme }: { scheme: Scheme }) {
62
190
  * Fluent's provider with the SiiWay theme, plus the library's strings and
63
191
  * router. Wrap the app in it once, where FluentProvider used to be.
64
192
  */
65
- export function UiProvider({
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({
66
210
  scheme,
211
+ kept,
212
+ themeMode: _options,
67
213
  page,
214
+ bothSchemes,
68
215
  accent,
69
216
  locale,
70
217
  strings,
@@ -72,7 +219,8 @@ export function UiProvider({
72
219
  onPageTitle,
73
220
  children,
74
221
  ...rest
75
- }: UiProviderProps) {
222
+ }: UiProviderProps & { scheme: Scheme; kept: ThemeModeState | null }) {
223
+ const both = !!(page && bothSchemes);
76
224
  const theme = useMemo(() => createTheme({ scheme, accent }), [scheme, accent]);
77
225
  const resolved = useMemo(() => pickStrings(locale, strings), [locale, strings]);
78
226
  const value = useMemo(
@@ -81,14 +229,27 @@ export function UiProvider({
81
229
  strings: resolved,
82
230
  router: router ?? DEFAULT_ROUTER,
83
231
  onPageTitle: onPageTitle ?? null,
232
+ themeMode: kept,
233
+ bothSchemes: both,
84
234
  }),
85
- [scheme, resolved, router, onPageTitle],
235
+ [scheme, resolved, router, onPageTitle, kept, both],
86
236
  );
87
- // Fluent paints its provider colorNeutralBackground1; the page is the canvas.
88
- const style = page ? { backgroundColor: CANVAS[scheme], ...rest.style } : rest.style;
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;
89
243
  return (
90
- <FluentProvider theme={theme} {...rest} style={style}>
91
- {page && <PageScheme scheme={scheme} />}
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} />}
92
253
  <UiContext.Provider value={value}>{children}</UiContext.Provider>
93
254
  </FluentProvider>
94
255
  );
@@ -1,17 +1,31 @@
1
- // The page canvas as a class, for the parts of the page drawn on it that must
2
- // be opaque: a sticky top bar, a full-page message. Tokens have no canvas
3
- // (it's darker than colorNeutralBackground1 in dark), so this picks CANVAS by
4
- // the provider's scheme.
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.
5
8
 
6
9
  import { makeStyles } from "@fluentui/react-components";
7
10
  import { CANVAS } from "@siiway/ui-core";
8
- import { useUiScheme } from "./context.js";
11
+ import { useContext } from "react";
12
+ import { UiContext } from "./context.js";
9
13
 
10
14
  const useStyles = makeStyles({
11
15
  light: { backgroundColor: CANVAS.light },
12
16
  dark: { backgroundColor: CANVAS.dark },
17
+ pageLight: { backgroundColor: `var(--siiway-canvas, ${CANVAS.light})` },
18
+ pageDark: { backgroundColor: `var(--siiway-canvas, ${CANVAS.dark})` },
13
19
  });
14
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
+ */
15
26
  export function useCanvasClass(): string {
16
- return useStyles()[useUiScheme()];
27
+ const styles = useStyles();
28
+ const { scheme, bothSchemes } = useContext(UiContext);
29
+ if (!bothSchemes) return styles[scheme];
30
+ return scheme === "dark" ? styles.pageDark : styles.pageLight;
17
31
  }
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  createContext,
3
3
  useContext,
4
+ useEffect,
4
5
  type ComponentType,
5
6
  type MouseEventHandler,
6
7
  type ReactNode,
7
8
  } from "react";
8
9
  import { UI_STRINGS, type Scheme, type UiStrings } from "@siiway/ui-core";
10
+ import type { ThemeModeState } from "../hooks/useThemeMode.js";
9
11
 
10
12
  /** Props the library passes to the app's link component. */
11
13
  export interface UiLinkProps {
@@ -21,13 +23,19 @@ export interface UiLinkProps {
21
23
  }
22
24
 
23
25
  /**
24
- * How library components link and navigate. React Router fits as is:
25
- * `router={{ Link, useNavigate }}`.
26
+ * How library components link, navigate and know the current page. React
27
+ * Router fits as is: `router={{ Link, useNavigate }}`, plus `useMatch`.
26
28
  */
27
29
  export interface UiRouter {
28
30
  Link: ComponentType<UiLinkProps>;
29
31
  /** A hook returning a navigate function. Called during render, like any hook. */
30
32
  useNavigate: () => (to: string) => void;
33
+ /**
34
+ * A hook: whether the reader is on `to`, or on a page below it unless
35
+ * `end`. SideNav asks it for items without `active`. React Router:
36
+ * `(to, end) => useMatch({ path: to, end }) !== null`.
37
+ */
38
+ useMatch?: (to: string, end: boolean) => boolean;
31
39
  }
32
40
 
33
41
  function AnchorLink({ to, state: _state, ...rest }: UiLinkProps) {
@@ -41,7 +49,7 @@ function useLocationNavigate() {
41
49
  /** Plain `<a href>` links and full page loads: for apps without a client router. */
42
50
  export const DEFAULT_ROUTER: UiRouter = { Link: AnchorLink, useNavigate: useLocationNavigate };
43
51
 
44
- /** Receives the page's title from PageHeader, and null when the page goes. */
52
+ /** Receives the page's title from its header, and null when the page goes. */
45
53
  export type PageTitleHandler = (title: string | null) => void;
46
54
 
47
55
  export interface UiContextValue {
@@ -49,14 +57,24 @@ export interface UiContextValue {
49
57
  strings: UiStrings;
50
58
  router: UiRouter;
51
59
  onPageTitle: PageTitleHandler | null;
60
+ /** The person's light/dark choice, when the page's provider keeps it. */
61
+ themeMode: ThemeModeState | null;
62
+ /** Under a page provider with `bothSchemes`: colours follow `<html data-scheme>`. */
63
+ bothSchemes: boolean;
52
64
  }
53
65
 
54
- export const UiContext = createContext<UiContextValue>({
66
+ // What components see outside any provider; UiProvider tells it apart from
67
+ // an outer provider's value by identity.
68
+ export const NO_PROVIDER: UiContextValue = {
55
69
  scheme: "light",
56
70
  strings: UI_STRINGS.en,
57
71
  router: DEFAULT_ROUTER,
58
72
  onPageTitle: null,
59
- });
73
+ themeMode: null,
74
+ bothSchemes: false,
75
+ };
76
+
77
+ export const UiContext = createContext<UiContextValue>(NO_PROVIDER);
60
78
 
61
79
  /** The library's own strings, in the locale given to `<UiProvider>`. */
62
80
  export function useUiStrings(): UiStrings {
@@ -68,6 +86,14 @@ export function useUiScheme(): Scheme {
68
86
  return useContext(UiContext).scheme;
69
87
  }
70
88
 
89
+ /**
90
+ * The person's light/dark choice that `<UiProvider>` keeps when it has no
91
+ * `scheme`: `{ mode, setMode, scheme }`. Null when the app keeps it itself.
92
+ */
93
+ export function useUiThemeMode(): ThemeModeState | null {
94
+ return useContext(UiContext).themeMode;
95
+ }
96
+
71
97
  /** A link through the app's router (React Router's `Link`, or `<a href>`). */
72
98
  export function UiLink(props: UiLinkProps) {
73
99
  const { Link } = useContext(UiContext).router;
@@ -79,7 +105,41 @@ export function useUiNavigate(): (to: string) => void {
79
105
  return useContext(UiContext).router.useNavigate();
80
106
  }
81
107
 
108
+ /**
109
+ * Whether the reader is on `to`, or on a page below it unless `end`, by the
110
+ * router's `useMatch`; false without one. Keep the same router: one that
111
+ * gains or loses `useMatch` changes the hooks this calls.
112
+ */
113
+ export function useUiMatch(to: string, end = false): boolean {
114
+ const { router } = useContext(UiContext);
115
+ return router.useMatch ? router.useMatch(to, end) : false;
116
+ }
117
+
82
118
  /** The app's page-title handler, if it gave `<UiProvider onPageTitle>`. */
83
119
  export function useUiPageTitle(): PageTitleHandler | null {
84
120
  return useContext(UiContext).onPageTitle;
85
121
  }
122
+
123
+ // The header each handler last heard from. Only it clears the title, so a
124
+ // page that leaves after the next one came (in a route transition) doesn't
125
+ // clear the next one's.
126
+ const titleOwners = new WeakMap<PageTitleHandler, object>();
127
+
128
+ // Names the browser tab after a page header ("Security · Prism"): `tabTitle`,
129
+ // or the title when it's plain text; a rich or empty title (a name still
130
+ // loading, say) leaves the site name.
131
+ export function useTabTitle(title: ReactNode, tabTitle?: string) {
132
+ const onPageTitle = useUiPageTitle();
133
+ const text = tabTitle ?? (typeof title === "string" ? title : null);
134
+ useEffect(() => {
135
+ if (!text || !onPageTitle) return;
136
+ const owner = {};
137
+ titleOwners.set(onPageTitle, owner);
138
+ onPageTitle(text);
139
+ return () => {
140
+ if (titleOwners.get(onPageTitle) !== owner) return;
141
+ titleOwners.delete(onPageTitle);
142
+ onPageTitle(null);
143
+ };
144
+ }, [text, onPageTitle]);
145
+ }