Skip to content

Commit 047cd06

Browse files
committed
Change internal structure of react-design-tokens
1 parent fadad65 commit 047cd06

33 files changed

+282
-314
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* @jsx jsx */
2+
import { jsx } from "theme-ui"
3+
import React from "react"
4+
import { readableColor } from "polished"
5+
import { Swatch, SwatchToken, SwatchValue } from "."
6+
import { tokenPropType, valuePropType } from "./propTypes"
7+
8+
export const ColorSwatch = ({ value, token }) => {
9+
const color = readableColor(value, "black", "white")
10+
return (
11+
<Swatch token={token} value={value}>
12+
<div
13+
sx={{
14+
p: 3,
15+
pt: 6,
16+
bg: value,
17+
overflow: "hidden"
18+
}}
19+
>
20+
<SwatchToken color={color}>{token}</SwatchToken>
21+
<SwatchValue color={color}>{value}</SwatchValue>
22+
</div>
23+
</Swatch>
24+
)
25+
}
26+
27+
ColorSwatch.propTypes = {
28+
...tokenPropType,
29+
...valuePropType
30+
}
31+
32+
/** @component */
33+
export default ColorSwatch
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Renders a color swatch with a readable text.
22

33
```jsx harmony
4-
<ColorSwatch token={'accent'} value={'#C0100F'} />
4+
<ColorSwatch token={"accent"} value={"#C0100F"} />
55
```
66

77
When clicked, the value of the token is copied to clipboard.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from "react"
2+
import { Grid, ThemeProvider } from "theme-ui"
3+
import { omitBy, pickBy, isString } from "lodash"
4+
import { Swatches, ColorSwatch, PaletteSwatch } from "."
5+
6+
export default function Colors({ theme }) {
7+
const gap = 2
8+
const colors = pickBy(theme.colors, isString)
9+
const palettes = omitBy(theme.colors, isString)
10+
return (
11+
<ThemeProvider theme={theme}>
12+
<Grid gap={gap}>
13+
<Swatches theme={theme} items={palettes}>
14+
{(token, value) => (
15+
<Grid
16+
gap={0}
17+
key={token}
18+
sx={{
19+
gridTemplateColumns: "repeat(auto-fit, minmax(90px, 1fr))"
20+
}}
21+
>
22+
<PaletteSwatch token={token} value={value} />
23+
</Grid>
24+
)}
25+
</Swatches>
26+
<Grid
27+
gap={gap}
28+
sx={{
29+
gridTemplateColumns: "repeat(auto-fit, minmax(90px, 1fr))"
30+
}}
31+
>
32+
<Swatches theme={theme} items={colors}>
33+
{(token, value) => <ColorSwatch token={token} value={value} key={token} />}
34+
</Swatches>
35+
</Grid>
36+
</Grid>
37+
</ThemeProvider>
38+
)
39+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
```jsx harmony
2-
import theme from '../theme'
3-
2+
import theme from "./theme"
43
;<Colors theme={theme} />
54
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react"
2+
import { Swatches, ColorSwatch } from "."
3+
import { tokenPropType, valuePropType } from "./propTypes"
4+
5+
export const PaletteSwatch = ({ token, value }) => (
6+
<Swatches items={value}>
7+
{(key, value) => (
8+
<ColorSwatch value={value} token={`${token}.${key}`} key={`${token}.${key}`} />
9+
)}
10+
</Swatches>
11+
)
12+
13+
PaletteSwatch.propTypes = {
14+
...tokenPropType,
15+
...valuePropType
16+
}
17+
18+
/** @component */
19+
export default PaletteSwatch
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Palette swatch renders all color tokens from a palette as defined in [System UI theme specification](https://system-ui.com/theme/)
2+
3+
It supports arrays
4+
5+
```jsx harmony
6+
const palette = [
7+
"#FFCDD2",
8+
"#EF9A9A",
9+
"#E57373",
10+
"#EF5350",
11+
"#F44336",
12+
"#E53935",
13+
"#D32F2F",
14+
"#C62828",
15+
"#B71C1C"
16+
]
17+
;<PaletteSwatch token={"red"} value={palette} />
18+
```
19+
20+
as well as object notation:
21+
22+
```jsx harmony
23+
import theme from "./theme"
24+
;<PaletteSwatch token={"slate"} value={theme.colors.slate} />
25+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* @jsx jsx */
2+
import React from "react"
3+
import { Grid, jsx, ThemeProvider } from "theme-ui"
4+
import { SpacingSwatch, Swatch, Swatches, SwatchToken } from "."
5+
6+
export default function Spacing({ theme }) {
7+
return (
8+
<ThemeProvider theme={theme}>
9+
<Grid gap={4}>
10+
<Swatches items={theme.space}>
11+
{(token, value) => (
12+
<Swatch token={token} value={value} key={token}>
13+
<Grid gap={3} sx={{ alignItems: "center", gridTemplateColumns: "auto 1fr" }}>
14+
<SwatchToken>{token}</SwatchToken>
15+
<SpacingSwatch value={value} />
16+
</Grid>
17+
</Swatch>
18+
)}
19+
</Swatches>
20+
</Grid>
21+
</ThemeProvider>
22+
)
23+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Spacing renders all `space` tokens from the theme in a convenient way.
22

33
```jsx harmony
4-
import theme from '../theme'
5-
4+
import theme from "./theme"
65
;<Spacing theme={theme} />
76
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* @jsx jsx */
2+
import React from "react"
3+
import { jsx } from "theme-ui"
4+
import { SwatchValue } from "."
5+
import { valuePropType } from "./propTypes"
6+
7+
const SpacingSwatch = ({ value, css: componentCSS, ...rest }) => {
8+
return (
9+
<div
10+
sx={{
11+
py: 2,
12+
color: "secondary",
13+
bg: "muted",
14+
width: value,
15+
...componentCSS
16+
}}
17+
{...rest}
18+
>
19+
<SwatchValue color="inherit" sx={{ mx: 2 }}>
20+
{value}
21+
</SwatchValue>
22+
</div>
23+
)
24+
}
25+
26+
SpacingSwatch.propTypes = {
27+
...valuePropType
28+
}
29+
30+
/** @component */
31+
export default SpacingSwatch
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
Spacing swatch renders a bar with a width of the provided `value` and its content as a text
22

33
```jsx harmony
4-
<SpacingSwatch value={'1rem'} />
4+
<SpacingSwatch value={"1rem"} />
55
```
66

77
You can override default styles using `css` prop:
88

99
```jsx harmony
1010
<SpacingSwatch
11-
value={'100px'}
11+
value={"100px"}
1212
css={{
13-
color: 'white',
14-
background: 'cornflowerblue'
13+
color: "white",
14+
background: "cornflowerblue"
1515
}}
1616
/>
1717
```

0 commit comments

Comments
 (0)