Skip to content

Commit 2144254

Browse files
authored
Merge pull request #622 from components-ai/style-elements
Add style element to enhance export
2 parents 0199529 + 3d31215 commit 2144254

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

.changeset/long-camels-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compai/css-gui': patch
3+
---
4+
5+
Add style element to enhance export

packages/gui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"typescript": "^4.6.4"
4242
},
4343
"dependencies": {
44+
"@emotion/hash": "^0.9.0",
4445
"@emotion/react": "^11.9.0",
4546
"@mdx-js/react": "^1.6.22",
4647
"@radix-ui/react-accordion": "^0.1.6",

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { format } from './format'
88
import { getAttrSyntax } from './util'
99
import { CodegenOptions } from './types'
1010
import { Theme } from '../../types/theme'
11+
import { inlineStylesToStyleElement } from '../transformers/inline-styles-to-style-element'
1112

1213
const h = (theme: Theme) => (tagName: string, props: any, children?: any[]) => {
1314
const newProps = toReactProps(props)
@@ -23,13 +24,20 @@ const h = (theme: Theme) => (tagName: string, props: any, children?: any[]) => {
2324

2425
export const enhanceSFC = async (node: HtmlNode, options: CodegenOptions) => {
2526
const root = editorSchemaToHast(node, { addSlotTagSyntax: true })
26-
const functionBody = stringifyHastNode(toH(h(options?.theme), root))
27+
const { node: htmlNode, styles } = inlineStylesToStyleElement(root)
28+
// @ts-ignore
29+
const functionBody = stringifyHastNode(toH(h(options?.theme), htmlNode))
30+
31+
const htmlString = `
32+
<style>${styles}</style>
33+
${functionBody}`
34+
const formattedHtmlString = await format('html', htmlString)
2735

2836
const output = `
2937
export default function Component({ html, state = {} }) {
3038
${getAttrSyntax(node)}
3139
return html\`
32-
${functionBody}
40+
${formattedHtmlString}
3341
\`
3442
}
3543
`

packages/gui/src/lib/transformers/editor-schema-to-hast.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ export const editorSchemaToHast = (node: any, options?: Options) => {
4040
}
4141
})
4242
.runSync(cloneDeep(node))
43+
4344
return processedTree
4445
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { cloneDeep } from 'lodash-es'
2+
import { unified } from 'unified'
3+
import { visit } from 'unist-util-visit'
4+
import hash from '@emotion/hash'
5+
import { stringifyCSSObject } from '../codegen/stringify-css-object'
6+
import { toCSSObject } from '../codegen/to-css-object'
7+
import { addCSSClassSyntax } from '../classes'
8+
9+
export const inlineStylesToStyleElement = (node: any) => {
10+
const styleMap: Record<string, any> = {}
11+
const processedTree = unified()
12+
.use(() => (tree) => {
13+
visit(tree, 'element', (node: any) => {
14+
if (!node.properties.style) {
15+
return
16+
}
17+
18+
const style = node.properties.style
19+
// @ts-ignore
20+
const selector = 'css-' + hash.default(JSON.stringify(style))
21+
22+
if (!node.properties.class) {
23+
node.properties.class = selector
24+
} else {
25+
node.properties.class = node.properties.class + ' ' + selector
26+
}
27+
28+
delete node.properties.style
29+
30+
styleMap[selector] = stringifyCSSObject(
31+
toCSSObject(style),
32+
addCSSClassSyntax(selector)
33+
)
34+
})
35+
})
36+
.runSync(cloneDeep(node))
37+
38+
return {
39+
node: processedTree,
40+
styles: Object.values(styleMap).join('\n'),
41+
}
42+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@
565565
resolved "https://registry.yarnpkg.com/@emotion/hash/-/hash-0.8.0.tgz#bbbff68978fefdbe68ccb533bc8cbe1d1afb5413"
566566
integrity sha512-kBJtf7PH6aWwZ6fka3zQ0p6SBYzx4fl1LoZXE2RrnYST9Xljm7WfKJrU4g/Xr3Beg72MLrp1AWNUmuYJTL7Cow==
567567

568+
"@emotion/hash@^0.9.0":
569+
version "0.9.0"
570+
resolved "https://registry.yarnpkg.com/@emotion/hash/-/hash-0.9.0.tgz#c5153d50401ee3c027a57a177bc269b16d889cb7"
571+
integrity sha512-14FtKiHhy2QoPIzdTcvh//8OyBlknNs2nXRwIhG904opCby3l+9Xaf/wuPvICBF0rc1ZCNBd3nKe9cd2mecVkQ==
572+
568573
"@emotion/is-prop-valid@^0.8.1":
569574
version "0.8.8"
570575
resolved "https://registry.yarnpkg.com/@emotion/is-prop-valid/-/is-prop-valid-0.8.8.tgz#db28b1c4368a259b60a97311d6a952d4fd01ac1a"

0 commit comments

Comments
 (0)