@pairdraw/dom-css 0.0.0-stage → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -2
  3. package/dist/browser.d.ts +29 -0
  4. package/dist/browser.js +2 -0
  5. package/dist/chunks/behaviours/args.js +59 -0
  6. package/dist/chunks/behaviours/args.js.map +1 -0
  7. package/dist/chunks/behaviours/controls.js +109 -0
  8. package/dist/chunks/behaviours/controls.js.map +1 -0
  9. package/dist/chunks/behaviours/reset.js +40 -0
  10. package/dist/chunks/behaviours/reset.js.map +1 -0
  11. package/dist/chunks/behaviours/roles.js +51 -0
  12. package/dist/chunks/behaviours/roles.js.map +1 -0
  13. package/dist/chunks/behaviours/states/conditions.js +103 -0
  14. package/dist/chunks/behaviours/states/conditions.js.map +1 -0
  15. package/dist/chunks/behaviours/states/css.js +172 -0
  16. package/dist/chunks/behaviours/states/css.js.map +1 -0
  17. package/dist/chunks/behaviours/states/layers.js +1736 -0
  18. package/dist/chunks/behaviours/states/layers.js.map +1 -0
  19. package/dist/chunks/behaviours/states/model.js +144 -0
  20. package/dist/chunks/behaviours/states/model.js.map +1 -0
  21. package/dist/chunks/behaviours/states/tailwind.js +198 -0
  22. package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
  23. package/dist/chunks/browser.js +1352 -0
  24. package/dist/chunks/browser.js.map +1 -0
  25. package/dist/chunks/export/bundle.js +337 -0
  26. package/dist/chunks/export/bundle.js.map +1 -0
  27. package/dist/chunks/export/components/drawn.js +60 -0
  28. package/dist/chunks/export/components/drawn.js.map +1 -0
  29. package/dist/chunks/export/components/fields.js +178 -0
  30. package/dist/chunks/export/components/fields.js.map +1 -0
  31. package/dist/chunks/export/components/groups.js +297 -0
  32. package/dist/chunks/export/components/groups.js.map +1 -0
  33. package/dist/chunks/export/components/icons.js +260 -0
  34. package/dist/chunks/export/components/icons.js.map +1 -0
  35. package/dist/chunks/export/components/layers.js +1594 -0
  36. package/dist/chunks/export/components/layers.js.map +1 -0
  37. package/dist/chunks/export/index.js +477 -0
  38. package/dist/chunks/export/index.js.map +1 -0
  39. package/dist/chunks/export/storybook/component.js +448 -0
  40. package/dist/chunks/export/storybook/component.js.map +1 -0
  41. package/dist/chunks/export/storybook/export.js +410 -0
  42. package/dist/chunks/export/storybook/export.js.map +1 -0
  43. package/dist/chunks/import/html.js +406 -0
  44. package/dist/chunks/import/html.js.map +1 -0
  45. package/dist/chunks/import/jsx/dev-runtime.js +37 -0
  46. package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
  47. package/dist/chunks/tokens/cssom-validator.js +10 -0
  48. package/dist/chunks/tokens/cssom-validator.js.map +1 -0
  49. package/dist/export.d.ts +594 -0
  50. package/dist/export.js +13 -0
  51. package/dist/index.d.ts +115 -0
  52. package/dist/index.js +7 -0
  53. package/dist/jsx-dev-runtime.d.ts +8 -0
  54. package/dist/jsx-dev-runtime.js +3 -0
  55. package/dist/jsx-runtime.d.ts +20 -0
  56. package/dist/jsx-runtime.js +3 -0
  57. package/dist/rolldown-runtime.js +13 -0
  58. package/dist/runtime.d.ts +10 -0
  59. package/dist/scene-graph.d.ts +2 -0
  60. package/dist/scene-graph.js +2 -0
  61. package/dist/scene-graph2.d.ts +10 -0
  62. package/dist/styling.d.ts +93 -0
  63. package/dist/tailwind.d.ts +39 -0
  64. package/dist/types.d.ts +89 -0
  65. package/package.json +73 -4
  66. package/src/behaviours/args.ts +85 -0
  67. package/src/behaviours/controls.ts +195 -0
  68. package/src/behaviours/index.ts +11 -0
  69. package/src/behaviours/reset.ts +37 -0
  70. package/src/behaviours/roles.ts +44 -0
  71. package/src/behaviours/states/conditions.ts +105 -0
  72. package/src/behaviours/states/css.ts +123 -0
  73. package/src/behaviours/states/index.ts +4 -0
  74. package/src/behaviours/states/layers.ts +205 -0
  75. package/src/behaviours/states/model.ts +175 -0
  76. package/src/behaviours/states/names.ts +39 -0
  77. package/src/behaviours/states/tailwind.ts +148 -0
  78. package/src/behaviours/states/types.ts +56 -0
  79. package/src/browser.ts +146 -0
  80. package/src/cssom.d.ts +38 -0
  81. package/src/export/bundle.ts +461 -0
  82. package/src/export/components/drawn.ts +73 -0
  83. package/src/export/components/fields.ts +178 -0
  84. package/src/export/components/groups.ts +78 -0
  85. package/src/export/components/icons.ts +230 -0
  86. package/src/export/components/layers.ts +121 -0
  87. package/src/export/components/model.ts +544 -0
  88. package/src/export/components/properties.ts +167 -0
  89. package/src/export/components/react/fields.ts +129 -0
  90. package/src/export/components/react/index.ts +606 -0
  91. package/src/export/components/react/shared.ts +76 -0
  92. package/src/export/components/references.ts +212 -0
  93. package/src/export/components/repeats.ts +128 -0
  94. package/src/export/components/shaders.ts +125 -0
  95. package/src/export/components/styling.ts +31 -0
  96. package/src/export/components/tree.ts +72 -0
  97. package/src/export/components/vue.ts +517 -0
  98. package/src/export/css.ts +57 -0
  99. package/src/export/grid.ts +42 -0
  100. package/src/export/html.ts +163 -0
  101. package/src/export/index.ts +44 -0
  102. package/src/export/projection.ts +581 -0
  103. package/src/export/storybook/component.ts +334 -0
  104. package/src/export/storybook/export.ts +430 -0
  105. package/src/export/storybook/groups.ts +161 -0
  106. package/src/export/storybook/module.ts +319 -0
  107. package/src/export/storybook/names.ts +51 -0
  108. package/src/export/tailwind-jsx.ts +156 -0
  109. package/src/export/text.ts +415 -0
  110. package/src/import/css-text.ts +4 -0
  111. package/src/import/css-values.ts +31 -0
  112. package/src/import/flow.ts +248 -0
  113. package/src/import/html.ts +88 -0
  114. package/src/import/jsx/core.ts +130 -0
  115. package/src/import/jsx/dev-runtime.ts +8 -0
  116. package/src/import/jsx/runtime.ts +76 -0
  117. package/src/import/scene-graph.ts +582 -0
  118. package/src/import/style-attribute.ts +25 -0
  119. package/src/import/tailwind.ts +54 -0
  120. package/src/import/text.ts +518 -0
  121. package/src/index.ts +51 -0
  122. package/src/runtime/browser/runtime.ts +301 -0
  123. package/src/runtime/headless/runtime.ts +99 -0
  124. package/src/runtime/headless/styles.ts +330 -0
  125. package/src/runtime/index.ts +11 -0
  126. package/src/tokens/conditions.ts +66 -0
  127. package/src/tokens/cssom-validator.ts +8 -0
  128. package/src/tokens/custom-properties.ts +331 -0
  129. package/src/tokens/expression.ts +128 -0
  130. package/src/tokens/formula.ts +140 -0
  131. package/src/tokens/index.ts +43 -0
  132. package/src/tokens/names.ts +161 -0
  133. package/src/tokens/references.ts +201 -0
  134. package/src/tokens/stylesheet.ts +304 -0
  135. package/src/tokens/validate.ts +86 -0
  136. package/src/tokens/values.ts +44 -0
  137. package/src/types.ts +59 -0
@@ -0,0 +1,334 @@
1
+ import dedent from 'dedent'
2
+ import { compact } from 'es-toolkit/array'
3
+
4
+ import { es } from '@pairdraw/emit'
5
+
6
+ import type {
7
+ ComponentModel,
8
+ ComponentNode,
9
+ GeneratedComponent,
10
+ GeneratedKind
11
+ } from '../components/model'
12
+ import { stylesheets } from './module'
13
+ import { claimName, identifierName, VARIANT_TAG } from './names'
14
+
15
+ /** Tells readers the file is generated; nothing parses it. */
16
+ const HEADER = '// Generated by Pairdraw. Re-export to update; edits are overwritten.'
17
+
18
+ const MODULE = es.parseModule(dedent`
19
+ import type { Meta, StoryObj } from '$storybook'
20
+ import { expect } from 'storybook/test'
21
+
22
+ const meta = {
23
+ title: $title,
24
+ tags: $tags,
25
+ component: $Component,
26
+ parameters: $parameters,
27
+ args: $args,
28
+ argTypes: $argTypes
29
+ } satisfies Meta<typeof $Component>
30
+
31
+ export default meta
32
+ type Story = StoryObj<typeof meta>
33
+ `)
34
+
35
+ const IMPORT_DEFAULT = es.parseModule(`import $Component from '$path'`)
36
+ const IMPORT_NAMED = es.parseModule(`import { $Component } from '$path'`)
37
+
38
+ const STORY = es.parseModule(`export const $name: Story = $story`)
39
+
40
+ /**
41
+ * What clicking a control does, as Reka marks it: the role to find it by, the element that
42
+ * changes, and the `data-state` it ends in.
43
+ */
44
+ const INTERACTIONS: Partial<Record<GeneratedKind, { role: string; state: string }>> = {
45
+ switch: { role: 'switch', state: 'checked' },
46
+ checkbox: { role: 'checkbox', state: 'checked' },
47
+ toggle: { role: 'button', state: 'on' },
48
+ collapsible: { role: 'button', state: 'open' }
49
+ }
50
+
51
+ const PLAY = es.parseExpression(dedent`
52
+ async ({ canvas, userEvent }) => {
53
+ const control = canvas.getByRole($role)
54
+ await userEvent.click(control)
55
+ await expect(control).toHaveAttribute('data-state', $state)
56
+ }
57
+ `)
58
+
59
+ /** A slider's Home key moves it to its minimum, as Reka and Radix both handle it. */
60
+ const SLIDER_PLAY = es.parseExpression(dedent`
61
+ async ({ canvas, userEvent }) => {
62
+ const thumb = canvas.getByRole('slider')
63
+ thumb.focus()
64
+ await userEvent.keyboard('{Home}')
65
+ await expect(thumb).toHaveAttribute('aria-valuenow', $min)
66
+ }
67
+ `)
68
+
69
+ /**
70
+ * A stepper steps a number field. Both Reka's and the native field's steppers are named by
71
+ * what they do, where a key would only step Reka's input, not a native number input.
72
+ */
73
+ const NUMBER_PLAY = es.parseExpression(dedent`
74
+ async ({ canvas, userEvent }) => {
75
+ await userEvent.click(canvas.getByRole('button', { name: $stepper }))
76
+ await expect(canvas.getByRole('spinbutton')).toHaveDisplayValue($value)
77
+ }
78
+ `)
79
+
80
+ /** Whether the component draws a part, such as a number field's increment. */
81
+ function drawsPart(node: ComponentNode, part: string): boolean {
82
+ return (
83
+ node.type === 'element' &&
84
+ (node.part === part || node.children.some((child) => drawsPart(child, part)))
85
+ )
86
+ }
87
+
88
+ /** Typing replaces a text field's words. */
89
+ const TEXT_PLAY = es.parseExpression(dedent`
90
+ async ({ canvas, userEvent }) => {
91
+ const field = canvas.getByRole('textbox')
92
+ await userEvent.clear(field)
93
+ await userEvent.type(field, 'Hello')
94
+ await expect(field).toHaveValue('Hello')
95
+ }
96
+ `)
97
+
98
+ /** The play function that operates a generated component, if it has one. */
99
+ function playOf(component: ComponentModel): es.SyntaxNode | undefined {
100
+ const interaction = INTERACTIONS[component.kind]
101
+ if (interaction)
102
+ return es.fill(PLAY, {
103
+ $role: es.string(interaction.role),
104
+ $state: es.string(interaction.state)
105
+ })
106
+ const { range } = component
107
+ if (component.kind === 'slider' && range)
108
+ return es.fill(SLIDER_PLAY, { $min: es.string(String(range.min)) })
109
+ if (component.kind === 'numberField' && range) {
110
+ const up = range.default + range.step <= range.max
111
+ const part = up ? 'increment' : 'decrement'
112
+ if (!drawsPart(component.tree, part)) return undefined
113
+ return es.fill(NUMBER_PLAY, {
114
+ $stepper: es.string(up ? 'Increase' : 'Decrease'),
115
+ $value: es.string(String(up ? range.default + range.step : range.default - range.step))
116
+ })
117
+ }
118
+ if (component.kind === 'textField' || component.kind === 'textarea') return TEXT_PLAY
119
+ return undefined
120
+ }
121
+
122
+ export interface ComponentStoriesData {
123
+ /** The Storybook renderer package the stories import their types from. */
124
+ storybook: string
125
+ title: string
126
+ /** Tags on the whole file, which its stories inherit. */
127
+ tags: string[]
128
+ /** Stylesheets the file imports, such as the fonts its text uses. */
129
+ styles: string[]
130
+ /** Only the Default story, as a plan can ask for. */
131
+ single?: boolean
132
+ component: ComponentModel
133
+ generated: Pick<GeneratedComponent, 'entry' | 'valueArg' | 'valueList'>
134
+ /** Design links for the whole file, as `parameters.design` entries. */
135
+ design: { name: string; type: string; url: string }[]
136
+ }
137
+
138
+ const FALSE = es.parseExpression('false')
139
+ const TRUE = es.parseExpression('true')
140
+
141
+ /** A number model's value as its prop takes it: on its own, or in a list for a slider. */
142
+ const numberArg = (value: number, list: boolean) =>
143
+ list ? es.array([es.number(value)]) : es.number(value)
144
+
145
+ /**
146
+ * A model's value at rest: off, the option a choice starts on, none for an open choice, the
147
+ * number a range starts at, or the words a field starts with.
148
+ */
149
+ function restValue(component: ComponentModel, list: boolean): es.SyntaxNode | null {
150
+ if (component.range) return numberArg(component.range.default, list)
151
+ if (component.text) return es.string(component.text.default)
152
+ if (!component.choice) return FALSE
153
+ return component.choice.default === null ? null : es.string(component.choice.default)
154
+ }
155
+
156
+ function restArgs(
157
+ component: ComponentModel,
158
+ model: string | null,
159
+ list: boolean
160
+ ): [string, es.SyntaxNode][] {
161
+ const rest = restValue(component, list)
162
+ return [
163
+ ...(model && rest ? [[model, rest] as [string, es.SyntaxNode]] : []),
164
+ ...(component.disabled ? [['disabled', FALSE] as [string, es.SyntaxNode]] : []),
165
+ ...component.props.map((prop): [string, es.SyntaxNode] => [prop.name, es.string(prop.default)]),
166
+ ...component.texts.map((text): [string, es.SyntaxNode] => [text.name, es.string(text.default)]),
167
+ ...component.booleans.map((prop): [string, es.SyntaxNode] => [
168
+ prop.name,
169
+ prop.default ? TRUE : FALSE
170
+ ])
171
+ ]
172
+ }
173
+
174
+ /** The control a model's value gets: a choice's options, a range, text, or a boolean. */
175
+ function modelControl(component: ComponentModel, list: boolean): es.SyntaxNode {
176
+ const control = (type: string) => es.object([['control', es.string(type)]])
177
+ const { choice, range } = component
178
+ if (choice)
179
+ return es.object([
180
+ ['control', es.string('select')],
181
+ ['options', es.array(choice.options.map(es.string))]
182
+ ])
183
+ // A slider's list of values is edited as one, not as a range.
184
+ if (range && list) return control('object')
185
+ if (range)
186
+ return es.object([
187
+ [
188
+ 'control',
189
+ es.object([
190
+ ['type', es.string('range')],
191
+ ['min', es.number(range.min)],
192
+ ['max', es.number(range.max)],
193
+ ['step', es.number(range.step)]
194
+ ])
195
+ ]
196
+ ])
197
+ return control(component.text ? 'text' : 'boolean')
198
+ }
199
+
200
+ function argTypes(component: ComponentModel, model: string | null, list: boolean): es.SyntaxNode {
201
+ const boolean = es.object([['control', es.string('boolean')]])
202
+ const select = (options: readonly string[]) =>
203
+ es.object([
204
+ ['control', es.string('select')],
205
+ ['options', es.array(options.map(es.string))]
206
+ ])
207
+ return es.object([
208
+ ...(model ? [[model, modelControl(component, list)] as [string, es.SyntaxNode]] : []),
209
+ ...(component.disabled ? [['disabled', boolean] as [string, es.SyntaxNode]] : []),
210
+ ...component.props.map((prop): [string, es.SyntaxNode] => [prop.name, select(prop.options)]),
211
+ ...component.texts.map((text): [string, es.SyntaxNode] => [
212
+ text.name,
213
+ es.object([['control', es.string('text')]])
214
+ ]),
215
+ ...component.booleans.map((prop): [string, es.SyntaxNode] => [prop.name, boolean])
216
+ ])
217
+ }
218
+
219
+ /** A story per state worth seeing: at rest, each boolean on, disabled, and each prop value. */
220
+ function stories(
221
+ component: ComponentModel,
222
+ model: string | null,
223
+ list: boolean
224
+ ): { name: string; story: es.SyntaxNode }[] {
225
+ // Stories are named apart from the component the file imports and the module's own names.
226
+ const taken = new Set([component.name, 'meta', 'Story'])
227
+ let first = true
228
+ const story = (label: string, args: [string, es.SyntaxNode][], play?: es.SyntaxNode) => {
229
+ // Every story after the first, Default, is another state, tagged so Storybook can hide it.
230
+ const variant = !first
231
+ first = false
232
+ return {
233
+ name: claimName(identifierName(label, 'Story'), taken),
234
+ story: es.object([
235
+ ...(variant
236
+ ? [['tags', es.array([es.string(VARIANT_TAG)])] as [string, es.SyntaxNode]]
237
+ : []),
238
+ ...(args.length > 0 ? [['args', es.object(args)] as [string, es.SyntaxNode]] : []),
239
+ ...(play ? [['play', play] as [string, es.SyntaxNode]] : [])
240
+ ])
241
+ }
242
+ }
243
+ const { range } = component
244
+ const boolean = !component.choice && !range && !component.text
245
+ return [
246
+ story('Default', [], playOf(component)),
247
+ ...(component.model && model && boolean ? [story(component.model, [[model, TRUE]])] : []),
248
+ // A range gets a story at each end it does not start at.
249
+ ...(range && model
250
+ ? (
251
+ [
252
+ ['Minimum', range.min],
253
+ ['Maximum', range.max]
254
+ ] as const
255
+ )
256
+ .filter(([, value]) => value !== range.default)
257
+ .map(([label, value]) => story(label, [[model, numberArg(value, list)]]))
258
+ : []),
259
+ // A choice gets a story per option it does not start on, such as each other tab.
260
+ ...(component.choice && model
261
+ ? component.choice.options
262
+ .filter((option) => option !== component.choice?.default)
263
+ .map((option) => story(option, [[model, es.string(option)]]))
264
+ : []),
265
+ ...(component.disabled ? [story('Disabled', [['disabled', TRUE]])] : []),
266
+ ...component.props.flatMap((prop) =>
267
+ prop.options
268
+ .filter((option) => option !== prop.default)
269
+ .map((option) => story(option, [[prop.name, es.string(option)]]))
270
+ ),
271
+ // A boolean gets a story drawn the other way, such as an icon shown that is hidden at rest.
272
+ ...component.booleans.map((prop) =>
273
+ story(`${prop.property} ${prop.default ? 'off' : 'on'}`, [
274
+ [prop.name, prop.default ? FALSE : TRUE]
275
+ ])
276
+ )
277
+ ]
278
+ }
279
+
280
+ /** CSF3 stories for a generated component, with a play function that operates it. */
281
+ export function printComponentStories(data: ComponentStoriesData): string {
282
+ const { component } = data
283
+ const parameters =
284
+ data.design.length > 0
285
+ ? es.object([
286
+ [
287
+ 'design',
288
+ es.array(
289
+ data.design.map((entry) =>
290
+ es.object([
291
+ ['name', es.string(entry.name)],
292
+ ['type', es.string(entry.type)],
293
+ ['url', es.string(entry.url)]
294
+ ])
295
+ )
296
+ )
297
+ ]
298
+ ])
299
+ : es.OMIT
300
+ const { entry, valueArg: model } = data.generated
301
+ const list = data.generated.valueList ?? false
302
+ const body = es.fill(MODULE, {
303
+ $storybook: es.string(data.storybook),
304
+ $Component: es.identifier(component.name),
305
+ $title: es.string(data.title),
306
+ $tags: es.array(data.tags.map(es.string)),
307
+ $parameters: parameters,
308
+ $args: es.object(restArgs(component, model, list)),
309
+ $argTypes: argTypes(component, model, list)
310
+ }).body
311
+ const componentImport = es.fill(entry.named ? IMPORT_NAMED : IMPORT_DEFAULT, {
312
+ $Component: es.identifier(component.name),
313
+ $path: es.string(entry.path)
314
+ }).body
315
+ const all = stories(component, model, list)
316
+ const exported = (data.single ? all.slice(0, 1) : all).flatMap(
317
+ (item) => es.fill(STORY, { $name: es.identifier(item.name), $story: item.story }).body
318
+ )
319
+ // Only a control with a play function uses `expect`.
320
+ const [types, expectImport, ...rest] = body
321
+ const imports = playOf(component) ? [types, expectImport] : [types]
322
+ const module = es.printModule({
323
+ type: 'Program',
324
+ sourceType: 'module',
325
+ body: compact([
326
+ ...imports,
327
+ ...stylesheets(data.styles),
328
+ ...componentImport,
329
+ ...rest,
330
+ ...exported
331
+ ])
332
+ })
333
+ return `${HEADER}\n${module}\n`
334
+ }