@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,86 @@
1
+ import type { CSSStyleSheetLike } from '@acemir/cssom'
2
+ import valueParser from 'postcss-value-parser'
3
+
4
+ type Parse = (cssText: string) => CSSStyleSheetLike
5
+
6
+ const AT_RULES = ['@media', '@supports', '@container'] as const
7
+
8
+ /** What a rule looks like to the checks below; the object model's own types are wider. */
9
+ type ParsedRule = {
10
+ cssRules?: unknown[]
11
+ selectorText?: string
12
+ style?: { length: number; getPropertyValue(property: string): string }
13
+ }
14
+
15
+ function rules(sheet: CSSStyleSheetLike): ParsedRule[] {
16
+ return sheet.cssRules.filter(
17
+ (rule): rule is ParsedRule => typeof rule === 'object' && rule !== null
18
+ )
19
+ }
20
+
21
+ /**
22
+ * Token strings come from documents, so each one is parsed in place before it is written. A value
23
+ * or condition that closes its rule early parses to a different number of rules and fails.
24
+ */
25
+ export function createTokenValidator(parse: Parse) {
26
+ /** `--name: value` is one declaration whose value reads back unchanged. */
27
+ function declaration(name: string, value: string): boolean {
28
+ const parsed = rules(parse(`:root{--${name}:${value}}`))
29
+ const [rule] = parsed
30
+ return (
31
+ parsed.length === 1 &&
32
+ rule.style?.length === 1 &&
33
+ rule.style.getPropertyValue(`--${name}`).trim() === value.trim() &&
34
+ (rule.cssRules?.length ?? 0) === 0
35
+ )
36
+ }
37
+
38
+ return {
39
+ declaration,
40
+
41
+ /**
42
+ * A custom property name without its `--`: one word to the CSS value tokenizer, which splits
43
+ * on spaces, colons, commas and slashes, and one declaration to the parser. It starts with a
44
+ * letter, digit or underscore, so `--` is never doubled into `----`.
45
+ */
46
+ name(name: string): boolean {
47
+ const { nodes } = valueParser(`--${name}`)
48
+ return (
49
+ /^\w/.test(name) &&
50
+ nodes.length === 1 &&
51
+ nodes[0]?.type === 'word' &&
52
+ declaration(name, '0')
53
+ )
54
+ },
55
+
56
+ /** A selector, or a `@media`, `@supports` or `@container` prelude, that wraps exactly one rule. */
57
+ condition(condition: string): boolean {
58
+ const trimmed = condition.trim()
59
+ if (trimmed.startsWith('@')) {
60
+ if (
61
+ !AT_RULES.some((rule) => trimmed.startsWith(`${rule} `) || trimmed.startsWith(`${rule}(`))
62
+ )
63
+ return false
64
+ const parsed = rules(parse(`${trimmed}{:root{--t:0}}`))
65
+ return parsed.length === 1 && parsed[0]?.cssRules?.length === 1
66
+ }
67
+ const parsed = rules(parse(`${trimmed}{--t:0}`))
68
+ const [rule] = parsed
69
+ return (
70
+ parsed.length === 1 &&
71
+ typeof rule.selectorText === 'string' &&
72
+ rule.style?.length === 1 &&
73
+ (rule.cssRules?.length ?? 0) === 0
74
+ )
75
+ }
76
+ }
77
+ }
78
+
79
+ export type TokenValidator = ReturnType<typeof createTokenValidator>
80
+
81
+ /** The browser's own CSS parser, through a constructable stylesheet. */
82
+ export function parseWithBrowser(cssText: string): CSSStyleSheetLike {
83
+ const sheet = new CSSStyleSheet()
84
+ sheet.replaceSync(cssText)
85
+ return { cssRules: [...sheet.cssRules] }
86
+ }
@@ -0,0 +1,44 @@
1
+ import type { TokenUnit, Variable, VariableScope } from '@pairdraw/scene-graph'
2
+ import { tokenNumberInUnit } from '@pairdraw/scene-graph/variables'
3
+
4
+ import { variableNamespace } from './names'
5
+
6
+ const UNITLESS_SCOPES = new Set<VariableScope>(['OPACITY', 'FONT_STYLE', 'FONT_VARIATIONS'])
7
+
8
+ /**
9
+ * A property that takes values in each token unit, where a browser checks or computes a CSS
10
+ * expression written for the token: `scale` takes any plain number, `width` any length.
11
+ */
12
+ export const TOKEN_UNIT_PROPERTY: Record<TokenUnit, string> = {
13
+ none: 'scale',
14
+ px: 'width',
15
+ rem: 'width',
16
+ '%': 'width',
17
+ ms: 'transition-duration',
18
+ s: 'transition-duration',
19
+ deg: 'rotate'
20
+ }
21
+
22
+ /**
23
+ * The unit a FLOAT token is written in. An explicit unit wins; opacity and font weights are
24
+ * unitless; every other number is a pixel length, which is what the canvas draws.
25
+ */
26
+ export function variableUnit(variable: Variable): TokenUnit {
27
+ if (variable.type !== 'FLOAT') return 'none'
28
+ if (variable.unit) return variable.unit
29
+ const scopes = variable.scopes ?? []
30
+ if (scopes.length > 0 && scopes.every((scope) => UNITLESS_SCOPES.has(scope))) return 'none'
31
+ return variableNamespace(variable) === 'font-weight' ? 'none' : 'px'
32
+ }
33
+
34
+ /** Six decimals keep `rem` exact for every pixel step down to 1/1024px (0.5px is 0.03125rem). */
35
+ function trimNumber(value: number): string {
36
+ return String(Number(value.toFixed(6)))
37
+ }
38
+
39
+ /** A stored number written in its unit: 24 as `rem` is `1.5rem`, 150 as `ms` is `150ms`. */
40
+ export function tokenNumberToCSS(value: number, unit: TokenUnit): string {
41
+ const number = trimNumber(tokenNumberInUnit(value, unit))
42
+ if (unit === 'none' || number === '0') return number
43
+ return `${number}${unit}`
44
+ }
package/src/types.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'
2
+ import type { ShaderPreset } from '@pairdraw/scene-graph/shaders'
3
+
4
+ import type { DesignTokens } from './tokens/references'
5
+
6
+ export type DesignNode = DesignElement | DesignText
7
+
8
+ export interface DesignDocument {
9
+ type: 'document'
10
+ children: DesignNode[]
11
+ stylesheets?: DesignStyleSheet[]
12
+ sourceGraph?: SceneGraph
13
+ /** Variable references the projection wrote, for Tailwind names and the token stylesheet. */
14
+ tokens?: DesignTokens
15
+ }
16
+
17
+ export interface DesignElement {
18
+ type: 'element'
19
+ tagName: string
20
+ attrs: Record<string, string>
21
+ children: DesignNode[]
22
+ inlineStyle?: DesignStyleDeclaration
23
+ computedStyle?: DesignStyleDeclaration
24
+ sourceSceneNodeId?: string
25
+ sourceSceneNode?: SceneNode
26
+ /**
27
+ * The shader the layer fills with, which plays behind its content: a framework draws it with
28
+ * the `shaders` library, static HTML shows `frame`, its still frame as a data URL.
29
+ */
30
+ shader?: { preset: ShaderPreset; frame?: string }
31
+ }
32
+
33
+ export interface DesignText {
34
+ type: 'text'
35
+ text: string
36
+ }
37
+
38
+ export interface DesignStyleSheet {
39
+ type: 'stylesheet'
40
+ cssText: string
41
+ href?: string
42
+ }
43
+
44
+ export type DesignStyleDeclaration = Record<string, string>
45
+
46
+ export interface CSSComputeOptions {
47
+ includeBrowserDefaults?: boolean
48
+ }
49
+
50
+ export interface CSSRuntime {
51
+ readonly kind: 'browser' | 'headless'
52
+ parseHTML(html: string): DesignDocument
53
+ serializeHTML(document: DesignDocument): string
54
+ computeStyles(
55
+ document: DesignDocument,
56
+ cssText?: string,
57
+ options?: CSSComputeOptions
58
+ ): Promise<DesignDocument>
59
+ }