@unpunnyfuns/swatchbook-blocks 0.59.0 → 0.60.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/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as _$react from "react";
2
2
  import { ReactElement, ReactNode } from "react";
3
- import { Axis, AxisVarianceResult, Diagnostic, Preset, TupleKey } from "@unpunnyfuns/swatchbook-core";
3
+ import { TokenGraph } from "@unpunnyfuns/swatchbook-core/graph";
4
+ import { Axis, Diagnostic, Preset } from "@unpunnyfuns/swatchbook-core";
4
5
  import { SlimListedToken } from "@unpunnyfuns/swatchbook-core/snapshot-for-wire";
5
6
 
6
7
  //#region src/format-color.d.ts
@@ -101,20 +102,11 @@ interface VirtualTokenShape {
101
102
  type VirtualTokenListingShape = SlimListedToken;
102
103
  type VirtualPresetShape = Preset;
103
104
  /**
104
- * Wire shape of one `Project.jointOverrides` entrysame as core's
105
- * `JointOverride` but with the block's `VirtualTokenShape` for the
106
- * token values that ship over the wire.
105
+ * Wire shape of the token graphaliased from core's authoritative
106
+ * `TokenGraph` so both the virtual module declaration and the React
107
+ * context stay aligned with the same definition.
107
108
  */
108
- interface VirtualJointOverrideShape {
109
- axes: Record<string, string>;
110
- tokens: Record<string, VirtualTokenShape>;
111
- }
112
- /**
113
- * Map from path → cached `AxisVarianceResult`. Snapshot carries this
114
- * so the `AxisVariance` block can O(1) look up which axes affect a
115
- * token instead of re-running variance analysis on every render.
116
- */
117
- type VirtualVarianceByPathShape = Record<string, AxisVarianceResult>;
109
+ type VirtualTokenGraph = TokenGraph;
118
110
  /**
119
111
  * Full project data read by blocks. Populated by the addon's preview
120
112
  * decorator (from the virtual module) or constructed by hand in
@@ -139,35 +131,19 @@ interface ProjectSnapshot {
139
131
  */
140
132
  listing?: Readonly<Record<string, VirtualTokenListingShape>>;
141
133
  /**
142
- * Per-axis cell maps `cells[axis][context]` is the resolved token
143
- * data for `{ ...defaults, [axis]: context }`. Bounded by
144
- * `Σ(axes × contexts)` regardless of cartesian product size.
145
- * Non-default cells store only the tokens whose value differs from
146
- * the default-cell baseline (delta cells).
147
- */
148
- cells: Record<string, Record<string, Record<string, VirtualTokenShape>>>;
149
- /**
150
- * `Project.jointOverrides` flattened to entries for wire transport.
151
- * Same ascending-arity iteration order the Map carries on the
152
- * server side. Empty array when no joint divergences exist.
153
- */
154
- jointOverrides: readonly (readonly [TupleKey, VirtualJointOverrideShape])[];
155
- /**
156
- * Cached per-path variance results. Blocks read this for O(1) axis
157
- * variance lookup instead of recomputing on each render.
134
+ * Pre-built token graph for the project. JSON-serializable; nodes
135
+ * carry per-axis writes plus alias edges. The blocks hook backs
136
+ * `resolveAt` and variance from this graph.
158
137
  */
159
- varianceByPath?: VirtualVarianceByPathShape;
138
+ tokenGraph?: VirtualTokenGraph;
160
139
  /** The default tuple — `{ axis: axis.default }` for every axis. */
161
140
  defaultTuple: Record<string, string>;
162
141
  /**
163
142
  * Pre-built `resolveAt(tuple)` accessor. The addon's preview
164
- * decorator instantiates this once per iframe lifetime the
165
- * underlying virtual-module exports (cells, jointOverrides, axes,
166
- * defaultTuple) are stable, so a single resolver instance with
167
- * internal per-tuple memoization is correct and avoids the
168
- * per-render rebuild dance the blocks side used to do. Hand-built
143
+ * decorator instantiates this once per iframe lifetime, backed by
144
+ * `resolveAllAt` over the stable `tokenGraph` export. Hand-built
169
145
  * snapshots (tests, MDX) can omit this; blocks fall back to
170
- * building locally from `cells` / `permutationsResolved`.
146
+ * building a graph-backed resolver from `tokenGraph`.
171
147
  */
172
148
  resolveAt?: (tuple: Record<string, string>) => Record<string, VirtualTokenShape>;
173
149
  }
@@ -883,5 +859,5 @@ declare function TypographyScale({
883
859
  sortDir
884
860
  }: TypographyScaleProps): ReactElement;
885
861
  //#endregion
886
- export { AliasChain, type AliasChainProps, AliasedBy, type AliasedByProps, AxesContext, AxisVariance, type AxisVarianceProps, BorderPreview, type BorderPreviewProps, BorderSample, type BorderSampleProps, COLOR_FORMATS, type ColorFormat, ColorFormatContext, ColorPalette, type ColorPaletteProps, ColorTable, type ColorTableProps, CompositeBreakdown, type CompositeBreakdownProps, CompositePreview, type CompositePreviewProps, ConsumerOutput, type ConsumerOutputProps, Diagnostics, type DiagnosticsProps, DimensionBar, type DimensionBarProps, type DimensionKind, DimensionScale, type DimensionScaleProps, FontFamilySample, type FontFamilySampleProps, FontWeightScale, type FontWeightScaleProps, type FormatColorResult, GradientPalette, type GradientPaletteProps, MotionPreview, type MotionPreviewProps, MotionSample, type MotionSampleProps, type MotionSpeed, type NormalizedColor, OpacityScale, type OpacityScaleProps, type ProjectSnapshot, ShadowPreview, type ShadowPreviewProps, ShadowSample, type ShadowSampleProps, StrokeStyleSample, type StrokeStyleSampleProps, SwatchbookContext, SwatchbookProvider, type SwatchbookProviderProps, ThemeContext, TokenDetail, type TokenDetailProps, TokenHeader, type TokenHeaderProps, TokenNavigator, type TokenNavigatorProps, TokenTable, type TokenTableProps, TokenUsageSnippet, type TokenUsageSnippetProps, TypographyScale, type TypographyScaleProps, type VirtualAxisShape as VirtualAxis, type VirtualAxisShape, type VirtualDiagnosticShape as VirtualDiagnostic, type VirtualDiagnosticShape, type VirtualPresetShape as VirtualPreset, type VirtualPresetShape, type VirtualTokenShape as VirtualToken, type VirtualTokenShape, type VirtualTokenListingShape, formatColor, useActiveAxes, useActiveTheme, useColorFormat, useOptionalSwatchbookData, useSwatchbookData };
862
+ export { AliasChain, type AliasChainProps, AliasedBy, type AliasedByProps, AxesContext, AxisVariance, type AxisVarianceProps, BorderPreview, type BorderPreviewProps, BorderSample, type BorderSampleProps, COLOR_FORMATS, type ColorFormat, ColorFormatContext, ColorPalette, type ColorPaletteProps, ColorTable, type ColorTableProps, CompositeBreakdown, type CompositeBreakdownProps, CompositePreview, type CompositePreviewProps, ConsumerOutput, type ConsumerOutputProps, Diagnostics, type DiagnosticsProps, DimensionBar, type DimensionBarProps, type DimensionKind, DimensionScale, type DimensionScaleProps, FontFamilySample, type FontFamilySampleProps, FontWeightScale, type FontWeightScaleProps, type FormatColorResult, GradientPalette, type GradientPaletteProps, MotionPreview, type MotionPreviewProps, MotionSample, type MotionSampleProps, type MotionSpeed, type NormalizedColor, OpacityScale, type OpacityScaleProps, type ProjectSnapshot, ShadowPreview, type ShadowPreviewProps, ShadowSample, type ShadowSampleProps, StrokeStyleSample, type StrokeStyleSampleProps, SwatchbookContext, SwatchbookProvider, type SwatchbookProviderProps, ThemeContext, TokenDetail, type TokenDetailProps, TokenHeader, type TokenHeaderProps, TokenNavigator, type TokenNavigatorProps, TokenTable, type TokenTableProps, TokenUsageSnippet, type TokenUsageSnippetProps, TypographyScale, type TypographyScaleProps, type VirtualAxisShape as VirtualAxis, type VirtualAxisShape, type VirtualDiagnosticShape as VirtualDiagnostic, type VirtualDiagnosticShape, type VirtualPresetShape as VirtualPreset, type VirtualPresetShape, type VirtualTokenShape as VirtualToken, type VirtualTokenShape, type VirtualTokenGraph, type VirtualTokenListingShape, formatColor, useActiveAxes, useActiveTheme, useColorFormat, useOptionalSwatchbookData, useSwatchbookData };
887
863
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -2,12 +2,12 @@ import './style.css';
2
2
  import Color from "colorjs.io";
3
3
  import { createContext, memo, useCallback, useContext, useDeferredValue, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
4
4
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
- import { buildResolveAt } from "@unpunnyfuns/swatchbook-core/resolve-at";
5
+ import { getVariance, listPaths, resolveAllAt } from "@unpunnyfuns/swatchbook-core/graph";
6
6
  import { makeCssVar } from "@unpunnyfuns/swatchbook-core/css-var";
7
7
  import { SWATCHBOOK_STYLE_ELEMENT_ID, ensureStyleElement } from "@unpunnyfuns/swatchbook-core/style-element";
8
8
  import { tupleToName } from "@unpunnyfuns/swatchbook-core/themes";
9
9
  import { addons } from "storybook/preview-api";
10
- import { axes, cells, css, cssVarPrefix, defaultTuple, diagnostics, jointOverrides, listing, presets, varianceByPath } from "virtual:swatchbook/tokens";
10
+ import { axes, css, cssVarPrefix, defaultTuple, diagnostics, listing, presets, tokenGraph } from "virtual:swatchbook/tokens";
11
11
  import { dataAttr } from "@unpunnyfuns/swatchbook-core/data-attr";
12
12
  import { matchPath } from "@unpunnyfuns/swatchbook-core/match-path";
13
13
  import { fuzzyFilter } from "@unpunnyfuns/swatchbook-core/fuzzy";
@@ -342,9 +342,7 @@ let snapshot = {
342
342
  css,
343
343
  cssVarPrefix,
344
344
  listing: listing ?? {},
345
- cells: cells ?? {},
346
- jointOverrides: jointOverrides ?? [],
347
- varianceByPath: varianceByPath ?? {},
345
+ tokenGraph,
348
346
  defaultTuple: defaultTuple ?? {},
349
347
  version: 0
350
348
  };
@@ -361,9 +359,7 @@ function ensureSubscribed() {
361
359
  css: payload.css ?? snapshot.css,
362
360
  cssVarPrefix: payload.cssVarPrefix ?? snapshot.cssVarPrefix,
363
361
  listing: payload.listing ?? snapshot.listing,
364
- cells: payload.cells ?? snapshot.cells,
365
- jointOverrides: payload.jointOverrides ?? snapshot.jointOverrides,
366
- varianceByPath: payload.varianceByPath ?? snapshot.varianceByPath,
362
+ tokenGraph: payload.tokenGraph ?? snapshot.tokenGraph,
367
363
  defaultTuple: payload.defaultTuple ?? snapshot.defaultTuple,
368
364
  version: snapshot.version + 1
369
365
  };
@@ -395,31 +391,27 @@ function defaultTuple$1(axes) {
395
391
  return out;
396
392
  }
397
393
  /**
398
- * Reconstruct a `resolveAt` accessor from snapshot data. Both `cells`
399
- * and `jointOverrides` ship as plain JSON in the same shape core uses
400
- * internally no Map reconstruction at the boundary. Stable identity
401
- * across calls with the same snapshot `useMemo` keyed on the
402
- * snapshot fields produces a referentially stable function.
394
+ * Build a `resolveAt` accessor backed by the token graph. Returns an
395
+ * empty resolver when no graph is present (test stubs, partial
396
+ * snapshots). Stable identity when memoized on `tokenGraph` the
397
+ * graph is a module-level virtual-module export so its reference stays
398
+ * constant for the lifetime of the iframe.
403
399
  */
404
- function makeResolveAt(snapshot) {
405
- const cells = snapshot.cells ?? {};
406
- const jointOverrides = snapshot.jointOverrides ?? [];
407
- const defaults = snapshot.defaultTuple ?? defaultTuple$1(snapshot.axes);
408
- const resolver = buildResolveAt(snapshot.axes, cells, jointOverrides, defaults);
409
- return (tuple) => resolver(tuple);
400
+ function makeResolveAt(graph) {
401
+ if (!graph) return () => ({});
402
+ return (tuple) => resolveAllAt(graph, tuple);
410
403
  }
411
404
  /**
412
405
  * Build the `resolveAt` accessor for a snapshot. Prefers the
413
406
  * snapshot's own `resolveAt` (the addon's preview decorator
414
407
  * pre-builds one at module load — see `previewResolveAt` in
415
- * `packages/addon/src/preview.tsx`), otherwise composes one from
416
- * `cells` + `jointOverrides` via `makeResolveAt`. Hand-built
417
- * snapshots should provide both via the test `withCellsShape`
418
- * helper or by populating the fields directly.
408
+ * `packages/addon/src/preview.tsx`), otherwise builds one from
409
+ * `tokenGraph`. Hand-built snapshots can omit `resolveAt`;
410
+ * the graph-backed fallback covers them.
419
411
  */
420
412
  function snapshotResolveAt(snapshot) {
421
413
  if (snapshot.resolveAt) return snapshot.resolveAt;
422
- return makeResolveAt(snapshot);
414
+ return makeResolveAt(snapshot.tokenGraph);
423
415
  }
424
416
  /**
425
417
  * Reads project data either from a mounted {@link SwatchbookProvider}
@@ -436,25 +428,22 @@ function snapshotResolveAt(snapshot) {
436
428
  function useProject() {
437
429
  const snapshot = useOptionalSwatchbookData();
438
430
  const axes = snapshot?.axes;
439
- const cells = snapshot?.cells;
440
- const jointOverrides = snapshot?.jointOverrides;
441
- const dataDefaultTuple = snapshot?.defaultTuple;
442
431
  const activeAxes = snapshot?.activeAxes;
443
432
  const activeTheme = snapshot?.activeTheme;
444
433
  const diagnostics = snapshot?.diagnostics;
445
434
  const cssVarPrefix = snapshot?.cssVarPrefix;
446
435
  const listing = snapshot?.listing;
447
- const varianceByPath = snapshot?.varianceByPath;
436
+ const tokenGraph = snapshot?.tokenGraph;
448
437
  const resolveAt = useMemo(() => {
449
438
  if (!snapshot) return null;
450
439
  return snapshotResolveAt(snapshot);
451
- }, [
452
- axes,
453
- cells,
454
- jointOverrides,
455
- dataDefaultTuple,
456
- activeTheme
457
- ]);
440
+ }, [tokenGraph, activeTheme]);
441
+ const derivedVarianceByPath = useMemo(() => {
442
+ if (!tokenGraph) return {};
443
+ const out = {};
444
+ for (const path of listPaths(tokenGraph)) out[path] = getVariance(tokenGraph, path);
445
+ return out;
446
+ }, [tokenGraph]);
458
447
  const providerData = useMemo(() => {
459
448
  if (!snapshot || !resolveAt || !axes || !activeAxes) return null;
460
449
  return {
@@ -465,22 +454,26 @@ function useProject() {
465
454
  diagnostics: diagnostics ?? [],
466
455
  cssVarPrefix: cssVarPrefix ?? "",
467
456
  listing: listing ?? {},
468
- varianceByPath: varianceByPath ?? {},
457
+ varianceByPath: derivedVarianceByPath,
458
+ tokenGraph: tokenGraph ?? {
459
+ nodes: {},
460
+ axes: [],
461
+ axisDefaults: {},
462
+ axisContexts: {}
463
+ },
469
464
  resolveAt
470
465
  };
471
466
  }, [
472
467
  snapshot,
473
468
  resolveAt,
474
469
  axes,
475
- cells,
476
- jointOverrides,
477
- dataDefaultTuple,
478
470
  activeTheme,
479
471
  activeAxes,
480
472
  diagnostics,
481
473
  cssVarPrefix,
482
474
  listing,
483
- varianceByPath
475
+ derivedVarianceByPath,
476
+ tokenGraph
484
477
  ]);
485
478
  const fallback = useVirtualModuleFallback(snapshot === null);
486
479
  return providerData ?? fallback;
@@ -509,17 +502,14 @@ function useVirtualModuleFallback(enabled) {
509
502
  tokens.axes
510
503
  ]);
511
504
  const activeTheme = contextPermutation || tupleToName(tokens.axes, activeAxes);
512
- const resolveAt = useMemo(() => makeResolveAt({
513
- axes: tokens.axes,
514
- cells: tokens.cells,
515
- jointOverrides: tokens.jointOverrides,
516
- defaultTuple: tokens.defaultTuple
517
- }), [
518
- tokens.axes,
519
- tokens.cells,
520
- tokens.jointOverrides,
521
- tokens.defaultTuple
522
- ]);
505
+ const resolveAt = useMemo(() => makeResolveAt(tokens.tokenGraph), [tokens.tokenGraph]);
506
+ const fallbackVarianceByPath = useMemo(() => {
507
+ const graph = tokens.tokenGraph;
508
+ if (!graph) return {};
509
+ const out = {};
510
+ for (const path of listPaths(graph)) out[path] = getVariance(graph, path);
511
+ return out;
512
+ }, [tokens.tokenGraph]);
523
513
  return useMemo(() => ({
524
514
  activeTheme,
525
515
  activeAxes,
@@ -528,7 +518,8 @@ function useVirtualModuleFallback(enabled) {
528
518
  diagnostics: tokens.diagnostics,
529
519
  cssVarPrefix: tokens.cssVarPrefix,
530
520
  listing: tokens.listing,
531
- varianceByPath: tokens.varianceByPath,
521
+ varianceByPath: fallbackVarianceByPath,
522
+ tokenGraph: tokens.tokenGraph,
532
523
  resolveAt
533
524
  }), [
534
525
  activeTheme,
@@ -537,7 +528,8 @@ function useVirtualModuleFallback(enabled) {
537
528
  tokens.diagnostics,
538
529
  tokens.cssVarPrefix,
539
530
  tokens.listing,
540
- tokens.varianceByPath,
531
+ fallbackVarianceByPath,
532
+ tokens.tokenGraph,
541
533
  resolveAt
542
534
  ]);
543
535
  }