@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.
- 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 +1594 -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 +73 -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 +606 -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,606 @@
|
|
|
1
|
+
import { HEADING_RESET } from '#dom-css/behaviours/reset'
|
|
2
|
+
import { stateStylesToCSS } from '#dom-css/behaviours/states/css'
|
|
3
|
+
import dedent from 'dedent'
|
|
4
|
+
import { compact } from 'es-toolkit/array'
|
|
5
|
+
import { omit } from 'es-toolkit/object'
|
|
6
|
+
import { camelCase, upperFirst } from 'es-toolkit/string'
|
|
7
|
+
|
|
8
|
+
import { es, jsx } from '@pairdraw/emit'
|
|
9
|
+
import type { ShaderComponent } from '@pairdraw/scene-graph/shaders'
|
|
10
|
+
|
|
11
|
+
import { drawingImports, iconDrawing, iconImportDeclarations, iconNamer } from '../icons'
|
|
12
|
+
import type {
|
|
13
|
+
ComponentElement,
|
|
14
|
+
ComponentGenerator,
|
|
15
|
+
GeneratedComponent,
|
|
16
|
+
ComponentModel,
|
|
17
|
+
ComponentNode,
|
|
18
|
+
GeneratedKind
|
|
19
|
+
} from '../model'
|
|
20
|
+
import type { ComponentReference, IconReference } from '../references'
|
|
21
|
+
import {
|
|
22
|
+
drawnEffects,
|
|
23
|
+
SHADER_CANVAS,
|
|
24
|
+
shaderCSS,
|
|
25
|
+
shaderEffectAttributes,
|
|
26
|
+
shaderEffectName,
|
|
27
|
+
shaderImport,
|
|
28
|
+
type ShaderLayer
|
|
29
|
+
} from '../shaders'
|
|
30
|
+
import { componentClasses } from '../styling'
|
|
31
|
+
import {
|
|
32
|
+
INPUT_PROPS,
|
|
33
|
+
inputElement,
|
|
34
|
+
kindParameters,
|
|
35
|
+
NUMBER_STATE,
|
|
36
|
+
partAttributes,
|
|
37
|
+
prepend,
|
|
38
|
+
STEPPERS,
|
|
39
|
+
type Parameter
|
|
40
|
+
} from './fields'
|
|
41
|
+
import { classValue, drawing, elementLayers, moduleClass, numeric, type MarkupUses } from './shared'
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The Radix primitive a kind renders, from the unified `radix-ui` package, and the component
|
|
45
|
+
* of it each part uses. Its root takes the value two ways (`checked` and `onCheckedChange`,
|
|
46
|
+
* `pressed`, `open`) and `disabled` itself, so the generated props extend the root's.
|
|
47
|
+
*/
|
|
48
|
+
const RADIX: Record<GeneratedKind, { namespace: string; parts: Record<string, string> } | null> = {
|
|
49
|
+
button: null,
|
|
50
|
+
switch: { namespace: 'Switch', parts: { root: 'Root', thumb: 'Thumb' } },
|
|
51
|
+
checkbox: { namespace: 'Checkbox', parts: { root: 'Root', indicator: 'Indicator' } },
|
|
52
|
+
toggle: { namespace: 'Toggle', parts: { root: 'Root' } },
|
|
53
|
+
collapsible: {
|
|
54
|
+
namespace: 'Collapsible',
|
|
55
|
+
parts: { root: 'Root', trigger: 'Trigger', content: 'Content' }
|
|
56
|
+
},
|
|
57
|
+
tabs: {
|
|
58
|
+
namespace: 'Tabs',
|
|
59
|
+
parts: { root: 'Root', list: 'List', trigger: 'Trigger', content: 'Content' }
|
|
60
|
+
},
|
|
61
|
+
radioGroup: { namespace: 'RadioGroup', parts: { root: 'Root' } },
|
|
62
|
+
toggleGroup: { namespace: 'ToggleGroup', parts: { root: 'Root' } },
|
|
63
|
+
accordion: { namespace: 'Accordion', parts: { root: 'Root' } },
|
|
64
|
+
radioGroupItem: { namespace: 'RadioGroup', parts: { root: 'Item', indicator: 'Indicator' } },
|
|
65
|
+
toggleGroupItem: { namespace: 'ToggleGroup', parts: { root: 'Item' } },
|
|
66
|
+
accordionItem: {
|
|
67
|
+
namespace: 'Accordion',
|
|
68
|
+
parts: { root: 'Item', trigger: 'Trigger', content: 'Content' }
|
|
69
|
+
},
|
|
70
|
+
slider: {
|
|
71
|
+
namespace: 'Slider',
|
|
72
|
+
parts: { root: 'Root', track: 'Track', range: 'Range', thumb: 'Thumb' }
|
|
73
|
+
},
|
|
74
|
+
progress: { namespace: 'Progress', parts: { root: 'Root', indicator: 'Indicator' } },
|
|
75
|
+
// Radix has no number or text field; these are native inputs in the design's own elements.
|
|
76
|
+
numberField: null,
|
|
77
|
+
textField: null,
|
|
78
|
+
textarea: null,
|
|
79
|
+
// A component without a behaviour is the design's own elements.
|
|
80
|
+
plain: null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** What a group's root fixes: one item chosen at a time, and an accordion that can all close. */
|
|
84
|
+
const ROOT_ATTRIBUTES: Partial<Record<GeneratedKind, es.SyntaxNode[]>> = {
|
|
85
|
+
toggleGroup: [jsx.attribute('type', jsx.stringValue('single'))],
|
|
86
|
+
accordion: [jsx.attribute('type', jsx.stringValue('single')), jsx.attribute('collapsible', null)]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const PRIMITIVE = (namespace: string) => `${namespace}Primitive`
|
|
90
|
+
|
|
91
|
+
const HEADING_STYLE = jsx.container(
|
|
92
|
+
es.object(
|
|
93
|
+
Object.entries(HEADING_RESET).map(
|
|
94
|
+
([property, value]) => [camelCase(property), es.string(value)] as const
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
/** Several classes on one element, skipping a caller's that is absent. */
|
|
100
|
+
const CLASS_NAMES = es.parseExpression("$classes.filter(Boolean).join(' ')")
|
|
101
|
+
|
|
102
|
+
/** Whether a layer renders as a native button: a button's root, or a number field's stepper. */
|
|
103
|
+
const isNativeButton = (node: ComponentElement, kind: GeneratedKind) =>
|
|
104
|
+
(kind === 'button' && node.part === 'root') ||
|
|
105
|
+
(kind === 'numberField' && node.part !== null && Object.hasOwn(STEPPERS, node.part))
|
|
106
|
+
|
|
107
|
+
/** A Radix primitive's part, a native button, or the design's tag. */
|
|
108
|
+
function tagOf(node: ComponentElement, kind: GeneratedKind): string {
|
|
109
|
+
const radix = RADIX[kind]
|
|
110
|
+
const part = node.part ? radix?.parts[node.part] : undefined
|
|
111
|
+
if (radix && part) return `${PRIMITIVE(radix.namespace)}.${part}`
|
|
112
|
+
return isNativeButton(node, kind) ? 'button' : node.tag
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `disabled || undefined`, so a native root sets `data-disabled` only while disabled. */
|
|
116
|
+
const disabledFlag = (kind: GeneratedKind) =>
|
|
117
|
+
es.parseExpression(
|
|
118
|
+
kind === 'numberField' ? 'disabled || undefined' : 'props.disabled || undefined'
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
/** The Iconify component, named apart from any component the design calls `Icon`. */
|
|
122
|
+
const ICONIFY = 'IconifyIcon'
|
|
123
|
+
|
|
124
|
+
/** An icon: its package's own component where it has the icon, Iconify's otherwise. */
|
|
125
|
+
function iconElement(node: IconReference, uses: MarkupUses, depth: number): es.SyntaxNode {
|
|
126
|
+
const className = jsx.attribute('className', classValue(uses.classOf(node.className)))
|
|
127
|
+
const found = iconDrawing(node.icon, uses.iconPackage, 'react')
|
|
128
|
+
const drawn = found && uses.nameIcon(found)
|
|
129
|
+
if (!drawn) {
|
|
130
|
+
uses.icons = true
|
|
131
|
+
return jsx.element(
|
|
132
|
+
ICONIFY,
|
|
133
|
+
[jsx.attribute('icon', jsx.stringValue(node.icon)), className],
|
|
134
|
+
[],
|
|
135
|
+
depth
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
uses.iconImports.push(...drawingImports(drawn))
|
|
139
|
+
if (drawn.type === 'component') return jsx.element(drawn.import.local, [className], [], depth)
|
|
140
|
+
return jsx.element(
|
|
141
|
+
drawn.component.local,
|
|
142
|
+
[jsx.attribute('icon', jsx.container(es.identifier(drawn.import.local))), className],
|
|
143
|
+
[],
|
|
144
|
+
depth
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** `defaultChecked`: Radix's uncontrolled value, which a nested control starts from. */
|
|
149
|
+
const defaultOf = (model: string) => `default${upperFirst(model)}`
|
|
150
|
+
|
|
151
|
+
/** A reference's prop value: a bare name for true, `{false}`, or a string. */
|
|
152
|
+
function referenceValue(value: string | boolean): es.SyntaxNode | null {
|
|
153
|
+
if (value === true) return null
|
|
154
|
+
if (value === false) return jsx.container(jsx.literal(false))
|
|
155
|
+
return jsx.stringValue(value)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function reference(node: ComponentReference, uses: MarkupUses, depth: number): es.SyntaxNode {
|
|
159
|
+
uses.components.add(node.component)
|
|
160
|
+
return drawing(
|
|
161
|
+
jsx.element(
|
|
162
|
+
node.component,
|
|
163
|
+
[
|
|
164
|
+
jsx.attribute('className', classValue(uses.classOf(node.className))),
|
|
165
|
+
...node.props.map((prop) => jsx.attribute(prop.name, referenceValue(prop.value))),
|
|
166
|
+
...(node.model ? [jsx.attribute(defaultOf(node.model), null)] : [])
|
|
167
|
+
],
|
|
168
|
+
[],
|
|
169
|
+
depth
|
|
170
|
+
),
|
|
171
|
+
node.layerId,
|
|
172
|
+
uses
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function element(node: ComponentNode, uses: MarkupUses, depth: number): es.SyntaxNode {
|
|
177
|
+
if (node.type === 'text') return jsx.text(node.value)
|
|
178
|
+
if (node.type === 'textProp') return jsx.container(es.identifier(node.name))
|
|
179
|
+
if (node.type === 'reference') return reference(node, uses, depth)
|
|
180
|
+
if (node.type === 'input')
|
|
181
|
+
return drawing(inputElement(node.className, uses, depth), node.layerId, uses)
|
|
182
|
+
if (node.type === 'icon') return drawing(iconElement(node, uses, depth), node.layerId, uses)
|
|
183
|
+
return designElement(node, uses, depth)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The design's own classes, the state styles' module class, and on the root the caller's,
|
|
188
|
+
* which the spread would otherwise lose to the generated one.
|
|
189
|
+
*/
|
|
190
|
+
function classNameOf(node: ComponentElement, root: boolean, uses: MarkupUses): es.SyntaxNode {
|
|
191
|
+
const classes = [
|
|
192
|
+
...(node.attrs.class ? [es.string(node.attrs.class)] : []),
|
|
193
|
+
uses.classOf(node.className),
|
|
194
|
+
...(root ? [es.parseExpression('props.className')] : [])
|
|
195
|
+
]
|
|
196
|
+
const only = classes.length === 1 ? classes.at(0) : undefined
|
|
197
|
+
return only ?? es.fill(CLASS_NAMES, { $classes: es.array(classes) })
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* What a root fixes before the caller's props: a group's single choice and its start, and a
|
|
202
|
+
* slider's or progress bar's range and the value it starts at.
|
|
203
|
+
*/
|
|
204
|
+
function rootAttributes(uses: MarkupUses): es.SyntaxNode[] {
|
|
205
|
+
const { component } = uses
|
|
206
|
+
const start = uses.choice?.default
|
|
207
|
+
const { range } = component
|
|
208
|
+
return [
|
|
209
|
+
...(ROOT_ATTRIBUTES[uses.kind] ?? []),
|
|
210
|
+
// A choice starts on the design's option, which the caller's props can change.
|
|
211
|
+
...(start ? [jsx.attribute('defaultValue', jsx.stringValue(start))] : []),
|
|
212
|
+
...(range && uses.kind === 'slider'
|
|
213
|
+
? [
|
|
214
|
+
jsx.attribute('defaultValue', jsx.container(es.array([es.number(range.default)]))),
|
|
215
|
+
numeric('min', range.min),
|
|
216
|
+
numeric('max', range.max),
|
|
217
|
+
numeric('step', range.step)
|
|
218
|
+
]
|
|
219
|
+
: []),
|
|
220
|
+
...(range && uses.kind === 'progress'
|
|
221
|
+
? [
|
|
222
|
+
// Radix takes a value within its range, as the indicator draws one.
|
|
223
|
+
jsx.attribute(
|
|
224
|
+
'value',
|
|
225
|
+
jsx.container(
|
|
226
|
+
es.fill(es.parseExpression('Math.min($max, Math.max($min, value))'), {
|
|
227
|
+
$min: es.number(range.min),
|
|
228
|
+
$max: es.number(range.max)
|
|
229
|
+
})
|
|
230
|
+
)
|
|
231
|
+
),
|
|
232
|
+
numeric('max', range.max)
|
|
233
|
+
]
|
|
234
|
+
: [])
|
|
235
|
+
]
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function bindingAttributes(
|
|
239
|
+
node: ComponentElement,
|
|
240
|
+
kind: GeneratedKind,
|
|
241
|
+
native: boolean
|
|
242
|
+
): es.SyntaxNode[] {
|
|
243
|
+
return node.bindings.flatMap((binding) => {
|
|
244
|
+
if (binding.type === 'prop')
|
|
245
|
+
return [jsx.attribute(binding.attribute, jsx.container(es.identifier(binding.prop.name)))]
|
|
246
|
+
// Radix sets `data-disabled` on its own roots; a native root needs it for the styles.
|
|
247
|
+
if (binding.type === 'disabled' && native)
|
|
248
|
+
return [jsx.attribute('data-disabled', jsx.container(disabledFlag(kind)))]
|
|
249
|
+
return []
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A shader playing behind its layer's content, with telemetry off, as every export has it. */
|
|
254
|
+
function shaderCanvas(shader: ShaderLayer, uses: MarkupUses, depth: number): es.SyntaxNode {
|
|
255
|
+
const effect =
|
|
256
|
+
(at: number) =>
|
|
257
|
+
(component: ShaderComponent): es.SyntaxNode => {
|
|
258
|
+
uses.shaders.add(component.type)
|
|
259
|
+
const children = drawnEffects(component.children).map(effect(at + 1))
|
|
260
|
+
return jsx.element(
|
|
261
|
+
shaderEffectName(component.type),
|
|
262
|
+
shaderEffectAttributes(component),
|
|
263
|
+
children,
|
|
264
|
+
at
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
return jsx.element(
|
|
268
|
+
SHADER_CANVAS,
|
|
269
|
+
[
|
|
270
|
+
jsx.attribute('className', classValue(uses.classOf(shader.className))),
|
|
271
|
+
jsx.attribute('disableTelemetry', null)
|
|
272
|
+
],
|
|
273
|
+
drawnEffects(shader.preset.components).map(effect(depth + 1)),
|
|
274
|
+
depth
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Words alone stay on the element's line, where a line break would add a space. */
|
|
279
|
+
function wordsOnly(node: ComponentElement): boolean {
|
|
280
|
+
const only = node.children.length === 1 ? node.children[0]?.type : undefined
|
|
281
|
+
return !node.slot && !node.shader && (only === 'text' || only === 'textProp')
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function designElement(node: ComponentElement, uses: MarkupUses, depth: number): es.SyntaxNode {
|
|
285
|
+
const kind = uses.kind
|
|
286
|
+
const root = node.part === 'root'
|
|
287
|
+
const native = root && !(node.part && RADIX[kind]?.parts[node.part])
|
|
288
|
+
const attributes = [
|
|
289
|
+
...(isNativeButton(node, kind) ? [jsx.attribute('type', jsx.stringValue('button'))] : []),
|
|
290
|
+
...(root ? rootAttributes(uses) : []),
|
|
291
|
+
// The root passes the caller's props on, such as `checked` or `onClick`, unless its input
|
|
292
|
+
// takes them.
|
|
293
|
+
...(root && !INPUT_PROPS[kind] ? [jsx.spread(es.identifier('props'))] : []),
|
|
294
|
+
...(node.value === undefined ? [] : [jsx.attribute('value', jsx.stringValue(node.value))]),
|
|
295
|
+
...Object.entries(omit(node.attrs, ['class'])).map(([name, value]) =>
|
|
296
|
+
jsx.attribute(name, jsx.stringValue(value))
|
|
297
|
+
),
|
|
298
|
+
...partAttributes(node, uses),
|
|
299
|
+
jsx.attribute('className', classValue(classNameOf(node, root, uses))),
|
|
300
|
+
...bindingAttributes(node, kind, native)
|
|
301
|
+
]
|
|
302
|
+
const inline = wordsOnly(node)
|
|
303
|
+
// Radix puts an accordion item's trigger in a header, which carries the heading level.
|
|
304
|
+
const header = kind === 'accordionItem' && node.part === 'trigger'
|
|
305
|
+
const childDepth = depth + (header ? 2 : 1)
|
|
306
|
+
const content = node.children.map((child) => element(child, uses, childDepth))
|
|
307
|
+
const shader = node.shader ? [shaderCanvas(node.shader, uses, childDepth)] : []
|
|
308
|
+
const drawn = drawing(
|
|
309
|
+
jsx.element(
|
|
310
|
+
tagOf(node, kind),
|
|
311
|
+
attributes,
|
|
312
|
+
[...shader, ...(node.slot ? [slotContent(node.slot, content, childDepth)] : content)],
|
|
313
|
+
depth + (header ? 1 : 0),
|
|
314
|
+
inline
|
|
315
|
+
),
|
|
316
|
+
node.layerId,
|
|
317
|
+
uses
|
|
318
|
+
)
|
|
319
|
+
const shown = header
|
|
320
|
+
? jsx.element(
|
|
321
|
+
`${PRIMITIVE('Accordion')}.Header`,
|
|
322
|
+
[jsx.attribute('style', HEADING_STYLE)],
|
|
323
|
+
[drawn],
|
|
324
|
+
depth
|
|
325
|
+
)
|
|
326
|
+
: drawn
|
|
327
|
+
return node.shownBy ? whenShown(node.shownBy, shown) : shown
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const LOGICAL = (operator: '&&' | '??', left: es.SyntaxNode, right: es.SyntaxNode) => ({
|
|
331
|
+
type: 'LogicalExpression',
|
|
332
|
+
operator,
|
|
333
|
+
left,
|
|
334
|
+
right
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
/** `{content ?? <>…</>}`: what the caller passes for a slot, or the design's content. */
|
|
338
|
+
function slotContent(slot: string, children: es.SyntaxNode[], depth: number): es.SyntaxNode {
|
|
339
|
+
return jsx.container(LOGICAL('??', es.identifier(slot), jsx.fragment(children, depth)))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** `{showIcon && <…/>}`: a layer a boolean prop shows. */
|
|
343
|
+
function whenShown(prop: string, drawn: es.SyntaxNode): es.SyntaxNode {
|
|
344
|
+
return jsx.container(LOGICAL('&&', es.identifier(prop), drawn))
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const MODULE = es.parseModule(dedent`
|
|
348
|
+
import type { ComponentProps } from 'react'
|
|
349
|
+
|
|
350
|
+
import styles from '$styles'
|
|
351
|
+
|
|
352
|
+
export type $Props = $Type
|
|
353
|
+
|
|
354
|
+
export function $Name($params: $Props) {
|
|
355
|
+
return $body
|
|
356
|
+
}
|
|
357
|
+
`)
|
|
358
|
+
|
|
359
|
+
const RADIX_IMPORT = es.parseModule(`import { $Namespace as $Primitive } from 'radix-ui'`)
|
|
360
|
+
const ICONIFY_IMPORT = es.parseModule(`import { Icon as ${ICONIFY} } from '@iconify/react'`).body
|
|
361
|
+
const IMPORT_COMPONENT = es.parseModule(`import { $Component } from '$path'`)
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* The props of a group that fixes one choice at a time, without the `type` it fixes. The
|
|
365
|
+
* root's own props join the single and multiple choice, which a single value can't satisfy.
|
|
366
|
+
*/
|
|
367
|
+
const SINGLE_PROPS: Partial<Record<GeneratedKind, string>> = {
|
|
368
|
+
toggleGroup: 'ToggleGroupSingleProps',
|
|
369
|
+
accordion: 'AccordionSingleProps'
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const qualified = (namespace: string, name: string) => ({
|
|
373
|
+
type: 'TSQualifiedName',
|
|
374
|
+
left: es.identifier(PRIMITIVE(namespace)),
|
|
375
|
+
right: es.identifier(name)
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
/** Props a kind declares itself, where no Radix root has them. */
|
|
379
|
+
const OWN_PROPS: Partial<Record<GeneratedKind, string>> = {
|
|
380
|
+
progress: "Omit<ComponentProps<typeof ProgressPrimitive.Root>, 'value'> & { value?: number }",
|
|
381
|
+
numberField:
|
|
382
|
+
"Omit<ComponentProps<'div'>, 'defaultValue' | 'onChange'> & { defaultValue?: number; onValueChange?: (value: number) => void; disabled?: boolean }",
|
|
383
|
+
textField: "ComponentProps<'input'>",
|
|
384
|
+
textarea: "ComponentProps<'textarea'>"
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The root's own props, or a single choice group's. */
|
|
388
|
+
function rootPropsType(kind: GeneratedKind, tag: string): es.SyntaxNode {
|
|
389
|
+
const own = OWN_PROPS[kind]
|
|
390
|
+
if (own) return es.parseType(own)
|
|
391
|
+
// A component without a behaviour takes its root element's props.
|
|
392
|
+
if (kind === 'plain') return es.parseType(`ComponentProps<'${tag}'>`)
|
|
393
|
+
const radix = RADIX[kind]
|
|
394
|
+
if (!radix) return es.parseType("ComponentProps<'button'>")
|
|
395
|
+
const single = SINGLE_PROPS[kind]
|
|
396
|
+
if (single)
|
|
397
|
+
return es.fill(es.parseType("Omit<$Props, 'type'>"), {
|
|
398
|
+
$Props: qualified(radix.namespace, single)
|
|
399
|
+
})
|
|
400
|
+
return es.fill(es.parseType('ComponentProps<typeof $Root>'), {
|
|
401
|
+
$Root: qualified(radix.namespace, radix.parts.root)
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** The root's own props, extended with the component's variant props when it has any. */
|
|
406
|
+
function propsType(component: ComponentModel): es.SyntaxNode {
|
|
407
|
+
const base = rootPropsType(component.kind, component.tree.tag)
|
|
408
|
+
const own = [component.props, component.texts, component.booleans, component.slots]
|
|
409
|
+
if (own.every((list) => list.length === 0)) return base
|
|
410
|
+
return {
|
|
411
|
+
type: 'TSIntersectionType',
|
|
412
|
+
types: [
|
|
413
|
+
base,
|
|
414
|
+
es.objectType([
|
|
415
|
+
...component.props.map((prop): [string, es.SyntaxNode, boolean] => [
|
|
416
|
+
prop.name,
|
|
417
|
+
es.stringUnionType(prop.options),
|
|
418
|
+
true
|
|
419
|
+
]),
|
|
420
|
+
...component.texts.map((text): [string, es.SyntaxNode, boolean] => [
|
|
421
|
+
text.name,
|
|
422
|
+
es.parseType('string'),
|
|
423
|
+
true
|
|
424
|
+
]),
|
|
425
|
+
...component.booleans.map((prop): [string, es.SyntaxNode, boolean] => [
|
|
426
|
+
prop.name,
|
|
427
|
+
es.parseType('boolean'),
|
|
428
|
+
true
|
|
429
|
+
]),
|
|
430
|
+
...component.slots.map((slot): [string, es.SyntaxNode, boolean] => [
|
|
431
|
+
slot.name,
|
|
432
|
+
es.parseType('ReactNode'),
|
|
433
|
+
true
|
|
434
|
+
])
|
|
435
|
+
])
|
|
436
|
+
]
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* `{ size = 'Small', label = 'Save', ...props }`: variant and text props with their defaults,
|
|
442
|
+
* kept off the root, and the rest passed on.
|
|
443
|
+
*/
|
|
444
|
+
function parameters(component: ComponentModel): es.SyntaxNode {
|
|
445
|
+
const own: Parameter[] = [
|
|
446
|
+
...kindParameters(component),
|
|
447
|
+
...[...component.props, ...component.texts].map((prop) => ({
|
|
448
|
+
name: prop.name,
|
|
449
|
+
default: es.string(prop.default)
|
|
450
|
+
})),
|
|
451
|
+
...component.booleans.map((prop) => ({
|
|
452
|
+
name: prop.name,
|
|
453
|
+
default: es.parseExpression(String(prop.default))
|
|
454
|
+
})),
|
|
455
|
+
// Slots are kept off the root too, which would otherwise get them as attributes.
|
|
456
|
+
...component.slots.map((slot) => ({ name: slot.name }))
|
|
457
|
+
]
|
|
458
|
+
if (own.length === 0) return es.identifier('props')
|
|
459
|
+
return {
|
|
460
|
+
type: 'ObjectPattern',
|
|
461
|
+
properties: [
|
|
462
|
+
...own.map((prop) => ({
|
|
463
|
+
type: 'Property',
|
|
464
|
+
kind: 'init',
|
|
465
|
+
key: es.identifier(prop.name),
|
|
466
|
+
value: prop.default
|
|
467
|
+
? { type: 'AssignmentPattern', left: es.identifier(prop.name), right: prop.default }
|
|
468
|
+
: es.identifier(prop.name),
|
|
469
|
+
computed: false,
|
|
470
|
+
method: false,
|
|
471
|
+
shorthand: true
|
|
472
|
+
})),
|
|
473
|
+
{ type: 'RestElement', argument: es.identifier('props') }
|
|
474
|
+
]
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* `import { useState, type ComponentProps, type ReactNode } from 'react'` with only what the
|
|
480
|
+
* component uses: a number field's state, its root's props, and its slots' content.
|
|
481
|
+
*/
|
|
482
|
+
function reactImport(uses: { state: boolean; props: boolean; slots: boolean }): es.SyntaxNode[] {
|
|
483
|
+
const types = [...(uses.props ? ['ComponentProps'] : []), ...(uses.slots ? ['ReactNode'] : [])]
|
|
484
|
+
if (!uses.state && types.length === 0) return []
|
|
485
|
+
const specifiers = uses.state
|
|
486
|
+
? ['useState', ...types.map((name) => `type ${name}`)].join(', ')
|
|
487
|
+
: types.join(', ')
|
|
488
|
+
return es.parseModule(`import ${uses.state ? '' : 'type '}{ ${specifiers} } from 'react'`).body
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* The prop a story sets the value with: Radix's uncontrolled value, so a story's control and
|
|
493
|
+
* its play function can both change it, which a slider takes as a list; a progress bar's own
|
|
494
|
+
* value, which nothing else changes.
|
|
495
|
+
*/
|
|
496
|
+
function valueArg(
|
|
497
|
+
kind: GeneratedKind,
|
|
498
|
+
model: string | null
|
|
499
|
+
): Pick<GeneratedComponent, 'valueArg' | 'valueList'> {
|
|
500
|
+
if (!model) return { valueArg: null }
|
|
501
|
+
if (kind === 'progress') return { valueArg: 'value' }
|
|
502
|
+
return { valueArg: defaultOf(model), ...(kind === 'slider' ? { valueList: true } : {}) }
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* A component as a React component on Radix UI: its parts as Radix primitives from the
|
|
507
|
+
* `radix-ui` package, its variants' state styles in a CSS module, and props that extend the
|
|
508
|
+
* Radix root's with its other variant properties.
|
|
509
|
+
*/
|
|
510
|
+
export const reactComponent: ComponentGenerator = async (component, options = {}) => {
|
|
511
|
+
const styling = options.styling ?? 'css'
|
|
512
|
+
const radix = RADIX[component.kind]
|
|
513
|
+
const stylesPath = `${component.name}.module.css`
|
|
514
|
+
const classOf = componentClasses(component, styling)
|
|
515
|
+
const uses: MarkupUses = {
|
|
516
|
+
component,
|
|
517
|
+
// Styled with Tailwind, a layer's utilities are written out; otherwise its module class.
|
|
518
|
+
classOf: (className) =>
|
|
519
|
+
styling === 'css' ? moduleClass(className) : es.string(classOf(className)),
|
|
520
|
+
kind: component.kind,
|
|
521
|
+
choice: component.choice,
|
|
522
|
+
components: new Set(),
|
|
523
|
+
layers: new WeakMap(),
|
|
524
|
+
icons: false,
|
|
525
|
+
iconPackage: options.icons ?? 'iconify',
|
|
526
|
+
iconImports: [],
|
|
527
|
+
nameIcon: iconNamer(component, [
|
|
528
|
+
ICONIFY,
|
|
529
|
+
'styles',
|
|
530
|
+
`${component.name}Props`,
|
|
531
|
+
...(radix ? [radix.namespace, PRIMITIVE(radix.namespace)] : [])
|
|
532
|
+
]),
|
|
533
|
+
shaders: new Set()
|
|
534
|
+
}
|
|
535
|
+
const body = element(component.tree, uses, 1)
|
|
536
|
+
const imports = [
|
|
537
|
+
...(uses.icons ? ICONIFY_IMPORT : []),
|
|
538
|
+
...iconImportDeclarations(uses.iconImports),
|
|
539
|
+
...(uses.shaders.size > 0 ? [shaderImport('shaders/react', uses.shaders)] : []),
|
|
540
|
+
...(radix
|
|
541
|
+
? es.fill(RADIX_IMPORT, {
|
|
542
|
+
$Namespace: es.identifier(radix.namespace),
|
|
543
|
+
$Primitive: es.identifier(PRIMITIVE(radix.namespace))
|
|
544
|
+
}).body
|
|
545
|
+
: []),
|
|
546
|
+
...[...uses.components].sort().flatMap(
|
|
547
|
+
(name) =>
|
|
548
|
+
es.fill(IMPORT_COMPONENT, {
|
|
549
|
+
$Component: es.identifier(name),
|
|
550
|
+
$path: es.string(`./${name}`)
|
|
551
|
+
}).body
|
|
552
|
+
)
|
|
553
|
+
]
|
|
554
|
+
const [, stylesImport, ...rest] = es.fill(MODULE, {
|
|
555
|
+
$styles: es.string(`./${stylesPath}`),
|
|
556
|
+
$Props: es.identifier(`${component.name}Props`),
|
|
557
|
+
$Type: propsType(component),
|
|
558
|
+
$Name: es.identifier(component.name),
|
|
559
|
+
$params: parameters(component),
|
|
560
|
+
$body: body
|
|
561
|
+
}).body
|
|
562
|
+
const state =
|
|
563
|
+
component.kind === 'numberField' && component.range ? NUMBER_STATE(component.range) : []
|
|
564
|
+
prepend(rest.at(-1), state)
|
|
565
|
+
const react = reactImport({
|
|
566
|
+
state: state.length > 0,
|
|
567
|
+
// A single choice group's props come from Radix's own type, not `ComponentProps`.
|
|
568
|
+
props: !SINGLE_PROPS[component.kind],
|
|
569
|
+
slots: component.slots.length > 0
|
|
570
|
+
})
|
|
571
|
+
const program = {
|
|
572
|
+
type: 'Program',
|
|
573
|
+
sourceType: 'module',
|
|
574
|
+
body: [...react, ...imports, ...(styling === 'css' ? [stylesImport] : []), ...rest]
|
|
575
|
+
}
|
|
576
|
+
// Tailwind styles the markup itself; a CSS module is written beside it otherwise.
|
|
577
|
+
const stylesheet =
|
|
578
|
+
styling === 'css'
|
|
579
|
+
? [
|
|
580
|
+
{
|
|
581
|
+
path: stylesPath,
|
|
582
|
+
content: compact([
|
|
583
|
+
(await stateStylesToCSS(component.styles)).css,
|
|
584
|
+
shaderCSS(component.tree)
|
|
585
|
+
]).join('\n')
|
|
586
|
+
}
|
|
587
|
+
]
|
|
588
|
+
: []
|
|
589
|
+
const model = component.model
|
|
590
|
+
// A group's items are their own component, which the group's markup uses.
|
|
591
|
+
const item = component.item ? await reactComponent(component.item, options) : null
|
|
592
|
+
return {
|
|
593
|
+
files: [
|
|
594
|
+
...(item?.files ?? []),
|
|
595
|
+
{
|
|
596
|
+
path: `${component.name}.tsx`,
|
|
597
|
+
content: `${jsx.printModule(program)}\n`,
|
|
598
|
+
// The template is filled with copies, so the body, which holds all the JSX, is read.
|
|
599
|
+
layerIds: elementLayers(body, uses.layers)
|
|
600
|
+
},
|
|
601
|
+
...stylesheet
|
|
602
|
+
],
|
|
603
|
+
entry: { path: `./${component.name}`, named: true },
|
|
604
|
+
...valueArg(component.kind, model)
|
|
605
|
+
}
|
|
606
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { es, jsx } from '@pairdraw/emit'
|
|
2
|
+
|
|
3
|
+
import type { IconDrawing, IconImport, IconPackage } from '../icons'
|
|
4
|
+
import type { ComponentModel, GeneratedKind } from '../model'
|
|
5
|
+
|
|
6
|
+
/** `styles.thumb`, or `styles['switch-thumb']` for a class that isn't an identifier. */
|
|
7
|
+
export function moduleClass(className: string): es.SyntaxNode {
|
|
8
|
+
return {
|
|
9
|
+
type: 'MemberExpression',
|
|
10
|
+
object: es.identifier('styles'),
|
|
11
|
+
property: es.isIdentifierName(className) ? es.identifier(className) : es.string(className),
|
|
12
|
+
computed: !es.isIdentifierName(className),
|
|
13
|
+
optional: false
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** What a component's markup uses, which its module imports. */
|
|
18
|
+
export interface MarkupUses {
|
|
19
|
+
component: ComponentModel
|
|
20
|
+
kind: GeneratedKind
|
|
21
|
+
choice: ComponentModel['choice']
|
|
22
|
+
/** Other generated components. */
|
|
23
|
+
components: Set<string>
|
|
24
|
+
/** Whether an icon is drawn by Iconify. */
|
|
25
|
+
icons: boolean
|
|
26
|
+
/** The package icons come from, and what the icons drawn from it import. */
|
|
27
|
+
iconPackage: IconPackage
|
|
28
|
+
iconImports: IconImport[]
|
|
29
|
+
/** Names an icon's imports apart from the component's other names. */
|
|
30
|
+
nameIcon: (drawing: IconDrawing) => IconDrawing
|
|
31
|
+
/** A layer's class as the component is styled: its CSS module class, or its utilities. */
|
|
32
|
+
classOf: (className: string) => es.SyntaxNode
|
|
33
|
+
/** The layer each JSX element draws, which code links back to. */
|
|
34
|
+
layers: WeakMap<es.SyntaxNode, string>
|
|
35
|
+
/** Effects of the `shaders` library its layers fill with. */
|
|
36
|
+
shaders: Set<string>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const numeric = (name: string, value: number) =>
|
|
40
|
+
jsx.attribute(name, jsx.container(es.number(value)))
|
|
41
|
+
|
|
42
|
+
/** A `className` value: a plain string as an attribute string, anything else in braces. */
|
|
43
|
+
export function classValue(value: es.SyntaxNode): es.SyntaxNode {
|
|
44
|
+
return value.type === 'Literal' && typeof value.value === 'string'
|
|
45
|
+
? jsx.stringValue(value.value)
|
|
46
|
+
: jsx.container(value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `element`, recorded as drawing `layerId`, so code can link back to that layer. */
|
|
50
|
+
export function drawing(
|
|
51
|
+
element: es.SyntaxNode,
|
|
52
|
+
layerId: string | undefined,
|
|
53
|
+
uses: MarkupUses
|
|
54
|
+
): es.SyntaxNode {
|
|
55
|
+
if (layerId) uses.layers.set(element, layerId)
|
|
56
|
+
return element
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The layer of each JSX element in `program`, in the order the elements open. */
|
|
60
|
+
export function elementLayers(
|
|
61
|
+
program: es.SyntaxNode,
|
|
62
|
+
layers: WeakMap<es.SyntaxNode, string>
|
|
63
|
+
): (string | null)[] {
|
|
64
|
+
const found: (string | null)[] = []
|
|
65
|
+
const visit = (value: unknown) => {
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
for (const item of value) visit(item)
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
if (!es.isNode(value)) return
|
|
71
|
+
if (value.type === 'JSXElement') found.push(layers.get(value) ?? null)
|
|
72
|
+
for (const item of Object.values(value)) visit(item)
|
|
73
|
+
}
|
|
74
|
+
visit(program)
|
|
75
|
+
return found
|
|
76
|
+
}
|