Skip to content

Commit

Permalink
Spike out a color import
Browse files Browse the repository at this point in the history
Closes #94
  • Loading branch information
johno committed May 2, 2022
1 parent 26fd8d8 commit eeae533
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/late-cameras-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@compai/css-gui': patch
---

Add importColors utility function
214 changes: 214 additions & 0 deletions apps/docs/components/examples/ColorImport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import { useState } from 'react'
import { Editor, importColors } from '@compai/css-gui'

// https://components.ai/theme/asa9lvO69pQGyRn8ZAXL?tab=export
const themeColors = {
gray: [
'#000000',
'#1c1c1c',
'#303030',
'#474747',
'#5d5d5d',
'#757575',
'#8c8c8c',
'#a3a3a3',
'#bababa',
'#d1d1d1',
'#e8e8e8',
'#ffffff',
],
'blue-gray': [
'#0e0e11',
'#21222a',
'#343544',
'#484a5e',
'#5c5f78',
'#717490',
'#8789a6',
'#9c9eba',
'#b1b3cb',
'#c6c8db',
'#dcdce9',
'#f1f1f6',
],
blue: [
'#0d0e1a',
'#182142',
'#1e336d',
'#254797',
'#325bbd',
'#476fde',
'#6284f6',
'#809aff',
'#9fb0ff',
'#bcc6ff',
'#d9deff',
'#f4f5ff',
],
indigo: [
'#120c1d',
'#211b4d',
'#2f297f',
'#3e38b0',
'#5049dd',
'#675bff',
'#8170ff',
'#9d87ff',
'#b7a0ff',
'#d0baff',
'#e5d6ff',
'#f7f1ff',
],
violet: [
'#170a1b',
'#321545',
'#501b71',
'#6d239d',
'#8a2fc5',
'#a641e7',
'#be58ff',
'#d374ff',
'#e392ff',
'#f0b1ff',
'#f9d0ff',
'#fef0ff',
],
magenta: [
'#170915',
'#381436',
'#5b1859',
'#7f1e7c',
'#a0289d',
'#bf3abb',
'#d853d2',
'#ea70e4',
'#f78ff0',
'#ffaef8',
'#ffcefc',
'#ffeefe',
],
red: [
'#19090a',
'#3e131a',
'#651829',
'#8d1d38',
'#b22749',
'#d23a5b',
'#ec5370',
'#ff7086',
'#ff909e',
'#ffb0b9',
'#ffd0d4',
'#fff0f1',
],
orange: [
'#200d02',
'#431706',
'#691e0a',
'#8e280d',
'#b13514',
'#d14721',
'#ea5e36',
'#fd7950',
'#ff9670',
'#ffb495',
'#ffd2be',
'#fff0e9',
],
gold: [
'#160f05',
'#402e11',
'#6e4d14',
'#9c6c18',
'#c68a20',
'#eba62e',
'#ffbe44',
'#ffd15e',
'#ffe07c',
'#ffeb9b',
'#fff3bc',
'#fffade',
],
yellow: [
'#0d0c04',
'#332f11',
'#585315',
'#7f7719',
'#a49a20',
'#c5ba2c',
'#e1d43f',
'#f6e857',
'#fff673',
'#fffe90',
'#ffffae',
'#ffffcc',
],
lime: [
'#161708',
'#323711',
'#505a15',
'#6e7c19',
'#8b9c22',
'#a6b932',
'#bed049',
'#d1e264',
'#e1ee83',
'#edf6a3',
'#f6fbc4',
'#fcfee6',
],
green: [
'#071209',
'#102816',
'#133f21',
'#18572d',
'#216f3b',
'#32864b',
'#499c60',
'#66b178',
'#85c492',
'#a7d6af',
'#cae7cf',
'#eef7ef',
],
teal: [
'#07110f',
'#102722',
'#133d35',
'#18544a',
'#216b5e',
'#328173',
'#499789',
'#65ac9e',
'#85c0b3',
'#a7d3c9',
'#cae5de',
'#eef7f4',
],
cyan: [
'#081111',
'#112627',
'#143c3e',
'#185356',
'#226a6d',
'#338084',
'#4b969a',
'#67abae',
'#87bfc1',
'#a8d2d4',
'#cbe4e5',
'#eef6f7',
],
}

export const ColorImportExample = () => {
const [styles, setStyles] = useState({ color: 'tomato' })
const colors = importColors(themeColors)

return (
<>
<Editor styles={styles} onChange={setStyles} theme={{ colors }} />
<p style={{ color: styles.color }}>Hello, world!</p>
</>
)
}
62 changes: 62 additions & 0 deletions apps/docs/pages/imports/color.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { ColorImportExample } from '../../components/examples/ColorImport'

# Import Colors

If you have a [System UI](https://system-ui.com/theme) compatible color palette, you can
use `importColors` to transform it to the theme used internally.

In the future, this will likely happen automatically, but for now it needs to be explicitly
called.

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

// https://components.ai/theme/asa9lvO69pQGyRn8ZAXL?tab=export
const themeColors = {
gray: [
'#000000',
'#1c1c1c',
'#303030',
'#474747',
'#5d5d5d',
'#757575',
'#8c8c8c',
'#a3a3a3',
'#bababa',
'#d1d1d1',
'#e8e8e8',
'#ffffff',
],
'blue-gray': [
'#0e0e11',
'#21222a',
'#343544',
'#484a5e',
'#5c5f78',
'#717490',
'#8789a6',
'#9c9eba',
'#b1b3cb',
'#c6c8db',
'#dcdce9',
'#f1f1f6',
],
}

export const MyEditor = () => {
const [styles, setStyles] = useState({ color: 'tomato' })
const colors = importColors(themeColors)

return (
<>
<Editor styles={styles} onChange={setStyles} theme={{ colors }} />
<p style={{ color: styles.color }}>Hello, world!</p>
</>
)
}
```

## Interactive Example

<ColorImportExample />
3 changes: 2 additions & 1 deletion packages/gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"pascalcase": "^2.0.0",
"react-feather": "^2.0.9",
"react-use-gesture": "^9.1.3",
"theme-ui": "^0.14.5"
"theme-ui": "^0.14.5",
"uuid": "^8.3.2"
}
}
43 changes: 43 additions & 0 deletions packages/gui/src/lib/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { v4 as uuid } from 'uuid'
import { Color } from '../types/css'

type ColorGroupArray = Array<Color>
type ColorObject = {
[name: string]: Color | ColorGroupArray
}

/**
* Turn a system-ui color object into an internally compatible shape
*
* {
* gray: ['#000', '#111', '#999'],
* tomato: 'tomato'
* }
*/
export const importColors = (colors: ColorObject) => {
return Object.entries(colors).map(([name, group]) => {
if (!Array.isArray(group)) {
return {
id: uuid(),
name,
colors: [
{
id: uuid(),
value: group,
},
],
}
}

return {
id: uuid(),
name,
colors: group.map((value) => {
return {
id: uuid(),
value: value,
}
}),
}
})
}
1 change: 1 addition & 0 deletions packages/gui/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './convert'
export * as codegen from './codegen'
export * from './codegen/to-css-object'
export * from './import'
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6355,6 +6355,11 @@ use@^3.1.0:
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==

uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

uvu@^0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/uvu/-/uvu-0.5.3.tgz#3d83c5bc1230f153451877bfc7f4aea2392219ae"
Expand Down

0 comments on commit eeae533

Please sign in to comment.