@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,205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sceneNodeToDesignDocument,
|
|
3
|
+
type SceneGraphToDesignOptions
|
|
4
|
+
} from '#dom-css/export/projection'
|
|
5
|
+
import type { DesignElement, DesignNode, DesignText } from '#dom-css/types'
|
|
6
|
+
import { omit } from 'es-toolkit/object'
|
|
7
|
+
|
|
8
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
9
|
+
import {
|
|
10
|
+
behaviourOwner,
|
|
11
|
+
layerPath,
|
|
12
|
+
partBinding,
|
|
13
|
+
readBehaviour
|
|
14
|
+
} from '@pairdraw/scene-graph/behaviours'
|
|
15
|
+
import { readIcon } from '@pairdraw/scene-graph/icons'
|
|
16
|
+
import { instanceMainComponent } from '@pairdraw/scene-graph/instances'
|
|
17
|
+
import { instanceSlotFrames, slotPropertyId } from '@pairdraw/scene-graph/slots'
|
|
18
|
+
|
|
19
|
+
import type { StateElement } from './types'
|
|
20
|
+
|
|
21
|
+
/** A variant's projected layer, keyed so the same layer matches across variants. */
|
|
22
|
+
export interface VariantLayer {
|
|
23
|
+
type: 'element'
|
|
24
|
+
key: string
|
|
25
|
+
kind: string | undefined
|
|
26
|
+
name: string
|
|
27
|
+
element: DesignElement
|
|
28
|
+
children: Array<VariantLayer | DesignText>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Where the set places a variant, which is not the component's own style. */
|
|
32
|
+
export const PLACEMENT = ['position', 'left', 'top', 'right', 'bottom', 'inset']
|
|
33
|
+
|
|
34
|
+
function textContent(element: DesignElement): string | null {
|
|
35
|
+
const texts = element.children.filter((child): child is DesignText => child.type === 'text')
|
|
36
|
+
return texts.length > 0 ? texts.map((text) => text.text).join('') : null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* What a layer draws other than its own box: an instance of a component (of any of its set's
|
|
41
|
+
* variants), or an icon. Undefined for any other layer.
|
|
42
|
+
*/
|
|
43
|
+
export function layerKind(graph: SceneGraph, node: SceneNode | undefined): string | undefined {
|
|
44
|
+
if (!node) return undefined
|
|
45
|
+
if (node.type === 'INSTANCE') {
|
|
46
|
+
const main = instanceMainComponent(graph, node)
|
|
47
|
+
const owner = main && behaviourOwner(graph, main)
|
|
48
|
+
return `instance ${owner?.id ?? ''}`
|
|
49
|
+
}
|
|
50
|
+
const icon = readIcon(node)
|
|
51
|
+
return icon ? `icon ${icon.name}` : undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function variantLayer(
|
|
55
|
+
graph: SceneGraph,
|
|
56
|
+
variant: SceneNode,
|
|
57
|
+
node: DesignNode,
|
|
58
|
+
parentKey: string,
|
|
59
|
+
index: number
|
|
60
|
+
): VariantLayer | DesignText {
|
|
61
|
+
if (node.type === 'text') return node
|
|
62
|
+
const source = node.sourceSceneNodeId
|
|
63
|
+
const path = source ? layerPath(graph, variant.id, source) : `${parentKey}/~${index}`
|
|
64
|
+
const text = textContent(node)
|
|
65
|
+
const kind = layerKind(graph, node.sourceSceneNode)
|
|
66
|
+
// A label that reads differently in a variant is a different layer, shown by its state, and
|
|
67
|
+
// so is a layer drawn as another component or icon, or as one instead of its own box.
|
|
68
|
+
const key = [path, ...(kind ? [kind] : []), ...(text === null ? [] : [text])].join('\0')
|
|
69
|
+
return {
|
|
70
|
+
type: 'element',
|
|
71
|
+
key,
|
|
72
|
+
kind,
|
|
73
|
+
name: node.sourceSceneNode?.name ?? node.tagName,
|
|
74
|
+
element: node,
|
|
75
|
+
children: node.children.map((child, i) => variantLayer(graph, variant, child, key, i))
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The tab panels a variant draws hidden behind the first. The generated tabs show each when its
|
|
81
|
+
* tab is chosen, so they are projected as drawn.
|
|
82
|
+
*/
|
|
83
|
+
function tabPanels(graph: SceneGraph, variant: SceneNode): Set<string> {
|
|
84
|
+
const owner = behaviourOwner(graph, variant)
|
|
85
|
+
const behaviour = owner && readBehaviour(owner)
|
|
86
|
+
const propertyId = behaviour?.kind === 'tabs' ? partBinding(behaviour, 'panels') : undefined
|
|
87
|
+
const panels = propertyId
|
|
88
|
+
? instanceSlotFrames(graph, variant).find((frame) => slotPropertyId(frame) === propertyId)
|
|
89
|
+
: undefined
|
|
90
|
+
return new Set(panels?.childIds)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The layers of a variant a boolean property shows or hides, which are projected however the
|
|
95
|
+
* variant draws them, since the generated component shows them from its prop.
|
|
96
|
+
*/
|
|
97
|
+
function visibilityBound(graph: SceneGraph, variant: SceneNode): Set<string> {
|
|
98
|
+
const bound = new Set<string>()
|
|
99
|
+
const visit = (node: SceneNode) => {
|
|
100
|
+
for (const child of graph.getChildren(node.id)) {
|
|
101
|
+
if (child.componentPropertyReferences.some((item) => item.field === 'VISIBLE'))
|
|
102
|
+
bound.add(child.id)
|
|
103
|
+
if (child.type !== 'INSTANCE') visit(child)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
visit(variant)
|
|
107
|
+
return bound
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A variant projected to DOM, without where the set places it. */
|
|
111
|
+
export function projectVariant(
|
|
112
|
+
graph: SceneGraph,
|
|
113
|
+
variant: SceneNode,
|
|
114
|
+
{ vectorElement }: Pick<SceneGraphToDesignOptions, 'vectorElement'> = {}
|
|
115
|
+
): VariantLayer | null {
|
|
116
|
+
const document = sceneNodeToDesignDocument(graph, variant.id, {
|
|
117
|
+
includeSourceIds: false,
|
|
118
|
+
vectorElement,
|
|
119
|
+
shown: new Set([...tabPanels(graph, variant), ...visibilityBound(graph, variant)])
|
|
120
|
+
})
|
|
121
|
+
const root = document.children.at(0)
|
|
122
|
+
if (root?.type !== 'element') return null
|
|
123
|
+
const style = root.inlineStyle ?? {}
|
|
124
|
+
// Absolutely placed layers inside still need the root as their containing block.
|
|
125
|
+
root.inlineStyle = {
|
|
126
|
+
...omit(style, PLACEMENT),
|
|
127
|
+
...(style.position ? { position: 'relative' } : {})
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
type: 'element',
|
|
131
|
+
key: '',
|
|
132
|
+
kind: undefined,
|
|
133
|
+
name: variant.name,
|
|
134
|
+
element: root,
|
|
135
|
+
children: root.children.map((child, i) => variantLayer(graph, variant, child, '', i))
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Every layer of a projected variant by key. */
|
|
140
|
+
export function layersByKey(
|
|
141
|
+
root: VariantLayer,
|
|
142
|
+
into = new Map<string, VariantLayer>()
|
|
143
|
+
): Map<string, VariantLayer> {
|
|
144
|
+
into.set(root.key, root)
|
|
145
|
+
for (const child of root.children) if (child.type === 'element') layersByKey(child, into)
|
|
146
|
+
return into
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A merged layer starting from a variant's layer, with that layer's style as its base. */
|
|
150
|
+
export function stateElement(layer: VariantLayer): StateElement {
|
|
151
|
+
return {
|
|
152
|
+
type: 'element',
|
|
153
|
+
key: layer.key,
|
|
154
|
+
kind: layer.kind,
|
|
155
|
+
name: layer.name,
|
|
156
|
+
tagName: layer.element.tagName,
|
|
157
|
+
attrs: { ...layer.element.attrs },
|
|
158
|
+
base: { ...layer.element.inlineStyle },
|
|
159
|
+
rules: [],
|
|
160
|
+
shader: layer.element.shader,
|
|
161
|
+
children: layer.children.map((child) => (child.type === 'text' ? child : stateElement(child)))
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function hideAtRest(element: StateElement, hidden: Set<StateElement>): void {
|
|
166
|
+
element.base = { ...element.base, display: 'none' }
|
|
167
|
+
hidden.add(element)
|
|
168
|
+
for (const child of element.children) if (child.type === 'element') hideAtRest(child, hidden)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Adds the layers only `variant` draws to the merged tree, after the sibling they follow
|
|
173
|
+
* there. Each keeps the style this variant draws it with, hidden at rest, and is recorded in
|
|
174
|
+
* `hidden` so the variants that draw it show it again.
|
|
175
|
+
*/
|
|
176
|
+
export function mergeVariant(
|
|
177
|
+
into: StateElement,
|
|
178
|
+
variant: VariantLayer,
|
|
179
|
+
hidden: Set<StateElement>
|
|
180
|
+
): void {
|
|
181
|
+
let previous = -1
|
|
182
|
+
for (const child of variant.children) {
|
|
183
|
+
if (child.type === 'text') continue
|
|
184
|
+
const index = into.children.findIndex(
|
|
185
|
+
(item) => item.type === 'element' && item.key === child.key
|
|
186
|
+
)
|
|
187
|
+
const existing = into.children.at(index)
|
|
188
|
+
if (index !== -1 && existing?.type === 'element') {
|
|
189
|
+
mergeVariant(existing, child, hidden)
|
|
190
|
+
previous = index
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
const added = stateElement(child)
|
|
194
|
+
hideAtRest(added, hidden)
|
|
195
|
+
previous += 1
|
|
196
|
+
into.children.splice(previous, 0, added)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Every element of the merged tree, root first. */
|
|
201
|
+
export function allElements(root: StateElement, into: StateElement[] = []): StateElement[] {
|
|
202
|
+
into.push(root)
|
|
203
|
+
for (const child of root.children) if (child.type === 'element') allElements(child, into)
|
|
204
|
+
return into
|
|
205
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { SceneGraphToDesignOptions } from '#dom-css/export/projection'
|
|
2
|
+
import type { DesignStyleDeclaration } from '#dom-css/types'
|
|
3
|
+
import { isEmptyObject, isEqual } from 'es-toolkit/predicate'
|
|
4
|
+
|
|
5
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
6
|
+
import { findLayerByPath } from '@pairdraw/scene-graph/behaviours'
|
|
7
|
+
|
|
8
|
+
import { variantConditions } from './conditions'
|
|
9
|
+
import {
|
|
10
|
+
allElements,
|
|
11
|
+
layersByKey,
|
|
12
|
+
mergeVariant,
|
|
13
|
+
projectVariant,
|
|
14
|
+
stateElement,
|
|
15
|
+
type VariantLayer
|
|
16
|
+
} from './layers'
|
|
17
|
+
import type { StateCondition, StateElement, StateRule, StateStyles } from './types'
|
|
18
|
+
|
|
19
|
+
/** Declarations `drawn` sets differently from `base`, with `unset` for ones it drops. */
|
|
20
|
+
export function difference(
|
|
21
|
+
base: DesignStyleDeclaration,
|
|
22
|
+
drawn: DesignStyleDeclaration
|
|
23
|
+
): DesignStyleDeclaration {
|
|
24
|
+
const changed: DesignStyleDeclaration = {}
|
|
25
|
+
for (const [property, value] of Object.entries(drawn))
|
|
26
|
+
if (base[property] !== value) changed[property] = value
|
|
27
|
+
for (const property of Object.keys(base)) if (!(property in drawn)) changed[property] = 'unset'
|
|
28
|
+
return changed
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* What a variant changes on a layer: what it draws differently, showing a layer hidden at
|
|
33
|
+
* rest again, or hiding a layer it doesn't draw.
|
|
34
|
+
*/
|
|
35
|
+
function variantStyle(
|
|
36
|
+
element: StateElement,
|
|
37
|
+
layer: VariantLayer | undefined,
|
|
38
|
+
hiddenAtRest: boolean
|
|
39
|
+
): DesignStyleDeclaration {
|
|
40
|
+
if (!layer) return hiddenAtRest ? {} : { display: 'none' }
|
|
41
|
+
const drawn = layer.element.inlineStyle ?? {}
|
|
42
|
+
if (!hiddenAtRest) return difference(element.base, drawn)
|
|
43
|
+
const display = Object.hasOwn(drawn, 'display') ? drawn.display : 'revert'
|
|
44
|
+
return difference(element.base, { ...drawn, display })
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The value a variant draws a property with on a layer: its own, or none where it lacks it. */
|
|
48
|
+
function drawnValue(
|
|
49
|
+
element: StateElement,
|
|
50
|
+
layer: VariantLayer | undefined,
|
|
51
|
+
property: string,
|
|
52
|
+
hiddenAtRest: boolean
|
|
53
|
+
): string {
|
|
54
|
+
if (!layer) return property === 'display' ? 'none' : (element.base[property] ?? 'unset')
|
|
55
|
+
const drawn = layer.element.inlineStyle ?? {}
|
|
56
|
+
if (Object.hasOwn(drawn, property)) return drawn[property] ?? 'unset'
|
|
57
|
+
if (property === 'display' && hiddenAtRest) return 'revert'
|
|
58
|
+
return 'unset'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Makes each variant draw a layer as its own design does where a rule for fewer conditions
|
|
63
|
+
* also matches it. A rule for small buttons also matches the small, pressed one, so whatever
|
|
64
|
+
* it sets that the pressed one draws otherwise, such as words only small rest buttons show,
|
|
65
|
+
* the pressed one sets back, as a rule with more conditions, which wins.
|
|
66
|
+
*/
|
|
67
|
+
function settleCombined(
|
|
68
|
+
element: StateElement,
|
|
69
|
+
variants: readonly { conditions: StateCondition[]; layers: Map<string, VariantLayer> }[],
|
|
70
|
+
hiddenAtRest: boolean
|
|
71
|
+
): void {
|
|
72
|
+
for (const variant of variants) {
|
|
73
|
+
const own = element.rules.find((rule) => isEqual(rule.conditions, variant.conditions))
|
|
74
|
+
const fix: DesignStyleDeclaration = {}
|
|
75
|
+
for (const rule of element.rules) {
|
|
76
|
+
if (!isPartOf(rule.conditions, variant.conditions)) continue
|
|
77
|
+
for (const property of Object.keys(rule.style)) {
|
|
78
|
+
if (own && Object.hasOwn(own.style, property)) continue
|
|
79
|
+
const layer = variant.layers.get(element.key)
|
|
80
|
+
fix[property] = drawnValue(element, layer, property, hiddenAtRest)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (isEmptyObject(fix)) continue
|
|
84
|
+
if (own) own.style = { ...own.style, ...fix }
|
|
85
|
+
else element.rules.push({ conditions: variant.conditions, style: fix })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isPartOf = (part: StateCondition[], whole: StateCondition[]) =>
|
|
90
|
+
part.length < whole.length &&
|
|
91
|
+
part.every((condition) => whole.some((other) => isEqual(condition, other)))
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Drops what a combined variant repeats. A rule for checked + hover also gets the rules for
|
|
95
|
+
* checked and for hover, so a declaration both of those already give with the same value
|
|
96
|
+
* goes; one where they disagree or say nothing stays. Rules left empty go.
|
|
97
|
+
*/
|
|
98
|
+
function pruneCombined(rules: StateRule[]): StateRule[] {
|
|
99
|
+
return rules.flatMap((rule) => {
|
|
100
|
+
const parts = rules.filter((other) => isPartOf(other.conditions, rule.conditions))
|
|
101
|
+
const repeated = (property: string, value: string) => {
|
|
102
|
+
const given = parts.filter((part) => property in part.style)
|
|
103
|
+
return given.length > 0 && given.every((part) => part.style[property] === value)
|
|
104
|
+
}
|
|
105
|
+
const style = Object.fromEntries(
|
|
106
|
+
Object.entries(rule.style).filter(([property, value]) => !repeated(property, value))
|
|
107
|
+
)
|
|
108
|
+
return isEmptyObject(style) ? [] : [{ ...rule, style }]
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The variants a layer draws: a set's components, or any other layer, such as a standalone
|
|
114
|
+
* component or a frame exported as a component, which is its own only variant.
|
|
115
|
+
*/
|
|
116
|
+
export function ownerVariants(graph: SceneGraph, owner: SceneNode): SceneNode[] {
|
|
117
|
+
if (owner.type !== 'COMPONENT_SET') return [owner]
|
|
118
|
+
return graph.getChildren(owner.id).filter((child) => child.type === 'COMPONENT')
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* A component set's variants as one markup tree with a rest style per layer and a rule per
|
|
123
|
+
* variant holding only what that variant changes, under the conditions that show it. Layers
|
|
124
|
+
* only some variants draw stay in the tree, hidden where absent.
|
|
125
|
+
*
|
|
126
|
+
* `null` when no variant shows the rest state, since every look then depends on a condition.
|
|
127
|
+
*/
|
|
128
|
+
export function stateStyles(
|
|
129
|
+
graph: SceneGraph,
|
|
130
|
+
set: SceneNode,
|
|
131
|
+
options: Pick<SceneGraphToDesignOptions, 'vectorElement'> = {}
|
|
132
|
+
): StateStyles | null {
|
|
133
|
+
const conditionsOf = variantConditions(graph, set)
|
|
134
|
+
const variants = ownerVariants(graph, set)
|
|
135
|
+
.filter((variant) => variant.visible)
|
|
136
|
+
.flatMap((variant) => {
|
|
137
|
+
const conditions = conditionsOf(variant)
|
|
138
|
+
const root = conditions && projectVariant(graph, variant, options)
|
|
139
|
+
return conditions && root ? [{ id: variant.id, conditions, root }] : []
|
|
140
|
+
})
|
|
141
|
+
const rest = variants.find((variant) => variant.conditions.length === 0)
|
|
142
|
+
if (!rest) return null
|
|
143
|
+
const others = variants.filter((variant) => variant !== rest)
|
|
144
|
+
|
|
145
|
+
const root = stateElement(rest.root)
|
|
146
|
+
const hiddenAtRest = new Set<StateElement>()
|
|
147
|
+
for (const variant of others) mergeVariant(root, variant.root, hiddenAtRest)
|
|
148
|
+
|
|
149
|
+
const elements = allElements(root)
|
|
150
|
+
const drawn = others.map((variant) => ({
|
|
151
|
+
conditions: variant.conditions,
|
|
152
|
+
layers: layersByKey(variant.root)
|
|
153
|
+
}))
|
|
154
|
+
for (const variant of drawn) {
|
|
155
|
+
for (const element of elements) {
|
|
156
|
+
const layer = variant.layers.get(element.key)
|
|
157
|
+
const style = variantStyle(element, layer, hiddenAtRest.has(element))
|
|
158
|
+
if (!isEmptyObject(style)) element.rules.push({ conditions: variant.conditions, style })
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
for (const element of elements) {
|
|
162
|
+
settleCombined(element, drawn, hiddenAtRest.has(element))
|
|
163
|
+
element.rules = pruneCombined(element.rules)
|
|
164
|
+
}
|
|
165
|
+
return { name: set.name, restId: rest.id, root }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The layer of the rest variant an element of the merged markup draws, if it has one. */
|
|
169
|
+
export function restLayerOf(
|
|
170
|
+
graph: SceneGraph,
|
|
171
|
+
styles: StateStyles,
|
|
172
|
+
element: StateElement
|
|
173
|
+
): string | undefined {
|
|
174
|
+
return findLayerByPath(graph, styles.restId, element.key.split('\0')[0] ?? '')?.id
|
|
175
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { claimName } from '#dom-css/export/storybook/names'
|
|
2
|
+
import { kebabCase } from 'es-toolkit/string'
|
|
3
|
+
|
|
4
|
+
import type { StateElement, StateStyles } from './types'
|
|
5
|
+
|
|
6
|
+
const NOT_NAME_PART = /[^a-z0-9-]/g
|
|
7
|
+
|
|
8
|
+
/** A CSS identifier made from a layer or set name, `fallback` when nothing of it is usable. */
|
|
9
|
+
export function cssName(text: string, fallback: string): string {
|
|
10
|
+
const name = kebabCase(text).replace(NOT_NAME_PART, '')
|
|
11
|
+
return /^[a-z]/.test(name) ? name : `${fallback}${name ? `-${name}` : ''}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A readable class per layer: the set's name for the root, `<root>__<layer>` below it,
|
|
16
|
+
* numbered where two layers share a name.
|
|
17
|
+
*/
|
|
18
|
+
export function layerClassNames(styles: StateStyles): Map<StateElement, string> {
|
|
19
|
+
const root = cssName(styles.name, 'component')
|
|
20
|
+
const names = new Map<StateElement, string>([[styles.root, root]])
|
|
21
|
+
const taken = new Set([root])
|
|
22
|
+
const visit = (element: StateElement) => {
|
|
23
|
+
for (const child of element.children) {
|
|
24
|
+
if (child.type !== 'element') continue
|
|
25
|
+
names.set(
|
|
26
|
+
child,
|
|
27
|
+
claimName(`${root}__${cssName(child.name, 'layer')}`, taken, { separator: '-' })
|
|
28
|
+
)
|
|
29
|
+
visit(child)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
visit(styles.root)
|
|
33
|
+
return names
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The attribute a generated component sets for a variant property it takes as a prop. */
|
|
37
|
+
export function propAttribute(name: string): string {
|
|
38
|
+
return `data-${cssName(name, 'prop')}`
|
|
39
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { splitWhitespace } from '#dom-css/export/html'
|
|
2
|
+
import type { DesignDocument, DesignNode, DesignStyleDeclaration } from '#dom-css/types'
|
|
3
|
+
import { compact } from 'es-toolkit/array'
|
|
4
|
+
import { twirl } from 'twirlwind'
|
|
5
|
+
|
|
6
|
+
import { cssName, propAttribute } from './names'
|
|
7
|
+
import type { StateCondition, StateElement, StateNode, StateStyles } from './types'
|
|
8
|
+
|
|
9
|
+
export interface StateTailwindOptions {
|
|
10
|
+
/** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */
|
|
11
|
+
themeVariables?: readonly string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A value inside a variant: bare when it is a plain word, else quoted with spaces as `_`. */
|
|
15
|
+
const variantValue = (value: string) =>
|
|
16
|
+
/^[\w-]+$/.test(value) ? value : `"${value.replace(/\s/g, '_').replace(/"/g, '\\"')}"`
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The variant a condition adds. On the root it tests the root itself; below it, it tests the
|
|
20
|
+
* root through its named group, so a control nested in another reacts only to its own root.
|
|
21
|
+
*/
|
|
22
|
+
function variant(condition: StateCondition, group: string | null): string {
|
|
23
|
+
const on = (name: string) => (group ? `group-${name}/${group}:` : `${name}:`)
|
|
24
|
+
if (condition.type === 'state') return on(`data-[state=${variantValue(condition.value)}]`)
|
|
25
|
+
if (condition.type === 'disabled') return on('data-disabled')
|
|
26
|
+
if (condition.type === 'filled') return on('has-[:is(input,textarea):not(:placeholder-shown)]')
|
|
27
|
+
if (condition.type === 'prop') {
|
|
28
|
+
const attribute = propAttribute(condition.name).slice('data-'.length)
|
|
29
|
+
return on(`data-[${attribute}=${variantValue(condition.value)}]`)
|
|
30
|
+
}
|
|
31
|
+
if (condition.state === 'focus') return on(condition.within ? 'focus-within' : 'focus-visible')
|
|
32
|
+
// A disabled control keeps its disabled look under the pointer.
|
|
33
|
+
const enabled = group ? `group-not-data-disabled/${group}:` : 'not-data-disabled:'
|
|
34
|
+
return `${enabled}${on(condition.state === 'hover' ? 'hover' : 'active')}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function utilities(style: DesignStyleDeclaration, options: StateTailwindOptions): string[] {
|
|
38
|
+
const css = Object.entries(style)
|
|
39
|
+
.map(([property, value]) => `${property}: ${value}`)
|
|
40
|
+
.join('; ')
|
|
41
|
+
return css
|
|
42
|
+
? splitWhitespace(twirl(css, { theme: { variables: options.themeVariables ?? [] } }))
|
|
43
|
+
: []
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What each element's utilities are written against. */
|
|
47
|
+
interface Groups {
|
|
48
|
+
/** The control's root group. */
|
|
49
|
+
root: string
|
|
50
|
+
/** A named group for each layer other than the root whose state a rule tests. */
|
|
51
|
+
anchors: Map<StateElement, string>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The group a rule's conditions test from `node`, or null when they test `node` itself. */
|
|
55
|
+
function testedGroup(
|
|
56
|
+
node: StateElement,
|
|
57
|
+
on: StateElement | undefined,
|
|
58
|
+
groups: Groups,
|
|
59
|
+
isRoot: boolean
|
|
60
|
+
): string | null {
|
|
61
|
+
if (on) return on === node ? null : (groups.anchors.get(on) ?? null)
|
|
62
|
+
return isRoot ? null : groups.root
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The utilities a layer takes: its group names, its rest style, and each rule behind its variants. */
|
|
66
|
+
function elementUtilities(
|
|
67
|
+
node: StateElement,
|
|
68
|
+
groups: Groups,
|
|
69
|
+
isRoot: boolean,
|
|
70
|
+
options: StateTailwindOptions
|
|
71
|
+
): string[] {
|
|
72
|
+
const anchor = groups.anchors.get(node)
|
|
73
|
+
return [
|
|
74
|
+
...(isRoot ? [`group/${groups.root}`] : []),
|
|
75
|
+
...(anchor ? [`group/${anchor}`] : []),
|
|
76
|
+
...utilities(node.base, options),
|
|
77
|
+
...node.rules.flatMap((rule) => {
|
|
78
|
+
const group = testedGroup(node, rule.on, groups, isRoot)
|
|
79
|
+
const prefix = [
|
|
80
|
+
...rule.conditions.map((condition) => variant(condition, group)),
|
|
81
|
+
...(rule.pseudo
|
|
82
|
+
? [rule.pseudo === 'placeholder' ? 'placeholder:' : `[&::${rule.pseudo}]:`]
|
|
83
|
+
: [])
|
|
84
|
+
].join('')
|
|
85
|
+
return utilities(rule.style, options).map((utility) => `${prefix}${utility}`)
|
|
86
|
+
})
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function designNode(node: StateNode, classes: Map<StateElement, string>): DesignNode {
|
|
91
|
+
if (node.type === 'text') return node
|
|
92
|
+
return {
|
|
93
|
+
type: 'element',
|
|
94
|
+
tagName: node.tagName,
|
|
95
|
+
attrs: {
|
|
96
|
+
...node.attrs,
|
|
97
|
+
class: compact([
|
|
98
|
+
...(node.attrs.class ? splitWhitespace(node.attrs.class) : []),
|
|
99
|
+
classes.get(node)
|
|
100
|
+
]).join(' ')
|
|
101
|
+
},
|
|
102
|
+
children: node.children.map((child) => designNode(child, classes)),
|
|
103
|
+
shader: node.shader
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The layers rules test other than the root, each named after the root's group. */
|
|
108
|
+
function anchorGroups(root: StateElement, group: string): Map<StateElement, string> {
|
|
109
|
+
const anchors = new Map<StateElement, string>()
|
|
110
|
+
const visit = (node: StateNode) => {
|
|
111
|
+
if (node.type === 'text') return
|
|
112
|
+
for (const rule of node.rules)
|
|
113
|
+
if (rule.on && !anchors.has(rule.on)) anchors.set(rule.on, `${group}-${anchors.size + 1}`)
|
|
114
|
+
for (const child of node.children) visit(child)
|
|
115
|
+
}
|
|
116
|
+
visit(root)
|
|
117
|
+
return anchors
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Each element's Tailwind utilities for the state styles: rest utilities, and each variant's
|
|
122
|
+
* changes behind the variants that show it. More stacked variants make a more specific
|
|
123
|
+
* selector, so a combined variant wins over each of its parts.
|
|
124
|
+
*/
|
|
125
|
+
export function stateTailwindClasses(
|
|
126
|
+
styles: StateStyles,
|
|
127
|
+
options: StateTailwindOptions = {}
|
|
128
|
+
): Map<StateElement, string> {
|
|
129
|
+
const root = cssName(styles.name, 'component')
|
|
130
|
+
const groups = { root, anchors: anchorGroups(styles.root, root) }
|
|
131
|
+
const classes = new Map<StateElement, string>()
|
|
132
|
+
const visit = (node: StateNode, isRoot: boolean) => {
|
|
133
|
+
if (node.type === 'text') return
|
|
134
|
+
classes.set(node, elementUtilities(node, groups, isRoot, options).join(' '))
|
|
135
|
+
for (const child of node.children) visit(child, false)
|
|
136
|
+
}
|
|
137
|
+
visit(styles.root, true)
|
|
138
|
+
return classes
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The state styles as Tailwind utilities on each element of the merged markup. */
|
|
142
|
+
export function stateStylesToTailwind(
|
|
143
|
+
styles: StateStyles,
|
|
144
|
+
options: StateTailwindOptions = {}
|
|
145
|
+
): DesignDocument {
|
|
146
|
+
const classes = stateTailwindClasses(styles, options)
|
|
147
|
+
return { type: 'document', children: [designNode(styles.root, classes)] }
|
|
148
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { DesignElement, DesignStyleDeclaration, DesignText } from '#dom-css/types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What must hold on a control's root for a variant to show. Reka and Radix set `data-state`
|
|
5
|
+
* and `data-disabled` themselves; the browser sets the interactions; a generated component
|
|
6
|
+
* sets `data-*` from its props for every other variant property.
|
|
7
|
+
*/
|
|
8
|
+
export type StateCondition =
|
|
9
|
+
| { type: 'state'; value: string }
|
|
10
|
+
| { type: 'disabled' }
|
|
11
|
+
/** `within` when focus lands on a layer inside the control, such as a field's input. */
|
|
12
|
+
| { type: 'interaction'; state: 'hover' | 'pressed' | 'focus'; within?: boolean }
|
|
13
|
+
/** A field whose input holds text. */
|
|
14
|
+
| { type: 'filled' }
|
|
15
|
+
| { type: 'prop'; name: string; value: string }
|
|
16
|
+
|
|
17
|
+
/** What a variant changes on a layer, and the conditions that show that variant. */
|
|
18
|
+
export interface StateRule {
|
|
19
|
+
conditions: StateCondition[]
|
|
20
|
+
/**
|
|
21
|
+
* The layer the conditions test when it is not the control's root, such as a tab trigger
|
|
22
|
+
* that Reka and Radix mark active itself: the layer the rule styles, or one around it.
|
|
23
|
+
*/
|
|
24
|
+
on?: StateElement
|
|
25
|
+
/** The part of the layer the rule styles: an input's placeholder, or a number input's spinner. */
|
|
26
|
+
pseudo?: 'placeholder' | '-webkit-inner-spin-button'
|
|
27
|
+
style: DesignStyleDeclaration
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A layer of the merged markup: its rest style and what each other variant changes. */
|
|
31
|
+
export interface StateElement {
|
|
32
|
+
type: 'element'
|
|
33
|
+
/** The layer path below the variant, with the text when variants label a layer differently. */
|
|
34
|
+
key: string
|
|
35
|
+
/** What the layer draws other than its own box, such as an instance or an icon (`layerKind`). */
|
|
36
|
+
kind: string | undefined
|
|
37
|
+
/** The layer's name, for class names. */
|
|
38
|
+
name: string
|
|
39
|
+
tagName: string
|
|
40
|
+
attrs: Record<string, string>
|
|
41
|
+
base: DesignStyleDeclaration
|
|
42
|
+
rules: StateRule[]
|
|
43
|
+
/** The shader the layer fills with at rest, as the projection keeps it. */
|
|
44
|
+
shader?: DesignElement['shader']
|
|
45
|
+
children: StateNode[]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type StateNode = StateElement | DesignText
|
|
49
|
+
|
|
50
|
+
export interface StateStyles {
|
|
51
|
+
/** The set's name, for the root's class name. */
|
|
52
|
+
name: string
|
|
53
|
+
/** The variant drawing the rest state, which the merged tree starts from. */
|
|
54
|
+
restId: string
|
|
55
|
+
root: StateElement
|
|
56
|
+
}
|