@pairdraw/dom-css 0.0.0-stage → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -2
  3. package/dist/browser.d.ts +29 -0
  4. package/dist/browser.js +2 -0
  5. package/dist/chunks/behaviours/args.js +59 -0
  6. package/dist/chunks/behaviours/args.js.map +1 -0
  7. package/dist/chunks/behaviours/controls.js +109 -0
  8. package/dist/chunks/behaviours/controls.js.map +1 -0
  9. package/dist/chunks/behaviours/reset.js +40 -0
  10. package/dist/chunks/behaviours/reset.js.map +1 -0
  11. package/dist/chunks/behaviours/roles.js +51 -0
  12. package/dist/chunks/behaviours/roles.js.map +1 -0
  13. package/dist/chunks/behaviours/states/conditions.js +103 -0
  14. package/dist/chunks/behaviours/states/conditions.js.map +1 -0
  15. package/dist/chunks/behaviours/states/css.js +172 -0
  16. package/dist/chunks/behaviours/states/css.js.map +1 -0
  17. package/dist/chunks/behaviours/states/layers.js +1736 -0
  18. package/dist/chunks/behaviours/states/layers.js.map +1 -0
  19. package/dist/chunks/behaviours/states/model.js +144 -0
  20. package/dist/chunks/behaviours/states/model.js.map +1 -0
  21. package/dist/chunks/behaviours/states/tailwind.js +198 -0
  22. package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
  23. package/dist/chunks/browser.js +1352 -0
  24. package/dist/chunks/browser.js.map +1 -0
  25. package/dist/chunks/export/bundle.js +337 -0
  26. package/dist/chunks/export/bundle.js.map +1 -0
  27. package/dist/chunks/export/components/drawn.js +60 -0
  28. package/dist/chunks/export/components/drawn.js.map +1 -0
  29. package/dist/chunks/export/components/fields.js +178 -0
  30. package/dist/chunks/export/components/fields.js.map +1 -0
  31. package/dist/chunks/export/components/groups.js +297 -0
  32. package/dist/chunks/export/components/groups.js.map +1 -0
  33. package/dist/chunks/export/components/icons.js +260 -0
  34. package/dist/chunks/export/components/icons.js.map +1 -0
  35. package/dist/chunks/export/components/layers.js +1594 -0
  36. package/dist/chunks/export/components/layers.js.map +1 -0
  37. package/dist/chunks/export/index.js +477 -0
  38. package/dist/chunks/export/index.js.map +1 -0
  39. package/dist/chunks/export/storybook/component.js +448 -0
  40. package/dist/chunks/export/storybook/component.js.map +1 -0
  41. package/dist/chunks/export/storybook/export.js +410 -0
  42. package/dist/chunks/export/storybook/export.js.map +1 -0
  43. package/dist/chunks/import/html.js +406 -0
  44. package/dist/chunks/import/html.js.map +1 -0
  45. package/dist/chunks/import/jsx/dev-runtime.js +37 -0
  46. package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
  47. package/dist/chunks/tokens/cssom-validator.js +10 -0
  48. package/dist/chunks/tokens/cssom-validator.js.map +1 -0
  49. package/dist/export.d.ts +594 -0
  50. package/dist/export.js +13 -0
  51. package/dist/index.d.ts +115 -0
  52. package/dist/index.js +7 -0
  53. package/dist/jsx-dev-runtime.d.ts +8 -0
  54. package/dist/jsx-dev-runtime.js +3 -0
  55. package/dist/jsx-runtime.d.ts +20 -0
  56. package/dist/jsx-runtime.js +3 -0
  57. package/dist/rolldown-runtime.js +13 -0
  58. package/dist/runtime.d.ts +10 -0
  59. package/dist/scene-graph.d.ts +2 -0
  60. package/dist/scene-graph.js +2 -0
  61. package/dist/scene-graph2.d.ts +10 -0
  62. package/dist/styling.d.ts +93 -0
  63. package/dist/tailwind.d.ts +39 -0
  64. package/dist/types.d.ts +89 -0
  65. package/package.json +73 -4
  66. package/src/behaviours/args.ts +85 -0
  67. package/src/behaviours/controls.ts +195 -0
  68. package/src/behaviours/index.ts +11 -0
  69. package/src/behaviours/reset.ts +37 -0
  70. package/src/behaviours/roles.ts +44 -0
  71. package/src/behaviours/states/conditions.ts +105 -0
  72. package/src/behaviours/states/css.ts +123 -0
  73. package/src/behaviours/states/index.ts +4 -0
  74. package/src/behaviours/states/layers.ts +205 -0
  75. package/src/behaviours/states/model.ts +175 -0
  76. package/src/behaviours/states/names.ts +39 -0
  77. package/src/behaviours/states/tailwind.ts +148 -0
  78. package/src/behaviours/states/types.ts +56 -0
  79. package/src/browser.ts +146 -0
  80. package/src/cssom.d.ts +38 -0
  81. package/src/export/bundle.ts +461 -0
  82. package/src/export/components/drawn.ts +73 -0
  83. package/src/export/components/fields.ts +178 -0
  84. package/src/export/components/groups.ts +78 -0
  85. package/src/export/components/icons.ts +230 -0
  86. package/src/export/components/layers.ts +121 -0
  87. package/src/export/components/model.ts +544 -0
  88. package/src/export/components/properties.ts +167 -0
  89. package/src/export/components/react/fields.ts +129 -0
  90. package/src/export/components/react/index.ts +606 -0
  91. package/src/export/components/react/shared.ts +76 -0
  92. package/src/export/components/references.ts +212 -0
  93. package/src/export/components/repeats.ts +128 -0
  94. package/src/export/components/shaders.ts +125 -0
  95. package/src/export/components/styling.ts +31 -0
  96. package/src/export/components/tree.ts +72 -0
  97. package/src/export/components/vue.ts +517 -0
  98. package/src/export/css.ts +57 -0
  99. package/src/export/grid.ts +42 -0
  100. package/src/export/html.ts +163 -0
  101. package/src/export/index.ts +44 -0
  102. package/src/export/projection.ts +581 -0
  103. package/src/export/storybook/component.ts +334 -0
  104. package/src/export/storybook/export.ts +430 -0
  105. package/src/export/storybook/groups.ts +161 -0
  106. package/src/export/storybook/module.ts +319 -0
  107. package/src/export/storybook/names.ts +51 -0
  108. package/src/export/tailwind-jsx.ts +156 -0
  109. package/src/export/text.ts +415 -0
  110. package/src/import/css-text.ts +4 -0
  111. package/src/import/css-values.ts +31 -0
  112. package/src/import/flow.ts +248 -0
  113. package/src/import/html.ts +88 -0
  114. package/src/import/jsx/core.ts +130 -0
  115. package/src/import/jsx/dev-runtime.ts +8 -0
  116. package/src/import/jsx/runtime.ts +76 -0
  117. package/src/import/scene-graph.ts +582 -0
  118. package/src/import/style-attribute.ts +25 -0
  119. package/src/import/tailwind.ts +54 -0
  120. package/src/import/text.ts +518 -0
  121. package/src/index.ts +51 -0
  122. package/src/runtime/browser/runtime.ts +301 -0
  123. package/src/runtime/headless/runtime.ts +99 -0
  124. package/src/runtime/headless/styles.ts +330 -0
  125. package/src/runtime/index.ts +11 -0
  126. package/src/tokens/conditions.ts +66 -0
  127. package/src/tokens/cssom-validator.ts +8 -0
  128. package/src/tokens/custom-properties.ts +331 -0
  129. package/src/tokens/expression.ts +128 -0
  130. package/src/tokens/formula.ts +140 -0
  131. package/src/tokens/index.ts +43 -0
  132. package/src/tokens/names.ts +161 -0
  133. package/src/tokens/references.ts +201 -0
  134. package/src/tokens/stylesheet.ts +304 -0
  135. package/src/tokens/validate.ts +86 -0
  136. package/src/tokens/values.ts +44 -0
  137. package/src/types.ts +59 -0
@@ -0,0 +1,544 @@
1
+ import { behaviourArgs, type BehaviourArgs } from '#dom-css/behaviours/args'
2
+ import { BUTTON_RESET } from '#dom-css/behaviours/reset'
3
+ import { allElements } from '#dom-css/behaviours/states/layers'
4
+ import { ownerVariants, restLayerOf, stateStyles } from '#dom-css/behaviours/states/model'
5
+ import { layerClassNames, propAttribute } from '#dom-css/behaviours/states/names'
6
+ import type { StateElement, StateStyles } from '#dom-css/behaviours/states/types'
7
+ import { uniq } from 'es-toolkit/array'
8
+ import { camelCase } from 'es-toolkit/string'
9
+
10
+ import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
11
+ import {
12
+ behaviourContract,
13
+ behaviourProperties,
14
+ layerPath,
15
+ readBehaviour,
16
+ textBinding,
17
+ type Behaviour
18
+ } from '@pairdraw/scene-graph/behaviours'
19
+ import { variantDefaultValue } from '@pairdraw/scene-graph/components'
20
+ import { slotPropertyId } from '@pairdraw/scene-graph/slots'
21
+
22
+ import type { SceneGraphToDesignOptions } from '../projection'
23
+ import { claimName, identifierName } from '../storybook/names'
24
+ import {
25
+ blockRoot,
26
+ freePlacedParts,
27
+ inputLayers,
28
+ inputValue,
29
+ numberInput,
30
+ rangeModel,
31
+ textModel
32
+ } from './fields'
33
+ import {
34
+ groupItems,
35
+ GROUP_ITEMS,
36
+ isGroup,
37
+ type GroupKind,
38
+ type ItemBuilder,
39
+ type ItemKind
40
+ } from './groups'
41
+ import type { IconPackage } from './icons'
42
+ import {
43
+ booleanProps,
44
+ slotProps,
45
+ textProps,
46
+ type BooleanProp,
47
+ type SlotProp,
48
+ type TextProp
49
+ } from './properties'
50
+ import {
51
+ referencedLayers,
52
+ type ComponentReference,
53
+ type ComponentReferences,
54
+ type IconReference,
55
+ type UsedLayer
56
+ } from './references'
57
+ import { activeTriggers, tabParts, type ChoiceModel, type RepeatedPart } from './repeats'
58
+ import { shaderLayers, type ShaderLayer } from './shaders'
59
+ import type { ComponentStyling } from './styling'
60
+ import { componentTree } from './tree'
61
+
62
+ /** Kinds generated as components so far; the rest keep static stories. */
63
+ const GENERATED_KINDS = [
64
+ 'button',
65
+ 'switch',
66
+ 'checkbox',
67
+ 'toggle',
68
+ 'collapsible',
69
+ 'tabs',
70
+ 'radioGroup',
71
+ 'toggleGroup',
72
+ 'accordion',
73
+ 'slider',
74
+ 'progress',
75
+ 'numberField',
76
+ 'textField',
77
+ 'textarea'
78
+ ] as const
79
+
80
+ /** What a generated component is: a control on its own, or a group's item. */
81
+ /**
82
+ * What a generated component is: a control on its own, a group's item, or `plain`, a component
83
+ * without a behaviour, which takes its properties as props and has no control of its own.
84
+ */
85
+ export type GeneratedKind = (typeof GENERATED_KINDS)[number] | ItemKind | 'plain'
86
+
87
+ /** A variant property the component takes as a prop and sets on its root as `data-*`. */
88
+ interface VariantProp {
89
+ /** The prop's identifier. */
90
+ name: string
91
+ /** The variant property it draws, which state styles name the condition after. */
92
+ property: string
93
+ options: string[]
94
+ default: string
95
+ }
96
+
97
+ /**
98
+ * What the control's root binds, which each framework writes in its own idiom: the two-way
99
+ * value (`v-model`, or Radix's `checked` and `onCheckedChange`), `disabled`, and a variant
100
+ * property the component sets as a `data-*` attribute from its prop.
101
+ */
102
+ export type ComponentBinding =
103
+ | { type: 'model'; name: string }
104
+ | { type: 'disabled' }
105
+ /** A group's item: the value it stands for, which the group chooses among. */
106
+ | { type: 'value' }
107
+ | { type: 'prop'; prop: VariantProp; attribute: string }
108
+
109
+ /** A layer of the generated markup, before a framework picks its element or component. */
110
+ export interface ComponentElement {
111
+ type: 'element'
112
+ /** The behaviour part the layer draws, `root` for the control itself, or null. */
113
+ part: string | null
114
+ /** The element the design projects to, used when no library component draws the part. */
115
+ tag: string
116
+ /** The readable class the state styles select it by. */
117
+ className: string
118
+ /** Attributes the design sets, such as an image's `src` or its own `class`. */
119
+ attrs: Record<string, string>
120
+ /** On the root only. */
121
+ bindings: ComponentBinding[]
122
+ /** The boolean prop that shows the layer, which hides it while off. */
123
+ shownBy?: string
124
+ /** The slot prop whose content the layer shows in place of its own. */
125
+ slot?: string
126
+ /** The layer of the rest variant it draws, which code links back to. */
127
+ layerId?: string
128
+ /** The value a repeated part stands for, such as a tab trigger's tab. */
129
+ value?: string
130
+ /** The shader the layer fills with, which plays behind its content. */
131
+ shader?: ShaderLayer
132
+ children: ComponentNode[]
133
+ }
134
+
135
+ /** A number the component binds two ways, within its range, starting where the design does. */
136
+ export interface RangeModel {
137
+ min: number
138
+ max: number
139
+ step: number
140
+ default: number
141
+ }
142
+
143
+ /**
144
+ * Text a field binds two ways: the words it starts with, and the placeholder an empty field
145
+ * shows when the design draws one.
146
+ */
147
+ export interface TextModel {
148
+ default: string
149
+ placeholder: string | null
150
+ }
151
+
152
+ export type ComponentNode =
153
+ | ComponentElement
154
+ | { type: 'text'; value: string }
155
+ /** The value of a text prop, drawn where the design binds a text layer to it. */
156
+ | { type: 'textProp'; name: string }
157
+ /** A field's input, drawn where the design binds its text layer to the field's text. */
158
+ | { type: 'input'; className: string; layerId?: string }
159
+ | ComponentReference
160
+ | IconReference
161
+
162
+ /** One component generated from a component set with a behaviour. */
163
+ export interface ComponentModel {
164
+ /** The component's identifier and file name. */
165
+ name: string
166
+ kind: GeneratedKind
167
+ styles: StateStyles
168
+ /** The markup every framework writes, with each layer's part and the root's bindings. */
169
+ tree: ComponentElement
170
+ /**
171
+ * The value the component binds two ways: a boolean such as `checked`, `pressed`, or `open`,
172
+ * or `value` when it chooses among `choice`, as tabs do.
173
+ */
174
+ model: string | null
175
+ /** The options a string model takes and the one it starts on. */
176
+ choice: ChoiceModel | null
177
+ /** The range a number model takes, as a slider, progress bar, or number field has. */
178
+ range: RangeModel | null
179
+ /** The text a text field or textarea binds. */
180
+ text: TextModel | null
181
+ /** A group's item takes the `value` it stands for, which the group chooses among. */
182
+ valueProp: boolean
183
+ /** The component of a group's items, generated beside the group's own. */
184
+ item: ComponentModel | null
185
+ /** Whether the set draws a disabled state, so the component takes `disabled`. */
186
+ disabled: boolean
187
+ props: VariantProp[]
188
+ texts: TextProp[]
189
+ booleans: BooleanProp[]
190
+ slots: SlotProp[]
191
+ /** The variant properties that draw its booleans and states, which references set. */
192
+ args: BehaviourArgs
193
+ }
194
+
195
+ /** A generated component's files, and what its stories need to know about it. */
196
+ export interface GeneratedComponent {
197
+ /**
198
+ * The component's files. A file of JSX lists the layer each of its JSX elements draws, in the
199
+ * order they open, `null` for an element no layer drew, such as a shader's canvas.
200
+ */
201
+ files: { path: string; content: string; layerIds?: (string | null)[] }[]
202
+ /** How stories import it: its path next to them, and whether it's a named export. */
203
+ entry: { path: string; named: boolean }
204
+ /** The prop a story sets the value with, if the component has one. */
205
+ valueArg: string | null
206
+ /** Whether that prop takes a list of the value, as Radix's slider does. */
207
+ valueList?: boolean
208
+ }
209
+
210
+ /** How a generator writes a component. */
211
+ export interface ComponentGeneratorOptions {
212
+ /** A stylesheet beside the component, by default, or Tailwind utilities in its markup. */
213
+ styling?: ComponentStyling
214
+ /** The package icons come from; Iconify by default, and for any icon the package lacks. */
215
+ icons?: IconPackage
216
+ }
217
+
218
+ export type ComponentGenerator = (
219
+ component: ComponentModel,
220
+ options?: ComponentGeneratorOptions
221
+ ) => Promise<GeneratedComponent>
222
+
223
+ /** Parts each kind renders as a native button, which the reset clears for the design. */
224
+ const BUTTON_PARTS: Record<GeneratedKind, readonly string[]> = {
225
+ button: ['root'],
226
+ switch: ['root'],
227
+ checkbox: ['root'],
228
+ toggle: ['root'],
229
+ collapsible: ['trigger'],
230
+ tabs: ['trigger'],
231
+ radioGroup: [],
232
+ toggleGroup: [],
233
+ accordion: [],
234
+ slider: [],
235
+ progress: [],
236
+ numberField: ['increment', 'decrement'],
237
+ textField: [],
238
+ textarea: [],
239
+ plain: [],
240
+ radioGroupItem: ['root'],
241
+ toggleGroupItem: ['root'],
242
+ accordionItem: ['trigger']
243
+ }
244
+
245
+ const isGenerated = (kind: Behaviour['kind']): kind is (typeof GENERATED_KINDS)[number] =>
246
+ (GENERATED_KINDS as readonly string[]).includes(kind)
247
+
248
+ /** What an owner generates as: a group's item when it is one, its own kind, or nothing. */
249
+ function generatedKind(kind: Behaviour['kind'], itemOf?: GroupKind): GeneratedKind | null {
250
+ if (itemOf) return GROUP_ITEMS[itemOf]
251
+ return isGenerated(kind) ? kind : null
252
+ }
253
+
254
+ /** Layer paths of the slot frames that draw each part, the same in every variant. */
255
+ function partPaths(graph: SceneGraph, set: SceneNode, behaviour: Behaviour): Map<string, string> {
256
+ const partOfSlot = new Map(Object.entries(behaviour.parts).map(([part, slot]) => [slot, part]))
257
+ const paths = new Map<string, string>()
258
+ const visit = (variant: SceneNode, node: SceneNode) => {
259
+ const part = partOfSlot.get(slotPropertyId(node) ?? '')
260
+ if (part) paths.set(layerPath(graph, variant.id, node.id), part)
261
+ for (const child of graph.getChildren(node.id)) visit(variant, child)
262
+ }
263
+ for (const variant of ownerVariants(graph, set))
264
+ for (const child of graph.getChildren(variant.id)) visit(variant, child)
265
+ return paths
266
+ }
267
+
268
+ /**
269
+ * The component a set with a behaviour generates, or `null` when its kind is not generated
270
+ * yet or its variants have no rest state to start from.
271
+ */
272
+ export interface ComponentModelOptions extends Pick<SceneGraphToDesignOptions, 'vectorElement'> {
273
+ /**
274
+ * The other components generated alongside, which instances of them use rather than
275
+ * drawing their layers.
276
+ */
277
+ references?: ComponentReferences
278
+ /** The component's identifier and file name; the owner's name by default. */
279
+ name?: string
280
+ /** A group's item component's name; the group's name with `Item` by default. */
281
+ itemName?: string
282
+ /** Generate the owner as the item of a group of this kind, which only lives in the group. */
283
+ itemOf?: GroupKind
284
+ }
285
+
286
+ /** Each layer's behaviour part: the root, and the slot frames that draw the others. */
287
+ function behaviourParts(
288
+ graph: SceneGraph,
289
+ set: SceneNode,
290
+ behaviour: Behaviour,
291
+ root: StateElement
292
+ ): Map<StateElement, string> {
293
+ const paths = partPaths(graph, set, behaviour)
294
+ const contract = behaviourContract(behaviour.kind).parts
295
+ const parts = new Map<StateElement, string>([[root, 'root']])
296
+ for (const element of allElements(root)) {
297
+ const part = paths.get(element.key)
298
+ if (part && contract.some((item) => item.id === part)) parts.set(element, part)
299
+ }
300
+ return parts
301
+ }
302
+
303
+ /** Clears the native button look from the parts a kind renders as buttons. */
304
+ function resetButtons(
305
+ kind: GeneratedKind,
306
+ parts: ReadonlyMap<StateElement, string>,
307
+ repeated: ReadonlyMap<StateElement, RepeatedPart>
308
+ ): void {
309
+ const all = [...parts, ...[...repeated].map(([element, item]) => [element, item.part] as const)]
310
+ for (const [element, part] of all)
311
+ if (BUTTON_PARTS[kind].includes(part)) element.base = { ...BUTTON_RESET, ...element.base }
312
+ }
313
+
314
+ /** The variant properties that are not values or states, as props set as `data-*`. */
315
+ function variantProps(
316
+ graph: SceneGraph,
317
+ set: SceneNode,
318
+ args: BehaviourArgs,
319
+ taken: Set<string>
320
+ ): VariantProp[] {
321
+ const bound = new Set([...args.booleans.keys(), args.states?.property, args.filled?.property])
322
+ return behaviourProperties(graph, set)
323
+ .filter((definition) => definition.type === 'VARIANT' && !bound.has(definition.name))
324
+ .map((definition) => ({
325
+ name: claimName(camelCase(identifierName(definition.name, 'Prop')), taken),
326
+ property: definition.name,
327
+ // A document may record no options, which the variants' own values then are.
328
+ options: uniq([
329
+ ...(definition.variantOptions ?? []),
330
+ ...ownerVariants(graph, set).map(
331
+ (variant) => variant.componentPropertyValues[definition.name] ?? ''
332
+ )
333
+ ]).filter((option) => option !== ''),
334
+ default: variantDefaultValue(graph, set, definition)
335
+ }))
336
+ }
337
+
338
+ /** What the root binds: its model, an item's value, `disabled`, and its variant props. */
339
+ function rootBindings(
340
+ model: string | null,
341
+ item: boolean,
342
+ disabled: boolean,
343
+ props: readonly VariantProp[]
344
+ ): ComponentBinding[] {
345
+ return [
346
+ ...(model ? [{ type: 'model' as const, name: model }] : []),
347
+ ...(item ? [{ type: 'value' as const }] : []),
348
+ ...(disabled ? [{ type: 'disabled' as const }] : []),
349
+ ...props.map((prop) => ({
350
+ type: 'prop' as const,
351
+ prop,
352
+ attribute: propAttribute(prop.property)
353
+ }))
354
+ ]
355
+ }
356
+
357
+ /**
358
+ * A group's items, built as the group's item component; null for other owners and for a
359
+ * group's item itself.
360
+ */
361
+ function itemsOf(
362
+ graph: SceneGraph,
363
+ behaviour: Behaviour,
364
+ variantIds: readonly string[],
365
+ parts: ReadonlyMap<StateElement, string>,
366
+ options: ComponentModelOptions,
367
+ build: (itemOf: GroupKind) => ItemBuilder
368
+ ): ReturnType<typeof groupItems> {
369
+ if (options.itemOf || !isGroup(behaviour.kind)) return null
370
+ const itemsElement = [...parts].find(([, part]) => part === 'items')?.[0]
371
+ return groupItems(graph, variantIds, itemsElement, build(behaviour.kind))
372
+ }
373
+
374
+ /**
375
+ * The value the component binds two ways: `value` when it chooses among options or holds a
376
+ * number or text, its boolean such as `checked`, or none for a group's item, which its group
377
+ * chooses.
378
+ */
379
+ function modelName(args: BehaviourArgs, item: boolean, valued: boolean): string | null {
380
+ if (valued) return 'value'
381
+ if (item) return null
382
+ return [...args.booleans.values()].find((arg) => arg.name !== 'disabled')?.name ?? null
383
+ }
384
+
385
+ /** Whether the set draws a disabled look, as a boolean or an interaction state. */
386
+ const drawsDisabled = (args: BehaviourArgs) =>
387
+ [...args.booleans.values()].some((arg) => arg.name === 'disabled') ||
388
+ args.states?.disabled !== undefined
389
+
390
+ /** The layers used rather than drawn: a group's items, then other generated components. */
391
+ function usedLayers(
392
+ graph: SceneGraph,
393
+ root: StateElement,
394
+ variantIds: readonly string[],
395
+ items: ReadonlyMap<StateElement, UsedLayer> | undefined,
396
+ references: ComponentReferences | undefined
397
+ ): Map<StateElement, UsedLayer> {
398
+ const used = new Map(items)
399
+ if (references)
400
+ for (const [element, layer] of referencedLayers(graph, root, variantIds, references, used))
401
+ used.set(element, layer)
402
+ return used
403
+ }
404
+
405
+ /**
406
+ * Sizes the design gives include a layer's padding and border, which a component's own
407
+ * stylesheet has to say, since no page reset around it does. A root without a width hugs its
408
+ * content, as the design draws it.
409
+ */
410
+ function borderBoxes(root: StateElement): void {
411
+ // A root the design sizes to its content hugs it, where a block would fill its container.
412
+ if (!Object.hasOwn(root.base, 'width')) root.base = { ...root.base, width: 'fit-content' }
413
+ for (const element of allElements(root)) {
414
+ const has = (property: string) => Object.hasOwn(element.base, property)
415
+ if ((has('width') || has('height')) && !has('box-sizing'))
416
+ element.base = { 'box-sizing': 'border-box', ...element.base }
417
+ }
418
+ }
419
+
420
+ /**
421
+ * Each layer's part, and tabs' triggers and panels, with the styles the generated parts need:
422
+ * native buttons cleared, the chosen tab's look, and moving parts free of their drawn place.
423
+ */
424
+ function drawnParts(
425
+ graph: SceneGraph,
426
+ set: SceneNode,
427
+ behaviour: Behaviour | null,
428
+ kind: GeneratedKind,
429
+ root: StateElement
430
+ ) {
431
+ // A component without a behaviour has no parts but its root.
432
+ const parts = behaviour
433
+ ? behaviourParts(graph, set, behaviour, root)
434
+ : new Map<StateElement, string>([[root, 'root']])
435
+ const tabs = behaviour?.kind === 'tabs' ? tabParts(parts) : null
436
+ const repeated = tabs?.repeated ?? new Map<StateElement, RepeatedPart>()
437
+ activeTriggers(repeated)
438
+ resetButtons(kind, parts, repeated)
439
+ freePlacedParts(kind, parts)
440
+ blockRoot(kind, root)
441
+ borderBoxes(root)
442
+ return { parts, tabs, repeated }
443
+ }
444
+
445
+ /** The number or text the component holds, and the text property a field's input shows. */
446
+ function valueModels(
447
+ graph: SceneGraph,
448
+ set: SceneNode,
449
+ kind: GeneratedKind,
450
+ behaviour: Behaviour | null,
451
+ args: BehaviourArgs
452
+ ): { range: RangeModel | null; text: TextModel | null; inputProperty: string | undefined } {
453
+ if (!behaviour) return { range: null, text: null, inputProperty: undefined }
454
+ const valueText = inputValue(kind)
455
+ const inputProperty = valueText && textBinding(behaviour, valueText)
456
+ const definition = behaviourProperties(graph, set).find((item) => item.id === inputProperty)
457
+ return {
458
+ range: rangeModel(kind, behaviour),
459
+ text: textModel(kind, definition, args),
460
+ inputProperty
461
+ }
462
+ }
463
+
464
+ export function componentModel(
465
+ graph: SceneGraph,
466
+ set: SceneNode,
467
+ options: ComponentModelOptions = {}
468
+ ): ComponentModel | null {
469
+ const behaviour = readBehaviour(set)
470
+ // A component without a behaviour is plain: its properties are its props.
471
+ const kind = behaviour ? generatedKind(behaviour.kind, options.itemOf) : 'plain'
472
+ const styles = kind ? stateStyles(graph, set, options) : null
473
+ const args = behaviourArgs(graph, set) ?? { booleans: new Map() }
474
+ if (!kind || !styles) return null
475
+ const name = options.name ?? identifierName(set.name, 'Component')
476
+
477
+ const { parts, tabs, repeated } = drawnParts(graph, set, behaviour, kind, styles.root)
478
+
479
+ // A group's items first, so its item component is used for them rather than a standalone one.
480
+ const variantIds = [styles.restId, ...ownerVariants(graph, set).map((variant) => variant.id)]
481
+ const items =
482
+ behaviour &&
483
+ itemsOf(
484
+ graph,
485
+ behaviour,
486
+ variantIds,
487
+ parts,
488
+ options,
489
+ (itemOf) => (owner) =>
490
+ componentModel(graph, owner, {
491
+ ...options,
492
+ name: options.itemName ?? `${name}Item`,
493
+ itemName: undefined,
494
+ itemOf
495
+ })
496
+ )
497
+
498
+ const choice = tabs?.choice ?? items?.choice ?? null
499
+ const { range, text, inputProperty } = valueModels(graph, set, kind, behaviour, args)
500
+ const model = modelName(args, !!options.itemOf, !!(choice ?? range ?? text))
501
+ const taken = new Set([...(model ? [model] : []), 'disabled', 'value'])
502
+ const props = variantProps(graph, set, args, taken)
503
+ const disabled = drawsDisabled(args)
504
+ const used = usedLayers(graph, styles.root, variantIds, items?.used, options.references)
505
+ const elements = allElements(styles.root)
506
+ const texts = textProps(graph, set, elements, taken, inputProperty)
507
+ const input = inputLayers(texts.input)
508
+ numberInput(input, range)
509
+ const { booleans, shownBy } = booleanProps(graph, set, elements, taken)
510
+ const { slots, slotOf } = slotProps(graph, set, elements, taken, parts)
511
+ return {
512
+ name,
513
+ kind,
514
+ styles,
515
+ tree: componentTree(
516
+ styles.root,
517
+ {
518
+ parts,
519
+ classes: layerClassNames(styles),
520
+ texts: texts.bound,
521
+ used,
522
+ repeated,
523
+ input,
524
+ shownBy,
525
+ slotOf,
526
+ shaders: shaderLayers(graph, styles.restId, elements),
527
+ layerOf: (element) => restLayerOf(graph, styles, element)
528
+ },
529
+ rootBindings(model, !!options.itemOf, disabled, props)
530
+ ),
531
+ model,
532
+ choice,
533
+ range,
534
+ text,
535
+ valueProp: !!options.itemOf,
536
+ item: items?.item ?? null,
537
+ disabled,
538
+ props,
539
+ texts: texts.texts,
540
+ booleans,
541
+ slots,
542
+ args
543
+ }
544
+ }