Skip to content

Commit 2a3a9ed

Browse files
author
LB
authored
Merge branch 'master' into prism-options
2 parents 89cc101 + c973516 commit 2a3a9ed

File tree

39 files changed

+688
-272
lines changed

39 files changed

+688
-272
lines changed

jest-preprocess.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = require('babel-jest').createTransformer({
2+
presets: ['babel-preset-gatsby'],
3+
})

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@babel/runtime": "^7.7.7",
2828
"@testing-library/react": "^9.1.3",
2929
"@types/jest": "^25.1.2",
30-
"babel-jest": "^24.9.0",
30+
"babel-jest": "^25.3.0",
3131
"husky": ">=4.0.7",
3232
"jest": "^24.8.0",
3333
"jest-canvas-mock": "^2.2.0",
@@ -96,7 +96,10 @@
9696
],
9797
"setupFiles": [
9898
"jest-canvas-mock"
99-
]
99+
],
100+
"transform": {
101+
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js"
102+
}
100103
},
101104
"husky": {
102105
"hooks": {

packages/color-modes/src/custom-properties.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const reservedKeys = {
1313
useCustomProperties: true,
1414
initialColorModeName: true,
1515
initialColorMode: true,
16+
useLocalStorage: true,
1617
}
1718

1819
const toPixel = (key, value) => {
@@ -86,6 +87,6 @@ export const createColorStyles = (theme = {}) => {
8687
...styles,
8788
color: 'text',
8889
bg: 'background',
89-
}
90+
},
9091
})(theme)
9192
}

packages/color-modes/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const useColorModeState = (theme = {}) => {
5050

5151
// initialize state
5252
React.useEffect(() => {
53-
const stored = storage.get()
53+
const stored = theme.useLocalStorage !== false && storage.get()
5454
document.body.classList.remove('theme-ui-' + stored)
5555
if (!stored && theme.useColorSchemeMediaQuery) {
5656
const query = getMediaQuery()
@@ -62,7 +62,7 @@ const useColorModeState = (theme = {}) => {
6262
}, [])
6363

6464
React.useEffect(() => {
65-
if (!mode) return
65+
if (!mode || theme.useLocalStorage === false) return
6666
storage.set(mode)
6767
}, [mode])
6868

packages/color-modes/test/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,27 @@ test('initializes mode based on localStorage', () => {
209209
expect(mode).toBe('dark')
210210
})
211211

212+
test('does not initialize mode based on localStorage if useLocalStorage is set to false', () => {
213+
window.localStorage.setItem(STORAGE_KEY, 'dark')
214+
let mode
215+
const Button = props => {
216+
const [colorMode, setMode] = useColorMode()
217+
mode = colorMode
218+
return <button children="test" />
219+
}
220+
const tree = render(
221+
<ThemeProvider
222+
theme={{
223+
useLocalStorage: false,
224+
}}>
225+
<ColorModeProvider>
226+
<Button />
227+
</ColorModeProvider>
228+
</ThemeProvider>
229+
)
230+
expect(mode).toBe('default')
231+
})
232+
212233
test('retains initial context', () => {
213234
let context
214235
const Consumer = props => {

packages/color/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export default props => (
2626

2727
## API
2828

29+
### `getColor`
30+
31+
```js
32+
import { getColor } from '@theme-ui/color'
33+
// getColor(theme, 'primary')
34+
```
35+
2936
### `darken`
3037

3138
Darken a color by an amount 0–1
@@ -185,7 +192,7 @@ We can take the result of any of the above helper functions (which return a func
185192
${alpha('primary', 0.5)(t)}
186193
${alpha('secondary', 0.5)(t)}
187194
)
188-
`
195+
`,
189196
}}
190197
/>
191198
```

packages/color/src/index.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,91 @@
11
import * as P from 'polished'
22
import { get, Theme } from '@theme-ui/css'
33

4-
const g = (t: Theme, c: string) =>
5-
get(t, `colors.${c}`, c)
4+
/**
5+
* Get color from theme.colors
6+
*/
7+
export const getColor = (theme: Theme, color: string) =>
8+
get(theme, `colors.${color}`, color)
69
.replace(/^var\(--(\w+)(.*?), /, '')
710
.replace(/\)/, '')
811

912
/**
1013
* Darken a color by an amount 0–1
1114
*/
1215
export const darken = (c: string, n: number) => (t: Theme) =>
13-
P.darken(n, g(t, c))
16+
P.darken(n, getColor(t, c))
1417
/**
1518
* Lighten a color by an amount 0–1
1619
*/
1720
export const lighten = (c: string, n: number) => (t: Theme) =>
18-
P.lighten(n, g(t, c))
21+
P.lighten(n, getColor(t, c))
1922
/**
2023
* Rotate the hue of a color by an amount 0–360
2124
*/
2225
export const rotate = (c: string, d: number) => (t: Theme) =>
23-
P.adjustHue(d, g(t, c))
26+
P.adjustHue(d, getColor(t, c))
2427

2528
/**
2629
* Set the hue of a color to a degree 0–360
2730
*/
28-
export const hue = (c: string, h: number) => (t: Theme) => P.setHue(h, g(t, c))
31+
export const hue = (c: string, h: number) => (t: Theme) =>
32+
P.setHue(h, getColor(t, c))
2933
/**
3034
* Set the saturation of a color to an amount 0–1
3135
*/
3236
export const saturation = (c: string, s: number) => (t: Theme) =>
33-
P.setSaturation(s, g(t, c))
37+
P.setSaturation(s, getColor(t, c))
3438
/**
3539
* Set the lightness of a color to an amount 0–1
3640
*/
3741
export const lightness = (c: string, l: number) => (t: Theme) =>
38-
P.setLightness(l, g(t, c))
42+
P.setLightness(l, getColor(t, c))
3943
/**
4044
* Desaturate a color by an amount 0–1
4145
*/
4246
export const desaturate = (c: string, n: number) => (t: Theme) =>
43-
P.desaturate(n, g(t, c))
47+
P.desaturate(n, getColor(t, c))
4448
/**
4549
* Saturate a color by an amount 0–1
4650
*/
4751
export const saturate = (c: string, n: number) => (t: Theme) =>
48-
P.saturate(n, g(t, c))
52+
P.saturate(n, getColor(t, c))
4953

5054
/**
5155
* Shade a color by an amount 0–1
5256
*/
53-
export const shade = (c: string, n: number) => (t: Theme) => P.shade(n, g(t, c))
57+
export const shade = (c: string, n: number) => (t: Theme) =>
58+
P.shade(n, getColor(t, c))
5459
/**
5560
* Tint a color by an amount 0–1
5661
*/
57-
export const tint = (c: string, n: number) => (t: Theme) => P.tint(n, g(t, c))
62+
export const tint = (c: string, n: number) => (t: Theme) =>
63+
P.tint(n, getColor(t, c))
5864

5965
export const transparentize = (c: string, n: number) => (t: Theme) =>
60-
P.transparentize(n, g(t, c))
66+
P.transparentize(n, getColor(t, c))
6167
/**
6268
* Set the transparency of a color to an amount 0-1
6369
*/
64-
export const alpha = (c: string, n: number) => (t: Theme) => P.rgba(g(t, c), n)
70+
export const alpha = (c: string, n: number) => (t: Theme) =>
71+
P.rgba(getColor(t, c), n)
6572

6673
/**
6774
* Mix two colors by a specific ratio
6875
*/
6976
export const mix = (a: string, b: string, n = 0.5) => (t: Theme) =>
70-
P.mix(n, g(t, a), g(t, b))
77+
P.mix(n, getColor(t, a), getColor(t, b))
7178

7279
/**
7380
* Get the complement of a color
7481
*/
75-
export const complement = (c: string) => (t: Theme) => P.complement(g(t, c))
82+
export const complement = (c: string) => (t: Theme) =>
83+
P.complement(getColor(t, c))
7684

7785
/**
7886
* Get the inverted color
7987
*/
80-
export const invert = (c: string) => (t: Theme) => P.invert(g(t, c))
88+
export const invert = (c: string) => (t: Theme) => P.invert(getColor(t, c))
8189

8290
/**
8391
* Desaturate the color to grayscale

packages/components/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
Primitive layout, typographic, and other components for use with Theme UI.
55

6+
**Note:** *This package is included in the main `theme-ui` package and a separate installation is not required.*
7+
68
```sh
79
npm i @theme-ui/components
810
```

packages/components/src/Text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import React from 'react'
22
import Box from './Box'
33

44
export const Text = React.forwardRef((props, ref) => (
5-
<Box ref={ref} {...props} __themeKey="text" />
5+
<Box ref={ref} variant="default" {...props} __themeKey="text" />
66
))

packages/components/test/__snapshots__/index.js.snap

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,6 @@ exports[`Embed renders 1`] = `
492492
`;
493493

494494
exports[`Field renders 1`] = `
495-
.emotion-2 {
496-
box-sizing: border-box;
497-
margin: 0;
498-
min-width: 0;
499-
}
500-
501495
.emotion-0 {
502496
box-sizing: border-box;
503497
margin: 0;
@@ -527,6 +521,12 @@ exports[`Field renders 1`] = `
527521
background-color: transparent;
528522
}
529523
524+
.emotion-2 {
525+
box-sizing: border-box;
526+
margin: 0;
527+
min-width: 0;
528+
}
529+
530530
<div
531531
className="emotion-2"
532532
>
@@ -1313,6 +1313,7 @@ exports[`Text renders 1`] = `
13131313
box-sizing: border-box;
13141314
margin: 0;
13151315
min-width: 0;
1316+
font-size: 20px;
13161317
}
13171318
13181319
<div

0 commit comments

Comments
 (0)