Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add merging array of variants #1226

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9f6b6de
feat: initial make variants as array
atanasster Oct 27, 2020
9643e96
use deepmerge.all and add test for function
atanasster Nov 6, 2020
047e909
docs: update changelog
atanasster Nov 7, 2020
ac3151c
fix: missing variant merge, add docs by Lauchlan
atanasster Nov 18, 2020
2612c3b
test: add test for multiple derived variants
hasparus Nov 23, 2020
8159640
Merge remote-tracking branch 'origin/master' into variants-as-array
hasparus Nov 23, 2020
9c87c52
chore: merge with master
hasparus Nov 23, 2020
da9a535
Merge branch 'master' into variants-as-array
atanasster Dec 1, 2020
4d64610
feat: added getVariantValue to extract variants
atanasster Dec 1, 2020
3b850c6
Update packages/components/test/index.js
atanasster Dec 2, 2020
5063ddd
test: update snapshots
atanasster Dec 2, 2020
cc0c301
Merge branch 'master' into variants-as-array
hasparus Dec 2, 2020
2f1ddb6
feat: add variant tuple to types
hasparus Dec 2, 2020
771b845
ref(components): prefix unused parameter with _ to make TS happier
hasparus Dec 2, 2020
3eb5de5
test: add temporary test
hasparus Dec 2, 2020
b4d9578
Merge branch 'develop' into variants-as-array
atanasster Dec 15, 2020
fccaefc
merge incoming changes
atanasster Dec 15, 2020
8af8bad
docs: fix wording as suggested by lachlanjs
atanasster Dec 15, 2020
94cc661
Merge branch 'develop' into variants-as-array
atanasster Dec 16, 2020
3984a58
support variant array prop in sx
atanasster Dec 16, 2020
1bd7c60
simplified test code
atanasster Dec 16, 2020
3e5f716
test and docs for variant as a function
atanasster Dec 17, 2020
e1a10b1
Merge branch 'develop' into variants-as-array
lachlanjc Oct 4, 2021
a1f2a9a
Merge branch 'develop' into variants-as-array
lachlanjc Oct 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/components/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ForwardRef<T, P> = React.ForwardRefExoticComponent<

export interface BoxOwnProps extends SpaceProps, ColorProps {
as?: React.ElementType
variant?: string
variant?: string | string[]
css?: InterpolationWithTheme<any>
}
export interface BoxProps
Expand Down
21 changes: 16 additions & 5 deletions packages/components/src/Box.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from '@emotion/styled'
import merge from 'deepmerge'
import { css, get } from '@theme-ui/css'
import { createShouldForwardProp } from '@styled-system/should-forward-prop'
import space from '@styled-system/space'
Expand All @@ -9,10 +10,20 @@ const shouldForwardProp = createShouldForwardProp([
...color.propNames,
])

const sx = props => css(props.sx)(props.theme)
const base = props => css(props.__css)(props.theme)
const variant = ({ theme, variant, __themeKey = 'variants' }) =>
css(get(theme, __themeKey + '.' + variant, get(theme, variant)))
const sx = (props) => css(props.sx)(props.theme)
const base = (props) => css(props.__css)(props.theme)
const variant = ({ theme, variant, __themeKey = 'variants' }) => {
if (Array.isArray(variant)) {
return css(
variant.reduce(
lachlanjc marked this conversation as resolved.
Show resolved Hide resolved
(styles, v) =>
merge(styles, get(theme, __themeKey + '.' + v, get(theme, v))),
{}
)
)
}
return css(get(theme, __themeKey + '.' + variant, get(theme, variant)))
}

export const Box = styled('div', {
shouldForwardProp,
Expand All @@ -27,7 +38,7 @@ export const Box = styled('div', {
space,
color,
sx,
props => props.css
(props) => props.css
)

export default Box
14 changes: 14 additions & 0 deletions packages/components/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const theme = {
p: 4,
bg: 'highlight',
},
bop: {
m: 1,
bg: 'muted',
},
},
cards: {
primary: {
Expand Down Expand Up @@ -134,6 +138,16 @@ describe('Box', () => {
expect(json).toHaveStyleRule('background-color', 'highlight')
expect(json).toHaveStyleRule('padding', '32px')
})
test('renders with variant array', () => {
const json = renderJSON(
<ThemeProvider theme={theme}>
<Box variant={['boxes.beep', 'boxes.bop']} />
</ThemeProvider>
)
expect(json).toHaveStyleRule('background-color', 'muted')
expect(json).toHaveStyleRule('padding', '32px')
expect(json).toHaveStyleRule('margin', '4px')
})

test('renders with base styles', () => {
const json = renderJSON(
Expand Down