@weasel-js/labkit 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/README.md +20 -2
  2. package/dist/_dts/{CanvasStackContext-kjILVnPj.d.ts → CanvasStackContext-kTEZvEgE.d.ts} +1 -1
  3. package/dist/_dts/{PrefsForm.d-CgxUequc.d.ts → PrefsForm.d-DDHFANkc.d.ts} +1 -1
  4. package/dist/_dts/{frac-A4R6v4ld.d.ts → frac-z7ker2Vx.d.ts} +11 -7
  5. package/dist/_dts/{index-CsU9WhjM.d.ts → index-Blv9uQzu.d.ts} +42 -9
  6. package/dist/_dts/types-B9_zrHmb.d.ts +166 -0
  7. package/dist/_dts/{types-BbXpvQa8.d.ts → types-BP2OCcpg.d.ts} +21 -3
  8. package/dist/_dts/types-x92Kfeme.d.ts +62 -0
  9. package/dist/_dts/{useTrialState-DKjqpv20.d.ts → useTrialState-D6Vb3T-g.d.ts} +7 -7
  10. package/dist/canvas/index.d.ts +9 -8
  11. package/dist/canvas/index.js +1 -2
  12. package/dist/chrome/index.d.ts +7 -6
  13. package/dist/chrome/index.js +5 -5
  14. package/dist/{chunk-ULDW42CR.js → chunk-2P6PP5N4.js} +33 -23
  15. package/dist/chunk-2P6PP5N4.js.map +1 -0
  16. package/dist/{chunk-W2FJR5FF.js → chunk-64ZCN3DA.js} +74 -76
  17. package/dist/chunk-64ZCN3DA.js.map +1 -0
  18. package/dist/{chunk-NSOVI3AZ.js → chunk-BKFVHKJH.js} +6 -6
  19. package/dist/{chunk-NSOVI3AZ.js.map → chunk-BKFVHKJH.js.map} +1 -1
  20. package/dist/chunk-E44SU6XS.js +50 -0
  21. package/dist/chunk-E44SU6XS.js.map +1 -0
  22. package/dist/{chunk-WS6ZRV75.js → chunk-FQMJUVHJ.js} +3 -3
  23. package/dist/{chunk-WS6ZRV75.js.map → chunk-FQMJUVHJ.js.map} +1 -1
  24. package/dist/{chunk-FJG4PHTL.js → chunk-I6JCVE24.js} +3 -3
  25. package/dist/chunk-I6JCVE24.js.map +1 -0
  26. package/dist/{chunk-UDXOYZEC.js → chunk-MOM3GOVY.js} +4 -4
  27. package/dist/{chunk-UDXOYZEC.js.map → chunk-MOM3GOVY.js.map} +1 -1
  28. package/dist/{chunk-TN7YSJVU.js → chunk-RS3HRQWU.js} +3 -3
  29. package/dist/{chunk-TN7YSJVU.js.map → chunk-RS3HRQWU.js.map} +1 -1
  30. package/dist/{chunk-UTOEDPNU.js → chunk-SMHP6XZ4.js} +7 -6
  31. package/dist/chunk-SMHP6XZ4.js.map +1 -0
  32. package/dist/{chunk-D5KQ5OY6.js → chunk-TJ7QY3OC.js} +3 -3
  33. package/dist/{chunk-D5KQ5OY6.js.map → chunk-TJ7QY3OC.js.map} +1 -1
  34. package/dist/{chunk-XKENZTNE.js → chunk-U3IYHIAE.js} +71 -55
  35. package/dist/chunk-U3IYHIAE.js.map +1 -0
  36. package/dist/chunk-W3ECWC2K.js +7335 -0
  37. package/dist/chunk-W3ECWC2K.js.map +1 -0
  38. package/dist/controls/index.d.ts +5 -4
  39. package/dist/controls/index.js +3 -3
  40. package/dist/dragdrop/index.d.ts +6 -5
  41. package/dist/dragdrop/index.js +1 -2
  42. package/dist/index.d.ts +139 -26
  43. package/dist/index.js +309 -144
  44. package/dist/index.js.map +1 -1
  45. package/dist/layers/index.d.ts +7 -6
  46. package/dist/layers/index.js +2 -3
  47. package/dist/loupe/index.d.ts +8 -7
  48. package/dist/loupe/index.js +1 -2
  49. package/dist/passthrough/weasel-canvas.d.ts +1 -3
  50. package/dist/passthrough/weasel-canvas.js +1 -1
  51. package/dist/passthrough/weasel-canvas.js.map +1 -1
  52. package/dist/passthrough/weasel-ui.d.ts +98 -20
  53. package/dist/passthrough/weasel-ui.js +1 -2
  54. package/dist/primitives/index.js +3 -4
  55. package/dist/state/index.d.ts +6 -3
  56. package/dist/state/index.js +3 -2
  57. package/dist/state/index.js.map +1 -1
  58. package/dist/styles.css +82 -15
  59. package/dist/surface/index.js +1 -2
  60. package/dist/ui/layers/index.js +1 -2
  61. package/dist/undo/index.d.ts +6 -6
  62. package/package.json +7 -7
  63. package/src/annotations/ExportMenu.test.tsx +16 -0
  64. package/src/annotations/ExportMenu.tsx +0 -5
  65. package/src/annotations/frac.test.ts +4 -4
  66. package/src/annotations/frac.ts +3 -2
  67. package/src/annotations/index.ts +1 -1
  68. package/src/annotations/store.ts +2 -2
  69. package/src/annotations/svgNodes.ts +1 -2
  70. package/src/config/builder.test.ts +56 -2
  71. package/src/config/builder.ts +73 -13
  72. package/src/config/index.ts +16 -1
  73. package/src/config/path.test.ts +109 -0
  74. package/src/config/path.ts +76 -0
  75. package/src/config/resolve.test.ts +93 -2
  76. package/src/config/resolve.ts +72 -29
  77. package/src/config/types.ts +99 -9
  78. package/src/config/visible.ts +6 -4
  79. package/src/controls/ControlPanel.stories.tsx +38 -4
  80. package/src/controls/ControlPanel.test.tsx +114 -0
  81. package/src/controls/ControlPanel.tsx +99 -50
  82. package/src/index.ts +17 -2
  83. package/src/instrument/serializers.ts +36 -0
  84. package/src/instrument/types.ts +4 -2
  85. package/src/lab/Lab.test.tsx +29 -1
  86. package/src/lab/Lab.tsx +10 -1
  87. package/src/lab/LabShell.test.tsx +8 -0
  88. package/src/lab/LabShell.tsx +17 -3
  89. package/src/lab/LabSwitcher.less +85 -0
  90. package/src/lab/LabSwitcher.test.tsx +96 -0
  91. package/src/lab/LabSwitcher.tsx +112 -0
  92. package/src/lab/index.ts +2 -0
  93. package/src/primitives/ZoomControl.less +6 -2
  94. package/src/primitives/ZoomControl.tsx +1 -0
  95. package/src/state/helpers.test.ts +20 -0
  96. package/src/state/helpers.ts +17 -1
  97. package/src/state/store.test.ts +155 -1
  98. package/src/state/store.ts +32 -26
  99. package/src/state/types.ts +24 -3
  100. package/src/state/useTrialState.ts +1 -1
  101. package/src/styles.less +1 -0
  102. package/src/theme/base.less +4 -4
  103. package/src/trial/Trial.config.test.tsx +44 -0
  104. package/src/trial/Trial.tsx +3 -3
  105. package/src/trial/TrialChrome.tsx +10 -5
  106. package/src/trial/trialOps.ts +7 -10
  107. package/dist/_dts/types-lg4TSCb2.d.ts +0 -164
  108. package/dist/_dts/weasel-canvas-FJTFi3ZZ.d.ts +0 -5041
  109. package/dist/chunk-54ZWZ5FQ.js +0 -29997
  110. package/dist/chunk-54ZWZ5FQ.js.map +0 -1
  111. package/dist/chunk-FJG4PHTL.js.map +0 -1
  112. package/dist/chunk-ISSVF5PT.js +0 -7263
  113. package/dist/chunk-ISSVF5PT.js.map +0 -1
  114. package/dist/chunk-ULDW42CR.js.map +0 -1
  115. package/dist/chunk-UTOEDPNU.js.map +0 -1
  116. package/dist/chunk-W2FJR5FF.js.map +0 -1
  117. package/dist/chunk-XKENZTNE.js.map +0 -1
@@ -1,3 +1,4 @@
1
+ import type { PrefGroup } from '@weasel-js/ui';
1
2
  import { describe, expect, it } from 'vitest';
2
3
  import { f } from './builder';
3
4
  import { resolveConfigSchema } from './resolve';
@@ -88,8 +89,23 @@ describe('resolveConfigSchema', () => {
88
89
  [],
89
90
  );
90
91
  expect(r.sections).toEqual([
91
- { label: 'Two', paths: ['b', 'c'] },
92
- { label: 'One', paths: ['a'] },
92
+ { at: '', label: 'Two', paths: ['b', 'c'] },
93
+ { at: '', label: 'One', paths: ['a'] },
94
+ ]);
95
+ });
96
+
97
+ it('takes `collapsed` from any one leaf under the heading', () => {
98
+ const r = resolveConfigSchema(
99
+ f.schema({
100
+ a: f.number(1).section('Advanced'),
101
+ b: f.number(1).section('Advanced', { collapsed: true }),
102
+ c: f.number(1).section('Plain'),
103
+ }),
104
+ [],
105
+ );
106
+ expect(r.sections).toEqual([
107
+ { at: '', label: 'Advanced', paths: ['a', 'b'], collapsed: true },
108
+ { at: '', label: 'Plain', paths: ['c'] },
93
109
  ]);
94
110
  });
95
111
 
@@ -117,3 +133,78 @@ describe('resolveConfigSchema', () => {
117
133
  expect(Object.keys(r.group.children)).toEqual(['z', 'a']);
118
134
  });
119
135
  });
136
+
137
+ describe('resolveConfigSchema / nested groups', () => {
138
+ const nested = () =>
139
+ resolveConfigSchema(
140
+ f.schema({
141
+ showGrid: f.boolean(true),
142
+ grid: f
143
+ .group({
144
+ size: f.number(20).range(5, 80),
145
+ color: f.color('#ffffff').showIf((c) => c.showGrid === true),
146
+ })
147
+ .label('Grid'),
148
+ }),
149
+ [],
150
+ );
151
+
152
+ it('emits a nested PrefGroup, not a flattened child', () => {
153
+ const r = nested();
154
+ const grid = r.group.children.grid as PrefGroup;
155
+ expect('children' in grid).toBe(true);
156
+ expect(Object.keys(grid.children)).toEqual(['size', 'color']);
157
+ expect(grid.name).toBe('Grid');
158
+ });
159
+
160
+ it('titleCases a group with no label', () => {
161
+ const r = resolveConfigSchema(f.schema({ gridStyle: f.group({ a: f.number(1) }) }), []);
162
+ expect((r.group.children.gridStyle as PrefGroup).name).toBe('Grid style');
163
+ });
164
+
165
+ it('runs the rule chain on a nested leaf, keyed by its own segment', () => {
166
+ const r = nested();
167
+ const grid = r.group.children.grid as PrefGroup;
168
+ const size = grid.children.size as unknown as Record<string, unknown>;
169
+ expect(size.name).toBe('Size');
170
+ expect(size.control).toBe('slider');
171
+ });
172
+
173
+ it('gives a rule the full dotted path alongside the key', () => {
174
+ const seen: { key: string; path: string }[] = [];
175
+ const spy: ConfigRule = (ctx) => {
176
+ seen.push({ key: ctx.key, path: ctx.path });
177
+ return null;
178
+ };
179
+ resolveConfigSchema(f.schema({ grid: f.group({ size: f.number(1) }) }), [spy]);
180
+ expect(seen).toContainEqual({ key: 'size', path: 'grid.size' });
181
+ });
182
+
183
+ it('keys showIf and renderers by the full dotted path', () => {
184
+ const r = nested();
185
+ expect(r.showIf.get('grid.color')?.({ showGrid: false })).toBe(false);
186
+ expect(r.showIf.get('color')).toBeUndefined();
187
+ });
188
+
189
+ it('a section inside a group buckets the children of that group', () => {
190
+ const r = resolveConfigSchema(
191
+ f.schema({
192
+ top: f.number(1).section('Outer'),
193
+ grid: f.group({ size: f.number(1).section('Inner'), color: f.color('#fff') }),
194
+ }),
195
+ [],
196
+ );
197
+ expect(r.sections).toEqual([
198
+ { at: '', label: 'Outer', paths: ['top'] },
199
+ { at: 'grid', label: 'Inner', paths: ['grid.size'] },
200
+ ]);
201
+ });
202
+
203
+ it('a group can itself sit under a section heading', () => {
204
+ const r = resolveConfigSchema(
205
+ f.schema({ grid: f.group({ size: f.number(1) }).section('Advanced') }),
206
+ [],
207
+ );
208
+ expect(r.sections).toEqual([{ at: '', label: 'Advanced', paths: ['grid'] }]);
209
+ });
210
+ });
@@ -1,8 +1,11 @@
1
1
  import type { PrefGroup, PrefLeaf } from '@weasel-js/ui';
2
- import { applyRules, builtinRules } from './rules';
2
+ import { isConfigBranch } from './builder';
3
+ import { applyRules, builtinRules, titleCase } from './rules';
3
4
  import type {
5
+ ConfigEntry,
4
6
  ConfigRule,
5
7
  ConfigSchema,
8
+ ConfigShape,
6
9
  ControlRenderer,
7
10
  LeafPatch,
8
11
  ResolvedConfig,
@@ -17,51 +20,91 @@ function defined(annotations: Record<string, unknown>): LeafPatch {
17
20
  return out as LeafPatch;
18
21
  }
19
22
 
23
+ /** What the walk collects on the side of the `PrefGroup` tree. */
24
+ interface Sink {
25
+ sections: SectionSpec[];
26
+ showIf: Map<string, (config: Record<string, unknown>) => boolean>;
27
+ renderers: Record<string, ControlRenderer>;
28
+ chain: readonly ConfigRule[];
29
+ }
30
+
20
31
  /**
21
32
  * Resolve a schema into the vocabulary weasel-ui renders, running each leaf
22
33
  * through the consumer's rules and then labkit's own.
23
34
  *
24
- * The group is flat — every leaf is a direct child — so a leaf's path is its
25
- * config key and both `ControlPanel` and `PrefsForm` address it identically.
35
+ * The group mirrors the schema's own nesting: an `f.group` becomes a nested
36
+ * `PrefGroup`, so a leaf's dotted path within the tree is the path its value
37
+ * is written at, and both `ControlPanel` and `PrefsForm` address it
38
+ * identically.
26
39
  */
27
40
  export function resolveConfigSchema<TC>(
28
41
  schema: ConfigSchema<TC>,
29
42
  rules: readonly ConfigRule[] = [],
30
43
  ): ResolvedConfig {
31
- const children: Record<string, PrefLeaf> = {};
32
- const sectionOrder: string[] = [];
33
- const sectionPaths = new Map<string, string[]>();
34
- const showIf = new Map<string, (config: Record<string, unknown>) => boolean>();
35
- const renderers: Record<string, ControlRenderer> = {};
44
+ const sink: Sink = {
45
+ sections: [],
46
+ showIf: new Map(),
47
+ renderers: {},
48
+ chain: [...rules, ...builtinRules],
49
+ };
50
+ const group = resolveShape(schema.nodes, '', '', sink);
51
+ return { group, sections: sink.sections, showIf: sink.showIf, renderers: sink.renderers };
52
+ }
36
53
 
37
- const chain = [...rules, ...builtinRules];
54
+ /** A section while it is still being filled. */
55
+ type OpenSection = Omit<SectionSpec, 'paths'> & { paths: string[] };
38
56
 
39
- for (const [key, node] of Object.entries(schema.nodes)) {
40
- const seed: LeafPatch = defined({
41
- ...node.annotations,
42
- ...(node.kind === null ? {} : { kind: node.kind }),
43
- });
57
+ function resolveShape(shape: ConfigShape, at: string, name: string, sink: Sink): PrefGroup {
58
+ const children: Record<string, PrefLeaf | PrefGroup> = {};
59
+ const sections = new Map<string, OpenSection>();
44
60
 
45
- const patch = applyRules(seed, { key, path: key, default: node.default }, chain);
46
- children[key] = { ...patch, default: node.default } as PrefLeaf;
61
+ for (const [key, entry] of Object.entries(shape)) {
62
+ const path = at === '' ? key : `${at}.${key}`;
63
+ children[key] = resolveEntry(entry, key, path, sink);
47
64
 
48
- const { section, showIf: predicate, render } = node.options;
65
+ const { section, showIf: predicate } = entry.options;
49
66
  if (section !== undefined) {
50
- if (!sectionPaths.has(section)) {
51
- sectionOrder.push(section);
52
- sectionPaths.set(section, []);
67
+ let spec = sections.get(section.label);
68
+ if (!spec) {
69
+ spec = { at, label: section.label, paths: [] };
70
+ sections.set(section.label, spec);
71
+ sink.sections.push(spec);
72
+ }
73
+ spec.paths.push(path);
74
+ // One node saying `collapsed` settles the section, so a schema does not
75
+ // have to repeat it on every node under the heading.
76
+ if (section.collapsed !== undefined) {
77
+ spec.collapsed = (spec.collapsed ?? false) || section.collapsed;
53
78
  }
54
- sectionPaths.get(section)?.push(key);
55
79
  }
56
- if (predicate) showIf.set(key, predicate);
57
- if (render) renderers[key] = render;
80
+ if (predicate) sink.showIf.set(path, predicate);
81
+ if (!isConfigBranch(entry) && entry.options.render) sink.renderers[path] = entry.options.render;
58
82
  }
59
83
 
60
- const group: PrefGroup = { name: '', children };
61
- const sections: SectionSpec[] = sectionOrder.map((label) => ({
62
- label,
63
- paths: sectionPaths.get(label) ?? [],
64
- }));
84
+ return { name, children };
85
+ }
65
86
 
66
- return { group, sections, showIf, renderers };
87
+ function resolveEntry(
88
+ entry: ConfigEntry,
89
+ key: string,
90
+ path: string,
91
+ sink: Sink,
92
+ ): PrefLeaf | PrefGroup {
93
+ if (isConfigBranch(entry)) {
94
+ const group = resolveShape(
95
+ entry.children,
96
+ path,
97
+ entry.annotations.name ?? titleCase(key),
98
+ sink,
99
+ );
100
+ return entry.annotations.description === undefined
101
+ ? group
102
+ : { ...group, description: entry.annotations.description };
103
+ }
104
+ const seed: LeafPatch = defined({
105
+ ...entry.annotations,
106
+ ...(entry.kind === null ? {} : { kind: entry.kind }),
107
+ });
108
+ const patch = applyRules(seed, { key, path, default: entry.default }, sink.chain);
109
+ return { ...patch, default: entry.default } as PrefLeaf;
67
110
  }
@@ -39,9 +39,11 @@ export type LeafPatch = Annotations & { kind?: string };
39
39
 
40
40
  /** What a rule is given for the leaf it is deciding about. */
41
41
  export interface ConfigRuleContext {
42
- /** The config key this leaf writes. */
42
+ /** The leaf's own key — its last path segment, and what a label is titled
43
+ * from. */
43
44
  key: string;
44
- /** Dotted path within the schema. Equal to `key` while schemas are flat. */
45
+ /** Dotted path within the schema, which is the path the value is written
46
+ * at. Equal to `key` for a leaf sitting at the root. */
45
47
  path: string;
46
48
  /** The leaf's default value. A rule may read it but never change it. */
47
49
  default: unknown;
@@ -57,17 +59,29 @@ export interface ConfigRuleContext {
57
59
  */
58
60
  export type ConfigRule = (ctx: ConfigRuleContext) => LeafPatch | null;
59
61
 
60
- /** A presentational bucket of leaves, rendered under one heading. */
62
+ /** A presentational bucket of nodes, rendered under one heading. Buckets
63
+ * siblings: a section never nests a value, which is what separates it from
64
+ * `f.group`. */
61
65
  export interface SectionSpec {
66
+ /** Dotted path of the group whose children this buckets. `''` is the root,
67
+ * which is where every section of a flat schema sits. */
68
+ at: string;
62
69
  label: string;
70
+ /** Full dotted paths, so a section under a group names its children the way
71
+ * everything else does. */
63
72
  paths: readonly string[];
73
+ /** Whether the section opens folded. Set, the panel folds this section
74
+ * whether or not it was given a panel-wide `collapse`; a fold the reader
75
+ * has since toggled outranks it. */
76
+ collapsed?: boolean;
64
77
  }
65
78
 
66
79
  /** A schema resolved against a set of rules: the vocabulary weasel-ui renders,
67
80
  * plus the three things labkit keeps on the side because `PrefLeaf` has no
68
81
  * field for them. */
69
82
  export interface ResolvedConfig {
70
- /** Flat: every leaf is a direct child, so a leaf's path is its config key. */
83
+ /** The schema as a `PrefGroup` tree: an `f.group` is a nested group, so a
84
+ * leaf's dotted path within it is the path its value is written at. */
71
85
  group: PrefGroup;
72
86
  sections: readonly SectionSpec[];
73
87
  showIf: ReadonlyMap<string, (config: Record<string, unknown>) => boolean>;
@@ -75,14 +89,34 @@ export interface ResolvedConfig {
75
89
  renderers: Readonly<Record<string, ControlRenderer>>;
76
90
  }
77
91
 
92
+ /** How a node names the section it belongs to: the heading, and how that
93
+ * section opens. Every node in one section repeats the heading; only one of
94
+ * them has to say `collapsed`. */
95
+ export interface SectionOption {
96
+ label: string;
97
+ collapsed?: boolean;
98
+ }
99
+
78
100
  /** Per-node extras that do not belong on a `PrefLeaf`. */
79
- export interface NodeOptions {
80
- section?: string;
81
- showIf?: (config: Record<string, unknown>) => boolean;
101
+ export interface NodeOptions extends BranchOptions {
82
102
  render?: ControlRenderer;
83
103
  validate?: (leaf: PrefLeaf) => string[];
84
104
  }
85
105
 
106
+ /** What a branch can say about itself, beyond its children. */
107
+ export interface BranchOptions {
108
+ section?: SectionOption;
109
+ /** Show this node only while the predicate holds. On a group it hides the
110
+ * whole subtree; the values stay in config either way. */
111
+ showIf?: (config: Record<string, unknown>) => boolean;
112
+ }
113
+
114
+ /** A group's own annotations: what it is called and, optionally, why. */
115
+ export interface BranchAnnotations {
116
+ name?: string;
117
+ description?: string;
118
+ }
119
+
86
120
  /** The builder's leaf: a kind (or null, to be decided by rules), a default,
87
121
  * an annotation bag, and the extras above. */
88
122
  export interface ConfigNode<T = unknown> {
@@ -92,15 +126,71 @@ export interface ConfigNode<T = unknown> {
92
126
  readonly options: Readonly<NodeOptions>;
93
127
  }
94
128
 
129
+ /** The builder's branch: named children, nested as deeply as the schema
130
+ * wants. A branch nests the value too — `grid: f.group({ size })` puts the
131
+ * value at `grid.size`, where `.section('Grid')` would have left it at
132
+ * `size`. */
133
+ export interface ConfigBranch<S extends ConfigShape = ConfigShape> {
134
+ readonly children: S;
135
+ readonly annotations: Readonly<BranchAnnotations>;
136
+ readonly options: Readonly<BranchOptions>;
137
+ }
138
+
139
+ /** Either half of a schema tree. */
140
+ export type ConfigEntry = ConfigNode | ConfigBranch;
141
+
142
+ /** The children of a schema or a group. */
143
+ export type ConfigShape = { readonly [key: string]: ConfigEntry };
144
+
95
145
  /** The value type a node produces. */
96
146
  export type NodeValue<N> = N extends ConfigNode<infer T> ? T : never;
97
147
 
148
+ /** The value type a schema entry produces — a leaf's own, or a branch's
149
+ * nested record. */
150
+ export type EntryValue<E> =
151
+ E extends ConfigBranch<infer S> ? InferConfig<S> : E extends ConfigNode<infer T> ? T : never;
152
+
98
153
  /** The config type a builder shape produces. */
99
- export type InferConfig<S> = { [K in keyof S]: NodeValue<S[K]> };
154
+ export type InferConfig<S> = { [K in keyof S]: EntryValue<S[K]> };
155
+
156
+ /** Whether a config value has children a path can descend into. Arrays and
157
+ * functions are values, not branches. */
158
+ type Branching<V> = V extends readonly unknown[]
159
+ ? false
160
+ : V extends (...args: never[]) => unknown
161
+ ? false
162
+ : V extends object
163
+ ? true
164
+ : false;
165
+
166
+ /**
167
+ * Every dotted path a config offers, a group's own path included. A config
168
+ * whose shape is not known — `unknown`, or a bare record — gives `string`,
169
+ * which is what keeps a generic instrument writable.
170
+ */
171
+ export type ConfigPath<T> = unknown extends T
172
+ ? string
173
+ : T extends object
174
+ ? {
175
+ [K in keyof T & string]: Branching<T[K]> extends true ? K | `${K}.${ConfigPath<T[K]>}` : K;
176
+ }[keyof T & string]
177
+ : never;
178
+
179
+ /** The type at a dotted path, or `unknown` where the path is not one the
180
+ * config's type spells out. */
181
+ export type ValueAtPath<T, P extends string> = unknown extends T
182
+ ? unknown
183
+ : P extends `${infer Head}.${infer Rest}`
184
+ ? Head extends keyof T
185
+ ? ValueAtPath<T[Head], Rest>
186
+ : unknown
187
+ : P extends keyof T
188
+ ? T[P]
189
+ : unknown;
100
190
 
101
191
  /** An instrument's config, declared once. */
102
192
  export interface ConfigSchema<TC> {
103
- readonly nodes: Readonly<Record<string, ConfigNode>>;
193
+ readonly nodes: ConfigShape;
104
194
  /** The starting config — what `defaultConfig()` would have returned. */
105
195
  defaults(): TC;
106
196
  }
@@ -1,9 +1,11 @@
1
+ import { schemaNodeAtPath } from './path';
1
2
  import type { ResolvedConfig } from './types';
2
3
 
3
4
  /**
4
- * Whether a leaf's row should be drawn right now.
5
+ * Whether the row at a dotted path should be drawn right now. A group answers
6
+ * for its whole subtree — hiding one hides everything under it.
5
7
  *
6
- * `hidden` is the leaf's own static flag; `showIf` is the schema's predicate
8
+ * `hidden` is the node's own static flag; `showIf` is the schema's predicate
7
9
  * over the live config. Both are presentational — a hidden leaf keeps its
8
10
  * value and the instrument still reads it.
9
11
  */
@@ -13,8 +15,8 @@ export function isLeafVisible(
13
15
  config: Record<string, unknown>,
14
16
  showHidden = false,
15
17
  ): boolean {
16
- const leaf = resolved.group.children[path];
17
- if (leaf && 'hidden' in leaf && leaf.hidden === true && !showHidden) return false;
18
+ const node = schemaNodeAtPath(resolved.group, path);
19
+ if (node && 'hidden' in node && node.hidden === true && !showHidden) return false;
18
20
  const predicate = resolved.showIf.get(path);
19
21
  return predicate ? predicate(config) : true;
20
22
  }
@@ -2,6 +2,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite';
2
2
  import { PropertyRow } from '@weasel-js/ui';
3
3
  import { useState } from 'react';
4
4
  import { f } from '../config/builder';
5
+ import { withValueAtPath } from '../config/path';
5
6
  import { resolveConfigSchema } from '../config/resolve';
6
7
  import type { ConfigRule, ControlRenderer } from '../config/types';
7
8
  import { ControlPanel } from './ControlPanel';
@@ -60,7 +61,7 @@ function Harness({ fields }: { fields: ConfigField[] }) {
60
61
  <ControlPanel
61
62
  fields={fields}
62
63
  config={config}
63
- setConfig={(key, value) => setConfig((prev) => ({ ...prev, [key as string]: value }))}
64
+ setConfig={(path, value) => setConfig((prev) => withValueAtPath(prev, path, value))}
64
65
  />
65
66
  );
66
67
  }
@@ -102,7 +103,7 @@ function SchemaHarness({
102
103
  <ControlPanel
103
104
  schema={schema}
104
105
  config={config}
105
- setConfig={(key, value) => setConfig((prev) => ({ ...prev, [key as string]: value }))}
106
+ setConfig={(path, value) => setConfig((prev) => withValueAtPath(prev, path, value))}
106
107
  renderers={renderers}
107
108
  />
108
109
  );
@@ -169,7 +170,7 @@ export const Described: Story = {
169
170
  <ControlPanel
170
171
  schema={schema}
171
172
  config={config}
172
- setConfig={(key, value) => setConfig((prev) => ({ ...prev, [key as string]: value }))}
173
+ setConfig={(path, value) => setConfig((prev) => withValueAtPath(prev, path, value))}
173
174
  />
174
175
  );
175
176
  },
@@ -195,8 +196,41 @@ export const Conditional: Story = {
195
196
  <ControlPanel
196
197
  schema={schema}
197
198
  config={config}
198
- setConfig={(key, value) => setConfig((prev) => ({ ...prev, [key as string]: value }))}
199
+ setConfig={(path, value) => setConfig((prev) => withValueAtPath(prev, path, value))}
199
200
  />
200
201
  );
201
202
  },
202
203
  };
204
+
205
+ const nested = f.schema({
206
+ showGrid: f.boolean(true),
207
+ grid: f
208
+ .group({
209
+ size: f.number(20).range(5, 80).step(5).label('Cell size'),
210
+ color: f.color('#3a86ff'),
211
+ })
212
+ .describe('Written at config.grid.size and config.grid.color.'),
213
+ export: f.group({
214
+ format: f.enum('png', ['png', 'svg']),
215
+ scale: f.number(2).range(1, 4).step(1),
216
+ }),
217
+ });
218
+
219
+ /** `f.group` nests the value as well as the heading: these rows write to
220
+ * `grid.size` and `export.scale`, not to `size` and `scale`. */
221
+ export const Nested: Story = {
222
+ render: () => {
223
+ const schema = resolveConfigSchema(nested, []);
224
+ const [config, setConfig] = useState<Record<string, unknown>>(nested.defaults());
225
+ return (
226
+ <>
227
+ <ControlPanel
228
+ schema={schema}
229
+ config={config}
230
+ setConfig={(path, value) => setConfig((prev) => withValueAtPath(prev, path, value))}
231
+ />
232
+ <pre>{JSON.stringify(config, null, 2)}</pre>
233
+ </>
234
+ );
235
+ },
236
+ };
@@ -480,4 +480,118 @@ describe('<ControlPanel> collapsible sections', () => {
480
480
  expect(onCollapse).toHaveBeenCalledWith('Advanced', false);
481
481
  expect(screen.getByLabelText('Seed')).not.toBeVisible();
482
482
  });
483
+
484
+ describe('a section the schema declares closed', () => {
485
+ const closedSchema = resolveConfigSchema(
486
+ f.schema({
487
+ showGrid: f.boolean(true),
488
+ seed: f.number(0).section('Advanced', { collapsed: true }),
489
+ }),
490
+ [],
491
+ );
492
+
493
+ it('opens folded, and is foldable, with no collapse props at all', () => {
494
+ render(<ControlPanel schema={closedSchema} config={config} setConfig={vi.fn()} />);
495
+ expect(screen.getByLabelText('Seed')).not.toBeVisible();
496
+ fireEvent.click(screen.getByRole('button', { name: 'Advanced' }));
497
+ expect(screen.getByLabelText('Seed')).toBeVisible();
498
+ });
499
+
500
+ it('yields to a remembered fold, so a toggle the reader made survives', () => {
501
+ render(
502
+ <ControlPanel
503
+ schema={closedSchema}
504
+ config={config}
505
+ setConfig={vi.fn()}
506
+ collapsed={{ Advanced: false }}
507
+ onCollapse={vi.fn()}
508
+ />,
509
+ );
510
+ expect(screen.getByLabelText('Seed')).toBeVisible();
511
+ });
512
+ });
513
+ });
514
+
515
+ describe('<ControlPanel> nested groups', () => {
516
+ const nested = () =>
517
+ resolveConfigSchema(
518
+ f.schema({
519
+ showGrid: f.boolean(true),
520
+ grid: f.group({ size: f.number(20).label('Cell size'), color: f.color('#ffffff') }),
521
+ }),
522
+ [],
523
+ );
524
+
525
+ it('renders a nested group under its own heading', () => {
526
+ render(
527
+ <ControlPanel
528
+ schema={nested()}
529
+ config={{ showGrid: true, grid: { size: 20, color: '#ffffff' } }}
530
+ setConfig={vi.fn()}
531
+ />,
532
+ );
533
+ const group = screen.getByRole('heading', { name: 'Grid' }).parentElement;
534
+ expect(group?.textContent).toContain('Cell size');
535
+ });
536
+
537
+ it('reads a nested leaf value out of the config tree', () => {
538
+ render(
539
+ <ControlPanel
540
+ schema={nested()}
541
+ config={{ showGrid: true, grid: { size: 44, color: '#ffffff' } }}
542
+ setConfig={vi.fn()}
543
+ />,
544
+ );
545
+ expect((screen.getByLabelText('Cell size') as HTMLInputElement).value).toBe('44');
546
+ });
547
+
548
+ it('falls back to the leaf default when the branch is missing', () => {
549
+ render(<ControlPanel schema={nested()} config={{ showGrid: true }} setConfig={vi.fn()} />);
550
+ expect((screen.getByLabelText('Cell size') as HTMLInputElement).value).toBe('20');
551
+ });
552
+
553
+ it('writes back with the full dotted path', () => {
554
+ const setConfig = vi.fn();
555
+ render(
556
+ <ControlPanel
557
+ schema={nested()}
558
+ config={{ showGrid: true, grid: { size: 20, color: '#ffffff' } }}
559
+ setConfig={setConfig}
560
+ />,
561
+ );
562
+ fireEvent.change(screen.getByLabelText('Cell size'), { target: { value: '40' } });
563
+ expect(setConfig).toHaveBeenCalledWith('grid.size', 40);
564
+ });
565
+
566
+ it('hides a whole group whose showIf is false', () => {
567
+ const schema = resolveConfigSchema(
568
+ f.schema({
569
+ showGrid: f.boolean(true),
570
+ grid: f.group({ size: f.number(20).label('Cell size') }).showIf((c) => c.showGrid === true),
571
+ }),
572
+ [],
573
+ );
574
+ render(
575
+ <ControlPanel
576
+ schema={schema}
577
+ config={{ showGrid: false, grid: { size: 20 } }}
578
+ setConfig={vi.fn()}
579
+ />,
580
+ );
581
+ expect(screen.queryByLabelText('Cell size')).not.toBeInTheDocument();
582
+ expect(screen.queryByRole('heading', { name: 'Grid' })).not.toBeInTheDocument();
583
+ });
584
+
585
+ it('takes a renderer keyed by the nested path', () => {
586
+ const schema = nested();
587
+ render(
588
+ <ControlPanel
589
+ schema={schema}
590
+ config={{ showGrid: true, grid: { size: 44, color: '#ffffff' } }}
591
+ setConfig={vi.fn()}
592
+ renderers={{ 'grid.size': (ctx) => <span>size:{String(ctx.value)}</span> }}
593
+ />,
594
+ );
595
+ expect(screen.getByText('size:44')).toBeInTheDocument();
596
+ });
483
597
  });