Skip to content

Commit 667fb83

Browse files
authored
Merge pull request #618 from components-ai/theme-with-codegen
Make sure all code generators are passed theme in export
2 parents 2b857c1 + 700259d commit 667fb83

File tree

10 files changed

+67
-39
lines changed

10 files changed

+67
-39
lines changed

.changeset/cold-kids-refuse.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Make sure all code generators are passed theme in export

packages/gui/src/lib/codegen/emotion.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toReactProps } from './to-react-props'
77
import { format } from './format'
88
import { getPropSyntax } from './util'
99
import { Theme } from '../../types/theme'
10+
import { CodegenOptions } from './types'
1011

1112
const h =
1213
(theme?: Theme) => (tagName: string, props: any, children?: any[]) => {
@@ -21,9 +22,9 @@ const h =
2122
return { tagName, props: newProps, children }
2223
}
2324

24-
export const emotion = async (node: HtmlNode, options: any) => {
25+
export const emotion = async (node: HtmlNode, options: CodegenOptions) => {
2526
const root = editorSchemaToHast(node, { addSlotSyntax: true })
26-
const functionBody = stringifyHastNode(toH(h(options.theme), root))
27+
const functionBody = stringifyHastNode(toH(h(options?.theme), root))
2728

2829
const output = `
2930
/** @jsxImportSource @emotion/react */

packages/gui/src/lib/codegen/enhance-sfc.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ import { stringifyHastNode } from './stringify-hast-node-as-html'
66
import { toReactProps } from './to-react-props'
77
import { format } from './format'
88
import { getAttrSyntax } from './util'
9+
import { CodegenOptions } from './types'
10+
import { Theme } from '../../types/theme'
911

10-
const h = (tagName: string, props: any, children?: any[]) => {
12+
const h = (theme: Theme) => (tagName: string, props: any, children?: any[]) => {
1113
const newProps = toReactProps(props)
1214

1315
if (newProps.style) {
1416
const style = newProps.style
1517
delete newProps.style
16-
newProps.style = toCSSObject(style)
18+
newProps.style = toCSSObject(style, theme)
1719
}
1820

1921
return { tagName, props: newProps, children }
2022
}
2123

22-
export const enhanceSFC = async (node: HtmlNode) => {
24+
export const enhanceSFC = async (node: HtmlNode, options: CodegenOptions) => {
2325
const root = editorSchemaToHast(node, { addSlotTagSyntax: true })
24-
const functionBody = stringifyHastNode(toH(h, root))
26+
const functionBody = stringifyHastNode(toH(h(options?.theme), root))
2527

2628
const output = `
2729
export default function Component({ html, state = {} }) {

packages/gui/src/lib/codegen/html.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import rehypeStringify from 'rehype-stringify'
33
import { HtmlNode } from '../../components/html/types'
44
import { editorSchemaToHast } from '../transformers/editor-schema-to-hast'
55
import { format } from './format'
6+
import { CodegenOptions } from './types'
67

78
export const unstyledHtml = async (node: HtmlNode) => {
89
const root = editorSchemaToHast(node, {
@@ -12,11 +13,7 @@ export const unstyledHtml = async (node: HtmlNode) => {
1213
return format('html', output)
1314
}
1415

15-
type HTMLOptions = {
16-
selector?: string
17-
theme?: any
18-
}
19-
export const html = async (node: HtmlNode, { theme }: HTMLOptions = {}) => {
16+
export const html = async (node: HtmlNode, { theme }: CodegenOptions = {}) => {
2017
const res = await fetch('https://components.ai/api/v1/gui/export/html', {
2118
method: 'POST',
2219
headers: {

packages/gui/src/lib/codegen/react.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import { format } from './format'
55
import { html as toHtml } from './html'
66
import { stringifyHastNode } from './stringify-hast-node-as-jsx'
77
import { toReactProps } from './to-react-props'
8+
import { CodegenOptions } from './types'
89

910
const h = (tagName: string, props: any, children?: any[]) => {
1011
const newProps = toReactProps(props)
1112

1213
return { tagName, props: newProps, children }
1314
}
1415

15-
export const react = async (node: HtmlNode): Promise<string> => {
16-
const html = await toHtml(node)
16+
export const react = async (
17+
node: HtmlNode,
18+
options: CodegenOptions
19+
): Promise<string> => {
20+
const html = await toHtml(node, options)
1721
const { styles } = await extractStyles(html)
1822
const jsx = stringifyHastNode(toH(h, node as any))
1923

packages/gui/src/lib/codegen/styled-jsx.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import { format } from './format'
55
import { html as toHtml } from './html'
66
import { stringifyHastNode } from './stringify-hast-node-as-jsx'
77
import { toReactProps } from './to-react-props'
8+
import { CodegenOptions } from './types'
89

910
const h = (tagName: string, props: any, children?: any[]) => {
1011
const newProps = toReactProps(props)
1112

1213
return { tagName, props: newProps, children }
1314
}
1415

15-
export const styledJsx = async (node: HtmlNode): Promise<string> => {
16-
const html = await toHtml(node)
16+
export const styledJsx = async (
17+
node: HtmlNode,
18+
options: CodegenOptions
19+
): Promise<string> => {
20+
const html = await toHtml(node, options)
1721
const { styles } = await extractStyles(html)
1822
const jsx = stringifyHastNode(toH(h, node as any))
1923

packages/gui/src/lib/codegen/theme-ui.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,28 @@ import { stringifyHastNode } from './stringify-hast-node-as-jsx'
66
import { toReactProps } from './to-react-props'
77
import { format } from './format'
88
import { getPropSyntax } from './util'
9+
import { CodegenOptions } from './types'
10+
import { Theme } from '../../types/theme'
911

10-
const h = (tagName: string, props: any, children?: any[]) => {
11-
const newProps = toReactProps(props)
12+
const h =
13+
(theme?: Theme) => (tagName: string, props: any, children?: any[]) => {
14+
const newProps = toReactProps(props)
1215

13-
if (newProps.style) {
14-
const style = newProps.style
15-
delete newProps.style
16-
newProps.sx = toCSSObject(style)
17-
}
16+
if (newProps.style) {
17+
const style = newProps.style
18+
delete newProps.style
19+
newProps.sx = toCSSObject(style, theme)
20+
}
1821

19-
return { tagName, props: newProps, children }
20-
}
22+
return { tagName, props: newProps, children }
23+
}
2124

22-
export const themeUI = async (node: HtmlNode) => {
25+
export const themeUI = async (
26+
node: HtmlNode,
27+
{ theme }: CodegenOptions = {}
28+
) => {
2329
const root = editorSchemaToHast(node, { addSlotSyntax: true })
24-
const functionBody = stringifyHastNode(toH(h, root))
30+
const functionBody = stringifyHastNode(toH(h(theme), root))
2531

2632
const output = `
2733
/** @jsxImportSource theme-ui */

packages/gui/src/lib/codegen/to-css-object.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@ export const stringifyProperty = (
2323
type StyleEntry = [string, Length | string | null | undefined]
2424
export const toCSSObject = (providedStyles: Styles, theme?: Theme): any => {
2525
const styles = stylesToEditorSchema(providedStyles)
26-
// @ts-ignore
27-
return Object.entries(styles).reduce((acc: Styles, curr: StyleEntry) => {
28-
const [property, value] = curr
29-
if (isNestedSelector(property.replace(/^:+/, ''))) {
26+
const cssObject = Object.entries(styles).reduce(
27+
// @ts-ignore
28+
(acc: Styles, curr: StyleEntry) => {
29+
const [property, value] = curr
30+
if (isNestedSelector(property.replace(/^:+/, ''))) {
31+
return {
32+
...acc,
33+
[stringifySelector(property)]: toCSSObject(value as Styles, theme),
34+
}
35+
}
3036
return {
3137
...acc,
32-
[stringifySelector(property)]: toCSSObject(value as Styles, theme),
38+
[property]: stringifyProperty(property, value, theme),
3339
}
34-
}
35-
return {
36-
...acc,
37-
[property]: stringifyProperty(property, value, theme),
38-
}
39-
}, {})
40+
},
41+
{}
42+
)
43+
return cssObject
4044
}

packages/gui/src/lib/codegen/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type CodegenOptions = {
2+
selector?: string
3+
theme?: any
4+
}

packages/gui/src/lib/codegen/vue.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { HtmlNode } from '../../components/html/types'
22
import { extractStyles } from './extract-styles'
33
import { format } from './format'
44
import { html as toHtml } from './html'
5+
import { CodegenOptions } from './types'
56

6-
export const vue = async (node: HtmlNode) => {
7-
const src = await toHtml(node)
7+
export const vue = async (node: HtmlNode, options: CodegenOptions) => {
8+
const src = await toHtml(node, options)
89
const { html, styles } = await extractStyles(src)
910

1011
const output = `

0 commit comments

Comments
 (0)