Skip to content

Commit 2917530

Browse files
author
Andrey Okonetchnikov
committed
fix: Make sure theme is resolved
1 parent a342e55 commit 2917530

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/mixins.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { transparentize } from '@theme-ui/color'
22

3+
// Check if it's a props object or the theme
4+
// theme-ui provides theme as an argument where styled-system uses props.theme
5+
function getTheme(theme = {}) {
6+
return theme['theme'] || theme
7+
}
38
/**
49
* Mixin to generate consistent box-shadow rule for focus rings and selections
510
*/
6-
export const focusBoxShadow = color => (t = {}) => {
7-
// Check if it's a props object or the theme
8-
// theme-ui provides theme as an argument where styled-system uses props.theme
9-
let theme = t.theme || t
11+
export const focusBoxShadow = color => (theme = {}) => {
1012
return {
11-
boxShadow: `0 0 0 0.2em ${transparentize(color, 0.75)(theme)}`
13+
boxShadow: `0 0 0 0.2em ${transparentize(color, 0.75)(getTheme(theme))}`
1214
}
1315
}
1416

@@ -23,17 +25,18 @@ export const focusBoxShadow = color => (t = {}) => {
2325
* }
2426
* `
2527
*/
26-
export const focusRingStyles = (color, disabled = false) => (theme = {}) => {
28+
export const focusRingStyles = (color, disabled = false) => theme => {
29+
const themeColor = transparentize(color, 0)(getTheme(theme)) // This serves as a getter from theme
2730
if (disabled) {
2831
return {
2932
outline: 'none'
3033
}
3134
}
3235
return {
3336
outline: 'none',
34-
borderColor: transparentize(color, 0)(theme), // This serves as a getter from theme
37+
borderColor: themeColor,
3538
transition: 'box-shadow .25s',
36-
...focusBoxShadow(color)(theme)
39+
...focusBoxShadow(themeColor)(theme)
3740
}
3841
}
3942

@@ -46,17 +49,18 @@ export const focusRingStyles = (color, disabled = false) => (theme = {}) => {
4649
* ${focusRing('red')}
4750
* `
4851
*/
49-
export const focusRing = (color, disabled = false, hover = false) => (theme = {}) => {
52+
export const focusRing = (color, disabled = false, hover = false) => theme => {
53+
const styles = focusRingStyles(color, disabled)(getTheme(theme))
5054
const baseStyles = {
5155
'.js-focus-visible &:focus:not(.focus-visible)': {
5256
outline: 0
5357
},
54-
'&.focus-visible': focusRingStyles(color, disabled)(theme)
58+
'&.focus-visible': styles
5559
}
5660
if (hover) {
5761
return {
5862
...baseStyles,
59-
'&:hover:not(:disabled)': focusRingStyles(color, disabled)(theme)
63+
'&:hover:not(:disabled)': styles
6064
}
6165
}
6266
return baseStyles

src/mixins.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Creates consistent `:focus` styles for any element.
66
77
```javascript
88
import 'focus-visible' // :focus-visible polyfill
9-
import styled from 'styled-components'
9+
import styled, { ThemeProvider } from 'styled-components'
1010
import { focusRing, WithSelector } from './'
1111

1212
const Button = styled('button')`
@@ -16,18 +16,24 @@ const Button = styled('button')`
1616
:hover {
1717
background: red;
1818
}
19-
${focusRing('red')}
19+
${focusRing('primary')}
2020
`
2121

22-
;<>
22+
;<ThemeProvider
23+
theme={{
24+
colors: {
25+
primary: '#fc0'
26+
}
27+
}}
28+
>
2329
<Button>Button</Button>{' '}
2430
<WithSelector selector=":hover">
2531
<Button>Focused</Button>
2632
</WithSelector>
2733
<WithSelector selector=".focus-visible">
2834
<Button>Focused</Button>
2935
</WithSelector>
30-
</>
36+
</ThemeProvider>
3137
```
3238

3339
### `focusBoxShadow`

0 commit comments

Comments
 (0)