Skip to content

AI-native design editor. Open-source Figma alternative.

feat(dom-css): add JSX authoring bridge

Danila Poyarkov1f55891c3d16
diff --git a/packages/dom-css/README.md b/packages/dom-css/README.md
index 00a8de9a7..d2251983f 100644
--- a/packages/dom-css/README.md
+++ b/packages/dom-css/README.md
@@ -70,6 +70,27 @@ const graph = await htmlToSceneGraph(
For DesignDOM output without creating a scene graph, use `htmlToDesignDocument()`.
+## JSX/DOM authoring
+
+Use the package as a JSX import source when you want DOM-shaped authoring that still flows through DesignDOM, CSSOM, and SceneGraph conversion:
+
+```tsx
+/** @jsxImportSource @open-pencil/dom-css */
+import { createBrowserCSSRuntime, jsxToSceneGraph } from '@open-pencil/dom-css'
+
+const graph = await jsxToSceneGraph(
+ <article class="card">
+ <h1>OpenPencil</h1>
+ </article>,
+ {
+ cssText: '.card { display: flex; width: 320px; padding: 24px; }',
+ runtime: createBrowserCSSRuntime({ sandbox: 'iframe' })
+ }
+)
+```
+
+The JSX runtime preserves `class`, attributes, inline `style`, text, fragments, and simple function components as DesignDOM. Class semantics still come from generated or authored CSS passed to a CSS runtime; the JSX layer does not interpret Tailwind or CSS utility names directly.
+
## Tailwind pipeline
Tailwind classes flow through Tailwind's own compiler, then through the CSS runtime. Prefer the browser runtime when a document is available so custom properties, `calc()`, modern colors, and browser-default behavior come from native `getComputedStyle()`:
@@ -93,6 +114,7 @@ const graph = await tailwindHTMLToSceneGraph(
- Browser-backed runtime adapter for native HTML parsing, serialization, and computed-style extraction
- Headless runtime adapter with `parse5` HTML parsing and CSSOM-backed style computation for basic selectors, nested CSSOM rules, cascade order, inheritance, common shorthands, and simple custom-property/calc values
- SceneGraph ⇄ DesignDOM conversion for simple HTML/CSS-shaped layouts, including flex alignment, logical padding, independent side borders, constraints, clipping, opacity, typography, and shadows
+- JSX runtime helpers for DOM-shaped authoring into DesignDOM and SceneGraph
- Tailwind v4 compiler adapter
- Browser oracle fixtures for CSS variables, `calc()`, modern color output, and Tailwind utility output
diff --git a/packages/dom-css/package.json b/packages/dom-css/package.json
index 3d2fbc156..a4b09832a 100644
--- a/packages/dom-css/package.json
+++ b/packages/dom-css/package.json
@@ -16,6 +16,18 @@
"bun": "./src/index.ts",
"import": "./dist/index.js",
"default": "./dist/index.js"
+ },
+ "./jsx-runtime": {
+ "types": "./dist/jsx-runtime.d.ts",
+ "bun": "./src/jsx/runtime.ts",
+ "import": "./dist/jsx-runtime.js",
+ "default": "./dist/jsx-runtime.js"
+ },
+ "./jsx-dev-runtime": {
+ "types": "./dist/jsx-dev-runtime.d.ts",
+ "bun": "./src/jsx/dev-runtime.ts",
+ "import": "./dist/jsx-dev-runtime.js",
+ "default": "./dist/jsx-dev-runtime.js"
}
},
"main": "./dist/index.js",
diff --git a/packages/dom-css/scripts/smoke-dist.ts b/packages/dom-css/scripts/smoke-dist.ts
index efc892058..fd8022518 100644
--- a/packages/dom-css/scripts/smoke-dist.ts
+++ b/packages/dom-css/scripts/smoke-dist.ts
@@ -1,10 +1,17 @@
-import {
- compileTailwindCSS,
- createHeadlessCSSRuntime,
- htmlToSceneGraph,
- serializeHTML,
- type DesignDocument
-} from '../dist/index.js'
+import type * as DomCSS from '../src/index'
+
+const distPath = '../dist/index.js'
+const dist = await import(distPath)
+
+const compileTailwindCSS: typeof DomCSS.compileTailwindCSS = dist.compileTailwindCSS
+const createHeadlessCSSRuntime: typeof DomCSS.createHeadlessCSSRuntime =
+ dist.createHeadlessCSSRuntime
+const htmlToSceneGraph: typeof DomCSS.htmlToSceneGraph = dist.htmlToSceneGraph
+const jsx: typeof DomCSS.jsx = dist.jsx
+const jsxToDesignDocument: typeof DomCSS.jsxToDesignDocument = dist.jsxToDesignDocument
+const serializeHTML: typeof DomCSS.serializeHTML = dist.serializeHTML
+
+type DesignDocument = DomCSS.DesignDocument
const document: DesignDocument = {
type: 'document',
@@ -37,3 +44,10 @@ const card = page ? graph.getChildren(page.id)[0] : undefined
if (card?.type !== 'FRAME' || card.width !== 320) {
throw new Error('Expected built htmlToSceneGraph() to produce a sized frame')
}
+
+const jsxDocument = await jsxToDesignDocument(
+ jsx('article', { class: 'card', children: 'OpenPencil' })
+)
+if (jsxDocument.children[0]?.type !== 'element') {
+ throw new Error('Expected built JSX helpers to produce DesignDOM elements')
+}
diff --git a/packages/dom-css/src/index.ts b/packages/dom-css/src/index.ts
index 3215e97c9..878d7abcf 100644
--- a/packages/dom-css/src/index.ts
+++ b/packages/dom-css/src/index.ts
@@ -9,6 +9,15 @@ export {
export { designDocumentToSceneGraph } from './to-scene-graph'
export { sceneGraphToDesignDocument } from './from-scene-graph'
export { compileTailwindCSS } from './tailwind'
+export {
+ Fragment,
+ jsx,
+ jsxToDesignDocument,
+ jsxToSceneGraph,
+ jsxs,
+ tailwindJSXToDesignDocument,
+ tailwindJSXToSceneGraph
+} from './jsx/runtime'
export type {
HTMLToDesignDocumentOptions,
HTMLToSceneGraphOptions,
@@ -17,6 +26,18 @@ export type {
} from './convert'
export type { ToDesignDocumentOptions } from './from-scene-graph'
export type { BrowserCSSRuntimeOptions } from './runtime'
+export type {
+ JSXChild,
+ JSXElementProps,
+ JSXStyleInput,
+ JSXStyleObject,
+ JSXStyleValue,
+ JSXTag,
+ JSXToDesignDocumentOptions,
+ JSXToSceneGraphOptions,
+ TailwindJSXToDesignDocumentOptions,
+ TailwindJSXToSceneGraphOptions
+} from './jsx/runtime'
export type { CompileTailwindCSSOptions } from './tailwind'
export type { ToSceneGraphOptions } from './to-scene-graph'
export type {
diff --git a/packages/dom-css/src/jsx/dev-runtime.ts b/packages/dom-css/src/jsx/dev-runtime.ts
new file mode 100644
index 000000000..819a4daba
--- /dev/null
+++ b/packages/dom-css/src/jsx/dev-runtime.ts
@@ -0,0 +1,8 @@
+import { jsx, type JSXElementProps, type JSXTag } from './runtime'
+
+export { Fragment } from './runtime'
+export type * from './runtime'
+
+export function jsxDEV(tag: JSXTag, props: JSXElementProps = {}) {
+ return jsx(tag, props)
+}
diff --git a/packages/dom-css/src/jsx/runtime.ts b/packages/dom-css/src/jsx/runtime.ts
new file mode 100644
index 000000000..f49ac6907
--- /dev/null
+++ b/packages/dom-css/src/jsx/runtime.ts
@@ -0,0 +1,191 @@
+import type { SceneGraph } from '@open-pencil/core/scene-graph'
+
+import { createCSSRuntime } from '../runtime'
+import { compileTailwindCSS, type CompileTailwindCSSOptions } from '../tailwind'
+import { designDocumentToSceneGraph, type ToSceneGraphOptions } from '../to-scene-graph'
+import type {
+ CSSComputeOptions,
+ CSSRuntime,
+ DesignDocument,
+ DesignNode,
+ DesignStyleDeclaration
+} from '../types'
+
+export const Fragment = Symbol.for('open-pencil.dom-css.fragment')
+
+type JSXComponent = (props: JSXElementProps) => JSXChild
+export type JSXTag = string | JSXComponent | typeof Fragment
+export type JSXStyleValue = string | number | null | undefined
+export type JSXStyleObject = Record<string, JSXStyleValue>
+export type JSXStyleInput = string | JSXStyleObject
+export type JSXChild = DesignNode | JSXChild[] | string | number | boolean | null | undefined
+
+export interface JSXElementProps {
+ children?: JSXChild
+ class?: string
+ className?: string
+ style?: JSXStyleInput
+ key?: string | number
+ [name: string]: unknown
+}
+
+export interface JSXToDesignDocumentOptions {
+ cssText?: string
+ runtime?: CSSRuntime
+ compute?: CSSComputeOptions
+}
+
+export interface JSXToSceneGraphOptions extends JSXToDesignDocumentOptions, ToSceneGraphOptions {}
+
+export interface TailwindJSXToDesignDocumentOptions
+ extends Omit<JSXToDesignDocumentOptions, 'cssText'>, CompileTailwindCSSOptions {}
+
+export interface TailwindJSXToSceneGraphOptions
+ extends TailwindJSXToDesignDocumentOptions, ToSceneGraphOptions {}
+
+function runtimeForOptions(runtime: CSSRuntime | undefined) {
+ return runtime ?? createCSSRuntime()
+}
+
+function cssPropertyName(name: string) {
+ if (name.startsWith('--')) return name
+ return name.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`)
+}
+
+function styleObjectToDeclaration(style: JSXStyleObject): DesignStyleDeclaration | undefined {
+ const entries = Object.entries(style)
+ .filter(
+ (entry): entry is [string, string | number] => entry[1] !== null && entry[1] !== undefined
+ )
+ .map(([property, value]) => [cssPropertyName(property), String(value)] as const)
+ .filter(([, value]) => value.length > 0)
+
+ if (entries.length === 0) return undefined
+ return Object.fromEntries(entries)
+}
+
+function styleStringToDeclaration(style: string): DesignStyleDeclaration | undefined {
+ const entries = style
+ .split(';')
+ .map((declaration) => declaration.split(':'))
+ .filter((parts): parts is [string, string, ...string[]] => parts.length >= 2)
+ .map(([property, ...value]) => [property.trim().toLowerCase(), value.join(':').trim()] as const)
+ .filter(([property, value]) => property.length > 0 && value.length > 0)
+
+ if (entries.length === 0) return undefined
+ return Object.fromEntries(entries)
+}
+
+function styleToDeclaration(style: JSXStyleInput | undefined): DesignStyleDeclaration | undefined {
+ if (typeof style === 'string') return styleStringToDeclaration(style)
+ if (style) return styleObjectToDeclaration(style)
+ return undefined
+}
+
+function attributeValue(value: unknown): string | undefined {
+ if (value === null || value === undefined || value === false) return undefined
+ if (value === true) return ''
+ if (typeof value === 'string') return value
+ if (typeof value === 'number' || typeof value === 'bigint') return String(value)
+ return undefined
+}
+
+function propsToAttrs(props: JSXElementProps): Record<string, string> {
+ const attrs: Record<string, string> = {}
+ const classValue = props.class ?? props.className
+ if (classValue) attrs.class = classValue
+
+ for (const [name, value] of Object.entries(props)) {
+ if (name === 'children' || name === 'class' || name === 'className' || name === 'style')
+ continue
+ if (name === 'key' || name.startsWith('on')) continue
+ const attr = attributeValue(value)
+ if (attr !== undefined) attrs[name] = attr
+ }
+
+ return attrs
+}
+
+function normalizeChild(child: JSXChild, nodes: DesignNode[]): void {
+ if (child === null || child === undefined || typeof child === 'boolean') return
+ if (Array.isArray(child)) {
+ for (const item of child) normalizeChild(item, nodes)
+ return
+ }
+ if (typeof child === 'string' || typeof child === 'number') {
+ nodes.push({ type: 'text', text: String(child) })
+ return
+ }
+ nodes.push(child)
+}
+
+function normalizeChildren(children: JSXChild): DesignNode[] {
+ const nodes: DesignNode[] = []
+ normalizeChild(children, nodes)
+ return nodes
+}
+
+export function jsx(tag: JSXTag, props: JSXElementProps = {}): DesignNode | DesignNode[] {
+ if (tag === Fragment) return normalizeChildren(props.children)
+ if (typeof tag === 'function') return normalizeChildren(tag(props))
+
+ return {
+ type: 'element',
+ tagName: tag,
+ attrs: propsToAttrs(props),
+ inlineStyle: styleToDeclaration(props.style),
+ children: normalizeChildren(props.children)
+ }
+}
+
+export const jsxs = jsx
+
+export async function jsxToDesignDocument(
+ input: JSXChild,
+ options: JSXToDesignDocumentOptions = {}
+): Promise<DesignDocument> {
+ const document: DesignDocument = {
+ type: 'document',
+ children: normalizeChildren(input)
+ }
+
+ if (!options.cssText && !options.runtime && !options.compute) return document
+ const runtime = runtimeForOptions(options.runtime)
+ return runtime.computeStyles(document, options.cssText, options.compute)
+}
+
+export async function jsxToSceneGraph(
+ input: JSXChild,
+ options: JSXToSceneGraphOptions = {}
+): Promise<SceneGraph> {
+ const document = await jsxToDesignDocument(input, options)
+ return designDocumentToSceneGraph(document, options)
+}
+
+export async function tailwindJSXToDesignDocument(
+ input: JSXChild,
+ candidates: string | Iterable<string>,
+ options: TailwindJSXToDesignDocumentOptions = {}
+): Promise<DesignDocument> {
+ const cssText = await compileTailwindCSS(candidates, options)
+ return jsxToDesignDocument(input, { ...options, cssText })
+}
+
+export async function tailwindJSXToSceneGraph(
+ input: JSXChild,
+ candidates: string | Iterable<string>,
+ options: TailwindJSXToSceneGraphOptions = {}
+): Promise<SceneGraph> {
+ const document = await tailwindJSXToDesignDocument(input, candidates, options)
+ return designDocumentToSceneGraph(document, options)
+}
+
+export namespace JSX {
+ export type Element = DesignNode | DesignNode[]
+ export interface ElementChildrenAttribute {
+ children: unknown
+ }
+ export interface IntrinsicElements {
+ [tagName: string]: JSXElementProps
+ }
+}
diff --git a/packages/dom-css/src/runtime/browser.ts b/packages/dom-css/src/runtime/browser.ts
index 734522c79..939dcefdd 100644
--- a/packages/dom-css/src/runtime/browser.ts
+++ b/packages/dom-css/src/runtime/browser.ts
@@ -54,6 +54,7 @@ const DEFAULT_COMPUTED_PROPERTIES = [
'padding-top',
'text-align',
'text-decoration-line',
+ 'text-shadow',
'width'
] as const
diff --git a/packages/dom-css/tests/jsx.test.tsx b/packages/dom-css/tests/jsx.test.tsx
new file mode 100644
index 000000000..948fbab6d
--- /dev/null
+++ b/packages/dom-css/tests/jsx.test.tsx
@@ -0,0 +1,94 @@
+/** @jsxImportSource @open-pencil/dom-css */
+import { describe, expect, it } from 'bun:test'
+
+import {
+ createHeadlessCSSRuntime,
+ jsxToDesignDocument,
+ jsxToSceneGraph,
+ serializeHTML,
+ tailwindJSXToSceneGraph
+} from '../src/index'
+import { tailwindCardClasses } from './helpers'
+
+function CardTitle(props: { children?: string }) {
+ return <h1 class="title">{props.children}</h1>
+}
+
+describe('@open-pencil/dom-css JSX', () => {
+ it('converts JSX elements into DesignDOM documents', async () => {
+ const document = await jsxToDesignDocument(
+ <article class="card" data-id="card-1" style={{ width: '320px', backgroundColor: 'white' }}>
+ <CardTitle>OpenPencil</CardTitle>
+ <p style="font-size: 14px; line-height: 20px">Design with JSX-shaped DOM.</p>
+ </article>
+ )
+ const card = document.children[0]
+
+ expect(card?.type).toBe('element')
+ if (card?.type !== 'element') return
+ expect(card.tagName).toBe('article')
+ expect(card.attrs.class).toBe('card')
+ expect(card.attrs['data-id']).toBe('card-1')
+ expect(card.inlineStyle?.width).toBe('320px')
+ expect(card.inlineStyle?.['background-color']).toBe('white')
+ expect(serializeHTML(document)).toContain('Design with JSX-shaped DOM.')
+ })
+
+ it('runs JSX through CSS runtime styles before scene graph conversion', async () => {
+ const graph = await jsxToSceneGraph(
+ <article class="card">
+ <h1 class="title">OpenPencil</h1>
+ </article>,
+ {
+ runtime: createHeadlessCSSRuntime(),
+ cssText: `
+ .card {
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+ width: 320px;
+ height: 160px;
+ padding: 24px;
+ background: white;
+ }
+ .title {
+ font-size: 24px;
+ font-weight: 700;
+ line-height: 32px;
+ }
+ `
+ }
+ )
+ const page = graph.getPages()[0]
+ const card = page ? graph.getChildren(page.id)[0] : undefined
+
+ expect(card?.type).toBe('FRAME')
+ if (card?.type !== 'FRAME') return
+ expect(card.width).toBe(320)
+ expect(card.height).toBe(160)
+ expect(card.layoutMode).toBe('VERTICAL')
+ expect(card.paddingLeft).toBe(24)
+
+ const title = graph.getChildren(card.id)[0]
+ expect(title?.type).toBe('TEXT')
+ expect(title?.fontSize).toBe(24)
+ })
+
+ it('feeds JSX Tailwind classes through generated CSS', async () => {
+ const classes = [...tailwindCardClasses]
+ const graph = await tailwindJSXToSceneGraph(
+ <article class={classes.join(' ')}>
+ <h1>OpenPencil</h1>
+ </article>,
+ classes,
+ { runtime: createHeadlessCSSRuntime() }
+ )
+ const page = graph.getPages()[0]
+ const card = page ? graph.getChildren(page.id)[0] : undefined
+
+ expect(card?.type).toBe('FRAME')
+ expect(card?.width).toBe(320)
+ expect(card?.height).toBe(176)
+ expect(card?.itemSpacing).toBe(12)
+ })
+})
diff --git a/packages/dom-css/tsconfig.json b/packages/dom-css/tsconfig.json
index 1d2b189ea..96dbc2e1d 100644
--- a/packages/dom-css/tsconfig.json
+++ b/packages/dom-css/tsconfig.json
@@ -12,11 +12,16 @@
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
+ "jsx": "react-jsx",
+ "jsxImportSource": "@open-pencil/dom-css",
"types": ["bun"],
"paths": {
- "#dom-css/*": ["./src/*"]
+ "#dom-css/*": ["./src/*"],
+ "@open-pencil/dom-css": ["./src/index.ts"],
+ "@open-pencil/dom-css/jsx-runtime": ["./src/jsx/runtime.ts"],
+ "@open-pencil/dom-css/jsx-dev-runtime": ["./src/jsx/dev-runtime.ts"]
}
},
- "include": ["src/**/*.ts", "src/**/*.d.ts"],
+ "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts"],
"exclude": ["node_modules", "dist"]
}
diff --git a/packages/dom-css/tsconfig.test.json b/packages/dom-css/tsconfig.test.json
index 5c81eab53..9cd84bf82 100644
--- a/packages/dom-css/tsconfig.test.json
+++ b/packages/dom-css/tsconfig.test.json
@@ -3,6 +3,13 @@
"compilerOptions": {
"noEmit": true
},
- "include": ["src/**/*.ts", "src/**/*.d.ts", "tests/**/*.ts", "scripts/**/*.ts"],
+ "include": [
+ "src/**/*.ts",
+ "src/**/*.tsx",
+ "src/**/*.d.ts",
+ "tests/**/*.ts",
+ "tests/**/*.tsx",
+ "scripts/**/*.ts"
+ ],
"exclude": ["node_modules", "dist"]
}
diff --git a/packages/dom-css/tsdown.config.ts b/packages/dom-css/tsdown.config.ts
index ec959a2a5..e32349c40 100644
--- a/packages/dom-css/tsdown.config.ts
+++ b/packages/dom-css/tsdown.config.ts
@@ -2,7 +2,9 @@ import { defineConfig } from 'tsdown'
export default defineConfig({
entry: {
- index: './src/index.ts'
+ index: './src/index.ts',
+ 'jsx-runtime': './src/jsx/runtime.ts',
+ 'jsx-dev-runtime': './src/jsx/dev-runtime.ts'
},
platform: 'neutral',
format: ['esm'],
diff --git a/tests/engine/dom-css/jsx.test.ts b/tests/engine/dom-css/jsx.test.ts
new file mode 100644
index 000000000..0eeae09ed
--- /dev/null
+++ b/tests/engine/dom-css/jsx.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it } from 'bun:test'
+
+import {
+ createHeadlessCSSRuntime,
+ jsx,
+ jsxToDesignDocument,
+ jsxToSceneGraph,
+ tailwindJSXToSceneGraph
+} from '@open-pencil/dom-css'
+
+import { tailwindCardClasses } from '#tests/helpers/dom-css'
+
+describe('@open-pencil/dom-css JSX authoring', () => {
+ it('converts JSX runtime output into DesignDOM', async () => {
+ const document = await jsxToDesignDocument(
+ jsx('article', {
+ class: 'card',
+ style: { width: '320px', backgroundColor: 'white' },
+ children: jsx('h1', { children: 'OpenPencil' })
+ })
+ )
+ const card = document.children[0]
+
+ expect(card?.type).toBe('element')
+ if (card?.type !== 'element') return
+ expect(card.tagName).toBe('article')
+ expect(card.attrs.class).toBe('card')
+ expect(card.inlineStyle?.width).toBe('320px')
+ expect(card.inlineStyle?.['background-color']).toBe('white')
+ })
+
+ it('converts JSX through CSS runtime into scene graph', async () => {
+ const graph = await jsxToSceneGraph(
+ jsx('article', {
+ class: 'card',
+ children: jsx('h1', { class: 'title', children: 'OpenPencil' })
+ }),
+ {
+ runtime: createHeadlessCSSRuntime(),
+ cssText: `
+ .card { display: flex; flex-direction: column; width: 320px; height: 160px; padding: 24px; }
+ .title { font-size: 24px; font-weight: 700; line-height: 32px; }
+ `
+ }
+ )
+ const page = graph.getPages()[0]
+ const card = page ? graph.getChildren(page.id)[0] : undefined
+
+ expect(card?.type).toBe('FRAME')
+ expect(card?.width).toBe(320)
+ expect(card?.height).toBe(160)
+ expect(card?.paddingLeft).toBe(24)
+ })
+
+ it('keeps Tailwind JSX flowing through generated CSS', async () => {
+ const classes = [...tailwindCardClasses]
+ const graph = await tailwindJSXToSceneGraph(
+ jsx('article', {
+ class: classes.join(' '),
+ children: jsx('h1', { children: 'OpenPencil' })
+ }),
+ classes,
+ { runtime: createHeadlessCSSRuntime() }
+ )
+ const page = graph.getPages()[0]
+ const card = page ? graph.getChildren(page.id)[0] : undefined
+
+ expect(card?.type).toBe('FRAME')
+ expect(card?.width).toBe(320)
+ expect(card?.height).toBe(176)
+ expect(card?.itemSpacing).toBe(12)
+ })
+})