Skip to content

Commit e8116c2

Browse files
authored
Merge pull request #92 from components-ai/styled-component
Use a styled component with built-in tag names to render elements
2 parents 38818c4 + 57fde2a commit e8116c2

File tree

19 files changed

+66
-67
lines changed

19 files changed

+66
-67
lines changed

.changeset/mighty-keys-mix.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+
Create a styled component with tag name members for rendering the canvas/element

apps/docs/components/examples/Demo.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from 'react'
22
import Link from 'next/link'
3-
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
3+
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'
44
import { initialStyles } from '../../data/initial-styles'
55
import { defaultTheme } from '../../data/default-theme'
66

@@ -47,7 +47,7 @@ export function Demo() {
4747
</Editor>
4848
</div>
4949
<div sx={{ flexGrow: 1 }}>
50-
<RenderElement tagName="p" styles={styles}>
50+
<styled.p styles={styles}>
5151
“The parameters comprise sequences which are theoretically infinite
5252
but limits are, of course, set to them in practice. There is an
5353
upward limit to size and certainly a downward one... Within these
@@ -64,7 +64,7 @@ export function Demo() {
6464
</Link>{' '}
6565
by Karl Gerstner
6666
</em>
67-
</RenderElement>
67+
</styled.p>
6868
</div>
6969
</div>
7070
<div className="full-bleed">

apps/docs/components/examples/Fieldset.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState } from 'react'
22
import {
33
Editor,
4-
RenderElement,
4+
styled,
55
Fieldset as FieldsetInput,
66
Inputs,
77
toCSSObject,
@@ -35,9 +35,9 @@ export const Fieldset = () => {
3535
</FieldsetInput>
3636
</>
3737
</Editor>
38-
<RenderElement tagName="p" styles={styles}>
38+
<styled.p styles={styles}>
3939
Hello, <b>world!</b>
40-
</RenderElement>
40+
</styled.p>
4141
<pre>{JSON.stringify(toCSSObject(styles), null, 2)}</pre>
4242
</>
4343
)

apps/docs/components/examples/FontFamily.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useState } from 'react'
2-
import { FontFamilyInput, RenderElement } from '@compai/css-gui'
2+
import { FontFamilyInput, styled } from '@compai/css-gui'
33

44
export const FontFamilyExample = () => {
55
const [fontFamily, setFontFamily] = useState('Abel')
66

77
return (
88
<>
9-
<RenderElement tagName='p' styles={{ fontFamily, fontSize: '24px' }}>
9+
<styled.p styles={{ fontFamily, fontSize: '24px' }}>
1010
Fun with fonts!
11-
</RenderElement>
11+
</styled.p>
1212
<FontFamilyInput value={fontFamily} onChange={setFontFamily} />
1313
</>
1414
)

apps/docs/pages/components/editor.mdx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on its contents.
1414

1515
```js
1616
import { useState } from 'react'
17-
import { Editor, RenderElement } from '@compai/css-gui'
17+
import { Editor, styled } from '@compai/css-gui'
1818

1919
export const MyEditor = () => {
2020
const [styles, setStyles] = useState({
@@ -26,9 +26,7 @@ export const MyEditor = () => {
2626
return (
2727
<>
2828
<Editor styles={styles} onChange={setStyles} />
29-
<RenderElement tagName="p" styles={styles}>
30-
Hello, world!
31-
</RenderElement>
29+
<styled.p styles={styles}>Hello, world!</styled.p>
3230
</>
3331
)
3432
}
@@ -42,7 +40,7 @@ used. You can compose inputs yourselves in this scenario:
4240
```js
4341
import { useState } from 'react'
4442
import Link from 'next/link'
45-
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
43+
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'
4644

4745
export function Demo() {
4846
const [styles, setStyles] = useState({})
@@ -67,7 +65,7 @@ export function Demo() {
6765
<Inputs.Height />
6866
</div>
6967
</Editor>
70-
<RenderElement tagName="p" styles={styles}>
68+
<styled.p styles={styles}>
7169
“The parameters comprise sequences which are theoretically infinite but
7270
limits are, of course, set to them in practice. There is an upward limit
7371
to size and certainly a downward one... Within these sequences there are
@@ -82,7 +80,7 @@ export function Demo() {
8280
Designing Programmes
8381
</a> by Karl Gerstner
8482
</em>
85-
</RenderElement>
83+
</styled.p>
8684
<pre>{codegen.css(styles)}</pre>
8785
</>
8886
)

apps/docs/pages/components/fieldset.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ all pseudo-elements and pseudo-selectors so you can also target
1616

1717
```js
1818
import { useState } from 'react'
19-
import { Editor, RenderElement, Fieldset, Inputs } from '@compai/css-gui'
19+
import { Editor, Fieldset, Inputs, styled } from '@compai/css-gui'
2020

2121
const MyEditor = () => {
2222
const [styles, setStyles] = useState({})
@@ -34,9 +34,7 @@ const MyEditor = () => {
3434
</Fieldset>
3535
</>
3636
</Editor>
37-
<RenderElement tagName="p" styles={styles}>
38-
Hello, world!
39-
</RenderElement>
37+
<styled.p styles={styles}>Hello, world!</styled.p>
4038
</>
4139
)
4240
}

apps/docs/pages/examples/border-images.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from 'react'
22
import Link from 'next/link'
3-
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
3+
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'
44
import { defaultTheme } from '../../data/default-theme'
55

66
const initialStyles = {
@@ -53,7 +53,7 @@ export default function BorderImage() {
5353
</Editor>
5454
</div>
5555
<div sx={{ flexGrow: 1, pr: 4 }}>
56-
<RenderElement tagName="p" styles={styles}>
56+
<styled.p styles={styles}>
5757
“The parameters comprise sequences which are theoretically infinite
5858
but limits are, of course, set to them in practice. There is an
5959
upward limit to size and certainly a downward one... Within these
@@ -70,7 +70,7 @@ export default function BorderImage() {
7070
</Link>{' '}
7171
by Karl Gerstner
7272
</em>
73-
</RenderElement>
73+
</styled.p>
7474
</div>
7575
</div>
7676
<div className="full-bleed">

apps/docs/pages/examples/filters.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RenderElement, Editor } from '@compai/css-gui'
1+
import { Editor, styled } from '@compai/css-gui'
22
import { useState } from 'react'
33

44
const initialStyles = {
@@ -17,8 +17,7 @@ export default function Filters() {
1717
justifyContent: 'center',
1818
}}
1919
>
20-
<RenderElement
21-
tagName="div"
20+
<styled.div
2221
styles={{
2322
width: '24rem',
2423
height: '24rem',
@@ -37,7 +36,7 @@ export default function Filters() {
3736
}}
3837
>
3938
Fun with filters
40-
</RenderElement>
39+
</styled.div>
4140
</div>
4241
</div>
4342
)

apps/docs/pages/examples/shadows.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RenderElement, Editor } from '@compai/css-gui'
1+
import { Editor, styled } from '@compai/css-gui'
22
import { useState } from 'react'
33

44
const initialStyles = {
@@ -45,8 +45,7 @@ export default function Shadows() {
4545
justifyContent: 'center',
4646
}}
4747
>
48-
<RenderElement
49-
tagName="div"
48+
<styled.div
5049
styles={{
5150
width: '12rem',
5251
height: '12rem',
@@ -57,7 +56,7 @@ export default function Shadows() {
5756
}}
5857
>
5958
Fun with shadows
60-
</RenderElement>
59+
</styled.div>
6160
</div>
6261
</div>
6362
)

apps/docs/pages/examples/transforms.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RenderElement, Editor } from '@compai/css-gui'
1+
import { Editor, styled } from '@compai/css-gui'
22
import { useState } from 'react'
33

44
const initialStyles = {
@@ -22,8 +22,7 @@ export default function Transforms() {
2222
justifyContent: 'center',
2323
}}
2424
>
25-
<RenderElement
26-
tagName="div"
25+
<styled.div
2726
styles={{
2827
width: '24rem',
2928
height: '24rem',
@@ -37,7 +36,7 @@ export default function Transforms() {
3736
}}
3837
>
3938
Fun with transforms
40-
</RenderElement>
39+
</styled.div>
4140
</div>
4241
</div>
4342
)

apps/docs/pages/examples/transitions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RenderElement, Editor } from '@compai/css-gui'
1+
import { Editor, styled } from '@compai/css-gui'
22
import { useState } from 'react'
33

44
const initialStyles = {
@@ -22,7 +22,7 @@ export default function Transitions() {
2222
return (
2323
<div>
2424
<Editor styles={styles} onChange={setStyles} />
25-
<RenderElement tagName="div" styles={styles} />
25+
<styled.div styles={styles} />
2626
</div>
2727
)
2828
}

apps/docs/pages/exports/css.mdx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ the style object to CSS that's then displayed in a `pre` tag.
5757

5858
```js
5959
import { useState } from 'react'
60-
import { Editor, RenderElement, codegen } from '@compai/css-gui'
60+
import { Editor, styled, codegen } from '@compai/css-gui'
6161

6262
export const MyEditor = () => {
6363
const [styles, setStyles] = useState({
@@ -67,9 +67,7 @@ export const MyEditor = () => {
6767
return (
6868
<>
6969
<Editor styles={styles} onChange={setStyles} />
70-
<RenderElement tagName="p" styles={styles}>
71-
Hello, world!
72-
</RenderElement>
70+
<styled.p styles={styles}>Hello, world!</styled.p>
7371
<pre>{codegen.css(styles)}</pre>
7472
</>
7573
)

apps/docs/pages/getting-started.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@ yarn add @compai/css-gui
2424

2525
## Usage
2626

27-
Import the `Editor` component and `RenderElement` to initialize controls
27+
Import the `Editor` and `styled` components to initialize controls
2828
and then render the element styles to your canvas.
2929

3030
```js
3131
import { useState } from 'react'
32-
import { Editor, RenderElement } from '@compai/css-gui'
32+
import { Editor, styled } from '@compai/css-gui'
3333

3434
export const MyEditor = () => {
3535
const [styles, setStyles] = useState({})
3636

3737
return (
3838
<>
3939
<Editor styles={styles} onChange={setStyles} />
40-
<RenderElement tagName="p" styles={styles}>
41-
Hello, world!
42-
</RenderElement>
40+
<styled.p styles={styles}>Hello, world!</styled.p>
4341
</>
4442
)
4543
}

apps/docs/pages/inputs/font-family.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { FontFamilyExample } from '../../components/examples/FontFamily'
77

88
This input supports a growing set of typefaces, which currently includes all
99
[Google Fonts](https://fonts.google.com) and system fonts. Style sheets links are
10-
generated automatically with `RenderElement`.
10+
generated automatically with `styled`.
1111

1212
### Example
1313

@@ -17,16 +17,14 @@ generated automatically with `RenderElement`.
1717

1818
```js
1919
import { useState } from 'react'
20-
import { FontFamilyInput, RenderElement } from '@compai/css-gui'
20+
import { FontFamilyInput, styled } from '@compai/css-gui'
2121

2222
const MyEditor = () => {
2323
const [fontFamily, setFontFamily] = useState('Abel')
2424

2525
return (
2626
<>
27-
<RenderElement tagName="p" styles={{ fontFamily }}>
28-
Fun with fonts!
29-
</RenderElement>
27+
<styled.p styles={{ fontFamily }}>Fun with fonts!</styled.p>
3028
<FontFamilyInput value={fontFamily} onChange={setFontFamily} />
3129
</>
3230
)

apps/docs/pages/introduction.mdx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ With CSS GUI you can quickly get up and running with parametric controls for sty
1111
You can use this to improve your creative coding workflow or build user interfaces
1212
where users get to control how their content looks.
1313

14-
At its essence, CSS GUI exposes an `Editor` and a `RenderElement` component to display
14+
At its essence, CSS GUI exposes an `Editor` and a `styled` component to display
1515
your controls and canvas, respectively.
1616

1717
```js
1818
import { useState } from 'react'
19-
import { Editor, RenderElement } from '@compai/css-gui'
19+
import { Editor, styled } from '@compai/css-gui'
2020

2121
export const MyEditor = () => {
2222
const [styles, setStyles] = useState({})
2323

2424
return (
2525
<>
2626
<Editor styles={styles} onChange={setStyles} />
27-
<RenderElement tagName="p" styles={styles}>
28-
Hello, world!
29-
</RenderElement>
27+
<styled.p styles={styles}>Hello, world!</styled.p>
3028
</>
3129
)
3230
}

packages/gui/src/components/RenderElement.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ export const RenderElement = ({
1515
}: RenderElementProps) => {
1616
const Component = tagName
1717
const styleObject = toCSSObject(styles)
18-
19-
18+
2019
return (
2120
// @ts-ignore
2221
<>
23-
<FontTags fontFamily={styles.fontFamily}/>
22+
<FontTags fontFamily={styles.fontFamily} />
2423
{/* @ts-ignore */}
2524
<Component {...props} sx={styleObject} />
2625
</>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { HTMLAttributes } from 'react'
2+
import { elements } from '../data/elements'
3+
import { RenderElement } from './RenderElement'
4+
5+
type StyledProps = HTMLAttributes<HTMLBaseElement> & {
6+
styles: any
7+
}
8+
export const styled: Record<string, any> = {}
9+
Object.keys(elements).forEach((field: string) => {
10+
styled[field] = ({ styles, ...props }: StyledProps) => (
11+
<RenderElement tagName={field} styles={styles} {...props} />
12+
)
13+
})

packages/gui/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './components/Editor'
2+
export { styled } from './components/Styled'
23
export { RenderElement } from './components/RenderElement'
34

45
export { Number as NumberInput } from './components/primitives'

0 commit comments

Comments
 (0)