@pairdraw/dom-css 0.0.0-stage → 0.16.1
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/LICENSE +21 -0
- package/README.md +219 -2
- package/dist/browser.d.ts +29 -0
- package/dist/browser.js +2 -0
- package/dist/chunks/behaviours/args.js +59 -0
- package/dist/chunks/behaviours/args.js.map +1 -0
- package/dist/chunks/behaviours/controls.js +109 -0
- package/dist/chunks/behaviours/controls.js.map +1 -0
- package/dist/chunks/behaviours/reset.js +40 -0
- package/dist/chunks/behaviours/reset.js.map +1 -0
- package/dist/chunks/behaviours/roles.js +51 -0
- package/dist/chunks/behaviours/roles.js.map +1 -0
- package/dist/chunks/behaviours/states/conditions.js +103 -0
- package/dist/chunks/behaviours/states/conditions.js.map +1 -0
- package/dist/chunks/behaviours/states/css.js +172 -0
- package/dist/chunks/behaviours/states/css.js.map +1 -0
- package/dist/chunks/behaviours/states/layers.js +1736 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -0
- package/dist/chunks/behaviours/states/model.js +144 -0
- package/dist/chunks/behaviours/states/model.js.map +1 -0
- package/dist/chunks/behaviours/states/tailwind.js +198 -0
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
- package/dist/chunks/browser.js +1352 -0
- package/dist/chunks/browser.js.map +1 -0
- package/dist/chunks/export/bundle.js +337 -0
- package/dist/chunks/export/bundle.js.map +1 -0
- package/dist/chunks/export/components/drawn.js +60 -0
- package/dist/chunks/export/components/drawn.js.map +1 -0
- package/dist/chunks/export/components/fields.js +178 -0
- package/dist/chunks/export/components/fields.js.map +1 -0
- package/dist/chunks/export/components/groups.js +297 -0
- package/dist/chunks/export/components/groups.js.map +1 -0
- package/dist/chunks/export/components/icons.js +260 -0
- package/dist/chunks/export/components/icons.js.map +1 -0
- package/dist/chunks/export/components/layers.js +1599 -0
- package/dist/chunks/export/components/layers.js.map +1 -0
- package/dist/chunks/export/index.js +477 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/export/storybook/component.js +448 -0
- package/dist/chunks/export/storybook/component.js.map +1 -0
- package/dist/chunks/export/storybook/export.js +410 -0
- package/dist/chunks/export/storybook/export.js.map +1 -0
- package/dist/chunks/import/html.js +406 -0
- package/dist/chunks/import/html.js.map +1 -0
- package/dist/chunks/import/jsx/dev-runtime.js +37 -0
- package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
- package/dist/chunks/tokens/cssom-validator.js +10 -0
- package/dist/chunks/tokens/cssom-validator.js.map +1 -0
- package/dist/export.d.ts +594 -0
- package/dist/export.js +13 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +7 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +20 -0
- package/dist/jsx-runtime.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/scene-graph.d.ts +2 -0
- package/dist/scene-graph.js +2 -0
- package/dist/scene-graph2.d.ts +10 -0
- package/dist/styling.d.ts +93 -0
- package/dist/tailwind.d.ts +39 -0
- package/dist/types.d.ts +89 -0
- package/package.json +88 -4
- package/src/behaviours/args.ts +85 -0
- package/src/behaviours/controls.ts +195 -0
- package/src/behaviours/index.ts +11 -0
- package/src/behaviours/reset.ts +37 -0
- package/src/behaviours/roles.ts +44 -0
- package/src/behaviours/states/conditions.ts +105 -0
- package/src/behaviours/states/css.ts +123 -0
- package/src/behaviours/states/index.ts +4 -0
- package/src/behaviours/states/layers.ts +205 -0
- package/src/behaviours/states/model.ts +175 -0
- package/src/behaviours/states/names.ts +39 -0
- package/src/behaviours/states/tailwind.ts +148 -0
- package/src/behaviours/states/types.ts +56 -0
- package/src/browser.ts +146 -0
- package/src/cssom.d.ts +38 -0
- package/src/export/bundle.ts +461 -0
- package/src/export/components/drawn.ts +73 -0
- package/src/export/components/fields.ts +178 -0
- package/src/export/components/groups.ts +78 -0
- package/src/export/components/icons.ts +230 -0
- package/src/export/components/layers.ts +121 -0
- package/src/export/components/model.ts +544 -0
- package/src/export/components/properties.ts +167 -0
- package/src/export/components/react/fields.ts +129 -0
- package/src/export/components/react/index.ts +412 -0
- package/src/export/components/react/props.ts +151 -0
- package/src/export/components/react/radix.ts +52 -0
- package/src/export/components/react/shared.ts +76 -0
- package/src/export/components/references.ts +212 -0
- package/src/export/components/repeats.ts +128 -0
- package/src/export/components/shaders.ts +125 -0
- package/src/export/components/styling.ts +31 -0
- package/src/export/components/tree.ts +72 -0
- package/src/export/components/vue.ts +517 -0
- package/src/export/css.ts +57 -0
- package/src/export/grid.ts +42 -0
- package/src/export/html.ts +163 -0
- package/src/export/index.ts +44 -0
- package/src/export/projection.ts +581 -0
- package/src/export/storybook/component.ts +334 -0
- package/src/export/storybook/export.ts +430 -0
- package/src/export/storybook/groups.ts +161 -0
- package/src/export/storybook/module.ts +319 -0
- package/src/export/storybook/names.ts +51 -0
- package/src/export/tailwind-jsx.ts +156 -0
- package/src/export/text.ts +415 -0
- package/src/import/css-text.ts +4 -0
- package/src/import/css-values.ts +31 -0
- package/src/import/flow.ts +248 -0
- package/src/import/html.ts +88 -0
- package/src/import/jsx/core.ts +130 -0
- package/src/import/jsx/dev-runtime.ts +8 -0
- package/src/import/jsx/runtime.ts +76 -0
- package/src/import/scene-graph.ts +582 -0
- package/src/import/style-attribute.ts +25 -0
- package/src/import/tailwind.ts +54 -0
- package/src/import/text.ts +518 -0
- package/src/index.ts +51 -0
- package/src/runtime/browser/runtime.ts +301 -0
- package/src/runtime/headless/runtime.ts +99 -0
- package/src/runtime/headless/styles.ts +330 -0
- package/src/runtime/index.ts +11 -0
- package/src/tokens/conditions.ts +66 -0
- package/src/tokens/cssom-validator.ts +8 -0
- package/src/tokens/custom-properties.ts +331 -0
- package/src/tokens/expression.ts +128 -0
- package/src/tokens/formula.ts +140 -0
- package/src/tokens/index.ts +43 -0
- package/src/tokens/names.ts +161 -0
- package/src/tokens/references.ts +201 -0
- package/src/tokens/stylesheet.ts +304 -0
- package/src/tokens/validate.ts +86 -0
- package/src/tokens/values.ts +44 -0
- package/src/types.ts +59 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { isEqual } from 'es-toolkit/predicate'
|
|
2
|
+
|
|
3
|
+
import type { ComponentPropertyReferenceField, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
4
|
+
|
|
5
|
+
/** What a layer draws, which an instance can change without a property. */
|
|
6
|
+
const DRAWN = [
|
|
7
|
+
'fills',
|
|
8
|
+
'strokes',
|
|
9
|
+
'effects',
|
|
10
|
+
'opacity',
|
|
11
|
+
'cornerRadius',
|
|
12
|
+
'fontFamily',
|
|
13
|
+
'fontSize',
|
|
14
|
+
'fontWeight',
|
|
15
|
+
'italic'
|
|
16
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
17
|
+
|
|
18
|
+
/** How a layer lays out what it holds, which an instance can change without a property. */
|
|
19
|
+
const LAYOUT = [
|
|
20
|
+
'layoutMode',
|
|
21
|
+
'itemSpacing',
|
|
22
|
+
'paddingTop',
|
|
23
|
+
'paddingRight',
|
|
24
|
+
'paddingBottom',
|
|
25
|
+
'paddingLeft',
|
|
26
|
+
'primaryAxisAlign',
|
|
27
|
+
'counterAxisAlign'
|
|
28
|
+
] as const satisfies readonly (keyof SceneNode)[]
|
|
29
|
+
|
|
30
|
+
const same = (a: SceneNode, b: SceneNode, fields: readonly (keyof SceneNode)[]) =>
|
|
31
|
+
fields.every((field) => isEqual(a[field], b[field]))
|
|
32
|
+
|
|
33
|
+
const bound = (node: SceneNode, field: ComponentPropertyReferenceField) =>
|
|
34
|
+
node.componentPropertyReferences.some((reference) => reference.field === field)
|
|
35
|
+
|
|
36
|
+
function childrenMatch(graph: SceneGraph, drawn: SceneNode, main: SceneNode): boolean {
|
|
37
|
+
const children = graph.getChildren(drawn.id)
|
|
38
|
+
const mains = graph.getChildren(main.id)
|
|
39
|
+
return (
|
|
40
|
+
children.length === mains.length &&
|
|
41
|
+
children.every((child, index) => {
|
|
42
|
+
const counterpart = mains.at(index)
|
|
43
|
+
return counterpart !== undefined && layerMatches(graph, child, counterpart)
|
|
44
|
+
})
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether a layer of an instance draws as its main component's does, apart from what a
|
|
50
|
+
* property of the component sets: the text a text property shows and what a boolean property
|
|
51
|
+
* shows or hides, which the component's props pass on.
|
|
52
|
+
*/
|
|
53
|
+
function layerMatches(graph: SceneGraph, drawn: SceneNode, main: SceneNode): boolean {
|
|
54
|
+
if (drawn.type !== main.type) return false
|
|
55
|
+
if (!bound(main, 'VISIBLE') && drawn.visible !== main.visible) return false
|
|
56
|
+
if (drawn.type === 'TEXT' && !bound(main, 'TEXT') && drawn.text !== main.text) return false
|
|
57
|
+
if (drawn.type === 'INSTANCE' && drawn.componentId !== main.componentId) return false
|
|
58
|
+
return same(drawn, main, DRAWN) && same(drawn, main, LAYOUT) && childrenMatch(graph, drawn, main)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether an instance draws exactly as the variant it shows, so a generated component given
|
|
63
|
+
* its property values draws it: the same look, layout, words, and nested components, apart
|
|
64
|
+
* from where its parent places and sizes it. An instance with any other change, such as edited
|
|
65
|
+
* text no property sets or a fill of its own, draws its own layers instead.
|
|
66
|
+
*/
|
|
67
|
+
export function drawsAsMain(graph: SceneGraph, instance: SceneNode, main: SceneNode): boolean {
|
|
68
|
+
return (
|
|
69
|
+
same(instance, main, DRAWN) &&
|
|
70
|
+
same(instance, main, LAYOUT) &&
|
|
71
|
+
childrenMatch(graph, instance, main)
|
|
72
|
+
)
|
|
73
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { BehaviourArgs } from '#dom-css/behaviours/args'
|
|
2
|
+
import { INPUT_RESET } from '#dom-css/behaviours/reset'
|
|
3
|
+
import type { StateElement } from '#dom-css/behaviours/states/types'
|
|
4
|
+
import { omit, pick } from 'es-toolkit/object'
|
|
5
|
+
import { isEmptyObject } from 'es-toolkit/predicate'
|
|
6
|
+
|
|
7
|
+
import { es } from '@pairdraw/emit'
|
|
8
|
+
import type { ComponentPropertyDefinition } from '@pairdraw/scene-graph'
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_NUMBER_SETTINGS,
|
|
11
|
+
numberSettings,
|
|
12
|
+
type Behaviour
|
|
13
|
+
} from '@pairdraw/scene-graph/behaviours'
|
|
14
|
+
|
|
15
|
+
import type { GeneratedKind, RangeModel, TextModel } from './model'
|
|
16
|
+
|
|
17
|
+
/** Kinds that hold a number in a range. */
|
|
18
|
+
const RANGED: ReadonlySet<GeneratedKind> = new Set(['slider', 'progress', 'numberField'])
|
|
19
|
+
|
|
20
|
+
/** Kinds whose text layer is an input, and the text value that layer shows. */
|
|
21
|
+
const INPUT_VALUES: Partial<Record<GeneratedKind, string>> = {
|
|
22
|
+
textField: 'value',
|
|
23
|
+
textarea: 'value',
|
|
24
|
+
numberField: 'text'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The text value whose bound layer a kind draws as its input, if it has one. */
|
|
28
|
+
export const inputValue = (kind: GeneratedKind): string | undefined => INPUT_VALUES[kind]
|
|
29
|
+
|
|
30
|
+
/** The number a ranged kind binds, starting where the design's behaviour does. */
|
|
31
|
+
export function rangeModel(kind: GeneratedKind, behaviour: Behaviour): RangeModel | null {
|
|
32
|
+
if (!RANGED.has(kind)) return null
|
|
33
|
+
return { ...(numberSettings(behaviour, 'value') ?? DEFAULT_NUMBER_SETTINGS) }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The text a text field or textarea binds. When the design draws it filled and empty, the
|
|
38
|
+
* empty field shows its text property's words as a placeholder and starts empty; otherwise it
|
|
39
|
+
* starts with them.
|
|
40
|
+
*/
|
|
41
|
+
export function textModel(
|
|
42
|
+
kind: GeneratedKind,
|
|
43
|
+
definition: ComponentPropertyDefinition | undefined,
|
|
44
|
+
args: BehaviourArgs
|
|
45
|
+
): TextModel | null {
|
|
46
|
+
if (kind !== 'textField' && kind !== 'textarea') return null
|
|
47
|
+
const words = typeof definition?.defaultValue === 'string' ? definition.defaultValue : ''
|
|
48
|
+
return args.filled ? { default: '', placeholder: words } : { default: words, placeholder: null }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A field's text layer drawn as its input, and the same layer in other words, which it replaces. */
|
|
52
|
+
export interface InputLayers {
|
|
53
|
+
element: StateElement
|
|
54
|
+
replaced: StateElement[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** What a text layer's look is, as opposed to where it sits. */
|
|
58
|
+
const TEXT_LOOK = [
|
|
59
|
+
'color',
|
|
60
|
+
'opacity',
|
|
61
|
+
'font-family',
|
|
62
|
+
'font-size',
|
|
63
|
+
'font-style',
|
|
64
|
+
'font-weight',
|
|
65
|
+
'letter-spacing',
|
|
66
|
+
'line-height',
|
|
67
|
+
'text-decoration',
|
|
68
|
+
'text-transform'
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The input among a field's bound text layers: the one shown at rest. A layer reading other
|
|
73
|
+
* words is the same layer in another state; drawn while filled, its look is the typed text's,
|
|
74
|
+
* and the resting look becomes the placeholder's.
|
|
75
|
+
*/
|
|
76
|
+
export function inputLayers(bound: readonly StateElement[]): InputLayers | null {
|
|
77
|
+
const element = bound.find((layer) => layer.base.display !== 'none') ?? bound.at(0)
|
|
78
|
+
if (!element) return null
|
|
79
|
+
const replaced = bound.filter((layer) => layer !== element)
|
|
80
|
+
const filled = replaced.find((layer) =>
|
|
81
|
+
layer.rules.some((rule) => rule.conditions.some((condition) => condition.type === 'filled'))
|
|
82
|
+
)
|
|
83
|
+
if (filled) {
|
|
84
|
+
const typed = pick(omit(filled.base, ['display']), TEXT_LOOK)
|
|
85
|
+
element.rules.push({
|
|
86
|
+
conditions: [],
|
|
87
|
+
pseudo: 'placeholder',
|
|
88
|
+
style: pick(element.base, TEXT_LOOK)
|
|
89
|
+
})
|
|
90
|
+
element.base = { ...element.base, ...typed }
|
|
91
|
+
// The layer shows in every state now, so states that hid or showed it no longer do.
|
|
92
|
+
element.rules = element.rules.filter((rule) => !rule.style.display)
|
|
93
|
+
}
|
|
94
|
+
element.base = { ...INPUT_RESET, ...omit(element.base, ['display', 'white-space']) }
|
|
95
|
+
return { element, replaced }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** What the library sets on a part it places itself, which the design's drawn place gives way to. */
|
|
99
|
+
const PLACED: Partial<Record<GeneratedKind, Record<string, readonly string[]>>> = {
|
|
100
|
+
slider: { range: ['left', 'right', 'width'], thumb: ['left', 'right', 'transform'] },
|
|
101
|
+
progress: { indicator: ['width'] }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Gives a number field's input, which the design draws as words that hug their value, room
|
|
106
|
+
* for its longest value rather than an input's default width, and no spinner of the browser's
|
|
107
|
+
* own, since the design draws its steppers.
|
|
108
|
+
*/
|
|
109
|
+
export function numberInput(input: InputLayers | null, range: RangeModel | null): void {
|
|
110
|
+
if (!input || !range) return
|
|
111
|
+
const { element } = input
|
|
112
|
+
element.base = { ...element.base, appearance: 'textfield' }
|
|
113
|
+
element.rules.push({
|
|
114
|
+
conditions: [],
|
|
115
|
+
pseudo: '-webkit-inner-spin-button',
|
|
116
|
+
style: { appearance: 'none', margin: '0' }
|
|
117
|
+
})
|
|
118
|
+
if (Object.hasOwn(element.base, 'width')) return
|
|
119
|
+
const digits = Math.max(String(range.min).length, String(range.max).length)
|
|
120
|
+
element.base = { ...element.base, width: `${digits + 1}ch` }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Kinds whose root Reka and Radix render as a span, which a size alone does not lay out. */
|
|
124
|
+
const SPAN_ROOTS: ReadonlySet<GeneratedKind> = new Set(['slider'])
|
|
125
|
+
|
|
126
|
+
/** Lays a span root out as the block the design draws, unless the design says otherwise. */
|
|
127
|
+
export function blockRoot(kind: GeneratedKind, root: StateElement): void {
|
|
128
|
+
if (SPAN_ROOTS.has(kind) && !Object.hasOwn(root.base, 'display'))
|
|
129
|
+
root.base = { ...root.base, display: 'block' }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Drops the place a slider or progress bar draws its moving parts at, which the value sets. */
|
|
133
|
+
export function freePlacedParts(
|
|
134
|
+
kind: GeneratedKind,
|
|
135
|
+
parts: ReadonlyMap<StateElement, string>
|
|
136
|
+
): void {
|
|
137
|
+
for (const [element, part] of parts) {
|
|
138
|
+
const placed = PLACED[kind]?.[part]
|
|
139
|
+
if (!placed) continue
|
|
140
|
+
element.base = omit(element.base, placed)
|
|
141
|
+
element.rules = element.rules
|
|
142
|
+
.map((rule) => ({ ...rule, style: omit(rule.style, placed) }))
|
|
143
|
+
.filter((rule) => !isEmptyObject(rule.style))
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const PERCENT = es.parseExpression(`({ width: \`\${$share}%\` })`)
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A progress bar's indicator style, reaching the value's share of its range, the value held
|
|
151
|
+
* within it: the value itself for a range of 0 to 100. Null for an empty range, which has no
|
|
152
|
+
* share.
|
|
153
|
+
*/
|
|
154
|
+
export function progressWidth(range: RangeModel, value: es.SyntaxNode): es.SyntaxNode | null {
|
|
155
|
+
const span = range.max - range.min
|
|
156
|
+
if (span <= 0) return null
|
|
157
|
+
// A value outside the range fills the indicator no further than its ends.
|
|
158
|
+
const clamped = es.fill(es.parseExpression('Math.min($max, Math.max($min, $value))'), {
|
|
159
|
+
$value: value,
|
|
160
|
+
$min: es.number(range.min),
|
|
161
|
+
$max: es.number(range.max)
|
|
162
|
+
})
|
|
163
|
+
const offset =
|
|
164
|
+
range.min === 0
|
|
165
|
+
? clamped
|
|
166
|
+
: es.fill(es.parseExpression('$value - $min'), {
|
|
167
|
+
$value: clamped,
|
|
168
|
+
$min: es.number(range.min)
|
|
169
|
+
})
|
|
170
|
+
const share =
|
|
171
|
+
span === 100
|
|
172
|
+
? offset
|
|
173
|
+
: es.fill(es.parseExpression('(($offset) / $span) * 100'), {
|
|
174
|
+
$offset: offset,
|
|
175
|
+
$span: es.number(span)
|
|
176
|
+
})
|
|
177
|
+
return es.fill(PERCENT, { $share: share })
|
|
178
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { StateElement } from '#dom-css/behaviours/states/types'
|
|
2
|
+
|
|
3
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
4
|
+
import { behaviourOwner, findLayerByPath } from '@pairdraw/scene-graph/behaviours'
|
|
5
|
+
import { instanceMainComponent } from '@pairdraw/scene-graph/instances'
|
|
6
|
+
|
|
7
|
+
import type { ComponentModel } from './model'
|
|
8
|
+
import { placementOnly, referenceValues, shownText, type UsedLayer } from './references'
|
|
9
|
+
import { slugValues, textOf, type ChoiceModel } from './repeats'
|
|
10
|
+
|
|
11
|
+
/** Groups whose items are their own primitive inside the group, as Reka and Radix have them. */
|
|
12
|
+
export const GROUP_ITEMS = {
|
|
13
|
+
radioGroup: 'radioGroupItem',
|
|
14
|
+
toggleGroup: 'toggleGroupItem',
|
|
15
|
+
accordion: 'accordionItem'
|
|
16
|
+
} as const
|
|
17
|
+
|
|
18
|
+
export type GroupKind = keyof typeof GROUP_ITEMS
|
|
19
|
+
export type ItemKind = (typeof GROUP_ITEMS)[GroupKind]
|
|
20
|
+
|
|
21
|
+
export const isGroup = (kind: string): kind is GroupKind => Object.hasOwn(GROUP_ITEMS, kind)
|
|
22
|
+
|
|
23
|
+
/** Builds the component of a group's items, named for the group. */
|
|
24
|
+
export type ItemBuilder = (owner: SceneNode) => ComponentModel | null
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A group's items: the component they share, generated as the group's item primitive, and
|
|
28
|
+
* each item in the items slot as a use of it with the value it stands for. The group chooses
|
|
29
|
+
* among those values, starting on the item the design draws on, or none.
|
|
30
|
+
*/
|
|
31
|
+
export function groupItems(
|
|
32
|
+
graph: SceneGraph,
|
|
33
|
+
variantIds: readonly string[],
|
|
34
|
+
itemsElement: StateElement | undefined,
|
|
35
|
+
build: ItemBuilder
|
|
36
|
+
): { item: ComponentModel; used: Map<StateElement, UsedLayer>; choice: ChoiceModel } | null {
|
|
37
|
+
const elements = (itemsElement?.children ?? []).filter(
|
|
38
|
+
(child): child is StateElement => child.type === 'element'
|
|
39
|
+
)
|
|
40
|
+
const instances = elements.map((element) => {
|
|
41
|
+
const path = element.key.split('\0')[0] ?? ''
|
|
42
|
+
const node = variantIds
|
|
43
|
+
.map((id) => findLayerByPath(graph, id, path))
|
|
44
|
+
.find((found) => found?.type === 'INSTANCE')
|
|
45
|
+
const main = node && instanceMainComponent(graph, node)
|
|
46
|
+
return node && main ? { node, owner: behaviourOwner(graph, main) } : null
|
|
47
|
+
})
|
|
48
|
+
const owner = instances.find((instance) => instance?.owner)?.owner
|
|
49
|
+
const item = owner && build(owner)
|
|
50
|
+
if (!owner || !item) return null
|
|
51
|
+
|
|
52
|
+
// An item is named by its label property when it has one, else by the words it reads.
|
|
53
|
+
const label = item.texts.at(0)
|
|
54
|
+
const values = slugValues(
|
|
55
|
+
elements.map((element, index) => {
|
|
56
|
+
const node = instances[index]?.node
|
|
57
|
+
return (label && node && shownText(graph, node, label.id)) || textOf(element)
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
const used = new Map<StateElement, UsedLayer>()
|
|
61
|
+
let chosen: string | null = null
|
|
62
|
+
for (const [index, element] of elements.entries()) {
|
|
63
|
+
const instance = instances[index]
|
|
64
|
+
const value = values.at(index)
|
|
65
|
+
if (instance?.owner?.id !== owner.id || value === undefined) continue
|
|
66
|
+
const { props, model } = referenceValues(graph, instance.node, item)
|
|
67
|
+
if (model) chosen ??= value
|
|
68
|
+
used.set(element, {
|
|
69
|
+
type: 'reference',
|
|
70
|
+
component: item.name,
|
|
71
|
+
props: [{ name: 'value', value }, ...props],
|
|
72
|
+
model: null
|
|
73
|
+
})
|
|
74
|
+
element.children = []
|
|
75
|
+
placementOnly(element)
|
|
76
|
+
}
|
|
77
|
+
return { item, used, choice: { options: values, default: chosen } }
|
|
78
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { groupBy } from 'es-toolkit/array'
|
|
2
|
+
import { upperFirst } from 'es-toolkit/string'
|
|
3
|
+
|
|
4
|
+
import { es } from '@pairdraw/emit'
|
|
5
|
+
|
|
6
|
+
import { claimName } from '../storybook/names'
|
|
7
|
+
import type { ComponentModel, ComponentNode } from './model'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where generated components take their icons from: Iconify, which draws every set, or one
|
|
11
|
+
* set's own package. An icon the chosen package does not have stays an Iconify icon.
|
|
12
|
+
*/
|
|
13
|
+
export const ICON_PACKAGES = [
|
|
14
|
+
'iconify',
|
|
15
|
+
'lucide',
|
|
16
|
+
'heroicons',
|
|
17
|
+
'react-icons',
|
|
18
|
+
'fontawesome'
|
|
19
|
+
] as const
|
|
20
|
+
export type IconPackage = (typeof ICON_PACKAGES)[number]
|
|
21
|
+
|
|
22
|
+
export function isIconPackage(value: unknown): value is IconPackage {
|
|
23
|
+
return ICON_PACKAGES.some((id) => id === value)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type IconFramework = 'react' | 'vue'
|
|
27
|
+
|
|
28
|
+
/** The packages a framework's components can take icons from; react-icons is React's alone. */
|
|
29
|
+
export function iconPackagesFor(framework: IconFramework): IconPackage[] {
|
|
30
|
+
return ICON_PACKAGES.filter((id) => id !== 'react-icons' || framework === 'react')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One name a module exports, and the name the component's script knows it by. */
|
|
34
|
+
export interface IconImport {
|
|
35
|
+
source: string
|
|
36
|
+
imported: string
|
|
37
|
+
local: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* How a component draws an icon from a package: as the icon's own component, or, for Font
|
|
42
|
+
* Awesome, as `FontAwesomeIcon` given the icon's definition.
|
|
43
|
+
*/
|
|
44
|
+
export type IconDrawing =
|
|
45
|
+
| { type: 'component'; import: IconImport }
|
|
46
|
+
| { type: 'fontawesome'; import: IconImport; component: IconImport }
|
|
47
|
+
|
|
48
|
+
/** `arrow-down-0-1` → `ArrowDown01`, as the icon packages name their exports. */
|
|
49
|
+
function pascalName(name: string): string {
|
|
50
|
+
return name
|
|
51
|
+
.split('-')
|
|
52
|
+
.filter((part) => part.length > 0)
|
|
53
|
+
.map(upperFirst)
|
|
54
|
+
.join('')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const one = (source: string, imported: string): IconImport => ({
|
|
58
|
+
source,
|
|
59
|
+
imported,
|
|
60
|
+
local: imported
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
function lucide(name: string, framework: IconFramework): IconImport {
|
|
64
|
+
// Each Lucide package also exports every icon prefixed, apart from any component of the design.
|
|
65
|
+
return one(
|
|
66
|
+
framework === 'react' ? 'lucide-react' : 'lucide-vue-next',
|
|
67
|
+
`Lucide${pascalName(name)}`
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Heroicons 2 names on Iconify: `x` outline, `x-solid`, `x-20-solid` mini, `x-16-solid` micro. */
|
|
72
|
+
const HEROICON_STYLES = [
|
|
73
|
+
{ suffix: '-20-solid', folder: '20/solid', reactIcons: 'HiMini' },
|
|
74
|
+
{ suffix: '-16-solid', folder: '16/solid', reactIcons: 'HiMicro' },
|
|
75
|
+
{ suffix: '-solid', folder: '24/solid', reactIcons: 'Hi' }
|
|
76
|
+
] as const
|
|
77
|
+
|
|
78
|
+
function heroicon(name: string): { base: string; folder: string; reactIcons: string } {
|
|
79
|
+
const style = HEROICON_STYLES.find(({ suffix }) => name.endsWith(suffix))
|
|
80
|
+
if (!style) return { base: name, folder: '24/outline', reactIcons: 'HiOutline' }
|
|
81
|
+
return { base: name.slice(0, -style.suffix.length), ...style }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Iconify sets that `react-icons` ships, by the module and the prefix of its export names. */
|
|
85
|
+
const REACT_ICONS_SETS: Partial<Record<string, { module: string; prefix: string }>> = {
|
|
86
|
+
lucide: { module: 'lu', prefix: 'Lu' },
|
|
87
|
+
tabler: { module: 'tb', prefix: 'Tb' },
|
|
88
|
+
feather: { module: 'fi', prefix: 'Fi' },
|
|
89
|
+
ri: { module: 'ri', prefix: 'Ri' },
|
|
90
|
+
bi: { module: 'bs', prefix: 'Bs' },
|
|
91
|
+
ph: { module: 'pi', prefix: 'Pi' },
|
|
92
|
+
'radix-icons': { module: 'rx', prefix: 'Rx' },
|
|
93
|
+
'simple-icons': { module: 'si', prefix: 'Si' },
|
|
94
|
+
'fa6-solid': { module: 'fa6', prefix: 'Fa' },
|
|
95
|
+
'fa6-brands': { module: 'fa6', prefix: 'Fa' },
|
|
96
|
+
'fa6-regular': { module: 'fa6', prefix: 'FaReg' }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function reactIcon(prefix: string, name: string): IconImport | null {
|
|
100
|
+
if (prefix === 'heroicons') {
|
|
101
|
+
const { base, reactIcons } = heroicon(name)
|
|
102
|
+
return one('react-icons/hi2', `${reactIcons}${pascalName(base)}`)
|
|
103
|
+
}
|
|
104
|
+
const set = REACT_ICONS_SETS[prefix]
|
|
105
|
+
return set ? one(`react-icons/${set.module}`, `${set.prefix}${pascalName(name)}`) : null
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Font Awesome's free packages by the Iconify sets of versions 5 and 6. */
|
|
109
|
+
const FONTAWESOME_STYLES: Partial<Record<string, { source: string; suffix: string }>> = {
|
|
110
|
+
'fa6-solid': { source: '@fortawesome/free-solid-svg-icons', suffix: '' },
|
|
111
|
+
'fa-solid': { source: '@fortawesome/free-solid-svg-icons', suffix: '' },
|
|
112
|
+
'fa6-regular': { source: '@fortawesome/free-regular-svg-icons', suffix: 'Regular' },
|
|
113
|
+
'fa-regular': { source: '@fortawesome/free-regular-svg-icons', suffix: 'Regular' },
|
|
114
|
+
'fa6-brands': { source: '@fortawesome/free-brands-svg-icons', suffix: '' },
|
|
115
|
+
'fa-brands': { source: '@fortawesome/free-brands-svg-icons', suffix: '' }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function fontAwesome(prefix: string, name: string, framework: IconFramework): IconDrawing | null {
|
|
119
|
+
const style = FONTAWESOME_STYLES[prefix]
|
|
120
|
+
if (!style) return null
|
|
121
|
+
const imported = `fa${pascalName(name)}`
|
|
122
|
+
return {
|
|
123
|
+
type: 'fontawesome',
|
|
124
|
+
// Solid and regular share names, so a regular icon is known by a name of its own.
|
|
125
|
+
import: { source: style.source, imported, local: `${imported}${style.suffix}` },
|
|
126
|
+
component: one(
|
|
127
|
+
framework === 'react' ? '@fortawesome/react-fontawesome' : '@fortawesome/vue-fontawesome',
|
|
128
|
+
'FontAwesomeIcon'
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* How `icon`, an Iconify `prefix:name`, is drawn from `iconPackage` in a component for
|
|
135
|
+
* `framework`; null when that package lacks the icon's set, so Iconify draws it.
|
|
136
|
+
*/
|
|
137
|
+
export function iconDrawing(
|
|
138
|
+
icon: string,
|
|
139
|
+
iconPackage: IconPackage,
|
|
140
|
+
framework: IconFramework
|
|
141
|
+
): IconDrawing | null {
|
|
142
|
+
const separator = icon.indexOf(':')
|
|
143
|
+
if (separator <= 0) return null
|
|
144
|
+
const prefix = icon.slice(0, separator)
|
|
145
|
+
const name = icon.slice(separator + 1)
|
|
146
|
+
if (!name) return null
|
|
147
|
+
switch (iconPackage) {
|
|
148
|
+
case 'lucide':
|
|
149
|
+
return prefix === 'lucide' ? { type: 'component', import: lucide(name, framework) } : null
|
|
150
|
+
case 'heroicons': {
|
|
151
|
+
if (prefix !== 'heroicons') return null
|
|
152
|
+
const { base, folder } = heroicon(name)
|
|
153
|
+
const source = `@heroicons/${framework}/${folder}`
|
|
154
|
+
return { type: 'component', import: one(source, `${pascalName(base)}Icon`) }
|
|
155
|
+
}
|
|
156
|
+
case 'react-icons': {
|
|
157
|
+
// react-icons is React's alone.
|
|
158
|
+
const found = framework === 'react' ? reactIcon(prefix, name) : null
|
|
159
|
+
return found ? { type: 'component', import: found } : null
|
|
160
|
+
}
|
|
161
|
+
case 'fontawesome':
|
|
162
|
+
return fontAwesome(prefix, name, framework)
|
|
163
|
+
default:
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The components a component's markup uses in place of its layers. */
|
|
169
|
+
function referencedComponents(node: ComponentNode, found: string[]): string[] {
|
|
170
|
+
if (node.type === 'reference') found.push(node.component)
|
|
171
|
+
if (node.type === 'element') for (const child of node.children) referencedComponents(child, found)
|
|
172
|
+
return found
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Names the icons a component imports apart from every other name its code knows: the
|
|
177
|
+
* component itself, its group item, the components it uses, and `reserved`, such as Iconify's.
|
|
178
|
+
* A layer named `Bell Icon` holding Heroicons' bell would otherwise import `BellIcon` beside
|
|
179
|
+
* declaring it; the import is aliased instead, and each module's icon keeps one name.
|
|
180
|
+
*/
|
|
181
|
+
export function iconNamer(
|
|
182
|
+
component: ComponentModel,
|
|
183
|
+
reserved: readonly string[]
|
|
184
|
+
): (drawing: IconDrawing) => IconDrawing {
|
|
185
|
+
const taken = new Set([
|
|
186
|
+
component.name,
|
|
187
|
+
...(component.item ? [component.item.name] : []),
|
|
188
|
+
...referencedComponents(component.tree, []),
|
|
189
|
+
...reserved
|
|
190
|
+
])
|
|
191
|
+
const locals = new Map<string, string>()
|
|
192
|
+
const named = (entry: IconImport): IconImport => {
|
|
193
|
+
const key = `${entry.source}\0${entry.imported}`
|
|
194
|
+
let local = locals.get(key)
|
|
195
|
+
if (local === undefined) {
|
|
196
|
+
local = claimName(entry.local, taken)
|
|
197
|
+
locals.set(key, local)
|
|
198
|
+
}
|
|
199
|
+
return { ...entry, local }
|
|
200
|
+
}
|
|
201
|
+
return (drawing) =>
|
|
202
|
+
drawing.type === 'fontawesome'
|
|
203
|
+
? { ...drawing, import: named(drawing.import), component: named(drawing.component) }
|
|
204
|
+
: { ...drawing, import: named(drawing.import) }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Every import a component's icons need, one declaration per module, in a stable order. */
|
|
208
|
+
export function iconImportDeclarations(imports: readonly IconImport[]): es.SyntaxNode[] {
|
|
209
|
+
const byLocal = new Map(imports.map((entry) => [entry.local, entry]))
|
|
210
|
+
const bySource = groupBy([...byLocal.values()], (entry) => entry.source)
|
|
211
|
+
return Object.keys(bySource)
|
|
212
|
+
.sort()
|
|
213
|
+
.map((source) => ({
|
|
214
|
+
type: 'ImportDeclaration',
|
|
215
|
+
importKind: 'value',
|
|
216
|
+
specifiers: (bySource[source] ?? [])
|
|
217
|
+
.toSorted((a, b) => a.local.localeCompare(b.local))
|
|
218
|
+
.map((entry) => ({
|
|
219
|
+
type: 'ImportSpecifier',
|
|
220
|
+
imported: es.identifier(entry.imported),
|
|
221
|
+
local: es.identifier(entry.local)
|
|
222
|
+
})),
|
|
223
|
+
source: es.string(source)
|
|
224
|
+
}))
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The imports a drawing needs. */
|
|
228
|
+
export function drawingImports(drawing: IconDrawing): IconImport[] {
|
|
229
|
+
return drawing.type === 'fontawesome' ? [drawing.import, drawing.component] : [drawing.import]
|
|
230
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { stateStylesToCSS } from '#dom-css/behaviours/states/css'
|
|
2
|
+
import { restLayerOf, stateStyles } from '#dom-css/behaviours/states/model'
|
|
3
|
+
import { stateStylesToTailwind } from '#dom-css/behaviours/states/tailwind'
|
|
4
|
+
import type { StateElement, StateNode, StateStyles } from '#dom-css/behaviours/states/types'
|
|
5
|
+
import type { DesignDocument, DesignElement, DesignNode } from '#dom-css/types'
|
|
6
|
+
import { zip } from 'es-toolkit/array'
|
|
7
|
+
|
|
8
|
+
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
9
|
+
|
|
10
|
+
import { extractImageAssets, type ExportHTMLFile } from '../bundle'
|
|
11
|
+
import { serializedElements, serializeHTML } from '../html'
|
|
12
|
+
import type { SceneGraphToDesignOptions } from '../projection'
|
|
13
|
+
import { claimName, identifierName } from '../storybook/names'
|
|
14
|
+
import { componentModel, type ComponentGeneratorOptions, type GeneratedComponent } from './model'
|
|
15
|
+
import { reactComponent } from './react'
|
|
16
|
+
import { vueComponent } from './vue'
|
|
17
|
+
|
|
18
|
+
/** The frameworks a layer can be generated as a component for. */
|
|
19
|
+
export const LAYER_COMPONENT_FRAMEWORKS = ['vue', 'react'] as const
|
|
20
|
+
export type LayerComponentFramework = (typeof LAYER_COMPONENT_FRAMEWORKS)[number]
|
|
21
|
+
|
|
22
|
+
/** A layer as a component: its name and the files that make it up. */
|
|
23
|
+
export interface LayerComponent {
|
|
24
|
+
name: string
|
|
25
|
+
layerId: string
|
|
26
|
+
files: GeneratedComponent['files']
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Each layer as a Vue or React component, as the Storybook export generates a component set's:
|
|
31
|
+
* a component set with its variants as props and its behaviour's parts, and any other layer as
|
|
32
|
+
* a plain component of the elements it draws. Names stay distinct across the layers.
|
|
33
|
+
*/
|
|
34
|
+
export async function layerComponents(
|
|
35
|
+
graph: SceneGraph,
|
|
36
|
+
layerIds: readonly string[],
|
|
37
|
+
framework: LayerComponentFramework,
|
|
38
|
+
options: Pick<SceneGraphToDesignOptions, 'vectorElement'> & ComponentGeneratorOptions = {}
|
|
39
|
+
): Promise<LayerComponent[]> {
|
|
40
|
+
const generate = framework === 'vue' ? vueComponent : reactComponent
|
|
41
|
+
const taken = new Set<string>()
|
|
42
|
+
const components: LayerComponent[] = []
|
|
43
|
+
for (const layerId of layerIds) {
|
|
44
|
+
const layer = graph.getNode(layerId)
|
|
45
|
+
if (!layer) continue
|
|
46
|
+
const name = claimName(identifierName(layer.name, 'Component'), taken)
|
|
47
|
+
// No other layer is a generated component here, but icons from a set are still used.
|
|
48
|
+
const model = componentModel(graph, layer, { ...options, name, references: new Map() })
|
|
49
|
+
if (!model) continue
|
|
50
|
+
const { files } = await generate(model, { styling: options.styling, icons: options.icons })
|
|
51
|
+
components.push({ name, layerId, files })
|
|
52
|
+
}
|
|
53
|
+
return components
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Layers as HTML and the stylesheet that styles it, and the images it points at. */
|
|
57
|
+
export interface LayerMarkup {
|
|
58
|
+
html: string
|
|
59
|
+
/** The layer each element of `html` draws, in the order they open, `null` for none. */
|
|
60
|
+
layerIds: (string | null)[]
|
|
61
|
+
css: string
|
|
62
|
+
images: ExportHTMLFile[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The state element each element of markup built from it draws, which it builds one for one. */
|
|
66
|
+
function drawnBy(
|
|
67
|
+
state: StateNode,
|
|
68
|
+
design: DesignNode,
|
|
69
|
+
found: Map<DesignElement, StateElement>
|
|
70
|
+
): void {
|
|
71
|
+
if (state.type === 'text' || design.type === 'text') return
|
|
72
|
+
found.set(design, state)
|
|
73
|
+
for (const [child, built] of zip(state.children, design.children)) drawnBy(child, built, found)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The layer of each element `serializeHTML` writes for the document, in the order they open. */
|
|
77
|
+
function markupLayers(graph: SceneGraph, styles: StateStyles, document: DesignDocument) {
|
|
78
|
+
const states = new Map<DesignElement, StateElement>()
|
|
79
|
+
for (const root of document.children) drawnBy(styles.root, root, states)
|
|
80
|
+
return serializedElements(document).map((element) => {
|
|
81
|
+
const state = element && states.get(element)
|
|
82
|
+
return (state && restLayerOf(graph, styles, state)) ?? null
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Layers as indented HTML with a readable class per layer and one stylesheet, as the generated
|
|
88
|
+
* components draw them, or with Tailwind utilities in place of the classes and no stylesheet.
|
|
89
|
+
* Images are files under `assetBasePath` rather than inlined.
|
|
90
|
+
*/
|
|
91
|
+
export async function layerMarkup(
|
|
92
|
+
graph: SceneGraph,
|
|
93
|
+
layerIds: readonly string[],
|
|
94
|
+
options: Pick<SceneGraphToDesignOptions, 'vectorElement'> &
|
|
95
|
+
ComponentGeneratorOptions & { assetBasePath?: string } = {}
|
|
96
|
+
): Promise<LayerMarkup> {
|
|
97
|
+
const taken = new Set<string>()
|
|
98
|
+
const html: string[] = []
|
|
99
|
+
const layers: (string | null)[] = []
|
|
100
|
+
const css: string[] = []
|
|
101
|
+
for (const layerId of layerIds) {
|
|
102
|
+
const layer = graph.getNode(layerId)
|
|
103
|
+
const styles = layer && stateStyles(graph, layer, options)
|
|
104
|
+
if (!layer || !styles) continue
|
|
105
|
+
// Two layers named alike would share class names, and their rules would collide.
|
|
106
|
+
const name = claimName(styles.name, taken, { separator: ' ' })
|
|
107
|
+
const named = { ...styles, name }
|
|
108
|
+
const sheet =
|
|
109
|
+
options.styling === 'tailwind'
|
|
110
|
+
? { document: stateStylesToTailwind(named), css: '' }
|
|
111
|
+
: await stateStylesToCSS(named)
|
|
112
|
+
html.push(serializeHTML(sheet.document, { indent: true }))
|
|
113
|
+
layers.push(...markupLayers(graph, named, sheet.document))
|
|
114
|
+
if (sheet.css) css.push(sheet.css)
|
|
115
|
+
}
|
|
116
|
+
const { html: page, files } = extractImageAssets(
|
|
117
|
+
html.join('\n\n'),
|
|
118
|
+
options.assetBasePath ?? 'assets'
|
|
119
|
+
)
|
|
120
|
+
return { html: page, layerIds: layers, css: css.join('\n'), images: files }
|
|
121
|
+
}
|