@pairdraw/dom-css 0.0.0-stage → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -2
  3. package/dist/browser.d.ts +29 -0
  4. package/dist/browser.js +2 -0
  5. package/dist/chunks/behaviours/args.js +59 -0
  6. package/dist/chunks/behaviours/args.js.map +1 -0
  7. package/dist/chunks/behaviours/controls.js +109 -0
  8. package/dist/chunks/behaviours/controls.js.map +1 -0
  9. package/dist/chunks/behaviours/reset.js +40 -0
  10. package/dist/chunks/behaviours/reset.js.map +1 -0
  11. package/dist/chunks/behaviours/roles.js +51 -0
  12. package/dist/chunks/behaviours/roles.js.map +1 -0
  13. package/dist/chunks/behaviours/states/conditions.js +103 -0
  14. package/dist/chunks/behaviours/states/conditions.js.map +1 -0
  15. package/dist/chunks/behaviours/states/css.js +172 -0
  16. package/dist/chunks/behaviours/states/css.js.map +1 -0
  17. package/dist/chunks/behaviours/states/layers.js +1736 -0
  18. package/dist/chunks/behaviours/states/layers.js.map +1 -0
  19. package/dist/chunks/behaviours/states/model.js +144 -0
  20. package/dist/chunks/behaviours/states/model.js.map +1 -0
  21. package/dist/chunks/behaviours/states/tailwind.js +198 -0
  22. package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
  23. package/dist/chunks/browser.js +1352 -0
  24. package/dist/chunks/browser.js.map +1 -0
  25. package/dist/chunks/export/bundle.js +337 -0
  26. package/dist/chunks/export/bundle.js.map +1 -0
  27. package/dist/chunks/export/components/drawn.js +60 -0
  28. package/dist/chunks/export/components/drawn.js.map +1 -0
  29. package/dist/chunks/export/components/fields.js +178 -0
  30. package/dist/chunks/export/components/fields.js.map +1 -0
  31. package/dist/chunks/export/components/groups.js +297 -0
  32. package/dist/chunks/export/components/groups.js.map +1 -0
  33. package/dist/chunks/export/components/icons.js +260 -0
  34. package/dist/chunks/export/components/icons.js.map +1 -0
  35. package/dist/chunks/export/components/layers.js +1599 -0
  36. package/dist/chunks/export/components/layers.js.map +1 -0
  37. package/dist/chunks/export/index.js +477 -0
  38. package/dist/chunks/export/index.js.map +1 -0
  39. package/dist/chunks/export/storybook/component.js +448 -0
  40. package/dist/chunks/export/storybook/component.js.map +1 -0
  41. package/dist/chunks/export/storybook/export.js +410 -0
  42. package/dist/chunks/export/storybook/export.js.map +1 -0
  43. package/dist/chunks/import/html.js +406 -0
  44. package/dist/chunks/import/html.js.map +1 -0
  45. package/dist/chunks/import/jsx/dev-runtime.js +37 -0
  46. package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
  47. package/dist/chunks/tokens/cssom-validator.js +10 -0
  48. package/dist/chunks/tokens/cssom-validator.js.map +1 -0
  49. package/dist/export.d.ts +594 -0
  50. package/dist/export.js +13 -0
  51. package/dist/index.d.ts +115 -0
  52. package/dist/index.js +7 -0
  53. package/dist/jsx-dev-runtime.d.ts +8 -0
  54. package/dist/jsx-dev-runtime.js +3 -0
  55. package/dist/jsx-runtime.d.ts +20 -0
  56. package/dist/jsx-runtime.js +3 -0
  57. package/dist/rolldown-runtime.js +13 -0
  58. package/dist/runtime.d.ts +10 -0
  59. package/dist/scene-graph.d.ts +2 -0
  60. package/dist/scene-graph.js +2 -0
  61. package/dist/scene-graph2.d.ts +10 -0
  62. package/dist/styling.d.ts +93 -0
  63. package/dist/tailwind.d.ts +39 -0
  64. package/dist/types.d.ts +89 -0
  65. package/package.json +88 -4
  66. package/src/behaviours/args.ts +85 -0
  67. package/src/behaviours/controls.ts +195 -0
  68. package/src/behaviours/index.ts +11 -0
  69. package/src/behaviours/reset.ts +37 -0
  70. package/src/behaviours/roles.ts +44 -0
  71. package/src/behaviours/states/conditions.ts +105 -0
  72. package/src/behaviours/states/css.ts +123 -0
  73. package/src/behaviours/states/index.ts +4 -0
  74. package/src/behaviours/states/layers.ts +205 -0
  75. package/src/behaviours/states/model.ts +175 -0
  76. package/src/behaviours/states/names.ts +39 -0
  77. package/src/behaviours/states/tailwind.ts +148 -0
  78. package/src/behaviours/states/types.ts +56 -0
  79. package/src/browser.ts +146 -0
  80. package/src/cssom.d.ts +38 -0
  81. package/src/export/bundle.ts +461 -0
  82. package/src/export/components/drawn.ts +73 -0
  83. package/src/export/components/fields.ts +178 -0
  84. package/src/export/components/groups.ts +78 -0
  85. package/src/export/components/icons.ts +230 -0
  86. package/src/export/components/layers.ts +121 -0
  87. package/src/export/components/model.ts +544 -0
  88. package/src/export/components/properties.ts +167 -0
  89. package/src/export/components/react/fields.ts +129 -0
  90. package/src/export/components/react/index.ts +412 -0
  91. package/src/export/components/react/props.ts +151 -0
  92. package/src/export/components/react/radix.ts +52 -0
  93. package/src/export/components/react/shared.ts +76 -0
  94. package/src/export/components/references.ts +212 -0
  95. package/src/export/components/repeats.ts +128 -0
  96. package/src/export/components/shaders.ts +125 -0
  97. package/src/export/components/styling.ts +31 -0
  98. package/src/export/components/tree.ts +72 -0
  99. package/src/export/components/vue.ts +517 -0
  100. package/src/export/css.ts +57 -0
  101. package/src/export/grid.ts +42 -0
  102. package/src/export/html.ts +163 -0
  103. package/src/export/index.ts +44 -0
  104. package/src/export/projection.ts +581 -0
  105. package/src/export/storybook/component.ts +334 -0
  106. package/src/export/storybook/export.ts +430 -0
  107. package/src/export/storybook/groups.ts +161 -0
  108. package/src/export/storybook/module.ts +319 -0
  109. package/src/export/storybook/names.ts +51 -0
  110. package/src/export/tailwind-jsx.ts +156 -0
  111. package/src/export/text.ts +415 -0
  112. package/src/import/css-text.ts +4 -0
  113. package/src/import/css-values.ts +31 -0
  114. package/src/import/flow.ts +248 -0
  115. package/src/import/html.ts +88 -0
  116. package/src/import/jsx/core.ts +130 -0
  117. package/src/import/jsx/dev-runtime.ts +8 -0
  118. package/src/import/jsx/runtime.ts +76 -0
  119. package/src/import/scene-graph.ts +582 -0
  120. package/src/import/style-attribute.ts +25 -0
  121. package/src/import/tailwind.ts +54 -0
  122. package/src/import/text.ts +518 -0
  123. package/src/index.ts +51 -0
  124. package/src/runtime/browser/runtime.ts +301 -0
  125. package/src/runtime/headless/runtime.ts +99 -0
  126. package/src/runtime/headless/styles.ts +330 -0
  127. package/src/runtime/index.ts +11 -0
  128. package/src/tokens/conditions.ts +66 -0
  129. package/src/tokens/cssom-validator.ts +8 -0
  130. package/src/tokens/custom-properties.ts +331 -0
  131. package/src/tokens/expression.ts +128 -0
  132. package/src/tokens/formula.ts +140 -0
  133. package/src/tokens/index.ts +43 -0
  134. package/src/tokens/names.ts +161 -0
  135. package/src/tokens/references.ts +201 -0
  136. package/src/tokens/stylesheet.ts +304 -0
  137. package/src/tokens/validate.ts +86 -0
  138. package/src/tokens/values.ts +44 -0
  139. package/src/types.ts +59 -0
@@ -0,0 +1,88 @@
1
+ import { parse, type DefaultTreeAdapterTypes } from 'parse5'
2
+
3
+ import type { SceneGraph } from '@pairdraw/scene-graph'
4
+
5
+ import { createCSSRuntime } from '../runtime'
6
+ import type { CSSComputeOptions, CSSRuntime, DesignDocument } from '../types'
7
+ import { mergeCSSText } from './css-text'
8
+ import { designDocumentToSceneGraph, type ToSceneGraphOptions } from './scene-graph'
9
+ import { compileTailwindCSS, type CompileTailwindCSSOptions } from './tailwind'
10
+
11
+ export interface HTMLToDesignDocumentOptions {
12
+ cssText?: string
13
+ runtime?: CSSRuntime
14
+ compute?: CSSComputeOptions
15
+ }
16
+
17
+ export interface HTMLToSceneGraphOptions extends HTMLToDesignDocumentOptions, ToSceneGraphOptions {}
18
+
19
+ export interface TailwindHTMLToDesignDocumentOptions
20
+ extends Omit<HTMLToDesignDocumentOptions, 'cssText'>, CompileTailwindCSSOptions {}
21
+
22
+ export interface TailwindHTMLToSceneGraphOptions
23
+ extends TailwindHTMLToDesignDocumentOptions, ToSceneGraphOptions {}
24
+
25
+ function runtimeForOptions(runtime: CSSRuntime | undefined) {
26
+ return runtime ?? createCSSRuntime()
27
+ }
28
+
29
+ function getChildNodes(node: DefaultTreeAdapterTypes.Node): DefaultTreeAdapterTypes.ChildNode[] {
30
+ return 'childNodes' in node ? node.childNodes : []
31
+ }
32
+
33
+ function textContent(node: DefaultTreeAdapterTypes.Node): string {
34
+ if (node.nodeName === '#text' && 'value' in node) return node.value
35
+ return getChildNodes(node).map(textContent).join('')
36
+ }
37
+
38
+ function collectStyleText(node: DefaultTreeAdapterTypes.Node, styles: string[]): void {
39
+ if ('tagName' in node && node.tagName.toLowerCase() === 'style') {
40
+ const css = textContent(node).trim()
41
+ if (css) styles.push(css)
42
+ return
43
+ }
44
+
45
+ for (const child of getChildNodes(node)) collectStyleText(child, styles)
46
+ }
47
+
48
+ function extractEmbeddedCSSText(html: string): string | undefined {
49
+ const styles: string[] = []
50
+ collectStyleText(parse(html), styles)
51
+ return styles.length > 0 ? styles.join('\n') : undefined
52
+ }
53
+
54
+ export async function htmlToDesignDocument(
55
+ html: string,
56
+ options: HTMLToDesignDocumentOptions = {}
57
+ ): Promise<DesignDocument> {
58
+ const runtime = runtimeForOptions(options.runtime)
59
+ const document = runtime.parseHTML(html)
60
+ const cssText = mergeCSSText(extractEmbeddedCSSText(html), options.cssText)
61
+ return runtime.computeStyles(document, cssText, options.compute)
62
+ }
63
+
64
+ export async function htmlToSceneGraph(
65
+ html: string,
66
+ options: HTMLToSceneGraphOptions = {}
67
+ ): Promise<SceneGraph> {
68
+ const document = await htmlToDesignDocument(html, options)
69
+ return designDocumentToSceneGraph(document, options)
70
+ }
71
+
72
+ export async function tailwindHTMLToDesignDocument(
73
+ html: string,
74
+ candidates: string | Iterable<string>,
75
+ options: TailwindHTMLToDesignDocumentOptions = {}
76
+ ): Promise<DesignDocument> {
77
+ const cssText = await compileTailwindCSS(candidates, options)
78
+ return htmlToDesignDocument(html, { ...options, cssText })
79
+ }
80
+
81
+ export async function tailwindHTMLToSceneGraph(
82
+ html: string,
83
+ candidates: string | Iterable<string>,
84
+ options: TailwindHTMLToSceneGraphOptions = {}
85
+ ): Promise<SceneGraph> {
86
+ const document = await tailwindHTMLToDesignDocument(html, candidates, options)
87
+ return designDocumentToSceneGraph(document, options)
88
+ }
@@ -0,0 +1,130 @@
1
+ import type { DesignDocument, DesignNode, DesignStyleDeclaration } from '#dom-css/types'
2
+
3
+ export const Fragment = Symbol.for('pairdraw.dom-css.fragment')
4
+
5
+ type JSXComponent = (props: JSXElementProps) => JSXChild
6
+ export type JSXTag = string | JSXComponent | typeof Fragment
7
+ export type JSXStyleValue = string | number | null | undefined
8
+ export type JSXStyleObject = Record<string, JSXStyleValue>
9
+ export type JSXStyleInput = string | JSXStyleObject
10
+ export type JSXChild = DesignNode | JSXChild[] | string | number | boolean | null | undefined
11
+
12
+ export interface JSXElementProps {
13
+ children?: JSXChild
14
+ class?: string
15
+ className?: string
16
+ style?: JSXStyleInput
17
+ key?: string | number
18
+ [name: string]: unknown
19
+ }
20
+
21
+ function cssPropertyName(name: string) {
22
+ if (name.startsWith('--')) return name
23
+ return name.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`)
24
+ }
25
+
26
+ function styleObjectToDeclaration(style: JSXStyleObject): DesignStyleDeclaration | undefined {
27
+ const entries = Object.entries(style)
28
+ .filter(
29
+ (entry): entry is [string, string | number] => entry[1] !== null && entry[1] !== undefined
30
+ )
31
+ .map(([property, value]) => [cssPropertyName(property), String(value)] as const)
32
+ .filter(([, value]) => value.length > 0)
33
+
34
+ if (entries.length === 0) return undefined
35
+ return Object.fromEntries(entries)
36
+ }
37
+
38
+ function styleStringToDeclaration(style: string): DesignStyleDeclaration | undefined {
39
+ const entries = style
40
+ .split(';')
41
+ .map((declaration) => declaration.split(':'))
42
+ .filter((parts): parts is [string, string, ...string[]] => parts.length >= 2)
43
+ .map(([property, ...value]) => [property.trim().toLowerCase(), value.join(':').trim()] as const)
44
+ .filter(([property, value]) => property.length > 0 && value.length > 0)
45
+
46
+ if (entries.length === 0) return undefined
47
+ return Object.fromEntries(entries)
48
+ }
49
+
50
+ function styleToDeclaration(style: JSXStyleInput | undefined): DesignStyleDeclaration | undefined {
51
+ if (typeof style === 'string') return styleStringToDeclaration(style)
52
+ if (style) return styleObjectToDeclaration(style)
53
+ return undefined
54
+ }
55
+
56
+ function attributeValue(value: unknown): string | undefined {
57
+ if (value === null || value === undefined || value === false) return undefined
58
+ if (value === true) return ''
59
+ if (typeof value === 'string') return value
60
+ if (typeof value === 'number' || typeof value === 'bigint') return String(value)
61
+ return undefined
62
+ }
63
+
64
+ function propsToAttrs(props: JSXElementProps): Record<string, string> {
65
+ const attrs: Record<string, string> = {}
66
+ const classValue = props.class ?? props.className
67
+ if (classValue) attrs.class = classValue
68
+
69
+ for (const [name, value] of Object.entries(props)) {
70
+ if (name === 'children' || name === 'class' || name === 'className' || name === 'style') {
71
+ continue
72
+ }
73
+ if (name === 'key' || name.startsWith('on')) continue
74
+ const attr = attributeValue(value)
75
+ if (attr !== undefined) attrs[name] = attr
76
+ }
77
+
78
+ return attrs
79
+ }
80
+
81
+ function normalizeChild(child: JSXChild, nodes: DesignNode[]): void {
82
+ if (child === null || child === undefined || typeof child === 'boolean') return
83
+ if (Array.isArray(child)) {
84
+ for (const item of child) normalizeChild(item, nodes)
85
+ return
86
+ }
87
+ if (typeof child === 'string' || typeof child === 'number') {
88
+ nodes.push({ type: 'text', text: String(child) })
89
+ return
90
+ }
91
+ nodes.push(child)
92
+ }
93
+
94
+ function normalizeJSXChildren(children: JSXChild): DesignNode[] {
95
+ const nodes: DesignNode[] = []
96
+ normalizeChild(children, nodes)
97
+ return nodes
98
+ }
99
+
100
+ export function jsx(tag: JSXTag, props: JSXElementProps = {}): DesignNode | DesignNode[] {
101
+ if (tag === Fragment) return normalizeJSXChildren(props.children)
102
+ if (typeof tag === 'function') return normalizeJSXChildren(tag(props))
103
+
104
+ return {
105
+ type: 'element',
106
+ tagName: tag,
107
+ attrs: propsToAttrs(props),
108
+ inlineStyle: styleToDeclaration(props.style),
109
+ children: normalizeJSXChildren(props.children)
110
+ }
111
+ }
112
+
113
+ export const jsxs = jsx
114
+
115
+ export function jsxToDesignDocumentCore(input: JSXChild): DesignDocument {
116
+ return {
117
+ type: 'document',
118
+ children: normalizeJSXChildren(input)
119
+ }
120
+ }
121
+
122
+ export namespace JSX {
123
+ export type Element = DesignNode | DesignNode[]
124
+ export interface ElementChildrenAttribute {
125
+ children: unknown
126
+ }
127
+ export interface IntrinsicElements {
128
+ [tagName: string]: JSXElementProps
129
+ }
130
+ }
@@ -0,0 +1,8 @@
1
+ import { jsx, type JSXElementProps, type JSXTag } from './runtime'
2
+
3
+ export { Fragment } from './runtime'
4
+ export type * from './runtime'
5
+
6
+ export function jsxDEV(tag: JSXTag, props: JSXElementProps = {}) {
7
+ return jsx(tag, props)
8
+ }
@@ -0,0 +1,76 @@
1
+ import type { CSSComputeOptions, CSSRuntime, DesignDocument } from '#dom-css/types'
2
+
3
+ import type { SceneGraph } from '@pairdraw/scene-graph'
4
+
5
+ import { designDocumentToSceneGraph, type ToSceneGraphOptions } from '../scene-graph'
6
+ import { compileTailwindCSS, type CompileTailwindCSSOptions } from '../tailwind'
7
+ import { jsxToDesignDocumentCore, type JSXChild } from './core'
8
+
9
+ export { Fragment, jsx, jsxs } from './core'
10
+ export type {
11
+ JSXChild,
12
+ JSXElementProps,
13
+ JSXStyleInput,
14
+ JSXStyleObject,
15
+ JSXStyleValue,
16
+ JSXTag
17
+ } from './core'
18
+
19
+ export interface JSXToDesignDocumentOptions {
20
+ cssText?: string
21
+ runtime?: CSSRuntime
22
+ compute?: CSSComputeOptions
23
+ }
24
+
25
+ export interface JSXToSceneGraphOptions extends JSXToDesignDocumentOptions, ToSceneGraphOptions {}
26
+
27
+ export interface TailwindJSXToDesignDocumentOptions
28
+ extends Omit<JSXToDesignDocumentOptions, 'cssText'>, CompileTailwindCSSOptions {}
29
+
30
+ export interface TailwindJSXToSceneGraphOptions
31
+ extends TailwindJSXToDesignDocumentOptions, ToSceneGraphOptions {}
32
+
33
+ async function runtimeForOptions(runtime: CSSRuntime | undefined) {
34
+ if (runtime) return runtime
35
+ const { createCSSRuntime } = await import('#dom-css/runtime')
36
+ return createCSSRuntime()
37
+ }
38
+
39
+ export async function jsxToDesignDocument(
40
+ input: JSXChild,
41
+ options: JSXToDesignDocumentOptions = {}
42
+ ): Promise<DesignDocument> {
43
+ const document = jsxToDesignDocumentCore(input)
44
+
45
+ if (!options.cssText && !options.runtime && !options.compute) return document
46
+ const runtime = await runtimeForOptions(options.runtime)
47
+ return runtime.computeStyles(document, options.cssText, options.compute)
48
+ }
49
+
50
+ export async function jsxToSceneGraph(
51
+ input: JSXChild,
52
+ options: JSXToSceneGraphOptions = {}
53
+ ): Promise<SceneGraph> {
54
+ const document = await jsxToDesignDocument(input, options)
55
+ return designDocumentToSceneGraph(document, options)
56
+ }
57
+
58
+ export async function tailwindJSXToDesignDocument(
59
+ input: JSXChild,
60
+ candidates: string | Iterable<string>,
61
+ options: TailwindJSXToDesignDocumentOptions = {}
62
+ ): Promise<DesignDocument> {
63
+ const cssText = await compileTailwindCSS(candidates, options)
64
+ return jsxToDesignDocument(input, { ...options, cssText })
65
+ }
66
+
67
+ export async function tailwindJSXToSceneGraph(
68
+ input: JSXChild,
69
+ candidates: string | Iterable<string>,
70
+ options: TailwindJSXToSceneGraphOptions = {}
71
+ ): Promise<SceneGraph> {
72
+ const document = await tailwindJSXToDesignDocument(input, candidates, options)
73
+ return designDocumentToSceneGraph(document, options)
74
+ }
75
+
76
+ export type { JSX } from './core'