@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,517 @@
|
|
|
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, kebabCase, upperFirst } from 'es-toolkit/string'
|
|
7
|
+
|
|
8
|
+
import { es, vue } from '@pairdraw/emit'
|
|
9
|
+
import type { ShaderComponent } from '@pairdraw/scene-graph/shaders'
|
|
10
|
+
|
|
11
|
+
import { claimName } from '../storybook/names'
|
|
12
|
+
import { progressWidth } from './fields'
|
|
13
|
+
import {
|
|
14
|
+
drawingImports,
|
|
15
|
+
iconDrawing,
|
|
16
|
+
iconImportDeclarations,
|
|
17
|
+
iconNamer,
|
|
18
|
+
type IconDrawing,
|
|
19
|
+
type IconImport,
|
|
20
|
+
type IconPackage
|
|
21
|
+
} from './icons'
|
|
22
|
+
import type {
|
|
23
|
+
ComponentBinding,
|
|
24
|
+
ComponentElement,
|
|
25
|
+
ComponentGenerator,
|
|
26
|
+
ComponentModel,
|
|
27
|
+
ComponentNode,
|
|
28
|
+
GeneratedKind
|
|
29
|
+
} from './model'
|
|
30
|
+
import type { ComponentReference, IconReference } from './references'
|
|
31
|
+
import {
|
|
32
|
+
drawnEffects,
|
|
33
|
+
SHADER_CANVAS,
|
|
34
|
+
shaderCSS,
|
|
35
|
+
shaderEffectName,
|
|
36
|
+
shaderImport,
|
|
37
|
+
type ShaderLayer
|
|
38
|
+
} from './shaders'
|
|
39
|
+
import { componentClasses } from './styling'
|
|
40
|
+
|
|
41
|
+
/** The Reka UI components a kind renders, by part, and the prop its model binds. */
|
|
42
|
+
const REKA: Record<
|
|
43
|
+
GeneratedKind,
|
|
44
|
+
{ parts: Record<string, string>; model?: 'modelValue' | 'open' }
|
|
45
|
+
> = {
|
|
46
|
+
button: { parts: {} },
|
|
47
|
+
switch: { parts: { root: 'SwitchRoot', thumb: 'SwitchThumb' }, model: 'modelValue' },
|
|
48
|
+
checkbox: {
|
|
49
|
+
parts: { root: 'CheckboxRoot', indicator: 'CheckboxIndicator' },
|
|
50
|
+
model: 'modelValue'
|
|
51
|
+
},
|
|
52
|
+
toggle: { parts: { root: 'Toggle' }, model: 'modelValue' },
|
|
53
|
+
collapsible: {
|
|
54
|
+
parts: {
|
|
55
|
+
root: 'CollapsibleRoot',
|
|
56
|
+
trigger: 'CollapsibleTrigger',
|
|
57
|
+
content: 'CollapsibleContent'
|
|
58
|
+
},
|
|
59
|
+
model: 'open'
|
|
60
|
+
},
|
|
61
|
+
tabs: {
|
|
62
|
+
parts: { root: 'TabsRoot', list: 'TabsList', trigger: 'TabsTrigger', content: 'TabsContent' },
|
|
63
|
+
model: 'modelValue'
|
|
64
|
+
},
|
|
65
|
+
radioGroup: { parts: { root: 'RadioGroupRoot' }, model: 'modelValue' },
|
|
66
|
+
toggleGroup: { parts: { root: 'ToggleGroupRoot' }, model: 'modelValue' },
|
|
67
|
+
accordion: { parts: { root: 'AccordionRoot' }, model: 'modelValue' },
|
|
68
|
+
radioGroupItem: { parts: { root: 'RadioGroupItem', indicator: 'RadioGroupIndicator' } },
|
|
69
|
+
toggleGroupItem: { parts: { root: 'ToggleGroupItem' } },
|
|
70
|
+
accordionItem: {
|
|
71
|
+
parts: { root: 'AccordionItem', trigger: 'AccordionTrigger', content: 'AccordionContent' }
|
|
72
|
+
},
|
|
73
|
+
slider: {
|
|
74
|
+
parts: { root: 'SliderRoot', track: 'SliderTrack', range: 'SliderRange', thumb: 'SliderThumb' },
|
|
75
|
+
model: 'modelValue'
|
|
76
|
+
},
|
|
77
|
+
progress: {
|
|
78
|
+
parts: { root: 'ProgressRoot', indicator: 'ProgressIndicator' },
|
|
79
|
+
model: 'modelValue'
|
|
80
|
+
},
|
|
81
|
+
numberField: {
|
|
82
|
+
parts: {
|
|
83
|
+
root: 'NumberFieldRoot',
|
|
84
|
+
increment: 'NumberFieldIncrement',
|
|
85
|
+
decrement: 'NumberFieldDecrement'
|
|
86
|
+
},
|
|
87
|
+
model: 'modelValue'
|
|
88
|
+
},
|
|
89
|
+
// Reka has no text field; the root is the design's own element around a native input.
|
|
90
|
+
textField: { parts: {} },
|
|
91
|
+
textarea: { parts: {} },
|
|
92
|
+
// A component without a behaviour is the design's own elements.
|
|
93
|
+
plain: { parts: {} }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Kinds whose two-way value is their input's, which binds it rather than the root. */
|
|
97
|
+
const INPUT_MODEL: ReadonlySet<GeneratedKind> = new Set(['textField', 'textarea'])
|
|
98
|
+
|
|
99
|
+
/** What a group's root fixes: one item chosen at a time, and an accordion that can all close. */
|
|
100
|
+
const ROOT_ATTRIBUTES: Partial<Record<GeneratedKind, vue.VueAttribute[]>> = {
|
|
101
|
+
toggleGroup: [vue.attribute('type', 'single')],
|
|
102
|
+
accordion: [vue.attribute('type', 'single'), vue.bound('collapsible', es.parseExpression('true'))]
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const identifier = (name: string) => es.identifier(name)
|
|
106
|
+
|
|
107
|
+
const HEADING_STYLE = Object.entries(HEADING_RESET)
|
|
108
|
+
.map(([property, value]) => `${property}: ${value}`)
|
|
109
|
+
.join('; ')
|
|
110
|
+
|
|
111
|
+
/** `disabled || undefined`, so a native button sets `data-disabled` only while disabled. */
|
|
112
|
+
const DISABLED_FLAG = es.parseExpression('disabled || undefined')
|
|
113
|
+
|
|
114
|
+
function bindingAttributes(
|
|
115
|
+
binding: ComponentBinding,
|
|
116
|
+
uses: TemplateUses,
|
|
117
|
+
native: boolean
|
|
118
|
+
): vue.VueAttribute[] {
|
|
119
|
+
const { kind } = uses
|
|
120
|
+
if (binding.type === 'model') {
|
|
121
|
+
if (INPUT_MODEL.has(kind)) return []
|
|
122
|
+
// Reka's slider takes a list of values, which the model's single one stands in.
|
|
123
|
+
const bound = uses.sliderValues ?? binding.name
|
|
124
|
+
return [vue.model(identifier(bound), REKA[kind].model === 'open' ? 'open' : undefined)]
|
|
125
|
+
}
|
|
126
|
+
if (binding.type === 'prop') return [vue.bound(binding.attribute, identifier(binding.prop.name))]
|
|
127
|
+
if (binding.type === 'value') return [vue.bound('value', identifier('value'))]
|
|
128
|
+
// Reka sets `data-disabled` on its own roots; a native root needs it for the state styles.
|
|
129
|
+
return [
|
|
130
|
+
...(native && kind !== 'button' ? [] : [vue.bound('disabled', identifier('disabled'))]),
|
|
131
|
+
...(native ? [vue.bound('data-disabled', DISABLED_FLAG)] : [])
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The number range a slider, progress bar, or number field's root is given. */
|
|
136
|
+
function rangeAttributes(component: ComponentModel): vue.VueAttribute[] {
|
|
137
|
+
const { range } = component
|
|
138
|
+
if (!range) return []
|
|
139
|
+
const bound = (name: 'min' | 'max' | 'step') => vue.bound(name, es.number(range[name]))
|
|
140
|
+
// A progress bar only takes its maximum; its minimum is where the indicator is empty.
|
|
141
|
+
return component.kind === 'progress'
|
|
142
|
+
? [bound('max')]
|
|
143
|
+
: [bound('min'), bound('max'), bound('step')]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function progressStyle(component: ComponentModel): vue.VueAttribute[] {
|
|
147
|
+
const { range, model } = component
|
|
148
|
+
const style = range && model ? progressWidth(range, identifier(model)) : null
|
|
149
|
+
return style ? [vue.bound('style', style)] : []
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** `element`, recorded as drawing `layerId`, so code can link back to that layer. */
|
|
153
|
+
function drawing(
|
|
154
|
+
element: vue.VueElement,
|
|
155
|
+
layerId: string | undefined,
|
|
156
|
+
uses: TemplateUses
|
|
157
|
+
): vue.VueElement {
|
|
158
|
+
if (layerId) uses.layers.set(element, layerId)
|
|
159
|
+
return element
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A field's input: Reka's for a number field, else a native input or textarea. */
|
|
163
|
+
function inputNode(className: string, uses: TemplateUses): vue.VueElement {
|
|
164
|
+
const { component } = uses
|
|
165
|
+
if (component.kind === 'numberField') {
|
|
166
|
+
uses.reka.add('NumberFieldInput')
|
|
167
|
+
return vue.element('NumberFieldInput', [vue.attribute('class', uses.classOf(className))])
|
|
168
|
+
}
|
|
169
|
+
const placeholder = component.text?.placeholder
|
|
170
|
+
return vue.element(component.kind === 'textarea' ? 'textarea' : 'input', [
|
|
171
|
+
vue.attribute('class', uses.classOf(className)),
|
|
172
|
+
...(component.model ? [vue.model(identifier(component.model))] : []),
|
|
173
|
+
...(placeholder ? [vue.attribute('placeholder', placeholder)] : []),
|
|
174
|
+
...(component.disabled ? [vue.bound('disabled', identifier('disabled'))] : [])
|
|
175
|
+
])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** What a template uses, which its script imports and declares. */
|
|
179
|
+
interface TemplateUses {
|
|
180
|
+
component: ComponentModel
|
|
181
|
+
kind: GeneratedKind
|
|
182
|
+
/** The list a slider's root binds, standing in for its single value. */
|
|
183
|
+
sliderValues: string | null
|
|
184
|
+
/** Reka components. */
|
|
185
|
+
reka: Set<string>
|
|
186
|
+
/** Other generated components. */
|
|
187
|
+
components: Set<string>
|
|
188
|
+
/** Whether an icon is drawn by Iconify. */
|
|
189
|
+
icons: boolean
|
|
190
|
+
/** The package icons come from, and what the icons drawn from it import. */
|
|
191
|
+
iconPackage: IconPackage
|
|
192
|
+
iconImports: IconImport[]
|
|
193
|
+
/** Names an icon's imports apart from the component's other names. */
|
|
194
|
+
nameIcon: (drawing: IconDrawing) => IconDrawing
|
|
195
|
+
/** The class a layer's readable class name stands for, as the component is styled. */
|
|
196
|
+
classOf: (className: string) => string
|
|
197
|
+
/** The layer each element draws, which code links back to. */
|
|
198
|
+
layers: WeakMap<vue.VueElement, string>
|
|
199
|
+
/** Effects of the `shaders` library its layers fill with. */
|
|
200
|
+
shaders: Set<string>
|
|
201
|
+
/** A ref per nested control drawn on, which its `v-model` binds. */
|
|
202
|
+
models: { name: string }[]
|
|
203
|
+
taken: Set<string>
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The Iconify component, named apart from any component the design calls `Icon`. */
|
|
207
|
+
const ICONIFY = 'IconifyIcon'
|
|
208
|
+
|
|
209
|
+
/** An icon: its package's own component where it has the icon, Iconify's otherwise. */
|
|
210
|
+
function iconElement(node: IconReference, uses: TemplateUses): vue.VueElement {
|
|
211
|
+
const className = vue.attribute('class', uses.classOf(node.className))
|
|
212
|
+
const found = iconDrawing(node.icon, uses.iconPackage, 'vue')
|
|
213
|
+
const drawn = found && uses.nameIcon(found)
|
|
214
|
+
if (!drawn) {
|
|
215
|
+
uses.icons = true
|
|
216
|
+
return vue.element(ICONIFY, [vue.attribute('icon', node.icon), className])
|
|
217
|
+
}
|
|
218
|
+
uses.iconImports.push(...drawingImports(drawn))
|
|
219
|
+
if (drawn.type === 'component') return vue.element(drawn.import.local, [className])
|
|
220
|
+
return vue.element(drawn.component.local, [
|
|
221
|
+
vue.bound('icon', identifier(drawn.import.local)),
|
|
222
|
+
className
|
|
223
|
+
])
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function reference(node: ComponentReference, uses: TemplateUses): vue.VueElement {
|
|
227
|
+
uses.components.add(node.component)
|
|
228
|
+
const model = node.model
|
|
229
|
+
? claimName(`${camelCase(node.component)}${upperFirst(node.model)}`, uses.taken)
|
|
230
|
+
: null
|
|
231
|
+
if (model) uses.models.push({ name: model })
|
|
232
|
+
return vue.element(node.component, [
|
|
233
|
+
vue.attribute('class', uses.classOf(node.className)),
|
|
234
|
+
...node.props.map((prop) =>
|
|
235
|
+
typeof prop.value === 'boolean'
|
|
236
|
+
? vue.bound(prop.name, es.parseExpression(String(prop.value)))
|
|
237
|
+
: vue.attribute(prop.name, prop.value)
|
|
238
|
+
),
|
|
239
|
+
// The control stays operable, starting from the value the design draws.
|
|
240
|
+
...(model && node.model ? [vue.model(identifier(model), node.model)] : [])
|
|
241
|
+
])
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** A design element's attributes: what shows it, its part's own, the design's, and bindings. */
|
|
245
|
+
function elementAttributes(
|
|
246
|
+
node: ComponentElement,
|
|
247
|
+
uses: TemplateUses,
|
|
248
|
+
{ native, button }: { native: boolean; button: boolean }
|
|
249
|
+
): vue.VueAttribute[] {
|
|
250
|
+
const root = node.part === 'root'
|
|
251
|
+
return [
|
|
252
|
+
...(node.shownBy ? [vue.renderIf(identifier(node.shownBy))] : []),
|
|
253
|
+
...(button ? [vue.attribute('type', 'button')] : []),
|
|
254
|
+
...(root ? [...(ROOT_ATTRIBUTES[uses.kind] ?? []), ...rangeAttributes(uses.component)] : []),
|
|
255
|
+
...(uses.kind === 'progress' && node.part === 'indicator' ? progressStyle(uses.component) : []),
|
|
256
|
+
...Object.entries(omit(node.attrs, ['class'])).map(([name, value]) =>
|
|
257
|
+
vue.attribute(name, value)
|
|
258
|
+
),
|
|
259
|
+
vue.attribute('class', compact([node.attrs.class, uses.classOf(node.className)]).join(' ')),
|
|
260
|
+
...(node.value === undefined ? [] : [vue.attribute('value', node.value)]),
|
|
261
|
+
...node.bindings.flatMap((binding) => bindingAttributes(binding, uses, native))
|
|
262
|
+
]
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** A shader playing behind its layer's content, with telemetry off, as every export has it. */
|
|
266
|
+
function shaderCanvas(shader: ShaderLayer, uses: TemplateUses): vue.VueNode {
|
|
267
|
+
const effect = (component: ShaderComponent): vue.VueNode => {
|
|
268
|
+
uses.shaders.add(component.type)
|
|
269
|
+
return vue.element(
|
|
270
|
+
shaderEffectName(component.type),
|
|
271
|
+
Object.entries(component.props ?? {}).map(([key, value]) =>
|
|
272
|
+
typeof value === 'string'
|
|
273
|
+
? vue.attribute(kebabCase(key), value)
|
|
274
|
+
: vue.bound(kebabCase(key), es.json(value))
|
|
275
|
+
),
|
|
276
|
+
drawnEffects(component.children).map(effect)
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
return vue.element(
|
|
280
|
+
SHADER_CANVAS,
|
|
281
|
+
[
|
|
282
|
+
vue.attribute('class', uses.classOf(shader.className)),
|
|
283
|
+
vue.bound('disable-telemetry', es.json(true))
|
|
284
|
+
],
|
|
285
|
+
drawnEffects(shader.preset.components).map(effect)
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function designElement(node: ComponentElement, uses: TemplateUses): vue.VueElement {
|
|
290
|
+
const reka = node.part ? REKA[uses.kind].parts[node.part] : undefined
|
|
291
|
+
if (reka) uses.reka.add(reka)
|
|
292
|
+
const native = node.part === 'root' && !reka
|
|
293
|
+
const button = native && uses.kind === 'button'
|
|
294
|
+
const content = node.children.map((child) => templateNode(child, uses))
|
|
295
|
+
const children = node.shader ? [shaderCanvas(node.shader, uses), ...content] : content
|
|
296
|
+
const element = drawing(
|
|
297
|
+
vue.element(
|
|
298
|
+
reka ?? (button ? 'button' : node.tag),
|
|
299
|
+
elementAttributes(node, uses, { native, button }),
|
|
300
|
+
// A slot frame shows what the caller passes, or the design's content.
|
|
301
|
+
node.slot
|
|
302
|
+
? [
|
|
303
|
+
...(node.shader ? [shaderCanvas(node.shader, uses)] : []),
|
|
304
|
+
vue.element('slot', [vue.attribute('name', node.slot)], content)
|
|
305
|
+
]
|
|
306
|
+
: children
|
|
307
|
+
),
|
|
308
|
+
node.layerId,
|
|
309
|
+
uses
|
|
310
|
+
)
|
|
311
|
+
// Reka puts an accordion item's trigger in a header, which carries the heading level.
|
|
312
|
+
if (uses.kind !== 'accordionItem' || node.part !== 'trigger') return element
|
|
313
|
+
uses.reka.add('AccordionHeader')
|
|
314
|
+
return vue.element('AccordionHeader', [vue.attribute('style', HEADING_STYLE)], [element])
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function templateNode(node: ComponentNode, uses: TemplateUses): vue.VueNode {
|
|
318
|
+
if (node.type === 'text') return vue.text(node.value)
|
|
319
|
+
if (node.type === 'textProp') return vue.interpolation(identifier(node.name))
|
|
320
|
+
if (node.type === 'reference') return drawing(reference(node, uses), node.layerId, uses)
|
|
321
|
+
if (node.type === 'input') return drawing(inputNode(node.className, uses), node.layerId, uses)
|
|
322
|
+
if (node.type === 'icon') return drawing(iconElement(node, uses), node.layerId, uses)
|
|
323
|
+
return designElement(node, uses)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function propsType(component: ComponentModel): es.SyntaxNode {
|
|
327
|
+
const members: [string, es.SyntaxNode, boolean][] = component.props.map((prop) => [
|
|
328
|
+
prop.name,
|
|
329
|
+
es.stringUnionType(prop.options),
|
|
330
|
+
true
|
|
331
|
+
])
|
|
332
|
+
if (component.disabled) members.unshift(['disabled', es.parseType('boolean'), true])
|
|
333
|
+
// A group's item always stands for a value, which has no default.
|
|
334
|
+
if (component.valueProp) members.unshift(['value', es.parseType('string'), false])
|
|
335
|
+
for (const text of component.texts) members.push([text.name, es.parseType('string'), true])
|
|
336
|
+
for (const prop of component.booleans) members.push([prop.name, es.parseType('boolean'), true])
|
|
337
|
+
return es.objectType(members)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** `defineProps`, with `withDefaults` when any prop has a default to give. */
|
|
341
|
+
/** Whether any prop has a default to give: `disabled`, a variant, a text, or a boolean. */
|
|
342
|
+
const hasDefaults = (component: ComponentModel) =>
|
|
343
|
+
component.disabled ||
|
|
344
|
+
component.props.length > 0 ||
|
|
345
|
+
component.texts.length > 0 ||
|
|
346
|
+
component.booleans.length > 0
|
|
347
|
+
|
|
348
|
+
function propsDeclaration(component: ComponentModel): es.SyntaxNode[] {
|
|
349
|
+
const optional = hasDefaults(component)
|
|
350
|
+
return optional
|
|
351
|
+
? es.fill(PROPS, { $Props: propsType(component), $defaults: propsDefaults(component) }).body
|
|
352
|
+
: es.fill(REQUIRED_PROPS, { $Props: propsType(component) }).body
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function propsDefaults(component: ComponentModel): es.SyntaxNode {
|
|
356
|
+
return es.object([
|
|
357
|
+
...(component.disabled ? [['disabled', es.parseExpression('false')] as const] : []),
|
|
358
|
+
...component.props.map((prop) => [prop.name, es.string(prop.default)] as const),
|
|
359
|
+
...component.texts.map((text) => [text.name, es.string(text.default)] as const),
|
|
360
|
+
...component.booleans.map(
|
|
361
|
+
(prop) => [prop.name, es.parseExpression(String(prop.default))] as const
|
|
362
|
+
)
|
|
363
|
+
])
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const PROPS = es.parseModule('withDefaults(defineProps<$Props>(), $defaults)')
|
|
367
|
+
const REQUIRED_PROPS = es.parseModule('defineProps<$Props>()')
|
|
368
|
+
const MODEL = es.parseModule('const $model = defineModel<boolean>($name, { default: false })')
|
|
369
|
+
const CHOICE_MODEL = es.parseModule(
|
|
370
|
+
'const $model = defineModel<$Type>($name, { default: $default })'
|
|
371
|
+
)
|
|
372
|
+
const OPEN_CHOICE_MODEL = es.parseModule('const $model = defineModel<$Type>($name)')
|
|
373
|
+
const VALUE_MODEL = es.parseModule(
|
|
374
|
+
'const $model = defineModel<$Type>($name, { default: $default })'
|
|
375
|
+
)
|
|
376
|
+
const SLIDER_VALUES = es.parseModule(dedent`
|
|
377
|
+
const $values = computed({
|
|
378
|
+
get: () => [$model.value],
|
|
379
|
+
set: (next: number[] | undefined) => {
|
|
380
|
+
$model.value = next?.[0] ?? $model.value
|
|
381
|
+
}
|
|
382
|
+
})
|
|
383
|
+
`)
|
|
384
|
+
|
|
385
|
+
function namedImport(names: readonly string[], source: string, local = (name: string) => name) {
|
|
386
|
+
return {
|
|
387
|
+
type: 'ImportDeclaration',
|
|
388
|
+
importKind: 'value',
|
|
389
|
+
specifiers: names.map((name) => ({
|
|
390
|
+
type: 'ImportSpecifier',
|
|
391
|
+
imported: identifier(name),
|
|
392
|
+
local: identifier(local(name))
|
|
393
|
+
})),
|
|
394
|
+
source: es.string(source)
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const IMPORT_COMPONENT = es.parseModule(`import $Component from '$path'`)
|
|
399
|
+
const MODEL_REF = es.parseModule('const $name = ref(true)')
|
|
400
|
+
|
|
401
|
+
/** What the script imports: Vue's helpers, Iconify, Reka, and other generated components. */
|
|
402
|
+
function scriptImports(uses: TemplateUses): es.SyntaxNode[] {
|
|
403
|
+
const helpers = compact([uses.sliderValues && 'computed', uses.models.length > 0 && 'ref'])
|
|
404
|
+
return [
|
|
405
|
+
...(helpers.length > 0 ? [namedImport(helpers, 'vue')] : []),
|
|
406
|
+
...(uses.icons ? [namedImport(['Icon'], '@iconify/vue', () => ICONIFY)] : []),
|
|
407
|
+
...iconImportDeclarations(uses.iconImports),
|
|
408
|
+
...(uses.reka.size > 0 ? [namedImport([...uses.reka].sort(), 'reka-ui')] : []),
|
|
409
|
+
...(uses.shaders.size > 0 ? [shaderImport('shaders/vue', uses.shaders)] : []),
|
|
410
|
+
...[...uses.components].sort().flatMap(
|
|
411
|
+
(name) =>
|
|
412
|
+
es.fill(IMPORT_COMPONENT, {
|
|
413
|
+
$Component: identifier(name),
|
|
414
|
+
$path: es.string(`./${name}.vue`)
|
|
415
|
+
}).body
|
|
416
|
+
)
|
|
417
|
+
]
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** The model's declaration: a choice, a number or text, or a boolean, and a slider's list. */
|
|
421
|
+
function modelDeclaration(component: ComponentModel, uses: TemplateUses): es.SyntaxNode[] {
|
|
422
|
+
const name = component.model
|
|
423
|
+
if (!name) return []
|
|
424
|
+
const { choice, range, text } = component
|
|
425
|
+
const base = { $model: identifier(name), $name: es.string(name) }
|
|
426
|
+
if (choice)
|
|
427
|
+
return es.fill(choice.default === null ? OPEN_CHOICE_MODEL : CHOICE_MODEL, {
|
|
428
|
+
...base,
|
|
429
|
+
$Type: es.stringUnionType(choice.options),
|
|
430
|
+
$default: choice.default === null ? es.OMIT : es.string(choice.default)
|
|
431
|
+
}).body
|
|
432
|
+
if (!range && !text) return es.fill(MODEL, base).body
|
|
433
|
+
const model = es.fill(VALUE_MODEL, {
|
|
434
|
+
...base,
|
|
435
|
+
$Type: es.parseType(range ? 'number' : 'string'),
|
|
436
|
+
$default: range ? es.number(range.default) : es.string(text?.default ?? '')
|
|
437
|
+
}).body
|
|
438
|
+
const values = uses.sliderValues
|
|
439
|
+
? es.fill(SLIDER_VALUES, { $values: identifier(uses.sliderValues), $model: identifier(name) })
|
|
440
|
+
.body
|
|
441
|
+
: []
|
|
442
|
+
return [...model, ...values]
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function script(component: ComponentModel, uses: TemplateUses): es.SyntaxNode {
|
|
446
|
+
const props = component.valueProp || hasDefaults(component) ? propsDeclaration(component) : []
|
|
447
|
+
const refs = uses.models.flatMap(
|
|
448
|
+
(item) => es.fill(MODEL_REF, { $name: identifier(item.name) }).body
|
|
449
|
+
)
|
|
450
|
+
return {
|
|
451
|
+
type: 'Program',
|
|
452
|
+
sourceType: 'module',
|
|
453
|
+
body: [...scriptImports(uses), ...props, ...modelDeclaration(component, uses), ...refs]
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* A component as a Vue single-file component on Reka UI: its variants' state styles in a
|
|
459
|
+
* scoped stylesheet, its parts as Reka components, and its props from the behaviour and its
|
|
460
|
+
* other variant properties.
|
|
461
|
+
*/
|
|
462
|
+
export const vueComponent: ComponentGenerator = async (component, options = {}) => {
|
|
463
|
+
const styling = options.styling ?? 'css'
|
|
464
|
+
const taken = new Set([
|
|
465
|
+
...component.props.map((prop) => prop.name),
|
|
466
|
+
...component.texts.map((text) => text.name),
|
|
467
|
+
...component.booleans.map((prop) => prop.name),
|
|
468
|
+
...component.slots.map((slot) => slot.name),
|
|
469
|
+
...(component.model ? [component.model] : []),
|
|
470
|
+
'disabled'
|
|
471
|
+
])
|
|
472
|
+
const uses: TemplateUses = {
|
|
473
|
+
component,
|
|
474
|
+
classOf: componentClasses(component, styling),
|
|
475
|
+
layers: new WeakMap(),
|
|
476
|
+
kind: component.kind,
|
|
477
|
+
sliderValues:
|
|
478
|
+
component.kind === 'slider' && component.model ? claimName('values', taken) : null,
|
|
479
|
+
reka: new Set(),
|
|
480
|
+
components: new Set(),
|
|
481
|
+
icons: false,
|
|
482
|
+
iconPackage: options.icons ?? 'iconify',
|
|
483
|
+
iconImports: [],
|
|
484
|
+
nameIcon: iconNamer(component, [ICONIFY, ...taken]),
|
|
485
|
+
shaders: new Set(),
|
|
486
|
+
models: [],
|
|
487
|
+
// Refs share the script with the component's own props and model.
|
|
488
|
+
taken
|
|
489
|
+
}
|
|
490
|
+
const template = templateNode(component.tree, uses)
|
|
491
|
+
// Tailwind styles the markup itself; a stylesheet is written beside it otherwise.
|
|
492
|
+
const style =
|
|
493
|
+
styling === 'css'
|
|
494
|
+
? compact([(await stateStylesToCSS(component.styles)).css, shaderCSS(component.tree)]).join(
|
|
495
|
+
'\n'
|
|
496
|
+
)
|
|
497
|
+
: undefined
|
|
498
|
+
const path = `${component.name}.vue`
|
|
499
|
+
// A group's items are their own component, which the group's template uses.
|
|
500
|
+
const item = component.item ? await vueComponent(component.item, options) : null
|
|
501
|
+
const sfc = { script: script(component, uses), template, style }
|
|
502
|
+
return {
|
|
503
|
+
files: [
|
|
504
|
+
...(item?.files ?? []),
|
|
505
|
+
{
|
|
506
|
+
path,
|
|
507
|
+
content: vue.printComponent(sfc),
|
|
508
|
+
layerIds: vue
|
|
509
|
+
.componentElements(sfc)
|
|
510
|
+
.map((element) => (element && uses.layers.get(element)) ?? null)
|
|
511
|
+
}
|
|
512
|
+
],
|
|
513
|
+
entry: { path: `./${path}`, named: false },
|
|
514
|
+
// `v-model` binds the value, and its prop takes a story's initial value.
|
|
515
|
+
valueArg: component.model
|
|
516
|
+
}
|
|
517
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Effect, Fill, ShadowEffect, Stroke, TextLength } from '@pairdraw/scene-graph'
|
|
2
|
+
import { colorToCSS, colorToHex } from '@pairdraw/scene-graph/color'
|
|
3
|
+
import type { Color } from '@pairdraw/scene-graph/primitives'
|
|
4
|
+
|
|
5
|
+
import type { DesignStyleDeclaration } from '../types'
|
|
6
|
+
|
|
7
|
+
/** Opaque colors as hex, which Tailwind matches to its palette; translucent ones as `rgba()`. */
|
|
8
|
+
export function cssColor(color: Color): string {
|
|
9
|
+
return color.a >= 1 ? colorToHex(color) : colorToCSS(color)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function fillToCSS(fill: Fill | undefined): string | undefined {
|
|
13
|
+
if (fill?.type !== 'SOLID' || !fill.visible) return undefined
|
|
14
|
+
return cssColor({ ...fill.color, a: fill.opacity })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function strokeColorToCSS(stroke: Stroke | undefined): string | undefined {
|
|
18
|
+
if (!stroke?.visible) return undefined
|
|
19
|
+
return cssColor({ ...stroke.color, a: stroke.opacity })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function shadowToCSS(effect: ShadowEffect): string {
|
|
23
|
+
const inset = effect.type === 'INNER_SHADOW' ? 'inset ' : ''
|
|
24
|
+
return `${inset}${effect.offset.x}px ${effect.offset.y}px ${effect.radius}px ${effect.spread}px ${cssColor(effect.color)}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function dropShadowToCSS(effect: Effect | undefined): string | undefined {
|
|
28
|
+
if (effect?.type !== 'DROP_SHADOW' || !effect.visible) return undefined
|
|
29
|
+
return shadowToCSS(effect)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Every visible shadow, in order, plus layer and background blur. */
|
|
33
|
+
export function effectsToCSS(effects: Effect[]): DesignStyleDeclaration {
|
|
34
|
+
const style: DesignStyleDeclaration = {}
|
|
35
|
+
const visible = effects.filter((effect) => effect.visible)
|
|
36
|
+
const shadows = visible.filter(
|
|
37
|
+
(effect): effect is ShadowEffect =>
|
|
38
|
+
effect.type === 'DROP_SHADOW' || effect.type === 'INNER_SHADOW'
|
|
39
|
+
)
|
|
40
|
+
if (shadows.length > 0) style['box-shadow'] = shadows.map(shadowToCSS).join(', ')
|
|
41
|
+
for (const effect of visible) {
|
|
42
|
+
if (effect.type === 'LAYER_BLUR' || effect.type === 'FOREGROUND_BLUR')
|
|
43
|
+
style.filter = `blur(${effect.radius}px)`
|
|
44
|
+
if (effect.type === 'BACKGROUND_BLUR') style['backdrop-filter'] = `blur(${effect.radius}px)`
|
|
45
|
+
}
|
|
46
|
+
return style
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A line height as CSS writes it: a percent as the unitless multiple that follows the font size. */
|
|
50
|
+
export function lineHeightToCSS(length: TextLength): string {
|
|
51
|
+
return length.unit === 'PIXELS' ? `${length.value}px` : String(length.value / 100)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A letter spacing as CSS writes it: a percent of the font size in `em`. */
|
|
55
|
+
export function letterSpacingToCSS(length: TextLength): string {
|
|
56
|
+
return length.unit === 'PIXELS' ? `${length.value}px` : `${length.value / 100}em`
|
|
57
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { GridTrack, SceneNode } from '@pairdraw/scene-graph'
|
|
2
|
+
|
|
3
|
+
import type { DesignStyleDeclaration } from '../types'
|
|
4
|
+
|
|
5
|
+
function trackToCSS(track: GridTrack): string {
|
|
6
|
+
if (track.sizing === 'FR') return `${track.value}fr`
|
|
7
|
+
if (track.sizing === 'FIXED') return `${track.value}px`
|
|
8
|
+
return 'auto'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Equal `1fr` tracks use Tailwind's `grid-cols-N` form, which keeps them from overflowing. */
|
|
12
|
+
function tracksToCSS(tracks: GridTrack[]): string {
|
|
13
|
+
if (tracks.every((track) => track.sizing === 'FR' && track.value === 1))
|
|
14
|
+
return `repeat(${tracks.length}, minmax(0, 1fr))`
|
|
15
|
+
return tracks.map(trackToCSS).join(' ')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function addGridContainer(
|
|
19
|
+
style: DesignStyleDeclaration,
|
|
20
|
+
node: SceneNode,
|
|
21
|
+
css: (field: string, literal: string) => string
|
|
22
|
+
): void {
|
|
23
|
+
style.display = 'grid'
|
|
24
|
+
if (node.gridTemplateColumns.length > 0)
|
|
25
|
+
style['grid-template-columns'] = tracksToCSS(node.gridTemplateColumns)
|
|
26
|
+
if (node.gridTemplateRows.length > 0)
|
|
27
|
+
style['grid-template-rows'] = tracksToCSS(node.gridTemplateRows)
|
|
28
|
+
if (node.gridColumnGap > 0) style['column-gap'] = css('gridColumnGap', `${node.gridColumnGap}px`)
|
|
29
|
+
if (node.gridRowGap > 0) style['row-gap'] = css('gridRowGap', `${node.gridRowGap}px`)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A span sets the whole placement, so the start follows it. */
|
|
33
|
+
export function addGridPlacement(style: DesignStyleDeclaration, node: SceneNode): void {
|
|
34
|
+
const position = node.gridPosition
|
|
35
|
+
if (!position) return
|
|
36
|
+
if (position.columnSpan > 1)
|
|
37
|
+
style['grid-column'] = `span ${position.columnSpan} / span ${position.columnSpan}`
|
|
38
|
+
if (position.column > 0) style['grid-column-start'] = String(position.column)
|
|
39
|
+
if (position.rowSpan > 1)
|
|
40
|
+
style['grid-row'] = `span ${position.rowSpan} / span ${position.rowSpan}`
|
|
41
|
+
if (position.row > 0) style['grid-row-start'] = String(position.row)
|
|
42
|
+
}
|