@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,319 @@
|
|
|
1
|
+
import dedent from 'dedent'
|
|
2
|
+
|
|
3
|
+
import { es } from '@pairdraw/emit'
|
|
4
|
+
|
|
5
|
+
export const STORYBOOK_FRAMEWORKS = ['react', 'vue', 'html'] as const
|
|
6
|
+
export type StorybookFramework = (typeof STORYBOOK_FRAMEWORKS)[number]
|
|
7
|
+
|
|
8
|
+
/** Tells readers the file is generated; nothing parses it. */
|
|
9
|
+
const HEADER = '// Generated by Pairdraw. Re-export to update; edits are overwritten.'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How a variant property is controlled. A boolean value's property becomes a boolean arg that
|
|
13
|
+
* picks its on or off variant; the interaction-state property is left to each story, with a
|
|
14
|
+
* `disabled` arg picking its disabled variant when the set draws one.
|
|
15
|
+
*/
|
|
16
|
+
type StoryControl =
|
|
17
|
+
| { type: 'boolean'; name: string; on: string; off: string }
|
|
18
|
+
| { type: 'state'; rest: string; disabled?: string }
|
|
19
|
+
|
|
20
|
+
export interface StoryProp {
|
|
21
|
+
name: string
|
|
22
|
+
options: string[]
|
|
23
|
+
control?: StoryControl
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Everything a story module varies by; the TypeScript around it is a fixed template. */
|
|
27
|
+
export interface StoryModuleData {
|
|
28
|
+
framework: StorybookFramework
|
|
29
|
+
title: string
|
|
30
|
+
/** Tags on the whole file, which its stories inherit. */
|
|
31
|
+
tags: string[]
|
|
32
|
+
/** Stylesheets the file imports, such as the fonts its text uses. */
|
|
33
|
+
styles: string[]
|
|
34
|
+
/** Group name used in the missing-variant error. */
|
|
35
|
+
name: string
|
|
36
|
+
props: StoryProp[]
|
|
37
|
+
variants: { values: string[]; html: string }[]
|
|
38
|
+
metaDesign: StoryDesign[]
|
|
39
|
+
/** Import path of each design image, by variant index. */
|
|
40
|
+
images: string[]
|
|
41
|
+
/** Every variant laid out in one story, each with its label as escaped HTML, if a gallery. */
|
|
42
|
+
gallery: { values: string[]; label: string }[] | null
|
|
43
|
+
stories: {
|
|
44
|
+
exportName: string
|
|
45
|
+
label: string
|
|
46
|
+
values: string[]
|
|
47
|
+
design: StoryDesign[]
|
|
48
|
+
tags: string[]
|
|
49
|
+
}[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type StoryDesign =
|
|
53
|
+
| { type: 'link'; url: string }
|
|
54
|
+
/** Refers to the design image of the variant with this index. */
|
|
55
|
+
| { type: 'image'; variant: number }
|
|
56
|
+
|
|
57
|
+
const MODULE = es.parseModule(dedent`
|
|
58
|
+
import type { Meta, StoryObj } from '$storybook'
|
|
59
|
+
|
|
60
|
+
type Args = $Args
|
|
61
|
+
|
|
62
|
+
const variants: Record<string, string> = $variants
|
|
63
|
+
|
|
64
|
+
function variantHTML(args: Args): string {
|
|
65
|
+
const key = JSON.stringify($key)
|
|
66
|
+
const html = variants[key]
|
|
67
|
+
if (html === undefined) throw new Error($missing + key)
|
|
68
|
+
return html
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const meta = {
|
|
72
|
+
title: $title,
|
|
73
|
+
tags: $tags,
|
|
74
|
+
parameters: $parameters,
|
|
75
|
+
args: $args,
|
|
76
|
+
argTypes: $argTypes,
|
|
77
|
+
render: (args) => $render
|
|
78
|
+
} satisfies Meta<Args>
|
|
79
|
+
|
|
80
|
+
export default meta
|
|
81
|
+
type Story = StoryObj<Args>
|
|
82
|
+
`)
|
|
83
|
+
|
|
84
|
+
const GALLERY = es.parseModule(dedent`
|
|
85
|
+
const labels: [string, string][] = $labels
|
|
86
|
+
|
|
87
|
+
function galleryHTML(): string {
|
|
88
|
+
const items = labels.map(
|
|
89
|
+
([key, label]) =>
|
|
90
|
+
'<figure style="margin: 0; display: flex; flex-direction: column; align-items: center; gap: 8px">' +
|
|
91
|
+
(variants[key] ?? '') +
|
|
92
|
+
'<figcaption style="font: 12px system-ui, sans-serif; color: #6b7280">' +
|
|
93
|
+
label +
|
|
94
|
+
'</figcaption></figure>'
|
|
95
|
+
)
|
|
96
|
+
return (
|
|
97
|
+
'<div style="display: flex; flex-wrap: wrap; align-items: flex-end; gap: 24px">' +
|
|
98
|
+
items.join('') +
|
|
99
|
+
'</div>'
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const Gallery: Story = { name: 'Gallery', render: () => $render }
|
|
104
|
+
`)
|
|
105
|
+
|
|
106
|
+
const IMAGE = es.parseModule(`const $name = new URL($path, import.meta.url).href`)
|
|
107
|
+
const STORY = es.parseModule(`export const $name: Story = $story`)
|
|
108
|
+
const STYLESHEET = es.parseModule(`import '$path'`)
|
|
109
|
+
|
|
110
|
+
/** The Storybook renderer package each framework's stories import their types from. */
|
|
111
|
+
export const STORYBOOK_PACKAGES: Record<StorybookFramework, string> = {
|
|
112
|
+
react: '@storybook/react-vite',
|
|
113
|
+
vue: '@storybook/vue3-vite',
|
|
114
|
+
html: '@storybook/html-vite'
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** How each framework renders a string of HTML, `$html`, as a story. */
|
|
118
|
+
const FRAMEWORKS: Record<StorybookFramework, { imports: es.SyntaxNode[]; render: es.SyntaxNode }> =
|
|
119
|
+
{
|
|
120
|
+
react: {
|
|
121
|
+
imports: es.parseModule(`import { createElement } from 'react'`).body,
|
|
122
|
+
render: es.parseExpression(
|
|
123
|
+
`createElement('div', { dangerouslySetInnerHTML: { __html: $html } })`
|
|
124
|
+
)
|
|
125
|
+
},
|
|
126
|
+
vue: {
|
|
127
|
+
imports: es.parseModule(`import { h } from 'vue'`).body,
|
|
128
|
+
render: es.parseExpression(`{ setup: () => () => h('div', { innerHTML: $html }) }`)
|
|
129
|
+
},
|
|
130
|
+
html: {
|
|
131
|
+
imports: [],
|
|
132
|
+
render: es.parseExpression('$html')
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const rendered = (framework: StorybookFramework, html: string) =>
|
|
137
|
+
es.fill(FRAMEWORKS[framework].render, { $html: es.parseExpression(html) })
|
|
138
|
+
|
|
139
|
+
const imageName = (variant: number) => `design${variant}`
|
|
140
|
+
|
|
141
|
+
const BOOLEAN = es.parseType('boolean')
|
|
142
|
+
const CONDITION = es.parseExpression('$test ? $then : $else')
|
|
143
|
+
const boolean = (value: boolean) => es.parseExpression(String(value))
|
|
144
|
+
|
|
145
|
+
function argRef(name: string): es.SyntaxNode {
|
|
146
|
+
return {
|
|
147
|
+
type: 'MemberExpression',
|
|
148
|
+
object: es.identifier('args'),
|
|
149
|
+
property: es.string(name),
|
|
150
|
+
computed: true,
|
|
151
|
+
optional: false
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The `disabled` arg a state property adds, unless a boolean value already takes the name. */
|
|
156
|
+
function disabledArg(data: StoryModuleData): { property: string; value: string } | null {
|
|
157
|
+
if (
|
|
158
|
+
data.props.some((prop) => prop.control?.type === 'boolean' && prop.control.name === 'disabled')
|
|
159
|
+
)
|
|
160
|
+
return null
|
|
161
|
+
for (const prop of data.props)
|
|
162
|
+
if (prop.control?.type === 'state' && prop.control.disabled !== undefined)
|
|
163
|
+
return { property: prop.name, value: prop.control.disabled }
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The arg a prop is set by: a boolean prop's own name, otherwise the property's. */
|
|
168
|
+
const argName = (prop: StoryProp) =>
|
|
169
|
+
prop.control?.type === 'boolean' ? prop.control.name : prop.name
|
|
170
|
+
|
|
171
|
+
/** The variant value a prop shows, as an expression over `args`. */
|
|
172
|
+
function propValue(data: StoryModuleData, prop: StoryProp): es.SyntaxNode {
|
|
173
|
+
const control = prop.control
|
|
174
|
+
if (control?.type === 'boolean')
|
|
175
|
+
return es.fill(CONDITION, {
|
|
176
|
+
$test: argRef(control.name),
|
|
177
|
+
$then: es.string(control.on),
|
|
178
|
+
$else: es.string(control.off)
|
|
179
|
+
})
|
|
180
|
+
const disabled = disabledArg(data)
|
|
181
|
+
if (disabled?.property === prop.name)
|
|
182
|
+
return es.fill(CONDITION, {
|
|
183
|
+
$test: argRef('disabled'),
|
|
184
|
+
$then: es.string(disabled.value),
|
|
185
|
+
$else: argRef(prop.name)
|
|
186
|
+
})
|
|
187
|
+
return argRef(prop.name)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function argsObject(data: StoryModuleData, values: string[]): es.SyntaxNode {
|
|
191
|
+
const entries = data.props.map((prop, i): [string, es.SyntaxNode] => {
|
|
192
|
+
const value = values[i] ?? ''
|
|
193
|
+
if (prop.control?.type === 'boolean')
|
|
194
|
+
return [prop.control.name, boolean(value === prop.control.on)]
|
|
195
|
+
// A disabled story says so through `disabled`, so clearing it shows the rest state.
|
|
196
|
+
if (prop.control?.type === 'state' && value === disabledArg(data)?.value)
|
|
197
|
+
return [prop.name, es.string(prop.control.rest)]
|
|
198
|
+
return [prop.name, es.string(value)]
|
|
199
|
+
})
|
|
200
|
+
const disabled = disabledArg(data)
|
|
201
|
+
if (disabled) {
|
|
202
|
+
const index = data.props.findIndex((prop) => prop.name === disabled.property)
|
|
203
|
+
entries.push(['disabled', boolean(values[index] === disabled.value)])
|
|
204
|
+
}
|
|
205
|
+
return es.object(entries)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function argTypes(data: StoryModuleData): es.SyntaxNode {
|
|
209
|
+
const entries = data.props.map((prop): [string, es.SyntaxNode] => {
|
|
210
|
+
if (prop.control?.type === 'boolean')
|
|
211
|
+
return [prop.control.name, es.object([['control', es.string('boolean')]])]
|
|
212
|
+
if (prop.control?.type === 'state')
|
|
213
|
+
return [prop.name, es.object([['table', es.object([['disable', boolean(true)]])]])]
|
|
214
|
+
return [
|
|
215
|
+
prop.name,
|
|
216
|
+
es.object([
|
|
217
|
+
['control', es.string('select')],
|
|
218
|
+
['options', es.array(prop.options.map(es.string))]
|
|
219
|
+
])
|
|
220
|
+
]
|
|
221
|
+
})
|
|
222
|
+
if (disabledArg(data)) entries.push(['disabled', es.object([['control', es.string('boolean')]])])
|
|
223
|
+
return es.object(entries)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function parameters(design: StoryDesign[]): es.SyntaxNode {
|
|
227
|
+
const entries = design.map((entry) =>
|
|
228
|
+
entry.type === 'link'
|
|
229
|
+
? es.object([
|
|
230
|
+
['name', es.string('Pairdraw')],
|
|
231
|
+
['type', es.string('link')],
|
|
232
|
+
['url', es.string(entry.url)]
|
|
233
|
+
])
|
|
234
|
+
: es.object([
|
|
235
|
+
['name', es.string('Design')],
|
|
236
|
+
['type', es.string('image')],
|
|
237
|
+
['url', es.identifier(imageName(entry.variant))]
|
|
238
|
+
])
|
|
239
|
+
)
|
|
240
|
+
return es.object([['design', es.array(entries)]])
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const NO_ARGS = es.parseType('Record<string, never>')
|
|
244
|
+
|
|
245
|
+
function argsType(data: StoryModuleData): es.SyntaxNode {
|
|
246
|
+
if (data.props.length === 0) return NO_ARGS
|
|
247
|
+
const entries = data.props.map((prop): [string, es.SyntaxNode] => [
|
|
248
|
+
argName(prop),
|
|
249
|
+
prop.control?.type === 'boolean' ? BOOLEAN : es.stringUnionType(prop.options)
|
|
250
|
+
])
|
|
251
|
+
if (disabledArg(data)) entries.push(['disabled', BOOLEAN])
|
|
252
|
+
return es.objectType(entries)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** `import './fonts/kit/fonts.css'` for each stylesheet, which the bundler loads with the stories. */
|
|
256
|
+
export const stylesheets = (paths: readonly string[]) =>
|
|
257
|
+
paths.flatMap((path) => es.fill(STYLESHEET, { $path: es.string(path) }).body)
|
|
258
|
+
|
|
259
|
+
/** Print a CSF3 story module for one component or component set. */
|
|
260
|
+
export function printStoryModule(data: StoryModuleData): string {
|
|
261
|
+
const framework = FRAMEWORKS[data.framework]
|
|
262
|
+
const [storybookImport, ...body] = es.fill(MODULE, {
|
|
263
|
+
$storybook: es.string(STORYBOOK_PACKAGES[data.framework]),
|
|
264
|
+
$Args: argsType(data),
|
|
265
|
+
$variants: es.object(
|
|
266
|
+
data.variants.map((variant) => [JSON.stringify(variant.values), es.string(variant.html)])
|
|
267
|
+
),
|
|
268
|
+
$key: es.array(data.props.map((prop) => propValue(data, prop))),
|
|
269
|
+
$missing: es.string(`${data.name} has no variant `),
|
|
270
|
+
$title: es.string(data.title),
|
|
271
|
+
$tags: es.array(data.tags.map(es.string)),
|
|
272
|
+
$parameters: data.metaDesign.length > 0 ? parameters(data.metaDesign) : es.OMIT,
|
|
273
|
+
$args: argsObject(data, data.variants[0]?.values ?? []),
|
|
274
|
+
$argTypes: argTypes(data),
|
|
275
|
+
$render: rendered(data.framework, 'variantHTML(args)')
|
|
276
|
+
}).body
|
|
277
|
+
const images = data.images.flatMap(
|
|
278
|
+
(path, i) =>
|
|
279
|
+
es.fill(IMAGE, { $name: es.identifier(imageName(i)), $path: es.string(`./${path}`) }).body
|
|
280
|
+
)
|
|
281
|
+
const stories = data.stories.flatMap(
|
|
282
|
+
(story) =>
|
|
283
|
+
es.fill(STORY, {
|
|
284
|
+
$name: es.identifier(story.exportName),
|
|
285
|
+
$story: es.object([
|
|
286
|
+
['name', es.string(story.label)],
|
|
287
|
+
...(story.tags.length > 0
|
|
288
|
+
? [['tags', es.array(story.tags.map(es.string))] as const]
|
|
289
|
+
: []),
|
|
290
|
+
...(story.design.length > 0 ? [['parameters', parameters(story.design)] as const] : []),
|
|
291
|
+
['args', argsObject(data, story.values)]
|
|
292
|
+
])
|
|
293
|
+
}).body
|
|
294
|
+
)
|
|
295
|
+
const gallery = data.gallery
|
|
296
|
+
? es.fill(GALLERY, {
|
|
297
|
+
$labels: es.array(
|
|
298
|
+
data.gallery.map((item) =>
|
|
299
|
+
es.array([es.string(JSON.stringify(item.values)), es.string(item.label)])
|
|
300
|
+
)
|
|
301
|
+
),
|
|
302
|
+
$render: rendered(data.framework, 'galleryHTML()')
|
|
303
|
+
}).body
|
|
304
|
+
: []
|
|
305
|
+
const module = es.printModule({
|
|
306
|
+
type: 'Program',
|
|
307
|
+
sourceType: 'module',
|
|
308
|
+
body: [
|
|
309
|
+
storybookImport,
|
|
310
|
+
...framework.imports,
|
|
311
|
+
...stylesheets(data.styles),
|
|
312
|
+
...images,
|
|
313
|
+
...body,
|
|
314
|
+
...stories,
|
|
315
|
+
...gallery
|
|
316
|
+
]
|
|
317
|
+
})
|
|
318
|
+
return `${HEADER}\n${module}\n`
|
|
319
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { pascalCase } from 'es-toolkit/string'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Claim `base`, or the first free `base<separator><n>` from 2 on. `key` decides which
|
|
5
|
+
* names collide, e.g. ignoring case for file names on case-insensitive file systems.
|
|
6
|
+
*/
|
|
7
|
+
export function claimName(
|
|
8
|
+
base: string,
|
|
9
|
+
taken: Set<string>,
|
|
10
|
+
{ separator = '', key = (name: string) => name } = {}
|
|
11
|
+
): string {
|
|
12
|
+
let name = base
|
|
13
|
+
for (let n = 2; taken.has(key(name)); n++) name = `${base}${separator}${n}`
|
|
14
|
+
taken.add(key(name))
|
|
15
|
+
return name
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const NOT_IDENTIFIER_PART = /[^\p{ID_Continue}]/gu
|
|
19
|
+
const IDENTIFIER_START = /^\p{ID_Start}/u
|
|
20
|
+
|
|
21
|
+
/** A PascalCase JavaScript identifier for `text`, prefixed with `fallback` when it cannot start one. */
|
|
22
|
+
export function identifierName(text: string, fallback: string): string {
|
|
23
|
+
const name = pascalCase(text).replace(NOT_IDENTIFIER_PART, '')
|
|
24
|
+
return IDENTIFIER_START.test(name) ? name : `${fallback}${name}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Storybook's `sanitize` from `src/csf/csf-utils.ts`, which turns titles into story ids.
|
|
29
|
+
* It is only exported from `storybook/internal/csf`, so it is copied here; a test keeps
|
|
30
|
+
* the copy equal to the installed Storybook.
|
|
31
|
+
*/
|
|
32
|
+
export function storyId(title: string): string {
|
|
33
|
+
return title
|
|
34
|
+
.toLowerCase()
|
|
35
|
+
.replace(/[ ’–—―′¿'`~!@#$%^&*()_|+\-=?;:'",.<>{}[\]\\/]/gi, '-')
|
|
36
|
+
.replace(/-+/g, '-')
|
|
37
|
+
.replace(/^-+/, '')
|
|
38
|
+
.replace(/-+$/, '')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Tags a story file carries, which Storybook's sidebar filter and `main.ts` `tags` options
|
|
43
|
+
* select by: every exported file, and the page it comes from.
|
|
44
|
+
*/
|
|
45
|
+
export const fileTags = (page: string) => ['pairdraw', `page:${storyId(page)}`]
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The tag every story but a file's first carries: each other variant or state. Excluding it
|
|
49
|
+
* in Storybook leaves one story per component, the one the design shows at rest.
|
|
50
|
+
*/
|
|
51
|
+
export const VARIANT_TAG = 'variant'
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { jsx, type SyntaxNode } from '@pairdraw/emit'
|
|
2
|
+
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
3
|
+
import type { ShaderComponent, ShaderPreset } from '@pairdraw/scene-graph/shaders'
|
|
4
|
+
|
|
5
|
+
import type { DesignDocument, DesignElement, DesignNode } from '../types'
|
|
6
|
+
import { shaderEffectAttributes } from './components/shaders'
|
|
7
|
+
import { SHADER_UTILITIES } from './components/styling'
|
|
8
|
+
import { mergeClassNames, serializeTailwindClasses } from './html'
|
|
9
|
+
import {
|
|
10
|
+
ownLayerName,
|
|
11
|
+
sceneNodeToDesignDocument,
|
|
12
|
+
type SceneGraphToDesignOptions
|
|
13
|
+
} from './projection'
|
|
14
|
+
|
|
15
|
+
/** Engine services the projection takes, such as drawing vector layers as SVG. */
|
|
16
|
+
type ProjectionServices = Pick<SceneGraphToDesignOptions, 'vectorElement'>
|
|
17
|
+
|
|
18
|
+
/** HTML attribute names that JSX spells differently. */
|
|
19
|
+
const JSX_ATTRIBUTE_NAMES: Record<string, string> = {
|
|
20
|
+
class: 'className',
|
|
21
|
+
for: 'htmlFor',
|
|
22
|
+
'xlink:href': 'xlinkHref'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** React spells SVG's dashed attributes in camel case, such as `fillRule`; data and ARIA stay. */
|
|
26
|
+
function jsxAttributeName(name: string): string {
|
|
27
|
+
const named = JSX_ATTRIBUTE_NAMES[name]
|
|
28
|
+
if (named) return named
|
|
29
|
+
if (name.startsWith('data-') || name.startsWith('aria-')) return name
|
|
30
|
+
return name.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Scene text layers become paragraphs, whose margins Tailwind's preflight removes; text laid
|
|
35
|
+
* out in blocks keeps the `<div>` that holds them.
|
|
36
|
+
*/
|
|
37
|
+
function tagName(node: DesignElement): string {
|
|
38
|
+
return node.sourceSceneNode?.type === 'TEXT' && node.tagName === 'span' ? 'p' : node.tagName
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function attributes(node: DesignElement, themeVariables: readonly string[]): SyntaxNode[] {
|
|
42
|
+
const { class: className, ...attrs } = node.attrs
|
|
43
|
+
const entries: [string, string | undefined][] = [
|
|
44
|
+
['data-name', node.sourceSceneNode && ownLayerName(node.sourceSceneNode)],
|
|
45
|
+
...Object.entries(attrs),
|
|
46
|
+
['className', mergeClassNames(className, serializeTailwindClasses(node, themeVariables))]
|
|
47
|
+
]
|
|
48
|
+
return entries.flatMap(([key, value]) =>
|
|
49
|
+
value === undefined ? [] : [jsx.attribute(jsxAttributeName(key), jsx.stringValue(value))]
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A layer's shader as the `shaders` library's `<Shader>`, each effect a child with the preset's
|
|
55
|
+
* props, with telemetry off as every Pairdraw export has it.
|
|
56
|
+
*/
|
|
57
|
+
function shaderElement(preset: ShaderPreset, depth: number): SyntaxNode {
|
|
58
|
+
const effect =
|
|
59
|
+
(at: number) =>
|
|
60
|
+
(component: ShaderComponent): SyntaxNode =>
|
|
61
|
+
jsx.element(
|
|
62
|
+
component.type,
|
|
63
|
+
shaderEffectAttributes(component),
|
|
64
|
+
(component.children ?? []).map(effect(at + 1)),
|
|
65
|
+
at
|
|
66
|
+
)
|
|
67
|
+
return jsx.element(
|
|
68
|
+
'Shader',
|
|
69
|
+
[
|
|
70
|
+
jsx.attribute('className', jsx.stringValue(SHADER_UTILITIES)),
|
|
71
|
+
jsx.attribute('disableTelemetry', null)
|
|
72
|
+
],
|
|
73
|
+
preset.components.map(effect(depth + 1)),
|
|
74
|
+
depth
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** How many elements a shader prints, which no layer produced. */
|
|
79
|
+
function shaderElementCount(components: readonly ShaderComponent[] = []): number {
|
|
80
|
+
return components.reduce(
|
|
81
|
+
(count, component) => count + 1 + shaderElementCount(component.children),
|
|
82
|
+
0
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function element(
|
|
87
|
+
node: DesignElement,
|
|
88
|
+
depth: number,
|
|
89
|
+
themeVariables: readonly string[]
|
|
90
|
+
): SyntaxNode {
|
|
91
|
+
const children = [
|
|
92
|
+
...(node.shader ? [shaderElement(node.shader.preset, depth + 1)] : []),
|
|
93
|
+
...node.children.map((child) => jsxNode(child, depth + 1, themeVariables))
|
|
94
|
+
]
|
|
95
|
+
// A lone text child stays on the element's line.
|
|
96
|
+
const inline = children.length === 1 && node.children[0]?.type === 'text'
|
|
97
|
+
return jsx.element(tagName(node), attributes(node, themeVariables), children, depth, inline)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function jsxNode(node: DesignNode, depth: number, themeVariables: readonly string[]): SyntaxNode {
|
|
101
|
+
return node.type === 'text' ? jsx.text(node.text) : element(node, depth, themeVariables)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Print a design document as Tailwind JSX, one top-level element per block. */
|
|
105
|
+
export function designDocumentToTailwindJSX(document: DesignDocument): string {
|
|
106
|
+
const themeVariables = document.tokens?.themeVariables() ?? []
|
|
107
|
+
return document.children
|
|
108
|
+
.map((node) => jsx.printJSX(jsxNode(node, 0, themeVariables)))
|
|
109
|
+
.join('\n\n')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Tailwind JSX for scene nodes, separated by blank lines. */
|
|
113
|
+
export function sceneNodesToTailwindJSX(
|
|
114
|
+
graph: SceneGraph,
|
|
115
|
+
nodeIds: string[],
|
|
116
|
+
options: ProjectionServices = {}
|
|
117
|
+
): string {
|
|
118
|
+
return sceneNodesToTailwindJSXWithLayers(graph, nodeIds, options).code
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Tailwind JSX with the layer behind each element, in the order elements open. */
|
|
122
|
+
export interface TailwindJSXWithLayers {
|
|
123
|
+
code: string
|
|
124
|
+
/** One entry per JSX element in pre-order; `null` for an element no layer produced. */
|
|
125
|
+
layerIds: Array<string | null>
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function collectLayerIds(node: DesignNode, layerIds: Array<string | null>): void {
|
|
129
|
+
if (node.type === 'text') return
|
|
130
|
+
layerIds.push(node.sourceSceneNodeId ?? node.sourceSceneNode?.id ?? null)
|
|
131
|
+
if (node.shader) {
|
|
132
|
+
const count = 1 + shaderElementCount(node.shader.preset.components)
|
|
133
|
+
for (let index = 0; index < count; index++) layerIds.push(null)
|
|
134
|
+
}
|
|
135
|
+
for (const child of node.children) collectLayerIds(child, layerIds)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function sceneNodesToTailwindJSXWithLayers(
|
|
139
|
+
graph: SceneGraph,
|
|
140
|
+
nodeIds: string[],
|
|
141
|
+
{ vectorElement }: ProjectionServices = {}
|
|
142
|
+
): TailwindJSXWithLayers {
|
|
143
|
+
const layerIds: Array<string | null> = []
|
|
144
|
+
const blocks: string[] = []
|
|
145
|
+
for (const id of nodeIds) {
|
|
146
|
+
const document = sceneNodeToDesignDocument(graph, id, {
|
|
147
|
+
includeSourceIds: false,
|
|
148
|
+
vectorElement
|
|
149
|
+
})
|
|
150
|
+
const code = designDocumentToTailwindJSX(document)
|
|
151
|
+
if (!code) continue
|
|
152
|
+
blocks.push(code)
|
|
153
|
+
for (const node of document.children) collectLayerIds(node, layerIds)
|
|
154
|
+
}
|
|
155
|
+
return { code: blocks.join('\n\n'), layerIds }
|
|
156
|
+
}
|