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

Commit f203fe3

Browse files
Support baseStyle setting in theme
1 parent 5a53916 commit f203fe3

File tree

4 files changed

+104
-18
lines changed

4 files changed

+104
-18
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`__get should return correct value for given path 1`] = `
4+
Object {
5+
"bg": "red.400",
6+
"color": "black",
7+
}
8+
`;
9+
10+
exports[`__get should return correct value for given path 2`] = `"red.400"`;
11+
12+
exports[`__get should return correct value for given path 3`] = `"black"`;
13+
14+
exports[`__get should return correct value for given path 4`] = `"#ff0000"`;
15+
16+
exports[`__get should return correct value for given path 5`] = `
17+
Object {
18+
"bg": "red.400",
19+
"color": "black",
20+
}
21+
`;
22+
23+
exports[`__get should return correct value for given path 6`] = `"red.400"`;
24+
25+
exports[`__get should return correct value for given path 7`] = `"black"`;
26+
27+
exports[`__get should return correct value for given path 8`] = `"#ff0000"`;

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { css } from '@emotion/css'
2-
import { composeSystem } from './styled-system'
2+
import { composeSystem, __get } from './styled-system'
33
import { hasOwn, extractChakraAttrs } from './object'
44

55
export const isVueComponent = (value) => {
6-
return (!!value && !!value.$el)
6+
return !!value && !!value.$el
77
}
88

99
/**
@@ -12,7 +12,7 @@ export const isVueComponent = (value) => {
1212
* for primitives with changes in the $parent $attrs
1313
* and $listeners objects
1414
* @param {String} property
15-
*/
15+
*/
1616
export function createWatcher (property) {
1717
return {
1818
handler (newVal, oldVal) {
@@ -32,8 +32,8 @@ export function createWatcher (property) {
3232
/**
3333
* Create mixin for style attributes
3434
* @param {String} name Component name
35-
*/
36-
export const createStyledAttrsMixin = (name, isPseudo) => ({
35+
*/
36+
export const createStyledAttrsMixin = name => ({
3737
name,
3838
inheritAttrs: false,
3939
inject: ['$chakraTheme', '$chakraColorMode'],
@@ -68,15 +68,24 @@ export const createStyledAttrsMixin = (name, isPseudo) => ({
6868
nativeAttrs
6969
}
7070
},
71+
baseStyle () {
72+
return __get(this.theme, `baseStyle.${name}`, {})
73+
},
7174
className () {
7275
const { styleAttrs } = this.splitProps
73-
const boxStylesObject = composeSystem(styleAttrs, this.theme)
76+
const boxStylesObject = composeSystem(
77+
{
78+
...this.baseStyle,
79+
...styleAttrs
80+
},
81+
this.theme
82+
)
7483
return css(boxStylesObject)
7584
},
7685
/** Computed attributes object */
7786
computedAttrs () {
7887
return {
79-
...name && { 'data-chakra-component': name },
88+
...(name && { 'data-chakra-component': name }),
8089
...this.splitProps.nativeAttrs
8190
}
8291
},

packages/chakra-ui-core/src/utils/styled-system.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ import { css } from '@chakra-ui/styled-system'
77
* @param {Object} obj Theme property
88
* @param {String} key Theme key
99
* @param {String} def Definition if non-existent
10-
* @param {*} p
11-
* @param {*} undef
1210
*/
13-
export const __get = (obj, key, def, p, undef) => {
14-
key = key && key.split ? key.split('.') : [key]
11+
export const __get = (obj, key, def) => {
12+
const keys = key && key.split ? key.split('.') : [key]
1513

16-
for (p = 0; p < key.length; p++) {
17-
obj = obj ? obj[key[p]] : undef
18-
}
19-
20-
return obj === undef ? def : obj
14+
return (
15+
keys.reduce((res, key) => res && res[key], obj) || def
16+
)
2117
}
2218

23-
export const composeSystem = (props = {}, theme = {}) =>
24-
css(props)(theme)
19+
export const composeSystem = (props = {}, theme = {}) => css(props)(theme)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { __get } from './styled-system'
2+
3+
describe('__get', () => {
4+
it('should return correct value for given path', () => {
5+
const theme = {
6+
baseStyle: {
7+
Button: {
8+
bg: 'red.400',
9+
color: 'black'
10+
}
11+
},
12+
colors: {
13+
red: {
14+
400: '#ff0000'
15+
}
16+
}
17+
}
18+
19+
expect(__get(theme, 1)).toBeUndefined()
20+
expect(__get(theme, 'someRandomKey')).toBeUndefined()
21+
expect(__get(theme, 'colors.red.500')).toBeUndefined()
22+
expect(__get(theme, 'baseStyle.Button.bg._focus')).toBeUndefined()
23+
24+
expect(__get(theme, 'baseStyle.Button')).toMatchSnapshot()
25+
expect(__get(theme, 'baseStyle.Button.bg')).toMatchSnapshot()
26+
expect(__get(theme, 'baseStyle.Button.color')).toMatchSnapshot()
27+
expect(__get(theme, 'colors.red.400')).toMatchSnapshot()
28+
29+
expect(__get(theme.baseStyle, 'Button')).toMatchSnapshot()
30+
expect(__get(theme.baseStyle, 'Button.bg')).toMatchSnapshot()
31+
expect(__get(theme.baseStyle, 'Button.color')).toMatchSnapshot()
32+
expect(__get(theme.colors, 'red.400')).toMatchSnapshot()
33+
})
34+
35+
it('should return provide default value if specified', () => {
36+
const theme = {
37+
baseStyle: {
38+
Button: {
39+
bg: 'red.400',
40+
color: 'black'
41+
}
42+
},
43+
colors: {
44+
red: {
45+
400: '#ff0000'
46+
}
47+
}
48+
}
49+
50+
expect(__get(theme, 1, 'default')).toEqual('default')
51+
expect(__get(theme, 'someRandomKey', 'default')).toEqual('default')
52+
expect(__get(theme, 'colors.red.500', 'default')).toEqual('default')
53+
expect(__get(theme, 'baseStyle.Button.bg._focus', 'default')).toEqual('default')
54+
})
55+
})

0 commit comments

Comments
 (0)