@pairdraw/dom-css 0.0.0-stage → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) 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 +1599 -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 +88 -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 +412 -0
  91. package/src/export/components/react/props.ts +151 -0
  92. package/src/export/components/react/radix.ts +52 -0
  93. package/src/export/components/react/shared.ts +76 -0
  94. package/src/export/components/references.ts +212 -0
  95. package/src/export/components/repeats.ts +128 -0
  96. package/src/export/components/shaders.ts +125 -0
  97. package/src/export/components/styling.ts +31 -0
  98. package/src/export/components/tree.ts +72 -0
  99. package/src/export/components/vue.ts +517 -0
  100. package/src/export/css.ts +57 -0
  101. package/src/export/grid.ts +42 -0
  102. package/src/export/html.ts +163 -0
  103. package/src/export/index.ts +44 -0
  104. package/src/export/projection.ts +581 -0
  105. package/src/export/storybook/component.ts +334 -0
  106. package/src/export/storybook/export.ts +430 -0
  107. package/src/export/storybook/groups.ts +161 -0
  108. package/src/export/storybook/module.ts +319 -0
  109. package/src/export/storybook/names.ts +51 -0
  110. package/src/export/tailwind-jsx.ts +156 -0
  111. package/src/export/text.ts +415 -0
  112. package/src/import/css-text.ts +4 -0
  113. package/src/import/css-values.ts +31 -0
  114. package/src/import/flow.ts +248 -0
  115. package/src/import/html.ts +88 -0
  116. package/src/import/jsx/core.ts +130 -0
  117. package/src/import/jsx/dev-runtime.ts +8 -0
  118. package/src/import/jsx/runtime.ts +76 -0
  119. package/src/import/scene-graph.ts +582 -0
  120. package/src/import/style-attribute.ts +25 -0
  121. package/src/import/tailwind.ts +54 -0
  122. package/src/import/text.ts +518 -0
  123. package/src/index.ts +51 -0
  124. package/src/runtime/browser/runtime.ts +301 -0
  125. package/src/runtime/headless/runtime.ts +99 -0
  126. package/src/runtime/headless/styles.ts +330 -0
  127. package/src/runtime/index.ts +11 -0
  128. package/src/tokens/conditions.ts +66 -0
  129. package/src/tokens/cssom-validator.ts +8 -0
  130. package/src/tokens/custom-properties.ts +331 -0
  131. package/src/tokens/expression.ts +128 -0
  132. package/src/tokens/formula.ts +140 -0
  133. package/src/tokens/index.ts +43 -0
  134. package/src/tokens/names.ts +161 -0
  135. package/src/tokens/references.ts +201 -0
  136. package/src/tokens/stylesheet.ts +304 -0
  137. package/src/tokens/validate.ts +86 -0
  138. package/src/tokens/values.ts +44 -0
  139. package/src/types.ts +59 -0
@@ -0,0 +1,167 @@
1
+ import { ownerVariants } from '#dom-css/behaviours/states/model'
2
+ import type { StateElement } from '#dom-css/behaviours/states/types'
3
+ import { camelCase } from 'es-toolkit/string'
4
+
5
+ import type { ComponentPropertyReferenceField, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
6
+ import { behaviourProperties, findLayerByPath } from '@pairdraw/scene-graph/behaviours'
7
+
8
+ import { claimName, identifierName } from '../storybook/names'
9
+
10
+ /** A text property the component takes as a string prop and draws in the layers bound to it. */
11
+ export interface TextProp {
12
+ /** The prop's identifier. */
13
+ name: string
14
+ /** The text property's id, which instances assign values by. */
15
+ id: string
16
+ /** The text property's name. */
17
+ property: string
18
+ default: string
19
+ }
20
+
21
+ /** A boolean property the component takes as a prop, showing the layers bound to it while on. */
22
+ export interface BooleanProp {
23
+ /** The prop's identifier. */
24
+ name: string
25
+ /** The boolean property's id, which instances assign values by. */
26
+ id: string
27
+ /** The boolean property's name. */
28
+ property: string
29
+ default: boolean
30
+ }
31
+
32
+ /**
33
+ * A slot property the component takes as content in place of the design's, which it shows
34
+ * when none is given.
35
+ */
36
+ export interface SlotProp {
37
+ /** The prop's identifier, and the slot's name in Vue. */
38
+ name: string
39
+ /** The slot property's id. */
40
+ id: string
41
+ /** The slot property's name. */
42
+ property: string
43
+ }
44
+
45
+ /**
46
+ * The layers bound to a property through `field`, each with the prop it draws. A layer is
47
+ * bound when it is in any variant, found by the path every variant shares.
48
+ */
49
+ function boundLayers(
50
+ graph: SceneGraph,
51
+ set: SceneNode,
52
+ elements: readonly StateElement[],
53
+ field: ComponentPropertyReferenceField,
54
+ props: ReadonlyMap<string, string>
55
+ ): Map<StateElement, string> {
56
+ const variants = ownerVariants(graph, set)
57
+ const bound = new Map<StateElement, string>()
58
+ for (const element of elements) {
59
+ const path = element.key.split('\0')[0] ?? ''
60
+ for (const variant of variants) {
61
+ const reference = findLayerByPath(graph, variant.id, path)?.componentPropertyReferences.find(
62
+ (item) => item.field === field
63
+ )
64
+ const prop = reference && props.get(reference.propertyId)
65
+ if (prop) {
66
+ bound.set(element, prop)
67
+ break
68
+ }
69
+ }
70
+ }
71
+ return bound
72
+ }
73
+
74
+ const propName = (property: string, taken: Set<string>, fallback: string) =>
75
+ claimName(camelCase(identifierName(property, fallback)), taken)
76
+
77
+ /** The set's boolean properties as props, and the layers each shows while it is on. */
78
+ export function booleanProps(
79
+ graph: SceneGraph,
80
+ set: SceneNode,
81
+ elements: readonly StateElement[],
82
+ taken: Set<string>
83
+ ): { booleans: BooleanProp[]; shownBy: Map<StateElement, string> } {
84
+ const booleans = behaviourProperties(graph, set)
85
+ .filter((definition) => definition.type === 'BOOLEAN')
86
+ .map((definition) => ({
87
+ name: propName(definition.name, taken, 'Shown'),
88
+ id: definition.id,
89
+ property: definition.name,
90
+ default: definition.defaultValue === 'true'
91
+ }))
92
+ const byId = new Map(booleans.map((prop) => [prop.id, prop.name]))
93
+ return { booleans, shownBy: boundLayers(graph, set, elements, 'VISIBLE', byId) }
94
+ }
95
+
96
+ /**
97
+ * The set's slot properties as props, and the frame each fills, except frames a behaviour
98
+ * draws its parts in, such as a switch's thumb, which stay its parts.
99
+ */
100
+ export function slotProps(
101
+ graph: SceneGraph,
102
+ set: SceneNode,
103
+ elements: readonly StateElement[],
104
+ taken: Set<string>,
105
+ parts: ReadonlyMap<StateElement, string>
106
+ ): { slots: SlotProp[]; slotOf: Map<StateElement, string> } {
107
+ const definitions = behaviourProperties(graph, set).filter(
108
+ (definition) => definition.type === 'SLOT'
109
+ )
110
+ const frames = elements.filter((element) => !parts.has(element))
111
+ const slots = definitions.map((definition) => ({
112
+ name: propName(definition.name, taken, 'Content'),
113
+ id: definition.id,
114
+ property: definition.name
115
+ }))
116
+ const byId = new Map(slots.map((prop) => [prop.id, prop.name]))
117
+ const slotOf = boundLayers(graph, set, frames, 'SLOT_CONTENT', byId)
118
+ // A slot no layer draws has nowhere to show its content, so it is no prop.
119
+ const drawn = new Set(slotOf.values())
120
+ return { slots: slots.filter((slot) => drawn.has(slot.name)), slotOf }
121
+ }
122
+
123
+ /**
124
+ * The set's text properties as props, and the layers bound to each. A layer is bound when it
125
+ * is in any variant, found by the path every variant shares.
126
+ */
127
+ export function textProps(
128
+ graph: SceneGraph,
129
+ set: SceneNode,
130
+ elements: readonly StateElement[],
131
+ taken: Set<string>,
132
+ /** A field's text property, which its input shows rather than a prop. */
133
+ input: string | undefined
134
+ ): { texts: TextProp[]; bound: Map<StateElement, string>; input: StateElement[] } {
135
+ const definitions = behaviourProperties(graph, set).filter(
136
+ (item) => item.type === 'TEXT' && item.id !== input
137
+ )
138
+ const texts = definitions.map((definition) => ({
139
+ name: claimName(camelCase(identifierName(definition.name, 'Text')), taken),
140
+ id: definition.id,
141
+ property: definition.name,
142
+ default: definition.defaultValue
143
+ }))
144
+ const byId = new Map(definitions.map((definition, i) => [definition.id, texts[i]]))
145
+ const variants = ownerVariants(graph, set)
146
+ const bound = new Map<StateElement, string>()
147
+ const inputs: StateElement[] = []
148
+ for (const element of elements) {
149
+ // A key ends with the words a layer reads, which a text prop draws in every variant.
150
+ const path = element.key.split('\0')[0] ?? ''
151
+ for (const variant of variants) {
152
+ const reference = findLayerByPath(graph, variant.id, path)?.componentPropertyReferences.find(
153
+ (item) => item.field === 'TEXT'
154
+ )
155
+ if (reference && reference.propertyId === input) {
156
+ inputs.push(element)
157
+ break
158
+ }
159
+ const prop = reference && byId.get(reference.propertyId)
160
+ if (prop) {
161
+ bound.set(element, prop.name)
162
+ break
163
+ }
164
+ }
165
+ }
166
+ return { texts, bound, input: inputs }
167
+ }
@@ -0,0 +1,129 @@
1
+ import dedent from 'dedent'
2
+
3
+ import { es, jsx } from '@pairdraw/emit'
4
+
5
+ import { progressWidth } from '../fields'
6
+ import type { ComponentElement, ComponentModel, GeneratedKind } from '../model'
7
+ import { classValue, numeric, type MarkupUses } from './shared'
8
+
9
+ /** Kinds whose native input takes the caller's props, rather than their root. */
10
+ export const INPUT_PROPS: Partial<Record<GeneratedKind, 'input' | 'textarea'>> = {
11
+ textField: 'input',
12
+ textarea: 'textarea'
13
+ }
14
+
15
+ /** A number field's stepper parts, which change its value by a step. */
16
+ export const STEPPERS: Record<string, { label: string; sign: '+' | '-' }> = {
17
+ increment: { label: 'Increase', sign: '+' },
18
+ decrement: { label: 'Decrease', sign: '-' }
19
+ }
20
+
21
+ /** What a part of a native or measured control needs: a stepper's step, an indicator's width. */
22
+ export function partAttributes(node: ComponentElement, uses: MarkupUses): es.SyntaxNode[] {
23
+ const { range } = uses.component
24
+ if (!range || !node.part) return []
25
+ if (uses.kind === 'progress' && node.part === 'indicator') {
26
+ const style = progressWidth(range, es.identifier('value'))
27
+ return style ? [jsx.attribute('style', jsx.container(style))] : []
28
+ }
29
+ const stepper = uses.kind === 'numberField' ? STEPPERS[node.part] : undefined
30
+ if (!stepper) return []
31
+ return [
32
+ jsx.attribute('aria-label', jsx.stringValue(stepper.label)),
33
+ jsx.attribute('disabled', jsx.container(es.identifier('disabled'))),
34
+ jsx.attribute(
35
+ 'onClick',
36
+ jsx.container(es.parseExpression(`() => commit(value ${stepper.sign} ${String(range.step)})`))
37
+ )
38
+ ]
39
+ }
40
+
41
+ const NUMBER_INPUT = [
42
+ ['type', jsx.stringValue('number')],
43
+ ['value', jsx.container(es.parseExpression("Number.isNaN(value) ? '' : value"))],
44
+ [
45
+ 'onChange',
46
+ jsx.container(es.parseExpression('(event) => setValue(event.target.valueAsNumber)'))
47
+ ],
48
+ ['onBlur', jsx.container(es.parseExpression('() => commit(value)'))]
49
+ ] as const
50
+
51
+ /** A field's input: a number input that commits within its range, or the input or textarea. */
52
+ export function inputElement(className: string, uses: MarkupUses, depth: number): es.SyntaxNode {
53
+ const { component } = uses
54
+ const classAttribute = jsx.attribute('className', classValue(uses.classOf(className)))
55
+ const { range } = component
56
+ if (range)
57
+ return jsx.element(
58
+ 'input',
59
+ [
60
+ ...NUMBER_INPUT.map(([name, value]) => jsx.attribute(name, value)),
61
+ numeric('min', range.min),
62
+ numeric('max', range.max),
63
+ numeric('step', range.step),
64
+ jsx.attribute('disabled', jsx.container(es.identifier('disabled'))),
65
+ classAttribute
66
+ ],
67
+ [],
68
+ depth
69
+ )
70
+ const start = component.text?.default
71
+ const placeholder = component.text?.placeholder
72
+ return jsx.element(
73
+ INPUT_PROPS[uses.kind] ?? 'input',
74
+ [
75
+ ...(placeholder ? [jsx.attribute('placeholder', jsx.stringValue(placeholder))] : []),
76
+ ...(start ? [jsx.attribute('defaultValue', jsx.stringValue(start))] : []),
77
+ // The input takes the caller's props, such as `name` or `onChange`.
78
+ jsx.spread(es.identifier('props')),
79
+ classAttribute
80
+ ],
81
+ [],
82
+ depth
83
+ )
84
+ }
85
+
86
+ /** A destructured prop, with the default it takes when the caller leaves it out. */
87
+ export interface Parameter {
88
+ name: string
89
+ default?: es.SyntaxNode
90
+ }
91
+
92
+ /** Props a kind reads itself: a progress bar's value, a number field's start and changes. */
93
+ export function kindParameters(component: ComponentModel): Parameter[] {
94
+ const { range } = component
95
+ if (component.kind === 'progress' && range)
96
+ return [{ name: 'value', default: es.number(range.default) }]
97
+ if (component.kind === 'numberField' && range)
98
+ return [
99
+ { name: 'defaultValue', default: es.number(range.default) },
100
+ { name: 'onValueChange' },
101
+ { name: 'disabled' }
102
+ ]
103
+ return []
104
+ }
105
+
106
+ /**
107
+ * A number field's value: typed freely, and settled within its range when it loses focus or a
108
+ * stepper moves it, which is when the caller hears of it.
109
+ */
110
+ export const NUMBER_STATE = (range: { min: number; max: number }) =>
111
+ es.fill(
112
+ es.parseModule(dedent`
113
+ const [value, setValue] = useState(defaultValue)
114
+ const commit = (next: number) => {
115
+ const settled = Number.isNaN(next) ? $min : Math.min($max, Math.max($min, next))
116
+ setValue(settled)
117
+ onValueChange?.(settled)
118
+ }
119
+ `),
120
+ { $min: es.number(range.min), $max: es.number(range.max) }
121
+ ).body
122
+
123
+ /** The component function's statements before it returns, such as a number field's state. */
124
+ export function prepend(declaration: es.SyntaxNode | undefined, statements: es.SyntaxNode[]): void {
125
+ const fn = es.child(declaration, 'declaration')
126
+ const body = es.child(fn, 'body')
127
+ if (!body || statements.length === 0) return
128
+ body.body = [...statements, ...es.children(body, 'body')]
129
+ }