@surfaces-ui/components 0.3.0 → 0.4.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/dist/styles.css +41 -2
- package/dist/styles.layer.css +41 -2
- package/dist/theme/ThemeProvider.js +26 -15
- package/dist/theme/ThemeProvider.js.map +1 -1
- package/package.json +6 -6
package/dist/styles.css
CHANGED
|
@@ -4346,6 +4346,11 @@ select.sf-input:disabled {
|
|
|
4346
4346
|
.sf-a2-grid[data-widths] {
|
|
4347
4347
|
grid-template-columns: minmax(0, 1fr);
|
|
4348
4348
|
}
|
|
4349
|
+
|
|
4350
|
+
/* One column: a column's width handle has nothing beside it (see the narrow container rules). */
|
|
4351
|
+
.sf-a2-grid > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"] {
|
|
4352
|
+
display: none;
|
|
4353
|
+
}
|
|
4349
4354
|
}
|
|
4350
4355
|
|
|
4351
4356
|
/* Page studio */
|
|
@@ -5421,6 +5426,13 @@ select.sf-input:disabled {
|
|
|
5421
5426
|
flex: 1 1 100% !important;
|
|
5422
5427
|
}
|
|
5423
5428
|
|
|
5429
|
+
/* Nor the handles that set them: with nothing beside a child, its handle would stand past the page's right edge. */
|
|
5430
|
+
.sf-a2-grid > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"],
|
|
5431
|
+
.sf-a2-row:where(:not([data-wrap="false"])) > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"],
|
|
5432
|
+
.sf-a2-row:where(:not([data-wrap="false"])) > .sf-a2-row-cell > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"] {
|
|
5433
|
+
display: none;
|
|
5434
|
+
}
|
|
5435
|
+
|
|
5424
5436
|
.sf-plan-review-step {
|
|
5425
5437
|
grid-template-columns: minmax(0, 1fr);
|
|
5426
5438
|
}
|
|
@@ -6326,12 +6338,17 @@ select.sf-input:disabled {
|
|
|
6326
6338
|
flex: 0 0 auto !important;
|
|
6327
6339
|
}
|
|
6328
6340
|
|
|
6329
|
-
/* Stacked, a panel is as tall as its content
|
|
6341
|
+
/* Stacked, a panel is as tall as its content and the page scrolls past it: a box that scrolls inside a page that
|
|
6342
|
+
scrolls catches a phone's swipe, and its scroll bars took room the content needed. */
|
|
6330
6343
|
.sf-workspace-panel {
|
|
6331
|
-
max-height: min(var(--sf-workspace-height, 32rem), 36rem);
|
|
6332
6344
|
margin-bottom: var(--sf-space-2);
|
|
6333
6345
|
}
|
|
6334
6346
|
|
|
6347
|
+
.sf-workspace-panel:not([data-fill]),
|
|
6348
|
+
.sf-workspace-panel:not([data-fill]) > .sf-workspace-panel-body {
|
|
6349
|
+
overflow: visible;
|
|
6350
|
+
}
|
|
6351
|
+
|
|
6335
6352
|
/* Content that scrolls inside itself has no height of its own to grow to, so a panel that fills is about a screenful
|
|
6336
6353
|
below the host's sticky bar: scrolled to, the whole conversation and its composer are in view. */
|
|
6337
6354
|
.sf-workspace-panel[data-fill]:not([data-folded]) {
|
|
@@ -8463,6 +8480,8 @@ button > .sf-item-control-icon:only-child {
|
|
|
8463
8480
|
/* Beside a component in a stack: the two will share a row, so the line is on that edge of it. */
|
|
8464
8481
|
.sf-arrange-insert[data-side="left"] { left: -1px; }
|
|
8465
8482
|
.sf-arrange-insert[data-side="right"] { right: -1px; }
|
|
8483
|
+
/* Down the whole page's edge: a column beside everything, drawn wider than the line beside one component. */
|
|
8484
|
+
.sf-arrange-insert[data-page] { width: var(--sf-space-1); }
|
|
8466
8485
|
|
|
8467
8486
|
.sf-arrange-frame[data-receiving],
|
|
8468
8487
|
.sf-arrange-root[data-receiving] {
|
|
@@ -8601,6 +8620,26 @@ button > .sf-item-control-icon:only-child {
|
|
|
8601
8620
|
color: var(--sf-color-text);
|
|
8602
8621
|
}
|
|
8603
8622
|
|
|
8623
|
+
/*
|
|
8624
|
+
* A container that holds something draws its strip over its own foot rather than in the page's flow, so the page
|
|
8625
|
+
* never moves under the pointer when a drag starts. Only an empty container's strip takes room: it needs it.
|
|
8626
|
+
*/
|
|
8627
|
+
.sf-arrange-inside[data-overlay] {
|
|
8628
|
+
position: absolute;
|
|
8629
|
+
inset-inline: var(--sf-space-1);
|
|
8630
|
+
bottom: var(--sf-space-1);
|
|
8631
|
+
z-index: 2;
|
|
8632
|
+
margin: 0;
|
|
8633
|
+
padding: var(--sf-space-1);
|
|
8634
|
+
background: var(--sf-color-surface-raised);
|
|
8635
|
+
font-size: var(--sf-font-size-xs);
|
|
8636
|
+
}
|
|
8637
|
+
.sf-arrange-root:has(> .sf-arrange-inside[data-overlay]) { position: relative; }
|
|
8638
|
+
/* Strips over a foot would cover each other where containers nest, so only the innermost one under the pointer shows
|
|
8639
|
+
its own. Dropping after a container's last child lands in the same place, so nothing is lost where hover is not. */
|
|
8640
|
+
.sf-arrange-inside[data-overlay]:not([data-over]) { visibility: hidden; }
|
|
8641
|
+
:is(.sf-arrange-frame, .sf-arrange-root):hover:not(:has(:is(.sf-arrange-frame, .sf-arrange-root):hover)) > .sf-arrange-inside[data-overlay] { visibility: visible; }
|
|
8642
|
+
|
|
8604
8643
|
/* Arrange's handles sit in the gap between two frames of a Grid or Row, not on the frame's own edge. */
|
|
8605
8644
|
.sf-arrange-frame > .sf-resize-handle[data-edge="right"],
|
|
8606
8645
|
.sf-arrange-view > .sf-resize-handle[data-edge="right"] { --sf-resize-offset: calc(var(--sf-arrange-gap, var(--sf-space-4)) / 2); }
|
package/dist/styles.layer.css
CHANGED
|
@@ -4348,6 +4348,11 @@ select.sf-input:disabled {
|
|
|
4348
4348
|
.sf-a2-grid[data-widths] {
|
|
4349
4349
|
grid-template-columns: minmax(0, 1fr);
|
|
4350
4350
|
}
|
|
4351
|
+
|
|
4352
|
+
/* One column: a column's width handle has nothing beside it (see the narrow container rules). */
|
|
4353
|
+
.sf-a2-grid > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"] {
|
|
4354
|
+
display: none;
|
|
4355
|
+
}
|
|
4351
4356
|
}
|
|
4352
4357
|
|
|
4353
4358
|
/* Page studio */
|
|
@@ -5423,6 +5428,13 @@ select.sf-input:disabled {
|
|
|
5423
5428
|
flex: 1 1 100% !important;
|
|
5424
5429
|
}
|
|
5425
5430
|
|
|
5431
|
+
/* Nor the handles that set them: with nothing beside a child, its handle would stand past the page's right edge. */
|
|
5432
|
+
.sf-a2-grid > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"],
|
|
5433
|
+
.sf-a2-row:where(:not([data-wrap="false"])) > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"],
|
|
5434
|
+
.sf-a2-row:where(:not([data-wrap="false"])) > .sf-a2-row-cell > :is(.sf-arrange-frame, .sf-arrange-view) > .sf-resize-handle[data-edge="right"] {
|
|
5435
|
+
display: none;
|
|
5436
|
+
}
|
|
5437
|
+
|
|
5426
5438
|
.sf-plan-review-step {
|
|
5427
5439
|
grid-template-columns: minmax(0, 1fr);
|
|
5428
5440
|
}
|
|
@@ -6328,12 +6340,17 @@ select.sf-input:disabled {
|
|
|
6328
6340
|
flex: 0 0 auto !important;
|
|
6329
6341
|
}
|
|
6330
6342
|
|
|
6331
|
-
/* Stacked, a panel is as tall as its content
|
|
6343
|
+
/* Stacked, a panel is as tall as its content and the page scrolls past it: a box that scrolls inside a page that
|
|
6344
|
+
scrolls catches a phone's swipe, and its scroll bars took room the content needed. */
|
|
6332
6345
|
.sf-workspace-panel {
|
|
6333
|
-
max-height: min(var(--sf-workspace-height, 32rem), 36rem);
|
|
6334
6346
|
margin-bottom: var(--sf-space-2);
|
|
6335
6347
|
}
|
|
6336
6348
|
|
|
6349
|
+
.sf-workspace-panel:not([data-fill]),
|
|
6350
|
+
.sf-workspace-panel:not([data-fill]) > .sf-workspace-panel-body {
|
|
6351
|
+
overflow: visible;
|
|
6352
|
+
}
|
|
6353
|
+
|
|
6337
6354
|
/* Content that scrolls inside itself has no height of its own to grow to, so a panel that fills is about a screenful
|
|
6338
6355
|
below the host's sticky bar: scrolled to, the whole conversation and its composer are in view. */
|
|
6339
6356
|
.sf-workspace-panel[data-fill]:not([data-folded]) {
|
|
@@ -8465,6 +8482,8 @@ button > .sf-item-control-icon:only-child {
|
|
|
8465
8482
|
/* Beside a component in a stack: the two will share a row, so the line is on that edge of it. */
|
|
8466
8483
|
.sf-arrange-insert[data-side="left"] { left: -1px; }
|
|
8467
8484
|
.sf-arrange-insert[data-side="right"] { right: -1px; }
|
|
8485
|
+
/* Down the whole page's edge: a column beside everything, drawn wider than the line beside one component. */
|
|
8486
|
+
.sf-arrange-insert[data-page] { width: var(--sf-space-1); }
|
|
8468
8487
|
|
|
8469
8488
|
.sf-arrange-frame[data-receiving],
|
|
8470
8489
|
.sf-arrange-root[data-receiving] {
|
|
@@ -8603,6 +8622,26 @@ button > .sf-item-control-icon:only-child {
|
|
|
8603
8622
|
color: var(--sf-color-text);
|
|
8604
8623
|
}
|
|
8605
8624
|
|
|
8625
|
+
/*
|
|
8626
|
+
* A container that holds something draws its strip over its own foot rather than in the page's flow, so the page
|
|
8627
|
+
* never moves under the pointer when a drag starts. Only an empty container's strip takes room: it needs it.
|
|
8628
|
+
*/
|
|
8629
|
+
.sf-arrange-inside[data-overlay] {
|
|
8630
|
+
position: absolute;
|
|
8631
|
+
inset-inline: var(--sf-space-1);
|
|
8632
|
+
bottom: var(--sf-space-1);
|
|
8633
|
+
z-index: 2;
|
|
8634
|
+
margin: 0;
|
|
8635
|
+
padding: var(--sf-space-1);
|
|
8636
|
+
background: var(--sf-color-surface-raised);
|
|
8637
|
+
font-size: var(--sf-font-size-xs);
|
|
8638
|
+
}
|
|
8639
|
+
.sf-arrange-root:has(> .sf-arrange-inside[data-overlay]) { position: relative; }
|
|
8640
|
+
/* Strips over a foot would cover each other where containers nest, so only the innermost one under the pointer shows
|
|
8641
|
+
its own. Dropping after a container's last child lands in the same place, so nothing is lost where hover is not. */
|
|
8642
|
+
.sf-arrange-inside[data-overlay]:not([data-over]) { visibility: hidden; }
|
|
8643
|
+
:is(.sf-arrange-frame, .sf-arrange-root):hover:not(:has(:is(.sf-arrange-frame, .sf-arrange-root):hover)) > .sf-arrange-inside[data-overlay] { visibility: visible; }
|
|
8644
|
+
|
|
8606
8645
|
/* Arrange's handles sit in the gap between two frames of a Grid or Row, not on the frame's own edge. */
|
|
8607
8646
|
.sf-arrange-frame > .sf-resize-handle[data-edge="right"],
|
|
8608
8647
|
.sf-arrange-view > .sf-resize-handle[data-edge="right"] { --sf-resize-offset: calc(var(--sf-arrange-gap, var(--sf-space-4)) / 2); }
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { MODES, defaultTheme, parseTheme, themeCss, } from "@surfaces-ui/tokens";
|
|
3
|
-
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, } from "react";
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, useSyncExternalStore, } from "react";
|
|
4
4
|
import { browserStorage } from "@surfaces-ui/data";
|
|
5
5
|
/**
|
|
6
6
|
* Reads a saved theme choice. Saved text can be edited by anyone with access
|
|
@@ -22,18 +22,22 @@ export function parseThemeState(text) {
|
|
|
22
22
|
}
|
|
23
23
|
const ThemeContext = createContext(undefined);
|
|
24
24
|
const NO_THEMES = [];
|
|
25
|
+
const DARK_QUERY = "(prefers-color-scheme: dark)";
|
|
26
|
+
function subscribeSystemDark(onChange) {
|
|
27
|
+
const media = globalThis.matchMedia?.(DARK_QUERY);
|
|
28
|
+
if (!media)
|
|
29
|
+
return () => { };
|
|
30
|
+
media.addEventListener("change", onChange);
|
|
31
|
+
return () => media.removeEventListener("change", onChange);
|
|
32
|
+
}
|
|
33
|
+
const systemDarkNow = () => globalThis.matchMedia?.(DARK_QUERY).matches ?? false;
|
|
34
|
+
/**
|
|
35
|
+
* Whether the system asks for dark. A server cannot know, so it renders light, and the page hydrates as light
|
|
36
|
+
* before it reads the system's choice: the HTML a server wrote and the first render in the browser always agree.
|
|
37
|
+
* The theme's CSS already follows `prefers-color-scheme` on its own, so the page is never shown in the wrong mode.
|
|
38
|
+
*/
|
|
25
39
|
function useSystemDark() {
|
|
26
|
-
|
|
27
|
-
const [dark, setDark] = useState(() => globalThis.matchMedia?.(query).matches ?? false);
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
const media = globalThis.matchMedia?.(query);
|
|
30
|
-
if (!media)
|
|
31
|
-
return;
|
|
32
|
-
const onChange = () => setDark(media.matches);
|
|
33
|
-
media.addEventListener("change", onChange);
|
|
34
|
-
return () => media.removeEventListener("change", onChange);
|
|
35
|
-
}, []);
|
|
36
|
-
return dark;
|
|
40
|
+
return useSyncExternalStore(subscribeSystemDark, systemDarkNow, () => false);
|
|
37
41
|
}
|
|
38
42
|
export function ThemeProvider({ children, initialTheme = defaultTheme, initialMode = "system", storage, storageKey = "surfaces.theme", scope = "root", themes: given, }) {
|
|
39
43
|
const outer = useContext(ThemeContext);
|
|
@@ -62,6 +66,10 @@ export function ThemeProvider({ children, initialTheme = defaultTheme, initialMo
|
|
|
62
66
|
const update = useCallback((patch) => {
|
|
63
67
|
chosen.current = true;
|
|
64
68
|
setState((prev) => {
|
|
69
|
+
// Asking for what is already set changes nothing, so a host that keeps the mode in step with its own
|
|
70
|
+
// toggle from an effect does not render again, and again.
|
|
71
|
+
if (Object.entries(patch).every(([key, value]) => prev[key] === value))
|
|
72
|
+
return prev;
|
|
65
73
|
const next = { ...prev, ...patch };
|
|
66
74
|
if (resolvedStorage)
|
|
67
75
|
void Promise.resolve(resolvedStorage.save(storageKey, JSON.stringify(next))).catch(() => { });
|
|
@@ -89,13 +97,16 @@ export function ThemeProvider({ children, initialTheme = defaultTheme, initialMo
|
|
|
89
97
|
else
|
|
90
98
|
root.removeAttribute("data-contrast");
|
|
91
99
|
}, [scope, state.theme.contrast]);
|
|
100
|
+
// The same functions on every render, so a host can list them in an effect's dependencies.
|
|
101
|
+
const setTheme = useCallback((theme) => update({ theme }), [update]);
|
|
102
|
+
const setMode = useCallback((mode) => update({ mode }), [update]);
|
|
92
103
|
const value = useMemo(() => ({
|
|
93
104
|
...state,
|
|
94
|
-
setTheme
|
|
95
|
-
setMode
|
|
105
|
+
setTheme,
|
|
106
|
+
setMode,
|
|
96
107
|
resolvedMode: state.mode === "system" ? (systemDark ? "dark" : "light") : state.mode,
|
|
97
108
|
themes,
|
|
98
|
-
}), [state,
|
|
109
|
+
}), [state, setTheme, setMode, systemDark, themes]);
|
|
99
110
|
return (_jsxs(ThemeContext.Provider, { value: value, children: [_jsx("style", { "data-sf-theme": scope === "root" ? "root" : scopeId, children: css }), scope === "root" ? (children) : (_jsx("div", { className: "sf-scope", "data-sf-scope": scopeId, "data-theme": state.mode === "system" ? undefined : state.mode, children: children }))] }));
|
|
100
111
|
}
|
|
101
112
|
/** Like `useTheme`, but returns undefined outside a ThemeProvider instead of throwing. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ThemeProvider.js","sourceRoot":"","sources":["../../src/theme/ThemeProvider.tsx"],"names":[],"mappings":";AAAA,OAAO,EAGL,KAAK,EACL,YAAY,EACZ,UAAU,EACV,QAAQ,GACT,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,KAAK,EACL,OAAO,EACP,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAC;AACf,OAAO,EAAqB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAQtE;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,IAA+B;IAC7D,IAAI,CAAC,IAAI;QAAE,OAAO,SAAS,CAAC;IAC5B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAwC,CAAC;QACvE,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;YAAE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C,IAAI,YAE3D,QAAQ,GACL,CACP,IACqB,CACzB,CAAC;AACJ,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,gBAAgB;IAC9B,OAAO,UAAU,CAAC,YAAY,CAAC,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,QAAQ;IACtB,MAAM,KAAK,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IACvC,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAC5E,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n type Mode,\n type Theme,\n MODES,\n defaultTheme,\n parseTheme,\n themeCss,\n} from \"@surfaces-ui/tokens\";\nimport {\n type ReactNode,\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport { type StateStorage, browserStorage } from \"@surfaces-ui/data\";\n\n/** What a person has chosen: the theme document and the light/dark mode. */\nexport interface ThemeState {\n theme: Theme;\n mode: Mode;\n}\n\n/**\n * Reads a saved theme choice. Saved text can be edited by anyone with access\n * to the storage, so the theme goes through `parseTheme` and an unknown mode\n * falls back to \"system\"; unreadable text is ignored.\n */\nexport function parseThemeState(text: string | null | undefined): ThemeState | undefined {\n if (!text) return undefined;\n try {\n const parsed = JSON.parse(text) as { theme?: unknown; mode?: unknown };\n if (!parsed || typeof parsed !== \"object\") return undefined;\n return { theme: parseTheme(parsed.theme, defaultTheme), mode: MODES.includes(parsed.mode as Mode) ? (parsed.mode as Mode) : \"system\" };\n } catch {\n return undefined;\n }\n}\n\nexport interface ThemeContextValue extends ThemeState {\n setTheme(theme: Theme): void;\n setMode(mode: Mode): void;\n /** The mode actually showing, with \"system\" resolved. */\n resolvedMode: \"light\" | \"dark\";\n /** The host's own themes, chosen by name beside the built-in presets (by a page, an agent's surface or `ThemePicker`). */\n themes: readonly Theme[];\n}\n\nconst ThemeContext = createContext<ThemeContextValue | undefined>(undefined);\n\nexport interface ThemeProviderProps {\n children: ReactNode;\n /** Theme used until the saved choice loads, and when it has nothing saved. */\n initialTheme?: Theme;\n initialMode?: Mode;\n /**\n * Where the choice is kept: the browser (the default), a host's bridge or a\n * server, through the same `StateStorage` every part of the library takes.\n * Pass `null` to keep the choice in memory only.\n */\n storage?: StateStorage | null;\n /** The key the choice is kept under. Default \"surfaces.theme\". */\n storageKey?: string;\n /**\n * \"root\" (default) themes the whole document. \"scoped\" themes only this\n * provider's subtree, so several themes can share a page.\n */\n scope?: \"root\" | \"scoped\";\n /**\n * The host's own themes (a company's brand themes, say), offered by name\n * beside the built-in presets: a page's or a surface's `theme` request can\n * choose one, and `ThemePicker` lists them. Pass them through `parseTheme`\n * first. A provider inside another keeps the outer one's unless given its own.\n */\n themes?: readonly Theme[];\n}\n\nconst NO_THEMES: readonly Theme[] = [];\n\nfunction useSystemDark(): boolean {\n const query = \"(prefers-color-scheme: dark)\";\n const [dark, setDark] = useState(() => globalThis.matchMedia?.(query).matches ?? false);\n useEffect(() => {\n const media = globalThis.matchMedia?.(query);\n if (!media) return;\n const onChange = () => setDark(media.matches);\n media.addEventListener(\"change\", onChange);\n return () => media.removeEventListener(\"change\", onChange);\n }, []);\n return dark;\n}\n\nexport function ThemeProvider({\n children,\n initialTheme = defaultTheme,\n initialMode = \"system\",\n storage,\n storageKey = \"surfaces.theme\",\n scope = \"root\",\n themes: given,\n}: ThemeProviderProps) {\n const outer = useContext(ThemeContext);\n const themes = given ?? outer?.themes ?? NO_THEMES;\n const resolvedStorage = useMemo(() => (storage === undefined ? browserStorage() : storage), [storage]);\n // A choice made before the saved one has loaded wins: the saved one is older.\n const chosen = useRef(false);\n const [state, setState] = useState<ThemeState>({ theme: initialTheme, mode: initialMode });\n const scopeId = `sf-${useId().replace(/[^a-zA-Z0-9]/g, \"\")}`;\n const systemDark = useSystemDark();\n\n useEffect(() => {\n if (!resolvedStorage) return;\n let cancelled = false;\n void Promise.resolve(resolvedStorage.load(storageKey))\n .then((text) => {\n const saved = parseThemeState(text);\n if (saved && !cancelled && !chosen.current) setState(saved);\n })\n .catch(() => {});\n return () => {\n cancelled = true;\n };\n }, [resolvedStorage, storageKey]);\n\n const update = useCallback(\n (patch: Partial<ThemeState>) => {\n chosen.current = true;\n setState((prev) => {\n const next = { ...prev, ...patch };\n if (resolvedStorage) void Promise.resolve(resolvedStorage.save(storageKey, JSON.stringify(next))).catch(() => {});\n return next;\n });\n },\n [resolvedStorage, storageKey],\n );\n\n const css = useMemo(\n () => themeCss(state.theme, scope === \"root\" ? {} : { selector: `[data-sf-scope=\"${scopeId}\"]` }),\n [state.theme, scope, scopeId],\n );\n\n useEffect(() => {\n if (scope !== \"root\") return;\n const root = document.documentElement;\n if (state.mode === \"system\") root.removeAttribute(\"data-theme\");\n else root.setAttribute(\"data-theme\", state.mode);\n }, [scope, state.mode]);\n\n // A theme that asks for more contrast says so on the page, so a host can drop its own decoration (a glow, a texture)\n // as it would for `prefers-contrast: more`.\n useEffect(() => {\n if (scope !== \"root\") return;\n const root = document.documentElement;\n if (state.theme.contrast === \"more\") root.setAttribute(\"data-contrast\", \"more\");\n else root.removeAttribute(\"data-contrast\");\n }, [scope, state.theme.contrast]);\n\n const value = useMemo<ThemeContextValue>(\n () => ({\n ...state,\n setTheme: (theme) => update({ theme }),\n setMode: (mode) => update({ mode }),\n resolvedMode: state.mode === \"system\" ? (systemDark ? \"dark\" : \"light\") : state.mode,\n themes,\n }),\n [state, update, systemDark, themes],\n );\n\n return (\n <ThemeContext.Provider value={value}>\n <style data-sf-theme={scope === \"root\" ? \"root\" : scopeId}>{css}</style>\n {scope === \"root\" ? (\n children\n ) : (\n <div\n className=\"sf-scope\"\n data-sf-scope={scopeId}\n data-theme={state.mode === \"system\" ? undefined : state.mode}\n >\n {children}\n </div>\n )}\n </ThemeContext.Provider>\n );\n}\n\n/** Like `useTheme`, but returns undefined outside a ThemeProvider instead of throwing. */\nexport function useOptionalTheme(): ThemeContextValue | undefined {\n return useContext(ThemeContext);\n}\n\nexport function useTheme(): ThemeContextValue {\n const value = useContext(ThemeContext);\n if (!value) throw new Error(\"useTheme must be used inside <ThemeProvider>\");\n return value;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ThemeProvider.js","sourceRoot":"","sources":["../../src/theme/ThemeProvider.tsx"],"names":[],"mappings":";AAAA,OAAO,EAGL,KAAK,EACL,YAAY,EACZ,UAAU,EACV,QAAQ,GACT,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAEL,aAAa,EACb,WAAW,EACX,UAAU,EACV,SAAS,EACT,KAAK,EACL,OAAO,EACP,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AACf,OAAO,EAAqB,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAQtE;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,IAA+B;IAC7D,IAAI,CAAC,IAAI;QAAE,OAAO,SAAS,CAAC;IAC5B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAwC,CAAC;QACvE,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ;YAAE,OAAO,SAAS,CAAC;QAC5D,OAAO,EAAE,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAY,CAAC,CAAC,CAAC,CAAE,MAAM,CAAC,IAAa,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;IACzI,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAWD,MAAM,YAAY,GAAG,aAAa,CAAgC,SAAS,CAAC,CAAC;AA6B7E,MAAM,SAAS,GAAqB,EAAE,CAAC;AAEvC,MAAM,UAAU,GAAG,8BAA8B,CAAC;AAElD,SAAS,mBAAmB,CAAC,QAAoB;IAC/C,MAAM,KAAK,GAAG,UAAU,CAAC,UAA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Q;IACtB,MAAM,KAAK,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;IACvC,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAC5E,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n type Mode,\n type Theme,\n MODES,\n defaultTheme,\n parseTheme,\n themeCss,\n} from \"@surfaces-ui/tokens\";\nimport {\n type ReactNode,\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n useSyncExternalStore,\n} from \"react\";\nimport { type StateStorage, browserStorage } from \"@surfaces-ui/data\";\n\n/** What a person has chosen: the theme document and the light/dark mode. */\nexport interface ThemeState {\n theme: Theme;\n mode: Mode;\n}\n\n/**\n * Reads a saved theme choice. Saved text can be edited by anyone with access\n * to the storage, so the theme goes through `parseTheme` and an unknown mode\n * falls back to \"system\"; unreadable text is ignored.\n */\nexport function parseThemeState(text: string | null | undefined): ThemeState | undefined {\n if (!text) return undefined;\n try {\n const parsed = JSON.parse(text) as { theme?: unknown; mode?: unknown };\n if (!parsed || typeof parsed !== \"object\") return undefined;\n return { theme: parseTheme(parsed.theme, defaultTheme), mode: MODES.includes(parsed.mode as Mode) ? (parsed.mode as Mode) : \"system\" };\n } catch {\n return undefined;\n }\n}\n\nexport interface ThemeContextValue extends ThemeState {\n setTheme(theme: Theme): void;\n setMode(mode: Mode): void;\n /** The mode actually showing, with \"system\" resolved. */\n resolvedMode: \"light\" | \"dark\";\n /** The host's own themes, chosen by name beside the built-in presets (by a page, an agent's surface or `ThemePicker`). */\n themes: readonly Theme[];\n}\n\nconst ThemeContext = createContext<ThemeContextValue | undefined>(undefined);\n\nexport interface ThemeProviderProps {\n children: ReactNode;\n /** Theme used until the saved choice loads, and when it has nothing saved. */\n initialTheme?: Theme;\n initialMode?: Mode;\n /**\n * Where the choice is kept: the browser (the default), a host's bridge or a\n * server, through the same `StateStorage` every part of the library takes.\n * Pass `null` to keep the choice in memory only.\n */\n storage?: StateStorage | null;\n /** The key the choice is kept under. Default \"surfaces.theme\". */\n storageKey?: string;\n /**\n * \"root\" (default) themes the whole document. \"scoped\" themes only this\n * provider's subtree, so several themes can share a page.\n */\n scope?: \"root\" | \"scoped\";\n /**\n * The host's own themes (a company's brand themes, say), offered by name\n * beside the built-in presets: a page's or a surface's `theme` request can\n * choose one, and `ThemePicker` lists them. Pass them through `parseTheme`\n * first. A provider inside another keeps the outer one's unless given its own.\n */\n themes?: readonly Theme[];\n}\n\nconst NO_THEMES: readonly Theme[] = [];\n\nconst DARK_QUERY = \"(prefers-color-scheme: dark)\";\n\nfunction subscribeSystemDark(onChange: () => void): () => void {\n const media = globalThis.matchMedia?.(DARK_QUERY);\n if (!media) return () => {};\n media.addEventListener(\"change\", onChange);\n return () => media.removeEventListener(\"change\", onChange);\n}\n\nconst systemDarkNow = () => globalThis.matchMedia?.(DARK_QUERY).matches ?? false;\n\n/**\n * Whether the system asks for dark. A server cannot know, so it renders light, and the page hydrates as light\n * before it reads the system's choice: the HTML a server wrote and the first render in the browser always agree.\n * The theme's CSS already follows `prefers-color-scheme` on its own, so the page is never shown in the wrong mode.\n */\nfunction useSystemDark(): boolean {\n return useSyncExternalStore(subscribeSystemDark, systemDarkNow, () => false);\n}\n\nexport function ThemeProvider({\n children,\n initialTheme = defaultTheme,\n initialMode = \"system\",\n storage,\n storageKey = \"surfaces.theme\",\n scope = \"root\",\n themes: given,\n}: ThemeProviderProps) {\n const outer = useContext(ThemeContext);\n const themes = given ?? outer?.themes ?? NO_THEMES;\n const resolvedStorage = useMemo(() => (storage === undefined ? browserStorage() : storage), [storage]);\n // A choice made before the saved one has loaded wins: the saved one is older.\n const chosen = useRef(false);\n const [state, setState] = useState<ThemeState>({ theme: initialTheme, mode: initialMode });\n const scopeId = `sf-${useId().replace(/[^a-zA-Z0-9]/g, \"\")}`;\n const systemDark = useSystemDark();\n\n useEffect(() => {\n if (!resolvedStorage) return;\n let cancelled = false;\n void Promise.resolve(resolvedStorage.load(storageKey))\n .then((text) => {\n const saved = parseThemeState(text);\n if (saved && !cancelled && !chosen.current) setState(saved);\n })\n .catch(() => {});\n return () => {\n cancelled = true;\n };\n }, [resolvedStorage, storageKey]);\n\n const update = useCallback(\n (patch: Partial<ThemeState>) => {\n chosen.current = true;\n setState((prev) => {\n // Asking for what is already set changes nothing, so a host that keeps the mode in step with its own\n // toggle from an effect does not render again, and again.\n if (Object.entries(patch).every(([key, value]) => prev[key as keyof ThemeState] === value)) return prev;\n const next = { ...prev, ...patch };\n if (resolvedStorage) void Promise.resolve(resolvedStorage.save(storageKey, JSON.stringify(next))).catch(() => {});\n return next;\n });\n },\n [resolvedStorage, storageKey],\n );\n\n const css = useMemo(\n () => themeCss(state.theme, scope === \"root\" ? {} : { selector: `[data-sf-scope=\"${scopeId}\"]` }),\n [state.theme, scope, scopeId],\n );\n\n useEffect(() => {\n if (scope !== \"root\") return;\n const root = document.documentElement;\n if (state.mode === \"system\") root.removeAttribute(\"data-theme\");\n else root.setAttribute(\"data-theme\", state.mode);\n }, [scope, state.mode]);\n\n // A theme that asks for more contrast says so on the page, so a host can drop its own decoration (a glow, a texture)\n // as it would for `prefers-contrast: more`.\n useEffect(() => {\n if (scope !== \"root\") return;\n const root = document.documentElement;\n if (state.theme.contrast === \"more\") root.setAttribute(\"data-contrast\", \"more\");\n else root.removeAttribute(\"data-contrast\");\n }, [scope, state.theme.contrast]);\n\n // The same functions on every render, so a host can list them in an effect's dependencies.\n const setTheme = useCallback((theme: Theme) => update({ theme }), [update]);\n const setMode = useCallback((mode: Mode) => update({ mode }), [update]);\n\n const value = useMemo<ThemeContextValue>(\n () => ({\n ...state,\n setTheme,\n setMode,\n resolvedMode: state.mode === \"system\" ? (systemDark ? \"dark\" : \"light\") : state.mode,\n themes,\n }),\n [state, setTheme, setMode, systemDark, themes],\n );\n\n return (\n <ThemeContext.Provider value={value}>\n <style data-sf-theme={scope === \"root\" ? \"root\" : scopeId}>{css}</style>\n {scope === \"root\" ? (\n children\n ) : (\n <div\n className=\"sf-scope\"\n data-sf-scope={scopeId}\n data-theme={state.mode === \"system\" ? undefined : state.mode}\n >\n {children}\n </div>\n )}\n </ThemeContext.Provider>\n );\n}\n\n/** Like `useTheme`, but returns undefined outside a ThemeProvider instead of throwing. */\nexport function useOptionalTheme(): ThemeContextValue | undefined {\n return useContext(ThemeContext);\n}\n\nexport function useTheme(): ThemeContextValue {\n const value = useContext(ThemeContext);\n if (!value) throw new Error(\"useTheme must be used inside <ThemeProvider>\");\n return value;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@surfaces-ui/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "React components for Surfaces UI",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"surfaces-ui",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"jsdom": "^31.0.0",
|
|
54
54
|
"react": "^19.3.1",
|
|
55
55
|
"react-dom": "^19.3.1",
|
|
56
|
-
"@surfaces-ui/discovery": "0.
|
|
56
|
+
"@surfaces-ui/discovery": "0.4.0"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@bufbuild/protobuf": "^2.17.0",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"@lezer/highlight": "^1.2.5",
|
|
68
68
|
"apache-arrow": "^21.2.0",
|
|
69
69
|
"echarts": "^6.1.0",
|
|
70
|
-
"@surfaces-ui/data": "0.
|
|
71
|
-
"@surfaces-ui/proto": "0.
|
|
72
|
-
"@surfaces-ui/tokens": "0.
|
|
73
|
-
"@surfaces-ui/trace": "0.
|
|
70
|
+
"@surfaces-ui/data": "0.4.0",
|
|
71
|
+
"@surfaces-ui/proto": "0.4.0",
|
|
72
|
+
"@surfaces-ui/tokens": "0.4.0",
|
|
73
|
+
"@surfaces-ui/trace": "0.4.0"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"react": ">=18",
|