Skip to content

Commit

Permalink
Merge pull request #92 from components-ai/styled-component
Browse files Browse the repository at this point in the history
Use a styled component with built-in tag names to render elements
  • Loading branch information
johno authored May 2, 2022
2 parents 38818c4 + 57fde2a commit e8116c2
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-keys-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compai/css-gui': patch
---

Create a styled component with tag name members for rendering the canvas/element
6 changes: 3 additions & 3 deletions apps/docs/components/examples/Demo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import Link from 'next/link'
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'
import { initialStyles } from '../../data/initial-styles'
import { defaultTheme } from '../../data/default-theme'

Expand Down Expand Up @@ -47,7 +47,7 @@ export function Demo() {
</Editor>
</div>
<div sx={{ flexGrow: 1 }}>
<RenderElement tagName="p" styles={styles}>
<styled.p styles={styles}>
“The parameters comprise sequences which are theoretically infinite
but limits are, of course, set to them in practice. There is an
upward limit to size and certainly a downward one... Within these
Expand All @@ -64,7 +64,7 @@ export function Demo() {
</Link>{' '}
by Karl Gerstner
</em>
</RenderElement>
</styled.p>
</div>
</div>
<div className="full-bleed">
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/components/examples/Fieldset.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'
import {
Editor,
RenderElement,
styled,
Fieldset as FieldsetInput,
Inputs,
toCSSObject,
Expand Down Expand Up @@ -35,9 +35,9 @@ export const Fieldset = () => {
</FieldsetInput>
</>
</Editor>
<RenderElement tagName="p" styles={styles}>
<styled.p styles={styles}>
Hello, <b>world!</b>
</RenderElement>
</styled.p>
<pre>{JSON.stringify(toCSSObject(styles), null, 2)}</pre>
</>
)
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/components/examples/FontFamily.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useState } from 'react'
import { FontFamilyInput, RenderElement } from '@compai/css-gui'
import { FontFamilyInput, styled } from '@compai/css-gui'

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

return (
<>
<RenderElement tagName='p' styles={{ fontFamily, fontSize: '24px' }}>
<styled.p styles={{ fontFamily, fontSize: '24px' }}>
Fun with fonts!
</RenderElement>
</styled.p>
<FontFamilyInput value={fontFamily} onChange={setFontFamily} />
</>
)
Expand Down
12 changes: 5 additions & 7 deletions apps/docs/pages/components/editor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on its contents.

```js
import { useState } from 'react'
import { Editor, RenderElement } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'

export const MyEditor = () => {
const [styles, setStyles] = useState({
Expand All @@ -26,9 +26,7 @@ export const MyEditor = () => {
return (
<>
<Editor styles={styles} onChange={setStyles} />
<RenderElement tagName="p" styles={styles}>
Hello, world!
</RenderElement>
<styled.p styles={styles}>Hello, world!</styled.p>
</>
)
}
Expand All @@ -42,7 +40,7 @@ used. You can compose inputs yourselves in this scenario:
```js
import { useState } from 'react'
import Link from 'next/link'
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'

export function Demo() {
const [styles, setStyles] = useState({})
Expand All @@ -67,7 +65,7 @@ export function Demo() {
<Inputs.Height />
</div>
</Editor>
<RenderElement tagName="p" styles={styles}>
<styled.p styles={styles}>
“The parameters comprise sequences which are theoretically infinite but
limits are, of course, set to them in practice. There is an upward limit
to size and certainly a downward one... Within these sequences there are
Expand All @@ -82,7 +80,7 @@ export function Demo() {
Designing Programmes
</a> by Karl Gerstner
</em>
</RenderElement>
</styled.p>
<pre>{codegen.css(styles)}</pre>
</>
)
Expand Down
6 changes: 2 additions & 4 deletions apps/docs/pages/components/fieldset.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ all pseudo-elements and pseudo-selectors so you can also target

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

const MyEditor = () => {
const [styles, setStyles] = useState({})
Expand All @@ -34,9 +34,7 @@ const MyEditor = () => {
</Fieldset>
</>
</Editor>
<RenderElement tagName="p" styles={styles}>
Hello, world!
</RenderElement>
<styled.p styles={styles}>Hello, world!</styled.p>
</>
)
}
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/pages/examples/border-images.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import Link from 'next/link'
import { Editor, Inputs, RenderElement, codegen } from '@compai/css-gui'
import { Editor, Inputs, styled, codegen } from '@compai/css-gui'
import { defaultTheme } from '../../data/default-theme'

const initialStyles = {
Expand Down Expand Up @@ -53,7 +53,7 @@ export default function BorderImage() {
</Editor>
</div>
<div sx={{ flexGrow: 1, pr: 4 }}>
<RenderElement tagName="p" styles={styles}>
<styled.p styles={styles}>
“The parameters comprise sequences which are theoretically infinite
but limits are, of course, set to them in practice. There is an
upward limit to size and certainly a downward one... Within these
Expand All @@ -70,7 +70,7 @@ export default function BorderImage() {
</Link>{' '}
by Karl Gerstner
</em>
</RenderElement>
</styled.p>
</div>
</div>
<div className="full-bleed">
Expand Down
7 changes: 3 additions & 4 deletions apps/docs/pages/examples/filters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderElement, Editor } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'
import { useState } from 'react'

const initialStyles = {
Expand All @@ -17,8 +17,7 @@ export default function Filters() {
justifyContent: 'center',
}}
>
<RenderElement
tagName="div"
<styled.div
styles={{
width: '24rem',
height: '24rem',
Expand All @@ -37,7 +36,7 @@ export default function Filters() {
}}
>
Fun with filters
</RenderElement>
</styled.div>
</div>
</div>
)
Expand Down
7 changes: 3 additions & 4 deletions apps/docs/pages/examples/shadows.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderElement, Editor } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'
import { useState } from 'react'

const initialStyles = {
Expand Down Expand Up @@ -45,8 +45,7 @@ export default function Shadows() {
justifyContent: 'center',
}}
>
<RenderElement
tagName="div"
<styled.div
styles={{
width: '12rem',
height: '12rem',
Expand All @@ -57,7 +56,7 @@ export default function Shadows() {
}}
>
Fun with shadows
</RenderElement>
</styled.div>
</div>
</div>
)
Expand Down
7 changes: 3 additions & 4 deletions apps/docs/pages/examples/transforms.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderElement, Editor } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'
import { useState } from 'react'

const initialStyles = {
Expand All @@ -22,8 +22,7 @@ export default function Transforms() {
justifyContent: 'center',
}}
>
<RenderElement
tagName="div"
<styled.div
styles={{
width: '24rem',
height: '24rem',
Expand All @@ -37,7 +36,7 @@ export default function Transforms() {
}}
>
Fun with transforms
</RenderElement>
</styled.div>
</div>
</div>
)
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/pages/examples/transitions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderElement, Editor } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'
import { useState } from 'react'

const initialStyles = {
Expand All @@ -22,7 +22,7 @@ export default function Transitions() {
return (
<div>
<Editor styles={styles} onChange={setStyles} />
<RenderElement tagName="div" styles={styles} />
<styled.div styles={styles} />
</div>
)
}
6 changes: 2 additions & 4 deletions apps/docs/pages/exports/css.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ the style object to CSS that's then displayed in a `pre` tag.

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

export const MyEditor = () => {
const [styles, setStyles] = useState({
Expand All @@ -67,9 +67,7 @@ export const MyEditor = () => {
return (
<>
<Editor styles={styles} onChange={setStyles} />
<RenderElement tagName="p" styles={styles}>
Hello, world!
</RenderElement>
<styled.p styles={styles}>Hello, world!</styled.p>
<pre>{codegen.css(styles)}</pre>
</>
)
Expand Down
8 changes: 3 additions & 5 deletions apps/docs/pages/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ yarn add @compai/css-gui

## Usage

Import the `Editor` component and `RenderElement` to initialize controls
Import the `Editor` and `styled` components to initialize controls
and then render the element styles to your canvas.

```js
import { useState } from 'react'
import { Editor, RenderElement } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'

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

return (
<>
<Editor styles={styles} onChange={setStyles} />
<RenderElement tagName="p" styles={styles}>
Hello, world!
</RenderElement>
<styled.p styles={styles}>Hello, world!</styled.p>
</>
)
}
Expand Down
8 changes: 3 additions & 5 deletions apps/docs/pages/inputs/font-family.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FontFamilyExample } from '../../components/examples/FontFamily'

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

### Example

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

```js
import { useState } from 'react'
import { FontFamilyInput, RenderElement } from '@compai/css-gui'
import { FontFamilyInput, styled } from '@compai/css-gui'

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

return (
<>
<RenderElement tagName="p" styles={{ fontFamily }}>
Fun with fonts!
</RenderElement>
<styled.p styles={{ fontFamily }}>Fun with fonts!</styled.p>
<FontFamilyInput value={fontFamily} onChange={setFontFamily} />
</>
)
Expand Down
8 changes: 3 additions & 5 deletions apps/docs/pages/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ With CSS GUI you can quickly get up and running with parametric controls for sty
You can use this to improve your creative coding workflow or build user interfaces
where users get to control how their content looks.

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

```js
import { useState } from 'react'
import { Editor, RenderElement } from '@compai/css-gui'
import { Editor, styled } from '@compai/css-gui'

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

return (
<>
<Editor styles={styles} onChange={setStyles} />
<RenderElement tagName="p" styles={styles}>
Hello, world!
</RenderElement>
<styled.p styles={styles}>Hello, world!</styled.p>
</>
)
}
Expand Down
5 changes: 2 additions & 3 deletions packages/gui/src/components/RenderElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ export const RenderElement = ({
}: RenderElementProps) => {
const Component = tagName
const styleObject = toCSSObject(styles)



return (
// @ts-ignore
<>
<FontTags fontFamily={styles.fontFamily}/>
<FontTags fontFamily={styles.fontFamily} />
{/* @ts-ignore */}
<Component {...props} sx={styleObject} />
</>
Expand Down
13 changes: 13 additions & 0 deletions packages/gui/src/components/Styled.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HTMLAttributes } from 'react'
import { elements } from '../data/elements'
import { RenderElement } from './RenderElement'

type StyledProps = HTMLAttributes<HTMLBaseElement> & {
styles: any
}
export const styled: Record<string, any> = {}
Object.keys(elements).forEach((field: string) => {
styled[field] = ({ styles, ...props }: StyledProps) => (
<RenderElement tagName={field} styles={styles} {...props} />
)
})
1 change: 1 addition & 0 deletions packages/gui/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './components/Editor'
export { styled } from './components/Styled'
export { RenderElement } from './components/RenderElement'

export { Number as NumberInput } from './components/primitives'
Expand Down
Loading

0 comments on commit e8116c2

Please sign in to comment.