basalt-ui 1.1.1 → 1.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/AGENTS.md +2 -2
- package/CHANGELOG.md +30 -0
- package/README.md +47 -5
- package/agent/rules/basalt-app.md +126 -0
- package/agent/rules/basalt-content.md +3 -1
- package/agent/skills/basalt-app/SKILL.md +28 -7
- package/dist/agent/part-list.d.ts.map +1 -1
- package/dist/agent/part-list.js +17 -7
- package/dist/agent/stick-to-bottom.js +2 -2
- package/dist/agent-chat/thread-detail-panel.d.ts.map +1 -1
- package/dist/agent-chat/thread-detail-panel.js +37 -33
- package/dist/agent-chat/thread-message.d.ts.map +1 -1
- package/dist/agent-chat/thread-message.js +8 -5
- package/dist/agent-chat/thread-outcome-card.js +3 -3
- package/dist/agent-chat/thread-workspace.js +1 -1
- package/dist/charts/primitives/ChartLegend.js +2 -0
- package/dist/charts/primitives/ChartTooltip.js +2 -1
- package/dist/cli/index.d.ts +15 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +29 -7
- package/dist/connectivity/connectivity-indicator.d.ts.map +1 -1
- package/dist/connectivity/connectivity-indicator.js +3 -3
- package/dist/content/article-card.module.css +7 -5
- package/dist/content/article-layout.module.css +16 -12
- package/dist/content/callout.module.css +4 -4
- package/dist/content/code-block.module.css +8 -7
- package/dist/content/guide.module.css +3 -3
- package/dist/content/mermaid.module.css +2 -2
- package/dist/content/prose.module.css +59 -29
- package/dist/content/reading-progress.module.css +2 -0
- package/dist/content/toc.module.css +6 -3
- package/dist/dashboard/delta-badge.d.ts.map +1 -1
- package/dist/dashboard/delta-badge.js +10 -2
- package/dist/dashboard/empty-state.d.ts.map +1 -1
- package/dist/dashboard/empty-state.js +3 -16
- package/dist/dashboard/settings-section.js +2 -2
- package/dist/dashboard/settings-section.module.css +2 -2
- package/dist/dashboard/stat-card.js +2 -1
- package/dist/data/virtual-list.d.ts.map +1 -1
- package/dist/data/virtual-list.js +12 -2
- package/dist/forms/FormErrorSummary.js +2 -2
- package/dist/guard/index.d.ts +20 -4
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +250 -37
- package/dist/guard/types.d.ts +24 -3
- package/dist/guard/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/provider/index.d.ts +30 -1
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +32 -7
- package/dist/provider/lab-theme.d.ts +39 -0
- package/dist/provider/lab-theme.d.ts.map +1 -0
- package/dist/provider/lab-theme.js +52 -0
- package/dist/shell/app-header.module.css +2 -2
- package/dist/shell/app-mobile-nav.module.css +1 -1
- package/dist/shell/app-sidebar-account.d.ts.map +1 -1
- package/dist/shell/app-sidebar-account.js +4 -2
- package/dist/shell/app-sidebar.d.ts.map +1 -1
- package/dist/shell/app-sidebar.js +4 -2
- package/dist/shell/app-sidebar.module.css +36 -26
- package/dist/shell/index.d.ts.map +1 -1
- package/dist/shell/index.js +11 -4
- package/dist/shell/sidebar-search.module.css +16 -4
- package/dist/styles.css +1 -1
- package/dist/surfaces.d.ts +15 -11
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +4 -3
- package/dist/theme/controls.module.css +1 -1
- package/dist/theme/floating.module.css +1 -1
- package/dist/theme/index.d.ts +116 -11
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +703 -566
- package/dist/theme/nav-link.module.css +1 -1
- package/dist/theme/use-basalt-spacing.d.ts +3 -0
- package/dist/theme/use-basalt-spacing.d.ts.map +1 -0
- package/dist/theme/use-basalt-spacing.js +8 -0
- package/dist/theme-lab/derive-controls.d.ts +13 -0
- package/dist/theme-lab/derive-controls.d.ts.map +1 -0
- package/dist/theme-lab/derive-controls.js +137 -0
- package/dist/theme-lab/derive-state.d.ts +55 -0
- package/dist/theme-lab/derive-state.d.ts.map +1 -0
- package/dist/theme-lab/derive-state.js +81 -0
- package/dist/theme-lab/index.d.ts +12 -2
- package/dist/theme-lab/index.d.ts.map +1 -1
- package/dist/theme-lab/index.js +13 -62
- package/dist/theme-toggle/index.d.ts.map +1 -1
- package/dist/theme-toggle/index.js +3 -2
- package/dist/tokens/derive.d.ts +89 -0
- package/dist/tokens/derive.d.ts.map +1 -0
- package/dist/tokens/derive.js +250 -0
- package/dist/tokens/hct.d.ts +34 -0
- package/dist/tokens/hct.d.ts.map +1 -0
- package/dist/tokens/hct.js +159 -0
- package/dist/tokens/index.d.ts +71 -7
- package/dist/tokens/index.d.ts.map +1 -1
- package/dist/tokens/index.js +284 -46
- package/dist/tokens/palette.d.ts +984 -167
- package/dist/tokens/palette.d.ts.map +1 -1
- package/dist/tokens/palette.js +617 -119
- package/dist/vite-color-mix.d.ts +30 -0
- package/dist/vite-color-mix.d.ts.map +1 -0
- package/dist/vite-color-mix.js +80 -0
- package/dist/vite.d.ts +82 -1
- package/dist/vite.d.ts.map +1 -1
- package/dist/vite.js +312 -0
- package/llms.txt +5 -4
- package/package.json +24 -9
- package/src/agent/part-list.tsx +13 -7
- package/src/agent/stick-to-bottom.tsx +2 -2
- package/src/agent-chat/thread-detail-panel.tsx +13 -10
- package/src/agent-chat/thread-message.tsx +8 -5
- package/src/agent-chat/thread-outcome-card.tsx +2 -2
- package/src/agent-chat/thread-workspace.tsx +2 -2
- package/src/charts/primitives/ChartLegend.tsx +2 -2
- package/src/charts/primitives/ChartTooltip.tsx +2 -2
- package/src/cli/check-theme.test.ts +18 -0
- package/src/cli/index.ts +55 -5
- package/src/connectivity/connectivity-indicator.tsx +8 -2
- package/src/content/article-card.module.css +7 -5
- package/src/content/article-layout.module.css +16 -12
- package/src/content/callout.module.css +4 -4
- package/src/content/code-block.module.css +8 -7
- package/src/content/guide.module.css +3 -3
- package/src/content/mermaid.module.css +2 -2
- package/src/content/prose.module.css +59 -29
- package/src/content/prose.module.css.test.ts +55 -0
- package/src/content/reading-progress.module.css +2 -0
- package/src/content/toc.module.css +6 -3
- package/src/dashboard/delta-badge.tsx +10 -4
- package/src/dashboard/empty-state.tsx +3 -16
- package/src/dashboard/settings-section.module.css +2 -2
- package/src/dashboard/settings-section.tsx +2 -2
- package/src/dashboard/stat-card.tsx +2 -2
- package/src/data/virtual-list.tsx +12 -2
- package/src/forms/FormErrorSummary.tsx +1 -1
- package/src/guard/check-source.test.ts +416 -5
- package/src/guard/index.ts +367 -45
- package/src/guard/types.ts +24 -2
- package/src/index.ts +1 -0
- package/src/provider/index.test.tsx +100 -0
- package/src/provider/index.tsx +79 -7
- package/src/provider/lab-theme.test.ts +147 -0
- package/src/provider/lab-theme.ts +166 -0
- package/src/shell/app-header.module.css +2 -2
- package/src/shell/app-mobile-nav.module.css +1 -1
- package/src/shell/app-sidebar-account.tsx +9 -2
- package/src/shell/app-sidebar.module.css +36 -26
- package/src/shell/app-sidebar.tsx +12 -2
- package/src/shell/index.tsx +14 -3
- package/src/shell/sidebar-search.module.css +16 -4
- package/src/styles.css +1 -1
- package/src/surfaces.ts +17 -10
- package/src/theme/contrast.test.ts +19 -3
- package/src/theme/controls.module.css +1 -1
- package/src/theme/density-option.test.ts +96 -0
- package/src/theme/density-relations.test.ts +428 -0
- package/src/theme/derive-theme.test.ts +101 -0
- package/src/theme/floating.module.css +1 -1
- package/src/theme/fonts-option.test.ts +69 -0
- package/src/theme/index.ts +930 -563
- package/src/theme/nav-link.module.css +1 -1
- package/src/theme/radius-option.test.ts +86 -0
- package/src/theme/radius.test.ts +132 -0
- package/src/theme/spacing.test.ts +432 -0
- package/src/theme/use-basalt-spacing.test.tsx +51 -0
- package/src/theme/use-basalt-spacing.ts +38 -0
- package/src/theme-lab/derive-controls.test.ts +94 -0
- package/src/theme-lab/derive-controls.tsx +227 -0
- package/src/theme-lab/derive-state.test.ts +71 -0
- package/src/theme-lab/derive-state.ts +163 -0
- package/src/theme-lab/index.tsx +29 -68
- package/src/theme-lab/theme-lab.test.ts +4 -18
- package/src/theme-toggle/index.tsx +9 -4
- package/src/tokens/build-fonts-css.test.ts +51 -0
- package/src/tokens/build-radius-css.test.ts +25 -0
- package/src/tokens/density.test.ts +274 -0
- package/src/tokens/derive-radius.test.ts +62 -0
- package/src/tokens/derive.test.ts +30 -0
- package/src/tokens/derive.ts +481 -0
- package/src/tokens/hct.ts +231 -0
- package/src/tokens/index.ts +395 -45
- package/src/tokens/palette.test.ts +114 -0
- package/src/tokens/palette.ts +1118 -180
- package/src/vite-color-mix.test.ts +46 -0
- package/src/vite-color-mix.ts +137 -0
- package/src/vite.test.ts +192 -0
- package/src/vite.ts +502 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `useBasaltSpacing` — the only path by which a non-default density level reaches `BasaltShell`'s
|
|
3
|
+
* AppShell header/navbar/rail dimensions and `AppSidebar`/`SidebarAccount`'s `<Menu width>` (see the
|
|
4
|
+
* hook's own doc). Nothing previously asserted it actually reads `theme.other.basaltDensity` off the
|
|
5
|
+
* running Mantine theme — that wiring could silently break (e.g. a typo'd key, or a dropped fallback)
|
|
6
|
+
* with every other density test still green, since those all exercise `deriveSpacing`/
|
|
7
|
+
* `createBasaltTheme` directly, never this hook.
|
|
8
|
+
*
|
|
9
|
+
* No `@testing-library/react`/jsdom is configured in this repo (see `tokens/build-fonts-css.test.ts`'s
|
|
10
|
+
* doc for that same limitation elsewhere) — `react-dom/server`'s `renderToStaticMarkup` is enough to
|
|
11
|
+
* invoke a hook inside a REAL `MantineProvider` tree without a DOM, so these assertions exercise the
|
|
12
|
+
* actual `useMantineTheme()` read, not a re-implementation of it.
|
|
13
|
+
*/
|
|
14
|
+
import { MantineProvider } from '@mantine/core'
|
|
15
|
+
import type { MantineProviderProps } from '@mantine/core'
|
|
16
|
+
import { describe, expect, test } from 'bun:test'
|
|
17
|
+
import { renderToStaticMarkup } from 'react-dom/server'
|
|
18
|
+
import { DEFAULT_SPACE_VALUES, deriveSpacing } from '../tokens/palette'
|
|
19
|
+
import { useBasaltSpacing } from './use-basalt-spacing'
|
|
20
|
+
|
|
21
|
+
function readBasaltSpacing(theme?: MantineProviderProps['theme']) {
|
|
22
|
+
let captured: ReturnType<typeof useBasaltSpacing> | undefined
|
|
23
|
+
function Probe() {
|
|
24
|
+
captured = useBasaltSpacing()
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
renderToStaticMarkup(
|
|
28
|
+
<MantineProvider theme={theme}>
|
|
29
|
+
<Probe />
|
|
30
|
+
</MantineProvider>,
|
|
31
|
+
)
|
|
32
|
+
return captured
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('useBasaltSpacing', () => {
|
|
36
|
+
test('falls back to DEFAULT_SPACE_VALUES when theme.other.basaltDensity is unset', () => {
|
|
37
|
+
// Fails if the hook drops its `?? DEFAULT_SPACE_VALUES` fallback (e.g. returns `undefined` at
|
|
38
|
+
// the shipped default density instead).
|
|
39
|
+
expect(readBasaltSpacing()).toEqual(DEFAULT_SPACE_VALUES)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('reads the resolved values off theme.other.basaltDensity when present', () => {
|
|
43
|
+
const retuned = deriveSpacing(-2)
|
|
44
|
+
// Fails if the hook stops reading `theme.other.basaltDensity` (e.g. always returns
|
|
45
|
+
// `DEFAULT_SPACE_VALUES` regardless of the running theme).
|
|
46
|
+
expect(readBasaltSpacing({ other: { basaltDensity: retuned } })).toEqual(retuned)
|
|
47
|
+
expect(readBasaltSpacing({ other: { basaltDensity: retuned } })).not.toEqual(
|
|
48
|
+
DEFAULT_SPACE_VALUES,
|
|
49
|
+
)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useBasaltSpacing — reads the ACTIVE resolved {@link SpaceValues} off the running Mantine theme,
|
|
3
|
+
* instead of the static level-0 snapshot (`SPACE`/`SPACE_SCALE`/`SPACE_STEP`/`ROW_LINE_HEIGHT` in
|
|
4
|
+
* `../tokens/palette`). `createBasaltTheme(overrides, { density })` stashes the resolved values on
|
|
5
|
+
* `theme.other.basaltDensity` ONLY when the level is non-zero (`./index.ts`'s `isDefaultDensity`
|
|
6
|
+
* short-circuit) — at level 0 that key is absent by design, so this hook falls back to
|
|
7
|
+
* `DEFAULT_SPACE_VALUES` (`deriveSpacing(0)`, `../tokens/palette`), which is exactly what
|
|
8
|
+
* `theme.other.basaltDensity` would have held had it not been omitted. That fallback is what keeps
|
|
9
|
+
* every consumer of this hook byte-identical at the shipped default density.
|
|
10
|
+
*
|
|
11
|
+
* Lives in `theme/`, not `tokens/`, because it calls `useMantineTheme()` — `basalt/token-layer-
|
|
12
|
+
* boundary` bans `@mantine/*` imports inside `tokens/`.
|
|
13
|
+
*
|
|
14
|
+
* Three `../shell` components consume it today, two distinct classes of use:
|
|
15
|
+
* - `BasaltShell` (`../shell/index.tsx`) — its AppShell header/navbar dimensions need the ACTIVE
|
|
16
|
+
* density level's numbers (`step.appShellHeaderHeight`/`.appShellHeaderMobileHeight`/
|
|
17
|
+
* `.appShellNavbarWidth`/`.appShellNavbarRailWidth`), not the frozen shipped-identity constants,
|
|
18
|
+
* because those boxes must grow/shrink with the density-tracked controls rendered inside them.
|
|
19
|
+
* - `AppSidebar` (`../shell/app-sidebar.tsx`) and `SidebarAccount` (`../shell/app-sidebar-
|
|
20
|
+
* account.tsx`) each read `step.sidebarSettingsMenuWidth`/`.sidebarAccountMenuWidth` for their
|
|
21
|
+
* own `<Menu width={…}>` — a numeric Mantine prop, the same "JS-consumed, no `--vx-*` var" shape
|
|
22
|
+
* as the AppShell dimensions above (see `sidebarAccountMenuWidth`'s doc in `../tokens/palette`
|
|
23
|
+
* for why a fixed dropdown width needs to track density at all).
|
|
24
|
+
*
|
|
25
|
+
* Deliberately INTERNAL for now — exported from this module (so `../shell` can reach it) but NOT
|
|
26
|
+
* re-exported by the root `.` barrel (`src/index.ts`), so it is not part of the published package
|
|
27
|
+
* surface (same convention `scaleSpace`'s doc comment in `../tokens/palette` uses for the same
|
|
28
|
+
* reason). These three `../shell` components are its only consumers today; promote it to the root
|
|
29
|
+
* barrel — with a surface entry in this package's `CLAUDE.md` — if/when a consumer outside the
|
|
30
|
+
* framework's own components needs the ACTIVE resolved spacing.
|
|
31
|
+
*/
|
|
32
|
+
import { useMantineTheme } from '@mantine/core'
|
|
33
|
+
import { DEFAULT_SPACE_VALUES } from '../tokens/palette'
|
|
34
|
+
import type { SpaceValues } from '../tokens/palette'
|
|
35
|
+
|
|
36
|
+
export function useBasaltSpacing(): SpaceValues {
|
|
37
|
+
return useMantineTheme().other?.basaltDensity ?? DEFAULT_SPACE_VALUES
|
|
38
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `DeriveControls`'s persisted-state migrations. v2→v3 bumped `STORAGE_VERSION` to `3` when
|
|
3
|
+
* `density` was added alongside `radius` + the six color knobs; v3→v4 bumped it again when
|
|
4
|
+
* `deriveSpacing`'s accepted range narrowed from `[-5, 5]` to `[-3, 3]` (see `derive-controls.tsx`'s
|
|
5
|
+
* module doc comment for both). No `migrate` is passed to `createPersistedState`, so a version
|
|
6
|
+
* mismatch falls through to `opts.initial` (`../state`'s `parseStorage`) rather than carrying
|
|
7
|
+
* `parsed.value` forward — `parsePersistedDeriveState` is the schema validator that rejection
|
|
8
|
+
* ultimately routes through.
|
|
9
|
+
*
|
|
10
|
+
* Tested at the validator seam directly (not through `createPersistedState`'s localStorage
|
|
11
|
+
* round-trip): no DOM render harness is configured in this package (see
|
|
12
|
+
* `../provider/build-fonts-css.test.ts`), and this repo's test environment has no `localStorage`
|
|
13
|
+
* either (see `../router-tanstack/multi-search-param-store.test.ts`).
|
|
14
|
+
*/
|
|
15
|
+
import { describe, expect, test } from 'bun:test'
|
|
16
|
+
import { DEFAULT_DERIVE_CONFIG } from '../tokens/derive'
|
|
17
|
+
import { DEFAULT_STATE, parsePersistedDeriveState } from './derive-controls'
|
|
18
|
+
|
|
19
|
+
/** A v1-shaped persisted value — the six color knobs + `applied`, no `radius`/`density` key at all. */
|
|
20
|
+
const V1_SHAPED_VALUE = {
|
|
21
|
+
accent: DEFAULT_DERIVE_CONFIG.accent,
|
|
22
|
+
neutral: DEFAULT_DERIVE_CONFIG.neutral,
|
|
23
|
+
lightLevel: DEFAULT_DERIVE_CONFIG.lightLevel,
|
|
24
|
+
darkLevel: DEFAULT_DERIVE_CONFIG.darkLevel,
|
|
25
|
+
vibrancy: DEFAULT_DERIVE_CONFIG.vibrancy,
|
|
26
|
+
accentBrightness: DEFAULT_DERIVE_CONFIG.accentBrightness,
|
|
27
|
+
applied: false,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A v2-shaped persisted value — the six color knobs + `radius` + `applied`, no `density` key. */
|
|
31
|
+
const V2_SHAPED_VALUE = { ...V1_SHAPED_VALUE, radius: 0 }
|
|
32
|
+
|
|
33
|
+
describe('a v1-shaped persisted value (no radius/density key) is rejected', () => {
|
|
34
|
+
test('parsePersistedDeriveState returns null, not a value with radius/density: undefined', () => {
|
|
35
|
+
const parsed = parsePersistedDeriveState(V1_SHAPED_VALUE)
|
|
36
|
+
expect(parsed).toBeNull()
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('a v2-shaped persisted value (radius present, no density key) is rejected', () => {
|
|
41
|
+
test('parsePersistedDeriveState returns null, not a value with density: undefined', () => {
|
|
42
|
+
const parsed = parsePersistedDeriveState(V2_SHAPED_VALUE)
|
|
43
|
+
expect(parsed).toBeNull()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('DEFAULT_STATE (what a rejection falls back to via parseStorage) has a real density', () => {
|
|
47
|
+
// No `migrate` is passed to `createPersistedState`, so a version mismatch (v1/v2 vs the current
|
|
48
|
+
// v3) sets the pre-validation value to `opts.initial` — DEFAULT_STATE — which this validator
|
|
49
|
+
// must then accept, landing a real `radius: 0` / `density: 0`, never `undefined`.
|
|
50
|
+
expect(parsePersistedDeriveState(DEFAULT_STATE)).toEqual(DEFAULT_STATE)
|
|
51
|
+
expect(DEFAULT_STATE.radius).toBe(0)
|
|
52
|
+
expect(DEFAULT_STATE.density).toBe(0)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('a v3-shaped persisted value (radius + density present) round-trips', () => {
|
|
57
|
+
test('parsePersistedDeriveState accepts it and preserves both levels', () => {
|
|
58
|
+
const v3Value = { ...V2_SHAPED_VALUE, radius: 3, density: -2 }
|
|
59
|
+
expect(parsePersistedDeriveState(v3Value)).toEqual(v3Value)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('an out-of-range density level is rejected', () => {
|
|
63
|
+
const badValue = { ...V2_SHAPED_VALUE, density: 99 }
|
|
64
|
+
expect(parsePersistedDeriveState(badValue)).toBeNull()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('a non-integer density level is rejected', () => {
|
|
68
|
+
const badValue = { ...V2_SHAPED_VALUE, density: 1.5 }
|
|
69
|
+
expect(parsePersistedDeriveState(badValue)).toBeNull()
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('a pre-v4 envelope holding a density outside the narrowed [-3, 3] range is rejected', () => {
|
|
74
|
+
// The exact live-crash path the v3->v4 bump exists to prevent: a v3 envelope from an earlier
|
|
75
|
+
// session could hold `density: 4` or `density: -5`, both valid under the OLD `isLevel` (-5..5)
|
|
76
|
+
// check that guarded `density` pre-v4. Falling through to `DEFAULT_STATE` here, instead of
|
|
77
|
+
// reaching `deriveSpacing` at render, is what the version bump buys.
|
|
78
|
+
test('density: 4 (in-range for radius, out of range for density) is rejected', () => {
|
|
79
|
+
const badValue = { ...V2_SHAPED_VALUE, density: 4 }
|
|
80
|
+
expect(parsePersistedDeriveState(badValue)).toBeNull()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('density: -5 (the old lower bound) is rejected', () => {
|
|
84
|
+
const badValue = { ...V2_SHAPED_VALUE, density: -5 }
|
|
85
|
+
expect(parsePersistedDeriveState(badValue)).toBeNull()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('radius: -5/5 stays accepted — only density narrowed', () => {
|
|
89
|
+
const lowRadius = { ...V2_SHAPED_VALUE, radius: -5, density: -3 }
|
|
90
|
+
const highRadius = { ...V2_SHAPED_VALUE, radius: 5, density: 3 }
|
|
91
|
+
expect(parsePersistedDeriveState(lowRadius)).toEqual(lowRadius)
|
|
92
|
+
expect(parsePersistedDeriveState(highRadius)).toEqual(highRadius)
|
|
93
|
+
})
|
|
94
|
+
})
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DeriveControls — DEV-tool live tuning of the six-knob derive config (`tokens/derive.ts`): accent
|
|
3
|
+
* seed, neutral family, light/dark surface levels, vibrancy, and accent brightness — plus a
|
|
4
|
+
* seventh, color-independent "Radius" level (`tokens/palette.ts`'s `deriveRadius`) and an eighth,
|
|
5
|
+
* "Density" level (`tokens/palette.ts`'s `deriveSpacing`).
|
|
6
|
+
*
|
|
7
|
+
* This is the DEV-tool path — for a PRODUCTION theme, pass `{ derive }` / `{ radius }` / `{ density }`
|
|
8
|
+
* to `createBasaltTheme` instead (see its JSDoc in `../theme`); that is the one place a consumer sets
|
|
9
|
+
* the palette identity for real. This component is for live-tweaking a config by eye during
|
|
10
|
+
* development, not for shipping one.
|
|
11
|
+
*
|
|
12
|
+
* It applies a config through BOTH halves of the theme, because a knob's reach spans both:
|
|
13
|
+
*
|
|
14
|
+
* 1. **CSS vars** — a `<style>` tag (see {@link applyDeriveOverride} below) for every `--vx-*` the
|
|
15
|
+
* palette / radius / density builders emit.
|
|
16
|
+
* 2. **The theme OBJECT** — `BasaltProvider` reads this same persisted store and rebuilds a real
|
|
17
|
+
* theme via `createBasaltTheme(undefined, { derive, radius, density })`, so `theme.radius` /
|
|
18
|
+
* `theme.spacing` (the generic Mantine `xs`..`xl` scales — every `p="md"` / `gap="sm"` in the app)
|
|
19
|
+
* and the numeric `defaultProps` baked in by `buildTheme` (Badge / SegmentedControl / Progress /
|
|
20
|
+
* Tooltip / Popover / Modal / Notification `radius`, Progress's `size`, Timeline's `bulletSize`)
|
|
21
|
+
* follow the sliders too. See `../provider/lab-theme.ts` — it merges only the DELTA the config
|
|
22
|
+
* makes against the shipped base, so a consumer's own theme overrides survive. A `<style>` tag
|
|
23
|
+
* alone could never reach a number inside a JS object: the sliders used to move the CSS-var
|
|
24
|
+
* surfaces and leave the generic Mantine scales at level 0, which made the tool under-report its
|
|
25
|
+
* own knobs unevenly across surfaces (the CSS-module-heavy app sidebar moved most, plain Mantine
|
|
26
|
+
* layout not at all) — precisely the reading a retune is judged by.
|
|
27
|
+
*
|
|
28
|
+
* ONE gap remains, and it is NOT theme-lab-only — it fails the PRODUCTION path identically.
|
|
29
|
+
* `tokens/index.ts`'s `VX` object (`VX.legendGap`/`VX.margin`/`VX.dotR` — chart legend gap, plot-area
|
|
30
|
+
* margins, data-point marker radius) is computed ONCE at module load from the STATIC level-0
|
|
31
|
+
* `SPACE_STEP` snapshot, not re-derived per `SpaceValues`, because visx SVG props read it as a plain
|
|
32
|
+
* JS number at import time, before any `density` option exists to retune it against. So chart
|
|
33
|
+
* geometry follows neither this slider nor `createBasaltTheme({ density })`. See `deriveSpacing`'s
|
|
34
|
+
* JSDoc (`tokens/palette.ts`) for the full accounting of what tracks density and what doesn't, and why.
|
|
35
|
+
*
|
|
36
|
+
* The CSS half applies through a `<style>` tag appended to the END of `<body>`, using the exact same
|
|
37
|
+
* per-scheme selectors `buildPaletteCss` emits (`html[data-mantine-color-scheme='light'|'dark']`)
|
|
38
|
+
* — equal CSS specificity, later in document order, so it wins the cascade tiebreak over
|
|
39
|
+
* `BasaltProvider`'s own injected palette `<style>` (also rendered in `<body>`) without
|
|
40
|
+
* `!important`. Ported from the argo/playground derive proof-of-concept, which validated this
|
|
41
|
+
* mechanism (a head-appended tag loses the tiebreak — it sits earlier in document order).
|
|
42
|
+
*
|
|
43
|
+
* Persisted to its own localStorage key/version, Standard-Schema-guarded so a corrupt envelope can
|
|
44
|
+
* never throw during render (falls back to the default config instead) — see `./derive-state`, the
|
|
45
|
+
* Mantine-free module that owns the store both halves above read.
|
|
46
|
+
*
|
|
47
|
+
* Now that the provider re-derives the palette from the same store, its injected `<style>` is a
|
|
48
|
+
* superset of this one (it also carries a consumer's `paletteOptions` groups) — this tag is kept
|
|
49
|
+
* because it is the only half that can apply BEFORE React mounts, which is what keeps a page reload
|
|
50
|
+
* from flashing the stock palette. Both emit the same values, so the tiebreak is a no-op.
|
|
51
|
+
*/
|
|
52
|
+
import {
|
|
53
|
+
Button,
|
|
54
|
+
ColorInput,
|
|
55
|
+
Group,
|
|
56
|
+
SegmentedControl,
|
|
57
|
+
Slider,
|
|
58
|
+
Stack,
|
|
59
|
+
Switch,
|
|
60
|
+
Text,
|
|
61
|
+
} from '@mantine/core'
|
|
62
|
+
import { useEffect, useState } from 'react'
|
|
63
|
+
import type { ReactNode } from 'react'
|
|
64
|
+
import { buildDensityCss, buildPaletteCss, buildRadiusCss } from '../tokens'
|
|
65
|
+
import type { DeriveConfig } from '../tokens/derive'
|
|
66
|
+
import { buildPaletteData, deriveRadius, deriveSpacing } from '../tokens/palette'
|
|
67
|
+
import {
|
|
68
|
+
DEFAULT_STATE,
|
|
69
|
+
NEUTRAL_OPTIONS,
|
|
70
|
+
isAccentHex,
|
|
71
|
+
readAppliedDeriveOverride,
|
|
72
|
+
toDeriveOverride,
|
|
73
|
+
useDeriveControlsState,
|
|
74
|
+
} from './derive-state'
|
|
75
|
+
import type { DeriveOverride } from './derive-state'
|
|
76
|
+
|
|
77
|
+
// The persisted state itself lives in `./derive-state` (Mantine-free) — `BasaltProvider` reads the
|
|
78
|
+
// same store to rebuild the theme object. Re-exported here so this module stays the one public entry
|
|
79
|
+
// point for the panel and its state shape.
|
|
80
|
+
export { DEFAULT_STATE, parsePersistedDeriveState, type PersistedDeriveState } from './derive-state'
|
|
81
|
+
|
|
82
|
+
const STYLE_TAG_ID = 'basalt-derive-controls-style'
|
|
83
|
+
|
|
84
|
+
const LEVEL_MARKS = [-5, 0, 5].map((v) => ({ value: v, label: String(v) }))
|
|
85
|
+
// Density's own, narrower range — see `deriveSpacing`'s JSDoc (`tokens/palette.ts`) for why it's
|
|
86
|
+
// [-3, 3] while radius and the four color knobs stay [-5, 5].
|
|
87
|
+
const DENSITY_LEVEL_MARKS = [-3, 0, 3].map((v) => ({ value: v, label: String(v) }))
|
|
88
|
+
|
|
89
|
+
const LEVEL_SLIDERS = [
|
|
90
|
+
['lightLevel', 'Light level'],
|
|
91
|
+
['darkLevel', 'Dark level'],
|
|
92
|
+
['vibrancy', 'Vibrancy'],
|
|
93
|
+
['accentBrightness', 'Brightness'],
|
|
94
|
+
['radius', 'Radius'],
|
|
95
|
+
['density', 'Density'],
|
|
96
|
+
] as const
|
|
97
|
+
|
|
98
|
+
/** Inject (or remove) the override `<style>` tag for a resolved config + radius level + density
|
|
99
|
+
* level. `null` removes it. */
|
|
100
|
+
function applyDeriveOverride(override: DeriveOverride | null): void {
|
|
101
|
+
if (override === null) {
|
|
102
|
+
document.getElementById(STYLE_TAG_ID)?.remove()
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
let styleEl = document.getElementById(STYLE_TAG_ID) as HTMLStyleElement | null
|
|
106
|
+
if (!styleEl) {
|
|
107
|
+
styleEl = document.createElement('style')
|
|
108
|
+
styleEl.id = STYLE_TAG_ID
|
|
109
|
+
// End-of-<body>, not <head>: BasaltProvider renders the stock palette <style> INSIDE the app
|
|
110
|
+
// tree (in <body>), so a head-appended tag would sit earlier in document order and lose the
|
|
111
|
+
// equal-specificity cascade tiebreak — the override would silently never apply.
|
|
112
|
+
document.body.appendChild(styleEl)
|
|
113
|
+
}
|
|
114
|
+
const paletteCss = buildPaletteCss(undefined, buildPaletteData(override.config))
|
|
115
|
+
const radiusCss = buildRadiusCss(deriveRadius(override.radiusLevel))
|
|
116
|
+
const densityCss = buildDensityCss(deriveSpacing(override.densityLevel))
|
|
117
|
+
styleEl.textContent = `${paletteCss}\n${radiusCss}\n${densityCss}`
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Chunk-load re-apply: re-inject a persisted `applied` override as soon as this module evaluates,
|
|
121
|
+
// before React mounts, so a full page reload doesn't flash the stock palette before the effect
|
|
122
|
+
// below runs. Inputs are fully validated by `parsePersistedDeriveState`, so this cannot throw.
|
|
123
|
+
// (The theme-object half needs no such pre-pass — `BasaltProvider` reads the same store during its
|
|
124
|
+
// FIRST render, so the numeric `defaultProps`/`spacing` are already at the right level.)
|
|
125
|
+
if (typeof document !== 'undefined') {
|
|
126
|
+
applyDeriveOverride(readAppliedDeriveOverride())
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export type DeriveControlsProps = {
|
|
130
|
+
/** Icon for the "Reset" action. Passed as a node — the framework ships no icon dep. */
|
|
131
|
+
resetIcon?: ReactNode
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The derive-config panel — accent seed, neutral family, light/dark surface levels, vibrancy, and
|
|
136
|
+
* accent brightness, with an "Apply" switch and a "Reset" action. See the module doc comment for
|
|
137
|
+
* the dev-vs-production split.
|
|
138
|
+
*/
|
|
139
|
+
export function DeriveControls({ resetIcon }: DeriveControlsProps) {
|
|
140
|
+
const [state, setState] = useDeriveControlsState()
|
|
141
|
+
|
|
142
|
+
// `ColorInput` feeds every keystroke here — a partial hex like `#12` must never reach `state`
|
|
143
|
+
// (and from there `deriveTokens`, which now throws on a malformed accent). Keep the raw
|
|
144
|
+
// in-progress text in local state so typing is never blocked, and only fold it into the
|
|
145
|
+
// persisted/derived config once it matches a full hex; an invalid in-progress value keeps the
|
|
146
|
+
// last valid accent applied.
|
|
147
|
+
const [accentDraft, setAccentDraft] = useState(state.accent)
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
setAccentDraft(state.accent)
|
|
150
|
+
}, [state.accent])
|
|
151
|
+
|
|
152
|
+
const handleAccentChange = (value: string) => {
|
|
153
|
+
setAccentDraft(value)
|
|
154
|
+
if (isAccentHex(value)) setState({ ...state, accent: value })
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Owns exactly one DOM node (#basalt-derive-controls-style). No cleanup function on purpose:
|
|
158
|
+
// unmounting this component (e.g. collapsing an accordion around it) must not tear the override
|
|
159
|
+
// down while `applied` stays true — only this effect re-running with `applied === false` (toggle
|
|
160
|
+
// off, or Reset) removes it. `state` is a referentially stable snapshot (see `../state`'s
|
|
161
|
+
// `createPersistedState`), so this runs once per write, not once per render.
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
applyDeriveOverride(toDeriveOverride(state))
|
|
164
|
+
}, [state])
|
|
165
|
+
|
|
166
|
+
const reset = () => setState(DEFAULT_STATE)
|
|
167
|
+
|
|
168
|
+
return (
|
|
169
|
+
<Stack gap="sm">
|
|
170
|
+
<ColorInput
|
|
171
|
+
size="xs"
|
|
172
|
+
format="hex"
|
|
173
|
+
label="Accent seed"
|
|
174
|
+
value={accentDraft}
|
|
175
|
+
onChange={handleAccentChange}
|
|
176
|
+
/>
|
|
177
|
+
<div>
|
|
178
|
+
<Text size="xs" fw={500} mb={4}>
|
|
179
|
+
Neutral family
|
|
180
|
+
</Text>
|
|
181
|
+
<SegmentedControl
|
|
182
|
+
size="xs"
|
|
183
|
+
fullWidth
|
|
184
|
+
aria-label="Neutral family"
|
|
185
|
+
value={state.neutral}
|
|
186
|
+
onChange={(neutral) =>
|
|
187
|
+
setState({ ...state, neutral: neutral as DeriveConfig['neutral'] })
|
|
188
|
+
}
|
|
189
|
+
data={NEUTRAL_OPTIONS}
|
|
190
|
+
/>
|
|
191
|
+
</div>
|
|
192
|
+
{LEVEL_SLIDERS.map(([key, label]) => {
|
|
193
|
+
// Density's slider range is narrower than the others (see `DENSITY_LEVEL_MARKS`'s doc) —
|
|
194
|
+
// `isLevel`/the shared `-5..5` marks still apply to radius and the four color knobs.
|
|
195
|
+
const isDensity = key === 'density'
|
|
196
|
+
return (
|
|
197
|
+
<div key={key}>
|
|
198
|
+
<Text size="xs" fw={500} mb={4}>
|
|
199
|
+
{label} ({state[key]})
|
|
200
|
+
</Text>
|
|
201
|
+
<Slider
|
|
202
|
+
size="xs"
|
|
203
|
+
min={isDensity ? -3 : -5}
|
|
204
|
+
max={isDensity ? 3 : 5}
|
|
205
|
+
step={1}
|
|
206
|
+
aria-label={label}
|
|
207
|
+
value={state[key]}
|
|
208
|
+
onChange={(v) => setState({ ...state, [key]: v })}
|
|
209
|
+
marks={isDensity ? DENSITY_LEVEL_MARKS : LEVEL_MARKS}
|
|
210
|
+
/>
|
|
211
|
+
</div>
|
|
212
|
+
)
|
|
213
|
+
})}
|
|
214
|
+
<Group justify="space-between" mt="xs">
|
|
215
|
+
<Switch
|
|
216
|
+
size="sm"
|
|
217
|
+
label="Apply"
|
|
218
|
+
checked={state.applied}
|
|
219
|
+
onChange={(e) => setState({ ...state, applied: e.currentTarget.checked })}
|
|
220
|
+
/>
|
|
221
|
+
<Button size="compact-xs" variant="default" leftSection={resetIcon} onClick={reset}>
|
|
222
|
+
Reset
|
|
223
|
+
</Button>
|
|
224
|
+
</Group>
|
|
225
|
+
</Stack>
|
|
226
|
+
)
|
|
227
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `toDeriveOverride` — the store-to-override projection BOTH halves of the theme lab go through (the
|
|
3
|
+
* `<style>` injection in `derive-controls.tsx` and the theme rebuild in `../provider/lab-theme.ts`).
|
|
4
|
+
*
|
|
5
|
+
* Worth its own file because `lab-theme.test.ts` builds a `DeriveOverride` by hand (its `at()`
|
|
6
|
+
* helper) and never calls this function, so nothing there would catch either of its two jobs
|
|
7
|
+
* regressing:
|
|
8
|
+
*
|
|
9
|
+
* 1. `applied: false` → `null`. This is the gate the whole "production path is untouched" claim rests
|
|
10
|
+
* on — every app that never mounts the panel reads `DEFAULT_STATE`, whose `applied` is false.
|
|
11
|
+
* 2. The config carries EXACTLY the six color knobs. `resolveDeriveConfig` is a plain spread, so the
|
|
12
|
+
* obvious simplification — handing it the whole persisted state — would silently carry
|
|
13
|
+
* `applied`/`radius`/`density` into the config, and from there onto `theme.other.basaltDerive`
|
|
14
|
+
* (read by `BasaltBridge`) and into `buildPaletteData`'s memo key. It would not throw and nothing
|
|
15
|
+
* else in the suite would notice.
|
|
16
|
+
*
|
|
17
|
+
* The persisted-envelope validator is tested separately, in `derive-controls.test.ts`.
|
|
18
|
+
*/
|
|
19
|
+
import { describe, expect, test } from 'bun:test'
|
|
20
|
+
import { DEFAULT_STATE, toDeriveOverride } from './derive-state'
|
|
21
|
+
import { DEFAULT_DERIVE_CONFIG } from '../tokens/derive'
|
|
22
|
+
|
|
23
|
+
describe('the applied gate', () => {
|
|
24
|
+
test('DEFAULT_STATE (applied: false) yields no override — the production path', () => {
|
|
25
|
+
expect(toDeriveOverride(DEFAULT_STATE)).toBeNull()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('a fully tuned state with applied: false still yields no override', () => {
|
|
29
|
+
const tuned = { ...DEFAULT_STATE, accent: '#ff0000', radius: -3, density: 2, applied: false }
|
|
30
|
+
expect(toDeriveOverride(tuned)).toBeNull()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('applied: true yields an override carrying both levels verbatim', () => {
|
|
34
|
+
const override = toDeriveOverride({ ...DEFAULT_STATE, applied: true, radius: -2, density: 3 })
|
|
35
|
+
expect(override).not.toBeNull()
|
|
36
|
+
expect(override?.radiusLevel).toBe(-2)
|
|
37
|
+
expect(override?.densityLevel).toBe(3)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
describe('the resolved config carries exactly the six derive knobs', () => {
|
|
42
|
+
const override = toDeriveOverride({
|
|
43
|
+
...DEFAULT_STATE,
|
|
44
|
+
applied: true,
|
|
45
|
+
accent: '#ff0000',
|
|
46
|
+
neutral: 'stone',
|
|
47
|
+
radius: -2,
|
|
48
|
+
density: 1,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('no state-only key leaks into the config', () => {
|
|
52
|
+
// Keyed off `DEFAULT_DERIVE_CONFIG` rather than an exported key list: adding a seventh knob to
|
|
53
|
+
// the config should extend this assertion automatically, not silently pass a stale list.
|
|
54
|
+
expect(Object.keys(override?.config ?? {}).sort()).toEqual(
|
|
55
|
+
Object.keys(DEFAULT_DERIVE_CONFIG).sort(),
|
|
56
|
+
)
|
|
57
|
+
expect(override?.config).not.toHaveProperty('applied')
|
|
58
|
+
expect(override?.config).not.toHaveProperty('radius')
|
|
59
|
+
expect(override?.config).not.toHaveProperty('density')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('the tuned knobs are carried through', () => {
|
|
63
|
+
expect(override?.config.accent).toBe('#ff0000')
|
|
64
|
+
expect(override?.config.neutral).toBe('stone')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('untouched knobs fall back to the shipped default per-knob', () => {
|
|
68
|
+
expect(override?.config.lightLevel).toBe(DEFAULT_DERIVE_CONFIG.lightLevel)
|
|
69
|
+
expect(override?.config.vibrancy).toBe(DEFAULT_DERIVE_CONFIG.vibrancy)
|
|
70
|
+
})
|
|
71
|
+
})
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme lab's persisted derive state — the Mantine-free half of `DeriveControls`.
|
|
3
|
+
*
|
|
4
|
+
* Split out of `derive-controls.tsx` because TWO layers read it now, and only one of them may see
|
|
5
|
+
* `@mantine/core`:
|
|
6
|
+
*
|
|
7
|
+
* - `derive-controls.tsx` (the panel UI) reads AND writes it, and injects the `--vx-*` override
|
|
8
|
+
* `<style>` from it.
|
|
9
|
+
* - `provider/lab-theme.ts` (the root layer) reads it to rebuild the actual Mantine theme object,
|
|
10
|
+
* so the numeric `defaultProps` and `theme.spacing` a `<style>` tag can never reach follow the
|
|
11
|
+
* sliders too.
|
|
12
|
+
*
|
|
13
|
+
* If the provider imported the panel module instead, every consumer's root chunk would pull the
|
|
14
|
+
* whole dev-tool UI (Slider/ColorInput/SegmentedControl) along with the six lines of state it
|
|
15
|
+
* actually needs. This module has zero `@mantine/*` and zero JSX for exactly that reason.
|
|
16
|
+
*/
|
|
17
|
+
import { createPersistedState, readPersistedValue } from '../state'
|
|
18
|
+
import { DEFAULT_DERIVE_CONFIG, resolveDeriveConfig } from '../tokens/derive'
|
|
19
|
+
import type { DeriveConfig } from '../tokens/derive'
|
|
20
|
+
|
|
21
|
+
const STORAGE_KEY = 'theme-lab-derive'
|
|
22
|
+
// v4: `deriveSpacing`'s accepted range narrowed from [-5, 5] to [-3, 3] (see that function's JSDoc,
|
|
23
|
+
// tokens/palette.ts) — bumped so a v3 envelope holding an out-of-range `density` (e.g. 4 or -5) from
|
|
24
|
+
// an earlier session fails validation and falls back to the default state instead of reaching
|
|
25
|
+
// `deriveSpacing` at render and throwing (a live crash path, not a nicety).
|
|
26
|
+
const STORAGE_VERSION = 4
|
|
27
|
+
|
|
28
|
+
/** Neutral-family choices — plain label/value data, rendered by the panel's `SegmentedControl`. */
|
|
29
|
+
export const NEUTRAL_OPTIONS = [
|
|
30
|
+
{ label: 'Zinc', value: 'zinc' },
|
|
31
|
+
{ label: 'Neutral', value: 'neutral' },
|
|
32
|
+
{ label: 'Stone', value: 'stone' },
|
|
33
|
+
{ label: 'Slate', value: 'slate' },
|
|
34
|
+
]
|
|
35
|
+
const NEUTRAL_VALUES = new Set(NEUTRAL_OPTIONS.map((o) => o.value))
|
|
36
|
+
|
|
37
|
+
const HEX_RE = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i
|
|
38
|
+
|
|
39
|
+
/** A full 3- or 6-digit hex — the gate a `ColorInput` keystroke must clear before it reaches state. */
|
|
40
|
+
export const isAccentHex = (value: string): boolean => HEX_RE.test(value)
|
|
41
|
+
|
|
42
|
+
/** Exported for `derive-controls.test.ts` — the v2→v3 migration test needs the shape + default. */
|
|
43
|
+
export type PersistedDeriveState = DeriveConfig & {
|
|
44
|
+
applied: boolean
|
|
45
|
+
radius: number
|
|
46
|
+
density: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const DEFAULT_STATE: PersistedDeriveState = {
|
|
50
|
+
...DEFAULT_DERIVE_CONFIG,
|
|
51
|
+
applied: false,
|
|
52
|
+
radius: 0,
|
|
53
|
+
density: 0,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const isLevel = (v: unknown): v is number =>
|
|
57
|
+
typeof v === 'number' && Number.isInteger(v) && v >= -5 && v <= 5
|
|
58
|
+
|
|
59
|
+
// Density's own validator — narrower than `isLevel` above (see `DENSITY_LEVEL_MARKS` in
|
|
60
|
+
// derive-controls.tsx). Kept separate rather than narrowing `isLevel` itself: radius and the four
|
|
61
|
+
// color knobs legitimately still need the full [-5, 5] range.
|
|
62
|
+
const isDensityLevel = (v: unknown): v is number =>
|
|
63
|
+
typeof v === 'number' && Number.isInteger(v) && v >= -3 && v <= 3
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Validate + normalize a persisted envelope, or return null if it is unusable. Exported for
|
|
67
|
+
* `derive-controls.test.ts` — the pure, headless seam to test the v1→v2 migration through: no DOM
|
|
68
|
+
* render harness is configured in this package (see `../provider/build-fonts-css.test.ts`), so
|
|
69
|
+
* `createPersistedState`'s full localStorage round-trip isn't reachable from a unit test, but this
|
|
70
|
+
* is exactly the validator `parseStorage` (`../state`) falls back to `initial` from on a rejection
|
|
71
|
+
* (this module passes no `migrate`, so a v1 envelope — pre-radius — hits this same rejection path).
|
|
72
|
+
*/
|
|
73
|
+
export function parsePersistedDeriveState(value: unknown): PersistedDeriveState | null {
|
|
74
|
+
if (typeof value !== 'object' || value === null) return null
|
|
75
|
+
const v = value as Record<string, unknown>
|
|
76
|
+
const valid =
|
|
77
|
+
typeof v['accent'] === 'string' &&
|
|
78
|
+
HEX_RE.test(v['accent']) &&
|
|
79
|
+
typeof v['neutral'] === 'string' &&
|
|
80
|
+
NEUTRAL_VALUES.has(v['neutral']) &&
|
|
81
|
+
isLevel(v['lightLevel']) &&
|
|
82
|
+
isLevel(v['darkLevel']) &&
|
|
83
|
+
isLevel(v['vibrancy']) &&
|
|
84
|
+
isLevel(v['accentBrightness']) &&
|
|
85
|
+
isLevel(v['radius']) &&
|
|
86
|
+
isDensityLevel(v['density']) &&
|
|
87
|
+
typeof v['applied'] === 'boolean'
|
|
88
|
+
if (!valid) return null
|
|
89
|
+
return {
|
|
90
|
+
accent: v['accent'] as string,
|
|
91
|
+
neutral: v['neutral'] as DeriveConfig['neutral'],
|
|
92
|
+
lightLevel: v['lightLevel'] as number,
|
|
93
|
+
darkLevel: v['darkLevel'] as number,
|
|
94
|
+
vibrancy: v['vibrancy'] as number,
|
|
95
|
+
accentBrightness: v['accentBrightness'] as number,
|
|
96
|
+
radius: v['radius'] as number,
|
|
97
|
+
density: v['density'] as number,
|
|
98
|
+
applied: v['applied'] as boolean,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The persisted-state hook. ONE module-scoped store (see `createPersistedState`), so the panel and
|
|
104
|
+
* `BasaltProvider` subscribe to the SAME instance — dragging a slider notifies both in the same
|
|
105
|
+
* tick, which is what keeps the injected CSS and the rebuilt theme object from ever disagreeing.
|
|
106
|
+
*/
|
|
107
|
+
export const useDeriveControlsState = createPersistedState<PersistedDeriveState>({
|
|
108
|
+
key: STORAGE_KEY,
|
|
109
|
+
version: STORAGE_VERSION,
|
|
110
|
+
initial: DEFAULT_STATE,
|
|
111
|
+
schema: {
|
|
112
|
+
'~standard': {
|
|
113
|
+
version: 1,
|
|
114
|
+
vendor: 'basalt-derive-controls',
|
|
115
|
+
validate: (value) => {
|
|
116
|
+
const parsed = parsePersistedDeriveState(value)
|
|
117
|
+
return parsed !== null
|
|
118
|
+
? { value: parsed }
|
|
119
|
+
: { issues: [{ message: 'invalid persisted derive-controls state' }] }
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* An ACTIVE lab override — the resolved color config plus the two integer levels. Both consumers
|
|
127
|
+
* (the `<style>` injection and the theme rebuild) take this exact bundle, so they can never drift
|
|
128
|
+
* apart on which config is live.
|
|
129
|
+
*/
|
|
130
|
+
export type DeriveOverride = { config: DeriveConfig; radiusLevel: number; densityLevel: number }
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The live override for a state value, or `null` when the "Apply" switch is off.
|
|
134
|
+
*
|
|
135
|
+
* Builds the `DeriveConfig` from the six knobs EXPLICITLY rather than spreading the whole state —
|
|
136
|
+
* `resolveDeriveConfig` is a plain spread, so handing it the persisted state would carry
|
|
137
|
+
* `applied`/`radius`/`density` into the config object (and from there onto
|
|
138
|
+
* `theme.other.basaltDerive`) as junk keys.
|
|
139
|
+
*/
|
|
140
|
+
export function toDeriveOverride(state: PersistedDeriveState): DeriveOverride | null {
|
|
141
|
+
if (!state.applied) return null
|
|
142
|
+
return {
|
|
143
|
+
config: resolveDeriveConfig({
|
|
144
|
+
accent: state.accent,
|
|
145
|
+
neutral: state.neutral,
|
|
146
|
+
lightLevel: state.lightLevel,
|
|
147
|
+
darkLevel: state.darkLevel,
|
|
148
|
+
vibrancy: state.vibrancy,
|
|
149
|
+
accentBrightness: state.accentBrightness,
|
|
150
|
+
}),
|
|
151
|
+
radiusLevel: state.radius,
|
|
152
|
+
densityLevel: state.density,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Read the active override straight from localStorage, outside React — the chunk-load path, before
|
|
158
|
+
* any component mounts. Fully validated by {@link parsePersistedDeriveState}, so it cannot throw.
|
|
159
|
+
*/
|
|
160
|
+
export function readAppliedDeriveOverride(): DeriveOverride | null {
|
|
161
|
+
const persisted = parsePersistedDeriveState(readPersistedValue(STORAGE_KEY, STORAGE_VERSION))
|
|
162
|
+
return persisted === null ? null : toDeriveOverride(persisted)
|
|
163
|
+
}
|