@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.
- 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 +1599 -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 +88 -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 +412 -0
- package/src/export/components/react/props.ts +151 -0
- package/src/export/components/react/radix.ts +52 -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,212 @@
|
|
|
1
|
+
import { layerKind } from '#dom-css/behaviours/states/layers'
|
|
2
|
+
import type { StateElement } from '#dom-css/behaviours/states/types'
|
|
3
|
+
import type { DesignStyleDeclaration } from '#dom-css/types'
|
|
4
|
+
import { isEmptyObject } from 'es-toolkit/predicate'
|
|
5
|
+
|
|
6
|
+
import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
7
|
+
import { behaviourOwner, findLayerByPath } from '@pairdraw/scene-graph/behaviours'
|
|
8
|
+
import { findComponentPropertyTargets } from '@pairdraw/scene-graph/components'
|
|
9
|
+
import { isIconModified, isNamedIcon, readIcon } from '@pairdraw/scene-graph/icons'
|
|
10
|
+
import { instanceMainComponent } from '@pairdraw/scene-graph/instances'
|
|
11
|
+
|
|
12
|
+
import { drawsAsMain } from './drawn'
|
|
13
|
+
import type { ComponentModel } from './model'
|
|
14
|
+
|
|
15
|
+
/** A prop a reference sets on the component it uses: a variant or text value, or `disabled`. */
|
|
16
|
+
interface ReferenceProp {
|
|
17
|
+
name: string
|
|
18
|
+
value: string | boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Another generated component used where the design places an instance of it, with the values
|
|
23
|
+
* the instance shows. Its own layers are the component's, so they are not drawn here.
|
|
24
|
+
*/
|
|
25
|
+
export interface ComponentReference {
|
|
26
|
+
type: 'reference'
|
|
27
|
+
/** The layer it draws, which code links back to. */
|
|
28
|
+
layerId?: string
|
|
29
|
+
/** The component's identifier and file name. */
|
|
30
|
+
component: string
|
|
31
|
+
/** The class the parent's state styles place it by. */
|
|
32
|
+
className: string
|
|
33
|
+
props: ReferenceProp[]
|
|
34
|
+
/** The two-way value the instance is drawn with, when it is on. */
|
|
35
|
+
model: string | null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** An icon from a set, drawn by the framework's Iconify component. */
|
|
39
|
+
export interface IconReference {
|
|
40
|
+
type: 'icon'
|
|
41
|
+
/** The layer it draws, which code links back to. */
|
|
42
|
+
layerId?: string
|
|
43
|
+
/** `prefix:name`. */
|
|
44
|
+
icon: string
|
|
45
|
+
className: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* How a parent places a layer, as opposed to how the layer looks. A component used in place of
|
|
50
|
+
* a layer keeps its own look and state styles; the parent only places it.
|
|
51
|
+
*/
|
|
52
|
+
const PLACEMENT =
|
|
53
|
+
/^(position|inset|left|top|right|bottom|(min-|max-)?(width|height)|margin(-.+)?|flex(-(grow|shrink|basis))?|align-self|justify-self|order|grid-(area|column|row)(-.+)?|transform(-origin)?|rotate|translate|scale|z-index)$/
|
|
54
|
+
|
|
55
|
+
/** Whether a declaration places a layer rather than drawing it. */
|
|
56
|
+
export const isPlacement = (property: string) => PLACEMENT.test(property)
|
|
57
|
+
|
|
58
|
+
/** Whether a declaration hides or shows a layer by state, which the parent decides too. */
|
|
59
|
+
const isVisibility = (property: string, value: string) =>
|
|
60
|
+
property === 'display' && (value === 'none' || value === 'revert')
|
|
61
|
+
|
|
62
|
+
/** Keeps only where the parent places a layer, and whether a state shows it. */
|
|
63
|
+
export function placementOnly(element: StateElement): void {
|
|
64
|
+
const placement = (style: DesignStyleDeclaration): DesignStyleDeclaration =>
|
|
65
|
+
Object.fromEntries(
|
|
66
|
+
Object.entries(style).filter(
|
|
67
|
+
([property, value]) => isPlacement(property) || isVisibility(property, value)
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
element.base = placement(element.base)
|
|
71
|
+
element.rules = element.rules
|
|
72
|
+
.map((rule) => ({ ...rule, style: placement(rule.style) }))
|
|
73
|
+
.filter((rule) => !isEmptyObject(rule.style))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Generated components by the id of the set they come from, which instances refer to. */
|
|
77
|
+
export type ComponentReferences = ReadonlyMap<string, ComponentModel>
|
|
78
|
+
|
|
79
|
+
/** The set a component belongs to, or the component itself when it stands alone. */
|
|
80
|
+
function ownerId(graph: SceneGraph, component: SceneNode): string {
|
|
81
|
+
const parent = component.parentId ? graph.getNode(component.parentId) : undefined
|
|
82
|
+
return parent?.type === 'COMPONENT_SET' ? parent.id : component.id
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The words an instance shows for a text property: an assignment that overrides the default,
|
|
87
|
+
* else what its bound text layer reads, which a direct edit changes, else the assignment.
|
|
88
|
+
*/
|
|
89
|
+
export function shownText(
|
|
90
|
+
graph: SceneGraph,
|
|
91
|
+
instance: SceneNode,
|
|
92
|
+
propertyId: string
|
|
93
|
+
): string | undefined {
|
|
94
|
+
const assigned = instance.componentPropertyAssignments[propertyId]
|
|
95
|
+
const main = instanceMainComponent(graph, instance)
|
|
96
|
+
const owner = main && behaviourOwner(graph, main)
|
|
97
|
+
const definition = owner?.componentPropertyDefinitions.find((item) => item.id === propertyId)
|
|
98
|
+
if (typeof assigned === 'string' && assigned !== definition?.defaultValue) return assigned
|
|
99
|
+
const target = findComponentPropertyTargets(graph, instance, propertyId).find(
|
|
100
|
+
(item) => item.field === 'TEXT' && item.node.type === 'TEXT'
|
|
101
|
+
)
|
|
102
|
+
return target?.node.text ?? (typeof assigned === 'string' ? assigned : undefined)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* What `instance` sets on `component`: the values it is drawn with that differ from defaults,
|
|
107
|
+
* and whether it is drawn on: its model, or for a group's item the value that marks it chosen.
|
|
108
|
+
*/
|
|
109
|
+
export function referenceValues(
|
|
110
|
+
graph: SceneGraph,
|
|
111
|
+
instance: SceneNode,
|
|
112
|
+
component: ComponentModel
|
|
113
|
+
): Pick<ComponentReference, 'props' | 'model'> {
|
|
114
|
+
const values = instanceMainComponent(graph, instance)?.componentPropertyValues ?? {}
|
|
115
|
+
const props: ReferenceProp[] = []
|
|
116
|
+
let model: string | null = null
|
|
117
|
+
for (const [property, arg] of component.args.booleans) {
|
|
118
|
+
if (values[property] !== arg.on) continue
|
|
119
|
+
const on = arg.name === component.model || (component.valueProp && arg.name !== 'disabled')
|
|
120
|
+
if (on) model = arg.name
|
|
121
|
+
else props.push({ name: arg.name, value: true })
|
|
122
|
+
}
|
|
123
|
+
const states = component.args.states
|
|
124
|
+
if (states?.disabled && values[states.property] === states.disabled)
|
|
125
|
+
props.push({ name: 'disabled', value: true })
|
|
126
|
+
for (const prop of component.props) {
|
|
127
|
+
const value = values[prop.property]
|
|
128
|
+
if (Object.hasOwn(values, prop.property) && value !== prop.default)
|
|
129
|
+
props.push({ name: prop.name, value })
|
|
130
|
+
}
|
|
131
|
+
for (const text of component.texts) {
|
|
132
|
+
const value = shownText(graph, instance, text.id)
|
|
133
|
+
if (value !== undefined && value !== text.default) props.push({ name: text.name, value })
|
|
134
|
+
}
|
|
135
|
+
for (const prop of component.booleans) {
|
|
136
|
+
const assignments = instance.componentPropertyAssignments
|
|
137
|
+
const value = Object.hasOwn(assignments, prop.id)
|
|
138
|
+
? assignments[prop.id] === 'true'
|
|
139
|
+
: prop.default
|
|
140
|
+
if (value !== prop.default) props.push({ name: prop.name, value })
|
|
141
|
+
}
|
|
142
|
+
return { props, model }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A used layer, before the parent's class name for it is known. */
|
|
146
|
+
export type UsedLayer = Omit<ComponentReference, 'className'> | Omit<IconReference, 'className'>
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* What the layer at `element` uses in place of drawing itself, if anything, read from the first
|
|
150
|
+
* variant that draws it as that element: the rest state, or the one that shows it, such as open
|
|
151
|
+
* content.
|
|
152
|
+
*/
|
|
153
|
+
function usedLayer(
|
|
154
|
+
graph: SceneGraph,
|
|
155
|
+
variantIds: readonly string[],
|
|
156
|
+
element: StateElement,
|
|
157
|
+
references: ComponentReferences
|
|
158
|
+
): UsedLayer | null {
|
|
159
|
+
const path = element.key.split('\0')[0] ?? ''
|
|
160
|
+
// The variant's layer at that path drawn as this element is: a frame, or that instance or icon.
|
|
161
|
+
const node = variantIds
|
|
162
|
+
.map((id) => findLayerByPath(graph, id, path))
|
|
163
|
+
.find((found) => found !== undefined && layerKind(graph, found) === element.kind)
|
|
164
|
+
if (!node) return null
|
|
165
|
+
const main = node.type === 'INSTANCE' ? instanceMainComponent(graph, node) : undefined
|
|
166
|
+
// An instance changed beyond its properties draws its own layers, which the component's
|
|
167
|
+
// props could not reproduce.
|
|
168
|
+
const component =
|
|
169
|
+
main && drawsAsMain(graph, node, main) ? references.get(ownerId(graph, main)) : undefined
|
|
170
|
+
if (component)
|
|
171
|
+
return {
|
|
172
|
+
type: 'reference',
|
|
173
|
+
component: component.name,
|
|
174
|
+
...referenceValues(graph, node, component)
|
|
175
|
+
}
|
|
176
|
+
const icon = readIcon(node)
|
|
177
|
+
return icon && isNamedIcon(icon) && !isIconModified(graph, node)
|
|
178
|
+
? { type: 'icon', icon: icon.name }
|
|
179
|
+
: null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The layers of a set's merged markup that are used rather than drawn: instances of other
|
|
184
|
+
* generated components, and icons from a set, read from the variants in order, the rest state
|
|
185
|
+
* first. Their own layers are dropped from the markup and the styles, which belong to the
|
|
186
|
+
* component or the icon.
|
|
187
|
+
*/
|
|
188
|
+
export function referencedLayers(
|
|
189
|
+
graph: SceneGraph,
|
|
190
|
+
root: StateElement,
|
|
191
|
+
variantIds: readonly string[],
|
|
192
|
+
references: ComponentReferences,
|
|
193
|
+
/** Layers already used otherwise, such as a group's items. */
|
|
194
|
+
taken: ReadonlyMap<StateElement, unknown> = new Map()
|
|
195
|
+
): Map<StateElement, UsedLayer> {
|
|
196
|
+
const used = new Map<StateElement, UsedLayer>()
|
|
197
|
+
const visit = (element: StateElement) => {
|
|
198
|
+
for (const child of element.children) {
|
|
199
|
+
if (child.type !== 'element' || taken.has(child)) continue
|
|
200
|
+
const layer = usedLayer(graph, variantIds, child, references)
|
|
201
|
+
if (!layer) {
|
|
202
|
+
visit(child)
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
used.set(child, layer)
|
|
206
|
+
child.children = []
|
|
207
|
+
if (layer.type === 'reference') placementOnly(child)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
visit(root)
|
|
211
|
+
return used
|
|
212
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { difference } from '#dom-css/behaviours/states/model'
|
|
2
|
+
import type { StateElement, StateNode } from '#dom-css/behaviours/states/types'
|
|
3
|
+
import type { DesignStyleDeclaration } from '#dom-css/types'
|
|
4
|
+
import { isEmptyObject } from 'es-toolkit/predicate'
|
|
5
|
+
import { kebabCase } from 'es-toolkit/string'
|
|
6
|
+
|
|
7
|
+
import { claimName } from '../storybook/names'
|
|
8
|
+
import { isPlacement } from './references'
|
|
9
|
+
|
|
10
|
+
/** A layer drawn once per value, such as a tab's trigger or its panel, and the value it is. */
|
|
11
|
+
export interface RepeatedPart {
|
|
12
|
+
part: string
|
|
13
|
+
value: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The string a component chooses among, such as the open tab, and the one it starts on, or
|
|
18
|
+
* none, as a radio group with no item chosen.
|
|
19
|
+
*/
|
|
20
|
+
export interface ChoiceModel {
|
|
21
|
+
options: string[]
|
|
22
|
+
default: string | null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The words a layer reads, joined across its text. */
|
|
26
|
+
export function textOf(node: StateNode): string {
|
|
27
|
+
if (node.type === 'text') return node.text
|
|
28
|
+
return node.children.map(textOf).join(' ')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const elementsOf = (element: StateElement | undefined) =>
|
|
32
|
+
(element?.children ?? []).filter((child): child is StateElement => child.type === 'element')
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The value each label stands for: the label as a slug, such as `account`, apart from the
|
|
36
|
+
* others, or its position when it has no words.
|
|
37
|
+
*/
|
|
38
|
+
export function slugValues(labels: readonly string[]): string[] {
|
|
39
|
+
const taken = new Set<string>()
|
|
40
|
+
return labels.map((label, index) => claimName(kebabCase(label) || String(index), taken))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Tabs' triggers, the layers in its list, and its panels, the layers in its panels slot, each
|
|
45
|
+
* with the value of the tab it belongs to, matched by position. The tabs start on the first.
|
|
46
|
+
*/
|
|
47
|
+
export function tabParts(parts: ReadonlyMap<StateElement, string>): {
|
|
48
|
+
repeated: Map<StateElement, RepeatedPart>
|
|
49
|
+
choice: ChoiceModel | null
|
|
50
|
+
} {
|
|
51
|
+
const partElement = (id: string) => [...parts].find(([, part]) => part === id)?.[0]
|
|
52
|
+
const triggers = elementsOf(partElement('list'))
|
|
53
|
+
const values = slugValues(triggers.map(textOf))
|
|
54
|
+
const repeated = new Map<StateElement, RepeatedPart>()
|
|
55
|
+
for (const [index, trigger] of triggers.entries())
|
|
56
|
+
repeated.set(trigger, { part: 'trigger', value: values[index] ?? String(index) })
|
|
57
|
+
for (const [index, panel] of elementsOf(partElement('panels')).entries()) {
|
|
58
|
+
const value = values.at(index)
|
|
59
|
+
if (value !== undefined) repeated.set(panel, { part: 'content', value })
|
|
60
|
+
}
|
|
61
|
+
const first = values.at(0)
|
|
62
|
+
return { repeated, choice: first === undefined ? null : { options: values, default: first } }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** How `drawn` looks different from `base`, leaving out where each is placed. */
|
|
66
|
+
function lookChange(
|
|
67
|
+
base: DesignStyleDeclaration,
|
|
68
|
+
drawn: DesignStyleDeclaration
|
|
69
|
+
): DesignStyleDeclaration {
|
|
70
|
+
const changed = Object.entries(difference(base, drawn))
|
|
71
|
+
return Object.fromEntries(changed.filter(([property]) => !isPlacement(property)))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `base` with `change` applied, where `unset` drops a declaration. */
|
|
75
|
+
function applied(
|
|
76
|
+
base: DesignStyleDeclaration,
|
|
77
|
+
change: DesignStyleDeclaration
|
|
78
|
+
): DesignStyleDeclaration {
|
|
79
|
+
return Object.fromEntries([
|
|
80
|
+
...Object.entries(base).filter(([property]) => change[property] !== 'unset'),
|
|
81
|
+
...Object.entries(change).filter(([, value]) => value !== 'unset')
|
|
82
|
+
])
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The layer of `into` where `from` has `child`: the same name, and the same one of that name. */
|
|
86
|
+
function counterpart(
|
|
87
|
+
child: StateElement,
|
|
88
|
+
from: StateElement,
|
|
89
|
+
into: StateElement
|
|
90
|
+
): StateElement | undefined {
|
|
91
|
+
const named = (element: StateElement) =>
|
|
92
|
+
elementsOf(element).filter((item) => item.name === child.name)
|
|
93
|
+
return named(into).at(named(from).indexOf(child))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Tabs draw the first trigger chosen and the others not. Every trigger rests with the look of
|
|
98
|
+
* the others and takes the first one's, layer by layer, while Reka or Radix marks it active;
|
|
99
|
+
* each keeps its own place. A layer is matched across triggers by name, and one without a
|
|
100
|
+
* counterpart keeps its own styles.
|
|
101
|
+
*/
|
|
102
|
+
export function activeTriggers(repeated: ReadonlyMap<StateElement, RepeatedPart>): void {
|
|
103
|
+
const triggers = [...repeated].filter(([, item]) => item.part === 'trigger').map(([el]) => el)
|
|
104
|
+
const first = triggers.at(0)
|
|
105
|
+
const second = triggers.at(1)
|
|
106
|
+
if (!first || !second) return
|
|
107
|
+
// The two looks as drawn, before the first trigger rests with the others' look.
|
|
108
|
+
const [active, inactive] = structuredClone([first, second])
|
|
109
|
+
for (const trigger of triggers) {
|
|
110
|
+
const visit = (layer: StateElement, drawn: StateElement, other: StateElement) => {
|
|
111
|
+
if (trigger === first) layer.base = applied(layer.base, lookChange(drawn.base, other.base))
|
|
112
|
+
// From the layer's own rest, so a trigger drawn apart still takes the chosen look.
|
|
113
|
+
const look = lookChange(layer.base, drawn.base)
|
|
114
|
+
if (!isEmptyObject(look))
|
|
115
|
+
layer.rules.push({
|
|
116
|
+
conditions: [{ type: 'state', value: 'active' }],
|
|
117
|
+
on: trigger,
|
|
118
|
+
style: look
|
|
119
|
+
})
|
|
120
|
+
for (const child of elementsOf(layer)) {
|
|
121
|
+
const drawnChild = counterpart(child, layer, drawn)
|
|
122
|
+
const otherChild = counterpart(child, layer, other)
|
|
123
|
+
if (drawnChild && otherChild) visit(child, drawnChild, otherChild)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
visit(trigger, active, inactive)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { StateElement } from '#dom-css/behaviours/states/types'
|
|
2
|
+
|
|
3
|
+
import { es, jsx } from '@pairdraw/emit'
|
|
4
|
+
import type { SceneGraph } from '@pairdraw/scene-graph'
|
|
5
|
+
import { findLayerByPath } from '@pairdraw/scene-graph/behaviours'
|
|
6
|
+
import {
|
|
7
|
+
isShaderPresetPaint,
|
|
8
|
+
shaderOfPaint,
|
|
9
|
+
type ShaderComponent,
|
|
10
|
+
type ShaderPreset,
|
|
11
|
+
type ShaderPresetPaint
|
|
12
|
+
} from '@pairdraw/scene-graph/shaders'
|
|
13
|
+
|
|
14
|
+
import { isPositioned } from '../projection'
|
|
15
|
+
import type { ComponentElement, ComponentNode } from './model'
|
|
16
|
+
|
|
17
|
+
/** A shader a layer fills with, drawn by the `shaders` library behind the layer's content. */
|
|
18
|
+
export interface ShaderLayer {
|
|
19
|
+
preset: ShaderPreset
|
|
20
|
+
/** The readable class the shader's canvas is styled by. */
|
|
21
|
+
className: string
|
|
22
|
+
/** Whether the layer is positioned already, so the shader can be placed inside it. */
|
|
23
|
+
positioned: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function shaderLayer(
|
|
27
|
+
node: StateElement,
|
|
28
|
+
className: string,
|
|
29
|
+
preset: ShaderPreset | undefined
|
|
30
|
+
): ShaderLayer | undefined {
|
|
31
|
+
if (!preset) return undefined
|
|
32
|
+
const position = node.base.position
|
|
33
|
+
return {
|
|
34
|
+
preset,
|
|
35
|
+
className: `${className}-shader`,
|
|
36
|
+
positioned: isPositioned(position)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The shader each element's layer fills with in the rest variant: the topmost visible shader
|
|
42
|
+
* fill, which the component plays behind the layer's content.
|
|
43
|
+
*/
|
|
44
|
+
export function shaderLayers(
|
|
45
|
+
graph: SceneGraph,
|
|
46
|
+
restId: string,
|
|
47
|
+
elements: readonly StateElement[]
|
|
48
|
+
): Map<StateElement, ShaderPreset> {
|
|
49
|
+
const shaders = new Map<StateElement, ShaderPreset>()
|
|
50
|
+
for (const element of elements) {
|
|
51
|
+
const layer = findLayerByPath(graph, restId, element.key.split('\0')[0] ?? '')
|
|
52
|
+
if (!layer) continue
|
|
53
|
+
const shader = layer.fills
|
|
54
|
+
.filter((fill) => fill.visible)
|
|
55
|
+
.map((fill) => shaderOfPaint(layer, fill))
|
|
56
|
+
.findLast((found): found is ShaderPresetPaint => found !== null && isShaderPresetPaint(found))
|
|
57
|
+
if (shader) shaders.set(element, shader.preset)
|
|
58
|
+
}
|
|
59
|
+
return shaders
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Whether an effect's name can be imported, as every effect the library has can. */
|
|
63
|
+
const isEffectName = (type: string) => es.isIdentifierName(type)
|
|
64
|
+
|
|
65
|
+
/** The effects of `components` a generated component draws: those it can import by name. */
|
|
66
|
+
export const drawnEffects = (components: readonly ShaderComponent[] | undefined) =>
|
|
67
|
+
(components ?? []).filter((component) => isEffectName(component.type))
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `import { Shader as ShaderCanvas, Aurora as ShaderAurora } from 'shaders/vue'`: the canvas and
|
|
71
|
+
* effects a component draws, under names apart from its own.
|
|
72
|
+
*/
|
|
73
|
+
export function shaderImport(source: 'shaders/vue' | 'shaders/react', effects: Iterable<string>) {
|
|
74
|
+
const names = ['Shader', ...[...effects].sort()]
|
|
75
|
+
return {
|
|
76
|
+
type: 'ImportDeclaration',
|
|
77
|
+
importKind: 'value',
|
|
78
|
+
specifiers: names.map((name) => ({
|
|
79
|
+
type: 'ImportSpecifier',
|
|
80
|
+
imported: es.identifier(name),
|
|
81
|
+
local: es.identifier(name === 'Shader' ? SHADER_CANVAS : shaderEffectName(name))
|
|
82
|
+
})),
|
|
83
|
+
source: es.string(source)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The local name a generated component imports effect `type` as, apart from its own names. */
|
|
88
|
+
export const shaderEffectName = (type: string) => `Shader${type}`
|
|
89
|
+
|
|
90
|
+
/** An effect's props as JSX attributes: strings as they are, anything else as JSON. */
|
|
91
|
+
export function shaderEffectAttributes(component: ShaderComponent): es.SyntaxNode[] {
|
|
92
|
+
return Object.entries(component.props ?? {}).map(([key, value]) =>
|
|
93
|
+
jsx.attribute(
|
|
94
|
+
key,
|
|
95
|
+
typeof value === 'string' ? jsx.stringValue(value) : jsx.container(es.json(value))
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The local name of the library's `Shader` canvas. */
|
|
101
|
+
export const SHADER_CANVAS = 'ShaderCanvas'
|
|
102
|
+
|
|
103
|
+
/** The elements of a component's tree that fill with a shader. */
|
|
104
|
+
export function shaderElements(node: ComponentNode): ComponentElement[] {
|
|
105
|
+
if (node.type !== 'element') return []
|
|
106
|
+
return [...(node.shader ? [node] : []), ...node.children.flatMap(shaderElements)]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The style that puts each shader behind its layer's content: the layer becomes its own
|
|
111
|
+
* stacking context, positioned if it is not already, and the shader fills it beneath
|
|
112
|
+
* everything it holds, clipped to its corners.
|
|
113
|
+
*/
|
|
114
|
+
export function shaderCSS(tree: ComponentElement): string {
|
|
115
|
+
return shaderElements(tree)
|
|
116
|
+
.flatMap(({ className, shader }) =>
|
|
117
|
+
shader
|
|
118
|
+
? [
|
|
119
|
+
`.${className} { isolation: isolate;${shader.positioned ? '' : ' position: relative;'} }`,
|
|
120
|
+
`.${shader.className} { position: absolute; inset: 0; z-index: -1; border-radius: inherit; overflow: hidden; pointer-events: none; }`
|
|
121
|
+
]
|
|
122
|
+
: []
|
|
123
|
+
)
|
|
124
|
+
.join('\n')
|
|
125
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { layerClassNames } from '#dom-css/behaviours/states/names'
|
|
2
|
+
import { stateTailwindClasses } from '#dom-css/behaviours/states/tailwind'
|
|
3
|
+
|
|
4
|
+
import type { ComponentModel } from './model'
|
|
5
|
+
import { shaderElements } from './shaders'
|
|
6
|
+
|
|
7
|
+
/** How a generated component is styled: a stylesheet, or Tailwind utilities in its markup. */
|
|
8
|
+
export const COMPONENT_STYLINGS = ['css', 'tailwind'] as const
|
|
9
|
+
export type ComponentStyling = (typeof COMPONENT_STYLINGS)[number]
|
|
10
|
+
|
|
11
|
+
/** Where a layer's shader plays: filling the layer, beneath its content, inside its corners. */
|
|
12
|
+
export const SHADER_UTILITIES = 'pointer-events-none absolute inset-0 -z-10 rounded-[inherit]'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The class each of a component's readable class names stands for. Styled with a stylesheet, a
|
|
16
|
+
* class is itself; with Tailwind it is the layer's utilities, its states behind their variants,
|
|
17
|
+
* and a shader canvas's placement.
|
|
18
|
+
*/
|
|
19
|
+
export function componentClasses(
|
|
20
|
+
component: ComponentModel,
|
|
21
|
+
styling: ComponentStyling
|
|
22
|
+
): (className: string) => string {
|
|
23
|
+
if (styling === 'css') return (className) => className
|
|
24
|
+
const readable = layerClassNames(component.styles)
|
|
25
|
+
const utilities = stateTailwindClasses(component.styles)
|
|
26
|
+
const byClass = new Map<string, string>()
|
|
27
|
+
for (const [element, className] of readable) byClass.set(className, utilities.get(element) ?? '')
|
|
28
|
+
for (const { shader } of shaderElements(component.tree))
|
|
29
|
+
if (shader) byClass.set(shader.className, SHADER_UTILITIES)
|
|
30
|
+
return (className) => byClass.get(className) ?? className
|
|
31
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { StateElement } from '#dom-css/behaviours/states/types'
|
|
2
|
+
|
|
3
|
+
import type { ShaderPreset } from '@pairdraw/scene-graph/shaders'
|
|
4
|
+
|
|
5
|
+
import type { InputLayers } from './fields'
|
|
6
|
+
import type { ComponentBinding, ComponentElement, ComponentNode } from './model'
|
|
7
|
+
import type { UsedLayer } from './references'
|
|
8
|
+
import type { RepeatedPart } from './repeats'
|
|
9
|
+
import { shaderLayer } from './shaders'
|
|
10
|
+
|
|
11
|
+
/** What each layer of the rest variant is to the component, gathered before its tree is built. */
|
|
12
|
+
export interface TreeLabels {
|
|
13
|
+
parts: Map<StateElement, string>
|
|
14
|
+
classes: Map<StateElement, string>
|
|
15
|
+
/** The text prop each bound text layer draws, by layer. */
|
|
16
|
+
texts: Map<StateElement, string>
|
|
17
|
+
/** Layers that use another component or an icon in place of drawing themselves. */
|
|
18
|
+
used: Map<StateElement, UsedLayer>
|
|
19
|
+
/** Layers drawn once per value, such as tab triggers and panels. */
|
|
20
|
+
repeated: Map<StateElement, RepeatedPart>
|
|
21
|
+
/** A field's text layer, drawn as its input, and the ones it replaces. */
|
|
22
|
+
input: InputLayers | null
|
|
23
|
+
/** The boolean prop that shows each bound layer. */
|
|
24
|
+
shownBy: Map<StateElement, string>
|
|
25
|
+
/** The slot prop each slot frame shows. */
|
|
26
|
+
slotOf: Map<StateElement, string>
|
|
27
|
+
/** The shader each layer fills with. */
|
|
28
|
+
shaders: Map<StateElement, ShaderPreset>
|
|
29
|
+
/** The layer of the rest variant each element draws. */
|
|
30
|
+
layerOf: (element: StateElement) => string | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function componentTree(
|
|
34
|
+
node: StateElement,
|
|
35
|
+
labels: TreeLabels,
|
|
36
|
+
bindings: ComponentBinding[]
|
|
37
|
+
): ComponentElement {
|
|
38
|
+
const repeated = labels.repeated.get(node)
|
|
39
|
+
const layerId = labels.layerOf(node)
|
|
40
|
+
const part = repeated?.part ?? labels.parts.get(node) ?? null
|
|
41
|
+
const text = labels.texts.get(node)
|
|
42
|
+
const className = labels.classes.get(node) ?? ''
|
|
43
|
+
return {
|
|
44
|
+
type: 'element',
|
|
45
|
+
part,
|
|
46
|
+
...(repeated ? { value: repeated.value } : {}),
|
|
47
|
+
tag: node.tagName,
|
|
48
|
+
className,
|
|
49
|
+
layerId,
|
|
50
|
+
attrs: node.attrs,
|
|
51
|
+
bindings: part === 'root' ? bindings : [],
|
|
52
|
+
shownBy: labels.shownBy.get(node),
|
|
53
|
+
slot: labels.slotOf.get(node),
|
|
54
|
+
shader: shaderLayer(node, className, labels.shaders.get(node)),
|
|
55
|
+
// A bound text layer draws its prop in place of the design's words and their runs.
|
|
56
|
+
children: text
|
|
57
|
+
? [{ type: 'textProp', name: text }]
|
|
58
|
+
: node.children.flatMap((child) =>
|
|
59
|
+
child.type === 'text' ? [{ type: 'text', value: child.text }] : childNode(child, labels)
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function childNode(element: StateElement, labels: TreeLabels): ComponentNode[] {
|
|
65
|
+
const className = labels.classes.get(element) ?? ''
|
|
66
|
+
const layerId = labels.layerOf(element)
|
|
67
|
+
if (labels.input?.element === element) return [{ type: 'input', className, layerId }]
|
|
68
|
+
if (labels.input?.replaced.includes(element)) return []
|
|
69
|
+
const used = labels.used.get(element)
|
|
70
|
+
if (!used) return [componentTree(element, labels, [])]
|
|
71
|
+
return [{ ...used, className, layerId }]
|
|
72
|
+
}
|