forked from components-ai/css.gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.ts
More file actions
43 lines (35 loc) · 1.09 KB
/
react.ts
File metadata and controls
43 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { toH } from 'hast-to-hyperscript'
import { HtmlNode } from '../../components/html/types'
import { extractStyles } from './extract-styles'
import { format } from './format'
import { html as toHtml } from './html'
import { stringifyHastNode } from './stringify-hast-node-as-jsx'
import { toReactProps } from './to-react-props'
import { CodegenOptions } from './types'
const h = (tagName: string, props: any, children?: any[]) => {
const newProps = toReactProps(props)
return { tagName, props: newProps, children }
}
export const react = async (
node: HtmlNode,
options: CodegenOptions
): Promise<string> => {
const html = await toHtml(node, options)
const { styles } = await extractStyles(html)
const jsx = stringifyHastNode(toH(h, node as any))
const output = `
import * as React from 'react'
export default function Component() {
return (
<>
<Styles />${jsx}
</>
)
}
function Styles() {
return <style dangerouslySetInnerHTML={{ __html: STYLES }} />
}
const STYLES = \`${styles}\`
`
return format('js', output)
}