@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,195 @@
|
|
|
1
|
+
import type { ComponentPropertyDefinition, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
2
|
+
import {
|
|
3
|
+
behaviourContract,
|
|
4
|
+
behaviourOwner,
|
|
5
|
+
behaviourProperties,
|
|
6
|
+
booleanBinding,
|
|
7
|
+
hasBehaviour,
|
|
8
|
+
layerPath,
|
|
9
|
+
partBinding,
|
|
10
|
+
readBehaviour,
|
|
11
|
+
textBinding,
|
|
12
|
+
type Behaviour
|
|
13
|
+
} from '@pairdraw/scene-graph/behaviours'
|
|
14
|
+
import { findComponentPropertyTargets } from '@pairdraw/scene-graph/components'
|
|
15
|
+
import { instanceMainComponent } from '@pairdraw/scene-graph/instances'
|
|
16
|
+
import { instanceSlotFrames, slotPropertyId } from '@pairdraw/scene-graph/slots'
|
|
17
|
+
|
|
18
|
+
/** A boolean value of a control and the property that draws it. */
|
|
19
|
+
export interface BooleanModel {
|
|
20
|
+
definition: ComponentPropertyDefinition
|
|
21
|
+
on?: string
|
|
22
|
+
off?: string
|
|
23
|
+
/** The value as the document's instance draws it. */
|
|
24
|
+
designed: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The variant property that draws a control's interaction states. */
|
|
28
|
+
export interface StatesModel {
|
|
29
|
+
name: string
|
|
30
|
+
values: NonNullable<Behaviour['states']>
|
|
31
|
+
/** The value the instance shows at rest. */
|
|
32
|
+
rest: string | undefined
|
|
33
|
+
/** Whether the document's instance is drawn disabled. */
|
|
34
|
+
designedDisabled: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An instance with a behaviour below a root layer: its values, states, and bound layers. */
|
|
38
|
+
export interface ControlModel {
|
|
39
|
+
/** The instance's layer path below the root. */
|
|
40
|
+
path: string
|
|
41
|
+
kind: Behaviour['kind']
|
|
42
|
+
behaviour: Behaviour
|
|
43
|
+
booleans: Record<string, BooleanModel>
|
|
44
|
+
states: StatesModel | null
|
|
45
|
+
/** Each bound part's slot frame, by part id, as a layer path below the root. */
|
|
46
|
+
parts: Record<string, string>
|
|
47
|
+
/** Each bound text value's text layer, by value id, as a layer path below the root. */
|
|
48
|
+
texts: Record<string, { path: string; designed: string }>
|
|
49
|
+
/** The children of the items slot that are controls themselves, in order. */
|
|
50
|
+
items: ControlModel[]
|
|
51
|
+
/**
|
|
52
|
+
* Layers the control decides to show, which the design may hide: tab panels, or a
|
|
53
|
+
* collapsible's content, as layer paths below the root.
|
|
54
|
+
*/
|
|
55
|
+
reveal: string[]
|
|
56
|
+
/** For tabs, each panel's layer path, in order. */
|
|
57
|
+
panels: string[]
|
|
58
|
+
/** For tabs, each trigger's layer path, in order. */
|
|
59
|
+
triggers: string[]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function designedBoolean(
|
|
63
|
+
graph: SceneGraph,
|
|
64
|
+
instance: SceneNode,
|
|
65
|
+
definition: ComponentPropertyDefinition,
|
|
66
|
+
on: string | undefined
|
|
67
|
+
): boolean {
|
|
68
|
+
if (definition.type === 'BOOLEAN') {
|
|
69
|
+
const assignments = instance.componentPropertyAssignments
|
|
70
|
+
const value = Object.hasOwn(assignments, definition.id)
|
|
71
|
+
? assignments[definition.id]
|
|
72
|
+
: definition.defaultValue
|
|
73
|
+
return value === 'true'
|
|
74
|
+
}
|
|
75
|
+
return instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name] === on
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function statesModel(
|
|
79
|
+
graph: SceneGraph,
|
|
80
|
+
instance: SceneNode,
|
|
81
|
+
owner: SceneNode,
|
|
82
|
+
behaviour: Behaviour
|
|
83
|
+
): StatesModel | null {
|
|
84
|
+
const states = behaviour.states
|
|
85
|
+
const definition = states
|
|
86
|
+
? behaviourProperties(graph, owner).find(
|
|
87
|
+
(item) => item.id === states.propertyId && item.type === 'VARIANT'
|
|
88
|
+
)
|
|
89
|
+
: undefined
|
|
90
|
+
if (!states || !definition) return null
|
|
91
|
+
const authored = instanceMainComponent(graph, instance)?.componentPropertyValues[definition.name]
|
|
92
|
+
return {
|
|
93
|
+
name: definition.name,
|
|
94
|
+
values: states,
|
|
95
|
+
rest: states.rest ?? authored,
|
|
96
|
+
designedDisabled: !!states.disabled && authored === states.disabled
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function controlModel(graph: SceneGraph, rootId: string, instance: SceneNode): ControlModel | null {
|
|
101
|
+
const component = instanceMainComponent(graph, instance)
|
|
102
|
+
const owner = component && behaviourOwner(graph, component)
|
|
103
|
+
const behaviour = owner && readBehaviour(owner)
|
|
104
|
+
if (!owner || !behaviour) return null
|
|
105
|
+
const path = layerPath(graph, rootId, instance.id)
|
|
106
|
+
const properties = behaviourProperties(graph, owner)
|
|
107
|
+
const booleans: Record<string, BooleanModel> = {}
|
|
108
|
+
for (const valueId of Object.keys(behaviour.booleans)) {
|
|
109
|
+
const binding = booleanBinding(behaviour, valueId)
|
|
110
|
+
const definition = properties.find((item) => item.id === binding?.propertyId)
|
|
111
|
+
if (!binding || !definition) continue
|
|
112
|
+
booleans[valueId] = {
|
|
113
|
+
definition,
|
|
114
|
+
on: binding.on,
|
|
115
|
+
off: binding.off,
|
|
116
|
+
designed: designedBoolean(graph, instance, definition, binding.on)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const frames = instanceSlotFrames(graph, instance)
|
|
120
|
+
const parts = Object.fromEntries(
|
|
121
|
+
Object.entries(behaviour.parts).flatMap(([partId, propertyId]) => {
|
|
122
|
+
const frame = frames.find((item) => slotPropertyId(item) === propertyId)
|
|
123
|
+
return frame ? [[partId, layerPath(graph, rootId, frame.id)]] : []
|
|
124
|
+
})
|
|
125
|
+
)
|
|
126
|
+
const texts = Object.fromEntries(
|
|
127
|
+
Object.keys(behaviour.texts).flatMap((valueId) => {
|
|
128
|
+
const propertyId = textBinding(behaviour, valueId)
|
|
129
|
+
const target = propertyId
|
|
130
|
+
? findComponentPropertyTargets(graph, instance, propertyId).find(
|
|
131
|
+
(item) => item.node.type === 'TEXT'
|
|
132
|
+
)
|
|
133
|
+
: undefined
|
|
134
|
+
return target
|
|
135
|
+
? [
|
|
136
|
+
[
|
|
137
|
+
valueId,
|
|
138
|
+
{ path: layerPath(graph, rootId, target.node.id), designed: target.node.text }
|
|
139
|
+
]
|
|
140
|
+
]
|
|
141
|
+
: []
|
|
142
|
+
})
|
|
143
|
+
)
|
|
144
|
+
const frameOf = (partId: string) => {
|
|
145
|
+
const propertyId = partBinding(behaviour, partId)
|
|
146
|
+
return propertyId ? frames.find((item) => slotPropertyId(item) === propertyId) : undefined
|
|
147
|
+
}
|
|
148
|
+
const itemsPart = behaviourContract(behaviour.kind).parts.find((part) => part.items)
|
|
149
|
+
const itemsFrame = itemsPart ? frameOf(itemsPart.id) : undefined
|
|
150
|
+
const items = (itemsFrame ? graph.getChildren(itemsFrame.id) : []).flatMap((child) => {
|
|
151
|
+
const item = controlModel(graph, rootId, child)
|
|
152
|
+
return item ? [item] : []
|
|
153
|
+
})
|
|
154
|
+
const childPaths = (frame: SceneNode | undefined) =>
|
|
155
|
+
(frame ? graph.getChildren(frame.id) : []).map((child) => layerPath(graph, rootId, child.id))
|
|
156
|
+
const panels = behaviour.kind === 'tabs' ? childPaths(frameOf('panels')) : []
|
|
157
|
+
const content = behaviour.kind === 'collapsible' ? frameOf('content') : undefined
|
|
158
|
+
return {
|
|
159
|
+
path,
|
|
160
|
+
kind: behaviour.kind,
|
|
161
|
+
behaviour,
|
|
162
|
+
booleans,
|
|
163
|
+
states: statesModel(graph, instance, owner, behaviour),
|
|
164
|
+
parts,
|
|
165
|
+
texts,
|
|
166
|
+
items,
|
|
167
|
+
reveal: content ? [layerPath(graph, rootId, content.id)] : panels,
|
|
168
|
+
panels,
|
|
169
|
+
triggers: behaviour.kind === 'tabs' ? childPaths(frameOf('list')) : []
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The controls below a root layer, by layer path, outermost first. Items of a group are listed
|
|
175
|
+
* both in the group's `items` and on their own, since each keeps its own state. Preview runs
|
|
176
|
+
* them as live islands; export generates components from them.
|
|
177
|
+
*/
|
|
178
|
+
export function behaviourControls(graph: SceneGraph, rootId: string): Map<string, ControlModel> {
|
|
179
|
+
const controls = new Map<string, ControlModel>()
|
|
180
|
+
const visit = (node: SceneNode) => {
|
|
181
|
+
if (hasBehaviour(graph, node)) {
|
|
182
|
+
const control = controlModel(graph, rootId, node)
|
|
183
|
+
if (control) controls.set(control.path, control)
|
|
184
|
+
}
|
|
185
|
+
for (const child of graph.getChildren(node.id)) visit(child)
|
|
186
|
+
}
|
|
187
|
+
const root = graph.getNode(rootId)
|
|
188
|
+
if (root) visit(root)
|
|
189
|
+
return controls
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** A control's boolean value, if the behaviour binds it. */
|
|
193
|
+
export function booleanOf(control: ControlModel, valueId: string): BooleanModel | undefined {
|
|
194
|
+
return Object.hasOwn(control.booleans, valueId) ? control.booleans[valueId] : undefined
|
|
195
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { behaviourArgs, type BehaviourArgs, type BooleanArg } from './args'
|
|
2
|
+
export {
|
|
3
|
+
behaviourControls,
|
|
4
|
+
booleanOf,
|
|
5
|
+
type BooleanModel,
|
|
6
|
+
type ControlModel,
|
|
7
|
+
type StatesModel
|
|
8
|
+
} from './controls'
|
|
9
|
+
export { BUTTON_RESET, INPUT_RESET } from './reset'
|
|
10
|
+
export { controlRoles, groupOf, type ControlRole } from './roles'
|
|
11
|
+
export * from './states'
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Element styles a native `<button>` adds, cleared so the design's own styles show. Preview
|
|
3
|
+
* and generated components render control roots and triggers as buttons for their semantics.
|
|
4
|
+
*/
|
|
5
|
+
export const BUTTON_RESET: Record<string, string> = {
|
|
6
|
+
border: 'none',
|
|
7
|
+
padding: '0',
|
|
8
|
+
margin: '0',
|
|
9
|
+
background: 'none',
|
|
10
|
+
font: 'inherit',
|
|
11
|
+
color: 'inherit',
|
|
12
|
+
'text-align': 'inherit',
|
|
13
|
+
cursor: 'pointer'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Element styles the heading around an accordion trigger adds, cleared so the trigger lays
|
|
18
|
+
* out as designed while the heading keeps its level.
|
|
19
|
+
*/
|
|
20
|
+
export const HEADING_RESET: Record<string, string> = {
|
|
21
|
+
margin: '0',
|
|
22
|
+
font: 'inherit'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Element styles an input or textarea adds, cleared so the field's text layer it replaces
|
|
27
|
+
* keeps its look. Preview and generated components both draw a field's text as one.
|
|
28
|
+
*/
|
|
29
|
+
export const INPUT_RESET: Record<string, string> = {
|
|
30
|
+
border: 'none',
|
|
31
|
+
outline: 'none',
|
|
32
|
+
padding: '0',
|
|
33
|
+
margin: '0',
|
|
34
|
+
background: 'transparent',
|
|
35
|
+
resize: 'none',
|
|
36
|
+
'box-sizing': 'border-box'
|
|
37
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ControlModel } from './controls'
|
|
2
|
+
|
|
3
|
+
/** What a layer below a root is to the controls in it. */
|
|
4
|
+
export type ControlRole =
|
|
5
|
+
| { type: 'root'; control: ControlModel }
|
|
6
|
+
| { type: 'part'; control: ControlModel; part: string }
|
|
7
|
+
| { type: 'item'; group: ControlModel; control: ControlModel; index: number }
|
|
8
|
+
| { type: 'trigger'; control: ControlModel; index: number }
|
|
9
|
+
| { type: 'panel'; control: ControlModel; index: number }
|
|
10
|
+
| { type: 'text'; control: ControlModel; valueId: string }
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Each layer's role by layer path: control roots, their parts, items of groups, tab triggers
|
|
14
|
+
* and panels, and the text layers that become inputs. Items of a group take the item role in
|
|
15
|
+
* place of their root role, since the group drives them.
|
|
16
|
+
*/
|
|
17
|
+
export function controlRoles(
|
|
18
|
+
controls: ReadonlyMap<string, ControlModel>
|
|
19
|
+
): Map<string, ControlRole> {
|
|
20
|
+
const roles = new Map<string, ControlRole>()
|
|
21
|
+
for (const control of controls.values()) {
|
|
22
|
+
if (!roles.has(control.path)) roles.set(control.path, { type: 'root', control })
|
|
23
|
+
for (const [part, path] of Object.entries(control.parts))
|
|
24
|
+
if (!roles.has(path)) roles.set(path, { type: 'part', control, part })
|
|
25
|
+
for (const [valueId, text] of Object.entries(control.texts))
|
|
26
|
+
roles.set(text.path, { type: 'text', control, valueId })
|
|
27
|
+
for (const [index, path] of control.triggers.entries())
|
|
28
|
+
roles.set(path, { type: 'trigger', control, index })
|
|
29
|
+
for (const [index, path] of control.panels.entries())
|
|
30
|
+
roles.set(path, { type: 'panel', control, index })
|
|
31
|
+
for (const [index, item] of control.items.entries())
|
|
32
|
+
roles.set(item.path, { type: 'item', group: control, control: item, index })
|
|
33
|
+
}
|
|
34
|
+
return roles
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The group a control is an item of, if any. */
|
|
38
|
+
export function groupOf(
|
|
39
|
+
roles: ReadonlyMap<string, ControlRole>,
|
|
40
|
+
control: ControlModel
|
|
41
|
+
): ControlModel | undefined {
|
|
42
|
+
const role = roles.get(control.path)
|
|
43
|
+
return role?.type === 'item' ? role.group : undefined
|
|
44
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { ComponentPropertyDefinition, SceneGraph, SceneNode } from '@pairdraw/scene-graph'
|
|
2
|
+
import {
|
|
3
|
+
behaviourProperties,
|
|
4
|
+
readBehaviour,
|
|
5
|
+
type BehaviourKind
|
|
6
|
+
} from '@pairdraw/scene-graph/behaviours'
|
|
7
|
+
import { variantDefaultValue } from '@pairdraw/scene-graph/components'
|
|
8
|
+
|
|
9
|
+
import { behaviourArgs, type BehaviourArgs, type BooleanArg } from '../args'
|
|
10
|
+
import type { StateCondition } from './types'
|
|
11
|
+
|
|
12
|
+
/** The `data-state` value a boolean prop sets when on, as Reka and Radix set it. */
|
|
13
|
+
const DATA_STATE_ON: Partial<Record<string, string>> = {
|
|
14
|
+
checked: 'checked',
|
|
15
|
+
pressed: 'on',
|
|
16
|
+
open: 'open'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const INTERACTIONS = ['hover', 'pressed', 'focus'] as const
|
|
20
|
+
|
|
21
|
+
/** Controls whose focus lands on a layer inside them: a slider's thumb, a field's input. */
|
|
22
|
+
const FOCUS_WITHIN = new Set<BehaviourKind>(['slider', 'textField', 'textarea', 'numberField'])
|
|
23
|
+
|
|
24
|
+
/** The condition a variant property's value adds, or `null` for its rest value. */
|
|
25
|
+
type ValueCondition = (value: string) => StateCondition | null
|
|
26
|
+
|
|
27
|
+
function booleanCondition(arg: BooleanArg): ValueCondition {
|
|
28
|
+
return (value) => {
|
|
29
|
+
if (value === arg.off) return null
|
|
30
|
+
if (arg.name === 'disabled') return { type: 'disabled' }
|
|
31
|
+
const state = DATA_STATE_ON[arg.name]
|
|
32
|
+
return state ? { type: 'state', value: state } : { type: 'prop', name: arg.name, value }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The interaction-state property's values: hover, pressed, focus, and disabled. */
|
|
37
|
+
function interactionCondition(set: SceneNode, states: NonNullable<BehaviourArgs['states']>) {
|
|
38
|
+
const behaviour = readBehaviour(set)
|
|
39
|
+
const within = behaviour ? FOCUS_WITHIN.has(behaviour.kind) : false
|
|
40
|
+
const byValue = new Map<string, StateCondition>()
|
|
41
|
+
for (const state of INTERACTIONS) {
|
|
42
|
+
const value = behaviour?.states?.[state]
|
|
43
|
+
if (value !== undefined && value !== states.rest)
|
|
44
|
+
byValue.set(value, {
|
|
45
|
+
type: 'interaction',
|
|
46
|
+
state,
|
|
47
|
+
...(state === 'focus' && within ? { within } : {})
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
if (states.disabled !== undefined) byValue.set(states.disabled, { type: 'disabled' })
|
|
51
|
+
const name = states.property
|
|
52
|
+
return (value: string): StateCondition | null => {
|
|
53
|
+
if (value === states.rest) return null
|
|
54
|
+
// A value the behaviour does not map to a state still shows its variant, as a prop.
|
|
55
|
+
return byValue.get(value) ?? { type: 'prop', name, value }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Any other variant property is a prop the generated component sets as `data-*`, at rest at
|
|
61
|
+
* its default value, the default variant's when the document leaves it empty.
|
|
62
|
+
*/
|
|
63
|
+
function propCondition(
|
|
64
|
+
graph: SceneGraph,
|
|
65
|
+
set: SceneNode,
|
|
66
|
+
definition: ComponentPropertyDefinition
|
|
67
|
+
): ValueCondition {
|
|
68
|
+
const rest = variantDefaultValue(graph, set, definition)
|
|
69
|
+
return (value) => (value === rest ? null : { type: 'prop', name: definition.name, value })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Reads the conditions that show each variant of a set: from its behaviour's boolean props and
|
|
74
|
+
* interaction states, and every other variant property as a prop. `null` for a variant that
|
|
75
|
+
* leaves a property out, which can't be placed among the others.
|
|
76
|
+
*/
|
|
77
|
+
export function variantConditions(
|
|
78
|
+
graph: SceneGraph,
|
|
79
|
+
set: SceneNode
|
|
80
|
+
): (variant: SceneNode) => StateCondition[] | null {
|
|
81
|
+
const args = behaviourArgs(graph, set)
|
|
82
|
+
const conditionOf = (definition: ComponentPropertyDefinition): ValueCondition => {
|
|
83
|
+
const boolean = args?.booleans.get(definition.name)
|
|
84
|
+
if (boolean) return booleanCondition(boolean)
|
|
85
|
+
if (args?.states?.property === definition.name) return interactionCondition(set, args.states)
|
|
86
|
+
const filled = args?.filled
|
|
87
|
+
if (filled?.property === definition.name)
|
|
88
|
+
return (value) => (value === filled.on ? { type: 'filled' } : null)
|
|
89
|
+
return propCondition(graph, set, definition)
|
|
90
|
+
}
|
|
91
|
+
const byProperty = new Map(
|
|
92
|
+
behaviourProperties(graph, set)
|
|
93
|
+
.filter((definition) => definition.type === 'VARIANT')
|
|
94
|
+
.map((definition) => [definition.name, conditionOf(definition)] as const)
|
|
95
|
+
)
|
|
96
|
+
return (variant) => {
|
|
97
|
+
const conditions: StateCondition[] = []
|
|
98
|
+
for (const [property, condition] of byProperty) {
|
|
99
|
+
if (!Object.hasOwn(variant.componentPropertyValues, property)) return null
|
|
100
|
+
const added = condition(variant.componentPropertyValues[property])
|
|
101
|
+
if (added) conditions.push(added)
|
|
102
|
+
}
|
|
103
|
+
return conditions
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DesignDocument,
|
|
3
|
+
DesignElement,
|
|
4
|
+
DesignNode,
|
|
5
|
+
DesignStyleDeclaration
|
|
6
|
+
} from '#dom-css/types'
|
|
7
|
+
import cssesc from 'cssesc'
|
|
8
|
+
import { compact, sortBy } from 'es-toolkit/array'
|
|
9
|
+
|
|
10
|
+
import { layerClassNames, propAttribute } from './names'
|
|
11
|
+
import type { StateCondition, StateElement, StateNode, StateStyles } from './types'
|
|
12
|
+
|
|
13
|
+
export interface StateStylesheet {
|
|
14
|
+
/** The merged markup, each layer carrying its class and no inline style. */
|
|
15
|
+
document: DesignDocument
|
|
16
|
+
css: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** A CSS string for an attribute selector's value. */
|
|
20
|
+
const quoted = (value: string) => cssesc(value, { quotes: 'double', wrap: true })
|
|
21
|
+
|
|
22
|
+
/** A field whose input or textarea shows text rather than its placeholder. */
|
|
23
|
+
const FILLED = ':has(:is(input, textarea):not(:placeholder-shown))'
|
|
24
|
+
|
|
25
|
+
/** The selector part a condition adds to the control's root. */
|
|
26
|
+
export function conditionSelector(condition: StateCondition): string {
|
|
27
|
+
if (condition.type === 'state') return `[data-state=${quoted(condition.value)}]`
|
|
28
|
+
if (condition.type === 'disabled') return '[data-disabled]'
|
|
29
|
+
if (condition.type === 'filled') return FILLED
|
|
30
|
+
if (condition.type === 'prop')
|
|
31
|
+
return `[${propAttribute(condition.name)}=${quoted(condition.value)}]`
|
|
32
|
+
if (condition.state === 'focus') return condition.within ? ':focus-within' : ':focus-visible'
|
|
33
|
+
// A disabled control keeps its disabled look under the pointer.
|
|
34
|
+
return `${condition.state === 'hover' ? ':hover' : ':active'}:not([data-disabled])`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function designNode(node: StateNode, classes: Map<StateElement, string>): DesignNode {
|
|
38
|
+
if (node.type === 'text') return node
|
|
39
|
+
const className = compact([node.attrs.class, classes.get(node)]).join(' ')
|
|
40
|
+
const element: DesignElement = {
|
|
41
|
+
type: 'element',
|
|
42
|
+
tagName: node.tagName,
|
|
43
|
+
attrs: { ...node.attrs, class: className },
|
|
44
|
+
children: node.children.map((child) => designNode(child, classes)),
|
|
45
|
+
shader: node.shader
|
|
46
|
+
}
|
|
47
|
+
return element
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The state styles as a stylesheet with a readable class per layer. A rule with more
|
|
52
|
+
* conditions comes later and has a more specific selector, so a combined variant wins over
|
|
53
|
+
* each of its parts. The sheet is built with the CSS object model, which rejects an invalid
|
|
54
|
+
* selector and prints the declarations.
|
|
55
|
+
*/
|
|
56
|
+
export async function stateStylesToCSS(styles: StateStyles): Promise<StateStylesheet> {
|
|
57
|
+
const classes = layerClassNames(styles)
|
|
58
|
+
const rootClass = classes.get(styles.root) ?? ''
|
|
59
|
+
const rules: { order: number; selector: string; style: DesignStyleDeclaration }[] = []
|
|
60
|
+
for (const [element, className] of classes) {
|
|
61
|
+
const own = element === styles.root ? '' : ` .${className}`
|
|
62
|
+
if (Object.keys(element.base).length > 0)
|
|
63
|
+
rules.push({ order: 0, selector: `.${rootClass}${own}`, style: element.base })
|
|
64
|
+
for (const rule of element.rules) {
|
|
65
|
+
const when = rule.conditions.map(conditionSelector).join('')
|
|
66
|
+
const on = rule.on && classes.get(rule.on)
|
|
67
|
+
const pseudo = rule.pseudo ? `::${rule.pseudo}` : ''
|
|
68
|
+
const tested = on
|
|
69
|
+
? ` .${on}${when}${rule.on === element ? '' : own}${pseudo}`
|
|
70
|
+
: `${when}${own}${pseudo}`
|
|
71
|
+
rules.push({
|
|
72
|
+
order: rule.conditions.length,
|
|
73
|
+
selector: `.${rootClass}${tested}`,
|
|
74
|
+
style: rule.style
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const sheet = await writableStyleSheet()
|
|
79
|
+
for (const rule of sortBy(rules, [(item) => item.order])) {
|
|
80
|
+
const index = sheet.insertRule(`${rule.selector} {}`)
|
|
81
|
+
for (const [property, value] of Object.entries(rule.style))
|
|
82
|
+
sheet.setProperty(index, property, value)
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
document: { type: 'document', children: [designNode(styles.root, classes)] },
|
|
86
|
+
css: sheet.text()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A stylesheet rules are inserted into and printed from. */
|
|
91
|
+
interface WritableStyleSheet {
|
|
92
|
+
/** Parses `rule` at the end, throwing on an invalid selector, and returns its index. */
|
|
93
|
+
insertRule(rule: string): number
|
|
94
|
+
setProperty(index: number, property: string, value: string): void
|
|
95
|
+
text(): string
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The browser's own CSS object model where there is one, and otherwise `@acemir/cssom`, loaded
|
|
100
|
+
* only when a stylesheet is written. That package's browser build only defines a global and
|
|
101
|
+
* exports nothing, so a browser bundle cannot use it.
|
|
102
|
+
*/
|
|
103
|
+
async function writableStyleSheet(): Promise<WritableStyleSheet> {
|
|
104
|
+
if (typeof globalThis.CSSStyleSheet === 'function') {
|
|
105
|
+
const sheet = new globalThis.CSSStyleSheet()
|
|
106
|
+
return {
|
|
107
|
+
insertRule: (rule) => sheet.insertRule(rule, sheet.cssRules.length),
|
|
108
|
+
setProperty: (index, property, value) => {
|
|
109
|
+
const rule = sheet.cssRules[index]
|
|
110
|
+
if (rule instanceof CSSStyleRule) rule.style.setProperty(property, value)
|
|
111
|
+
},
|
|
112
|
+
text: () => Array.from(sheet.cssRules, (rule) => rule.cssText).join('\n')
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const { CSSStyleSheet } = await import('@acemir/cssom')
|
|
116
|
+
const sheet = new CSSStyleSheet()
|
|
117
|
+
return {
|
|
118
|
+
insertRule: (rule) => sheet.insertRule(rule, sheet.cssRules.length),
|
|
119
|
+
setProperty: (index, property, value) =>
|
|
120
|
+
sheet.cssRules[index]?.style.setProperty(property, value),
|
|
121
|
+
text: () => sheet.toString()
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { stateStyles } from './model'
|
|
2
|
+
export type { StateCondition, StateElement, StateNode, StateRule, StateStyles } from './types'
|
|
3
|
+
export { conditionSelector, stateStylesToCSS, type StateStylesheet } from './css'
|
|
4
|
+
export { stateStylesToTailwind, stateTailwindClasses, type StateTailwindOptions } from './tailwind'
|