Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit f9ae5e5

Browse files
Support baseStyle as a function
1 parent 913d502 commit f9ae5e5

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

packages/chakra-ui-core/src/utils/component.test.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('createStyledAttrsMixin', () => {
2020
}
2121

2222
describe('baseStyle', () => {
23-
const renderFakeComponent = ({ theme, ...props }) => {
23+
const renderFakeComponent = ({ theme, colorMode, ...props }) => {
2424
const inlineAttrs = (props && props.inlineAttrs) || ''
2525
return render({
2626
template: `<FakeComponent ${inlineAttrs} />`,
@@ -29,7 +29,7 @@ describe('createStyledAttrsMixin', () => {
2929
},
3030
provide: () => ({
3131
$chakraTheme: () => toCSSVar(theme),
32-
$chakraColorMode: () => 'light'
32+
$chakraColorMode: () => colorMode || 'light'
3333
}),
3434
...props
3535
})
@@ -65,5 +65,38 @@ describe('createStyledAttrsMixin', () => {
6565
})
6666
expect(asFragment()).toMatchSnapshot()
6767
})
68+
69+
it('should accept baseStyle as a function', () => {
70+
const theme = {
71+
...defaultTheme,
72+
baseStyle: {
73+
FakeComponent: ({ colorMode, theme }) => ({
74+
bg: colorMode === 'light' ? 'red.400' : 'red.200',
75+
color: colorMode === 'light' ? 'black' : 'white',
76+
border: `2px solid ${
77+
colorMode === 'light'
78+
? theme.color.blue[600]
79+
: theme.colors.blue[200]
80+
}`
81+
})
82+
}
83+
}
84+
85+
// light mode
86+
const lightWrapper = renderFakeComponent({
87+
theme,
88+
colorMode: 'light',
89+
inlineAttrs: 'bg="blue.200"'
90+
})
91+
expect(lightWrapper.asFragment()).toMatchSnapshot()
92+
93+
// dark mode
94+
const darkWrapper = renderFakeComponent({
95+
theme,
96+
colorMode: 'dark',
97+
inlineAttrs: 'bg="vue.200"'
98+
})
99+
expect(darkWrapper.asFragment()).toMatchSnapshot()
100+
})
68101
})
69102
})

packages/chakra-ui-core/src/utils/components.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css } from '@emotion/css'
2+
import { runIfFn } from '@chakra-ui/utils'
23
import { composeSystem, __get } from './styled-system'
34
import { hasOwn, extractChakraAttrs } from './object'
45

@@ -69,7 +70,15 @@ export const createStyledAttrsMixin = name => ({
6970
}
7071
},
7172
baseStyle () {
72-
return __get(this.theme, `baseStyle.${name}`) || {}
73+
const { nativeAttrs } = this.splitProps
74+
const styleObjectOrFn = __get(this.theme, `baseStyle.${name}`)
75+
return (
76+
runIfFn(styleObjectOrFn, {
77+
theme: this.theme,
78+
colorMode: this.colorMode,
79+
...nativeAttrs
80+
}) || {}
81+
)
7382
},
7483
className () {
7584
const { styleAttrs } = this.splitProps

0 commit comments

Comments
 (0)