@pairdraw/dom-css 0.0.0-stage → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -2
  3. package/dist/browser.d.ts +29 -0
  4. package/dist/browser.js +2 -0
  5. package/dist/chunks/behaviours/args.js +59 -0
  6. package/dist/chunks/behaviours/args.js.map +1 -0
  7. package/dist/chunks/behaviours/controls.js +109 -0
  8. package/dist/chunks/behaviours/controls.js.map +1 -0
  9. package/dist/chunks/behaviours/reset.js +40 -0
  10. package/dist/chunks/behaviours/reset.js.map +1 -0
  11. package/dist/chunks/behaviours/roles.js +51 -0
  12. package/dist/chunks/behaviours/roles.js.map +1 -0
  13. package/dist/chunks/behaviours/states/conditions.js +103 -0
  14. package/dist/chunks/behaviours/states/conditions.js.map +1 -0
  15. package/dist/chunks/behaviours/states/css.js +172 -0
  16. package/dist/chunks/behaviours/states/css.js.map +1 -0
  17. package/dist/chunks/behaviours/states/layers.js +1736 -0
  18. package/dist/chunks/behaviours/states/layers.js.map +1 -0
  19. package/dist/chunks/behaviours/states/model.js +144 -0
  20. package/dist/chunks/behaviours/states/model.js.map +1 -0
  21. package/dist/chunks/behaviours/states/tailwind.js +198 -0
  22. package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
  23. package/dist/chunks/browser.js +1352 -0
  24. package/dist/chunks/browser.js.map +1 -0
  25. package/dist/chunks/export/bundle.js +337 -0
  26. package/dist/chunks/export/bundle.js.map +1 -0
  27. package/dist/chunks/export/components/drawn.js +60 -0
  28. package/dist/chunks/export/components/drawn.js.map +1 -0
  29. package/dist/chunks/export/components/fields.js +178 -0
  30. package/dist/chunks/export/components/fields.js.map +1 -0
  31. package/dist/chunks/export/components/groups.js +297 -0
  32. package/dist/chunks/export/components/groups.js.map +1 -0
  33. package/dist/chunks/export/components/icons.js +260 -0
  34. package/dist/chunks/export/components/icons.js.map +1 -0
  35. package/dist/chunks/export/components/layers.js +1594 -0
  36. package/dist/chunks/export/components/layers.js.map +1 -0
  37. package/dist/chunks/export/index.js +477 -0
  38. package/dist/chunks/export/index.js.map +1 -0
  39. package/dist/chunks/export/storybook/component.js +448 -0
  40. package/dist/chunks/export/storybook/component.js.map +1 -0
  41. package/dist/chunks/export/storybook/export.js +410 -0
  42. package/dist/chunks/export/storybook/export.js.map +1 -0
  43. package/dist/chunks/import/html.js +406 -0
  44. package/dist/chunks/import/html.js.map +1 -0
  45. package/dist/chunks/import/jsx/dev-runtime.js +37 -0
  46. package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
  47. package/dist/chunks/tokens/cssom-validator.js +10 -0
  48. package/dist/chunks/tokens/cssom-validator.js.map +1 -0
  49. package/dist/export.d.ts +594 -0
  50. package/dist/export.js +13 -0
  51. package/dist/index.d.ts +115 -0
  52. package/dist/index.js +7 -0
  53. package/dist/jsx-dev-runtime.d.ts +8 -0
  54. package/dist/jsx-dev-runtime.js +3 -0
  55. package/dist/jsx-runtime.d.ts +20 -0
  56. package/dist/jsx-runtime.js +3 -0
  57. package/dist/rolldown-runtime.js +13 -0
  58. package/dist/runtime.d.ts +10 -0
  59. package/dist/scene-graph.d.ts +2 -0
  60. package/dist/scene-graph.js +2 -0
  61. package/dist/scene-graph2.d.ts +10 -0
  62. package/dist/styling.d.ts +93 -0
  63. package/dist/tailwind.d.ts +39 -0
  64. package/dist/types.d.ts +89 -0
  65. package/package.json +73 -4
  66. package/src/behaviours/args.ts +85 -0
  67. package/src/behaviours/controls.ts +195 -0
  68. package/src/behaviours/index.ts +11 -0
  69. package/src/behaviours/reset.ts +37 -0
  70. package/src/behaviours/roles.ts +44 -0
  71. package/src/behaviours/states/conditions.ts +105 -0
  72. package/src/behaviours/states/css.ts +123 -0
  73. package/src/behaviours/states/index.ts +4 -0
  74. package/src/behaviours/states/layers.ts +205 -0
  75. package/src/behaviours/states/model.ts +175 -0
  76. package/src/behaviours/states/names.ts +39 -0
  77. package/src/behaviours/states/tailwind.ts +148 -0
  78. package/src/behaviours/states/types.ts +56 -0
  79. package/src/browser.ts +146 -0
  80. package/src/cssom.d.ts +38 -0
  81. package/src/export/bundle.ts +461 -0
  82. package/src/export/components/drawn.ts +73 -0
  83. package/src/export/components/fields.ts +178 -0
  84. package/src/export/components/groups.ts +78 -0
  85. package/src/export/components/icons.ts +230 -0
  86. package/src/export/components/layers.ts +121 -0
  87. package/src/export/components/model.ts +544 -0
  88. package/src/export/components/properties.ts +167 -0
  89. package/src/export/components/react/fields.ts +129 -0
  90. package/src/export/components/react/index.ts +606 -0
  91. package/src/export/components/react/shared.ts +76 -0
  92. package/src/export/components/references.ts +212 -0
  93. package/src/export/components/repeats.ts +128 -0
  94. package/src/export/components/shaders.ts +125 -0
  95. package/src/export/components/styling.ts +31 -0
  96. package/src/export/components/tree.ts +72 -0
  97. package/src/export/components/vue.ts +517 -0
  98. package/src/export/css.ts +57 -0
  99. package/src/export/grid.ts +42 -0
  100. package/src/export/html.ts +163 -0
  101. package/src/export/index.ts +44 -0
  102. package/src/export/projection.ts +581 -0
  103. package/src/export/storybook/component.ts +334 -0
  104. package/src/export/storybook/export.ts +430 -0
  105. package/src/export/storybook/groups.ts +161 -0
  106. package/src/export/storybook/module.ts +319 -0
  107. package/src/export/storybook/names.ts +51 -0
  108. package/src/export/tailwind-jsx.ts +156 -0
  109. package/src/export/text.ts +415 -0
  110. package/src/import/css-text.ts +4 -0
  111. package/src/import/css-values.ts +31 -0
  112. package/src/import/flow.ts +248 -0
  113. package/src/import/html.ts +88 -0
  114. package/src/import/jsx/core.ts +130 -0
  115. package/src/import/jsx/dev-runtime.ts +8 -0
  116. package/src/import/jsx/runtime.ts +76 -0
  117. package/src/import/scene-graph.ts +582 -0
  118. package/src/import/style-attribute.ts +25 -0
  119. package/src/import/tailwind.ts +54 -0
  120. package/src/import/text.ts +518 -0
  121. package/src/index.ts +51 -0
  122. package/src/runtime/browser/runtime.ts +301 -0
  123. package/src/runtime/headless/runtime.ts +99 -0
  124. package/src/runtime/headless/styles.ts +330 -0
  125. package/src/runtime/index.ts +11 -0
  126. package/src/tokens/conditions.ts +66 -0
  127. package/src/tokens/cssom-validator.ts +8 -0
  128. package/src/tokens/custom-properties.ts +331 -0
  129. package/src/tokens/expression.ts +128 -0
  130. package/src/tokens/formula.ts +140 -0
  131. package/src/tokens/index.ts +43 -0
  132. package/src/tokens/names.ts +161 -0
  133. package/src/tokens/references.ts +201 -0
  134. package/src/tokens/stylesheet.ts +304 -0
  135. package/src/tokens/validate.ts +86 -0
  136. package/src/tokens/values.ts +44 -0
  137. package/src/types.ts +59 -0
@@ -0,0 +1,163 @@
1
+ import { escapeAttribute, escapeText } from 'entities'
2
+ import { twirl } from 'twirlwind'
3
+
4
+ import type { DesignDocument, DesignElement, DesignNode, DesignText } from '../types'
5
+
6
+ export interface SerializeHTMLOptions {
7
+ style?: 'inline' | 'tailwind'
8
+ /** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */
9
+ themeVariables?: readonly string[]
10
+ /**
11
+ * Put each child of an element that holds only elements on its own indented line, for code
12
+ * people read. Text keeps its whitespace; whitespace between elements in a projected layer
13
+ * changes nothing, since its children are placed by flex, grid, or absolute position.
14
+ */
15
+ indent?: boolean
16
+ }
17
+
18
+ const VOID_ELEMENTS = new Set([
19
+ 'area',
20
+ 'base',
21
+ 'br',
22
+ 'col',
23
+ 'embed',
24
+ 'hr',
25
+ 'img',
26
+ 'input',
27
+ 'link',
28
+ 'meta',
29
+ 'param',
30
+ 'source',
31
+ 'track',
32
+ 'wbr'
33
+ ])
34
+
35
+ export function splitWhitespace(value: string): string[] {
36
+ const parts: string[] = []
37
+ let current = ''
38
+ for (const char of value) {
39
+ if (char === ' ' || char === '\n' || char === '\t' || char === '\r' || char === '\f') {
40
+ if (current.length > 0) parts.push(current)
41
+ current = ''
42
+ } else {
43
+ current += char
44
+ }
45
+ }
46
+ if (current.length > 0) parts.push(current)
47
+ return parts
48
+ }
49
+
50
+ function serializeText(node: DesignText): string {
51
+ return escapeText(node.text)
52
+ }
53
+
54
+ function serializeStyle(node: DesignElement): string | undefined {
55
+ if (!node.inlineStyle || Object.keys(node.inlineStyle).length === 0) return undefined
56
+ return Object.entries(node.inlineStyle)
57
+ .filter(([, value]) => value !== '')
58
+ .map(([property, value]) => `${property}: ${value}`)
59
+ .join('; ')
60
+ }
61
+
62
+ export function serializeTailwindClasses(
63
+ node: DesignElement,
64
+ themeVariables: readonly string[] = []
65
+ ): string | undefined {
66
+ const style = serializeStyle(node)
67
+ if (!style) return undefined
68
+ const className = twirl(style, { theme: { variables: themeVariables } })
69
+ return className.length > 0 ? className : undefined
70
+ }
71
+
72
+ export function mergeClassNames(...values: Array<string | undefined>): string | undefined {
73
+ const className = values
74
+ .flatMap((value) => (value ? splitWhitespace(value) : []))
75
+ .map((value) => value.trim())
76
+ .filter((value) => value.length > 0)
77
+ .join(' ')
78
+ return className.length > 0 ? className : undefined
79
+ }
80
+
81
+ function serializeAttrs(node: DesignElement, options: SerializeHTMLOptions): string {
82
+ const style = serializeStyle(node)
83
+ const tailwindClass =
84
+ options.style === 'tailwind'
85
+ ? serializeTailwindClasses(node, options.themeVariables)
86
+ : undefined
87
+ const attrsWithoutStyle = { ...node.attrs }
88
+ delete attrsWithoutStyle.style
89
+ const sourceAttrs = options.style === 'tailwind' && tailwindClass ? attrsWithoutStyle : node.attrs
90
+ const attrs: Record<string, string | undefined> = { ...sourceAttrs }
91
+ if (tailwindClass) attrs.class = mergeClassNames(node.attrs.class, tailwindClass)
92
+ if (style && options.style !== 'tailwind') attrs.style = style
93
+ const serialized = Object.entries(attrs)
94
+ .filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1] !== '')
95
+ .map(([name, value]) => `${name}="${escapeAttribute(value)}"`)
96
+
97
+ if (serialized.length === 0) return ''
98
+ return ` ${serialized.join(' ')}`
99
+ }
100
+
101
+ /** Static HTML cannot play a shader, so its layer shows the still frame behind its content. */
102
+ const SHADER_FRAME_STYLE =
103
+ 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover;z-index:-1;border-radius:inherit;pointer-events:none'
104
+
105
+ function shaderFrame(node: DesignElement): string {
106
+ const frame = node.shader?.frame
107
+ return frame ? `<img src="${escapeAttribute(frame)}" alt="" style="${SHADER_FRAME_STYLE}">` : ''
108
+ }
109
+
110
+ function serializeElement(
111
+ node: DesignElement,
112
+ options: SerializeHTMLOptions,
113
+ depth: number
114
+ ): string {
115
+ // SVG names such as `linearGradient` keep their case, as an SVG parser needs them.
116
+ const tagName = node.tagName
117
+ const attrs = serializeAttrs(node, options)
118
+ if (VOID_ELEMENTS.has(tagName.toLowerCase())) return `<${tagName}${attrs}>`
119
+ const children = [
120
+ shaderFrame(node),
121
+ ...node.children.map((child) => serializeNode(child, options, depth + 1))
122
+ ].filter((child) => child !== '')
123
+ const block =
124
+ options.indent && children.length > 0 && node.children.every((child) => child.type !== 'text')
125
+ if (!block) return `<${tagName}${attrs}>${children.join('')}</${tagName}>`
126
+ const inner = ' '.repeat(depth + 1)
127
+ const outer = ' '.repeat(depth)
128
+ return `<${tagName}${attrs}>\n${children.map((child) => inner + child).join('\n')}\n${outer}</${tagName}>`
129
+ }
130
+
131
+ export function serializeNode(
132
+ node: DesignNode,
133
+ options: SerializeHTMLOptions = {},
134
+ depth = 0
135
+ ): string {
136
+ return node.type === 'text' ? serializeText(node) : serializeElement(node, options, depth)
137
+ }
138
+
139
+ /**
140
+ * The elements `serializeHTML` writes, in the order they open: the document's own, and `null`
141
+ * for a shader's still frame, which it adds in front of its layer's content.
142
+ */
143
+ export function serializedElements(document: DesignDocument): (DesignElement | null)[] {
144
+ const visit = (node: DesignNode): (DesignElement | null)[] => {
145
+ if (node.type === 'text') return []
146
+ if (VOID_ELEMENTS.has(node.tagName.toLowerCase())) return [node]
147
+ return [node, ...(node.shader?.frame ? [null] : []), ...node.children.flatMap(visit)]
148
+ }
149
+ return document.children.flatMap(visit)
150
+ }
151
+
152
+ export function serializeHTML(
153
+ document: DesignDocument,
154
+ options: SerializeHTMLOptions = {}
155
+ ): string {
156
+ const resolved: SerializeHTMLOptions = {
157
+ ...options,
158
+ themeVariables: options.themeVariables ?? document.tokens?.themeVariables()
159
+ }
160
+ return document.children
161
+ .map((node) => serializeNode(node, resolved))
162
+ .join(options.indent ? '\n' : '')
163
+ }
@@ -0,0 +1,44 @@
1
+ // SceneGraph → HTML and JSX without the CSS runtimes, so browsers can bundle it.
2
+ export {
3
+ sceneGraphToDesignDocument,
4
+ sceneNodeToDesignDocument,
5
+ type SceneGraphToDesignOptions,
6
+ type VectorElementRenderer
7
+ } from './projection'
8
+ export { exportHTMLBundle } from './bundle'
9
+ export {
10
+ designDocumentToTailwindJSX,
11
+ sceneNodesToTailwindJSX,
12
+ sceneNodesToTailwindJSXWithLayers,
13
+ type TailwindJSXWithLayers
14
+ } from './tailwind-jsx'
15
+ export { serializeHTML } from './html'
16
+ export {
17
+ LAYER_COMPONENT_FRAMEWORKS,
18
+ layerComponents,
19
+ layerMarkup,
20
+ type LayerComponent,
21
+ type LayerComponentFramework,
22
+ type LayerMarkup
23
+ } from './components/layers'
24
+ export { COMPONENT_STYLINGS, type ComponentStyling } from './components/styling'
25
+ export {
26
+ ICON_PACKAGES,
27
+ iconPackagesFor,
28
+ isIconPackage,
29
+ type IconFramework,
30
+ type IconPackage
31
+ } from './components/icons'
32
+ export * from '../tokens'
33
+ export * from '../behaviours'
34
+ export type {
35
+ ExportHTMLBundle,
36
+ ExportHTMLBundleOptions,
37
+ ExportHTMLFile,
38
+ TailwindStylesheetId,
39
+ TailwindStylesheetLoader,
40
+ WebFontFaceAsset,
41
+ WebFontFaceRequest,
42
+ WebFontFaceResolver
43
+ } from './bundle'
44
+ export type { DesignDocument, DesignElement, DesignNode } from '../types'