Skip to content

Commit 05ea6e7

Browse files
committed
fix(color-mode): fix camelCase properties
1 parent 64f91b5 commit 05ea6e7

File tree

4 files changed

+35
-48
lines changed

4 files changed

+35
-48
lines changed

packages/core/src/colorModes.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function hasColorModes(theme) {
4444
export function createColorStyles(theme, { targetSelector = 'body' } = {}) {
4545
if (!hasColorModes(theme)) return null
4646
const { modes, ...colors } = theme.colors
47-
const styles = toCustomPropertiesDeclarations(
47+
let styles = toCustomPropertiesDeclarations(
4848
colors,
4949
XSTYLED_COLORS_PREFIX,
5050
theme,
@@ -64,17 +64,17 @@ export function createColorStyles(theme, { targetSelector = 'body' } = {}) {
6464
SYSTEM_MODES.forEach(mode => {
6565
if (modes[mode]) {
6666
const mediaQuery = getMediaQuery(getColorModeQuery(mode))
67-
styles[mediaQuery] = getModePropertiesDeclarations(mode)
67+
styles += `${mediaQuery}{${getModePropertiesDeclarations(mode)}}`
6868
}
6969
})
7070
}
7171

7272
Object.keys(modes).forEach(mode => {
7373
const key = `&.${getColorModeClassName(mode)}`
74-
styles[key] = getModePropertiesDeclarations(mode)
74+
styles += `${key}{${getModePropertiesDeclarations(mode)}}`
7575
})
7676

77-
return { [targetSelector]: styles }
77+
return `${targetSelector}{${styles}}`
7878
}
7979

8080
function hasCustomPropertiesEnabled(theme) {

packages/core/src/colorModes.test.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,9 @@ describe('#createColorStyles', () => {
8080
},
8181
},
8282
}
83-
expect(createColorStyles(theme)).toEqual({
84-
body: {
85-
'--xstyled-colors-black': '#000',
86-
'--xstyled-colors-white': '#fff',
87-
'--xstyled-colors-red': '#ff0000',
88-
'--xstyled-colors-danger': '#ff0000',
89-
'--xstyled-colors-text': '#000',
90-
'@media (prefers-color-scheme: dark)': {
91-
'--xstyled-colors-black': '#000',
92-
'--xstyled-colors-white': '#fff',
93-
'--xstyled-colors-red': '#ff4400',
94-
'--xstyled-colors-danger': '#ff4400',
95-
'--xstyled-colors-text': '#fff',
96-
},
97-
'&.xstyled-color-mode-dark': {
98-
'--xstyled-colors-black': '#000',
99-
'--xstyled-colors-white': '#fff',
100-
'--xstyled-colors-red': '#ff4400',
101-
'--xstyled-colors-danger': '#ff4400',
102-
'--xstyled-colors-text': '#fff',
103-
},
104-
},
105-
})
83+
expect(createColorStyles(theme)).toBe(
84+
`body{--xstyled-colors-black: #000;--xstyled-colors-white: #fff;--xstyled-colors-red: #ff0000;--xstyled-colors-danger: #ff0000;--xstyled-colors-text: #000;@media (prefers-color-scheme: dark){--xstyled-colors-black: #000;--xstyled-colors-white: #fff;--xstyled-colors-red: #ff4400;--xstyled-colors-danger: #ff4400;--xstyled-colors-text: #fff;}&.xstyled-color-mode-dark{--xstyled-colors-black: #000;--xstyled-colors-white: #fff;--xstyled-colors-red: #ff4400;--xstyled-colors-danger: #ff4400;--xstyled-colors-text: #fff;}}`,
85+
)
10686
})
10787
})
10888

packages/core/src/customProperties.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function toCustomPropertiesDeclarations(
3636
object,
3737
parent,
3838
theme = object,
39-
state = {},
39+
state = { value: '' },
4040
) {
4141
for (const key in object) {
4242
const value = object[key]
@@ -46,14 +46,14 @@ export function toCustomPropertiesDeclarations(
4646
continue
4747
}
4848
if (string(value)) {
49-
state[toVarName(name)] = value
49+
state.value += `${toVarName(name)}: ${value};`
5050
continue
5151
}
5252
if (func(value)) {
53-
state[toVarName(name)] = value({ theme })
53+
state.value += `${toVarName(name)}: ${value({ theme })};`
5454
continue
5555
}
5656
}
5757

58-
return state
58+
return state.value
5959
}

packages/core/src/customProperties.test.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ describe('#toCustomPropertiesReferences', () => {
3838
})
3939
})
4040

41+
it('handles camel case', () => {
42+
expect(
43+
toCustomPropertiesReferences({
44+
colors: { fooBar: 'x' },
45+
}),
46+
).toEqual({
47+
colors: {
48+
fooBar: 'var(--colors-fooBar, x)',
49+
},
50+
})
51+
})
52+
4153
it('supports prefix', () => {
4254
expect(
4355
toCustomPropertiesReferences(
@@ -54,32 +66,29 @@ describe('#toCustomPropertiesReferences', () => {
5466

5567
describe('#toCustomPropertiesDeclarations', () => {
5668
it('only transforms string', () => {
57-
expect(toCustomPropertiesDeclarations({ foo: 'bar' })).toEqual({
58-
'--foo': 'bar',
59-
})
60-
expect(toCustomPropertiesDeclarations({ foo: 2 })).toEqual({})
61-
expect(toCustomPropertiesDeclarations({ foo: null })).toEqual({})
62-
expect(toCustomPropertiesDeclarations({ foo: false })).toEqual({})
63-
expect(toCustomPropertiesDeclarations({ foo: undefined })).toEqual({})
69+
expect(toCustomPropertiesDeclarations({ foo: 'bar' })).toBe('--foo: bar;')
70+
expect(toCustomPropertiesDeclarations({ foo: 2 })).toBe('')
71+
expect(toCustomPropertiesDeclarations({ foo: null })).toBe('')
72+
expect(toCustomPropertiesDeclarations({ foo: false })).toBe('')
73+
expect(toCustomPropertiesDeclarations({ foo: undefined })).toBe('')
74+
})
75+
76+
it('handles camel case', () => {
77+
expect(toCustomPropertiesDeclarations({ fooBar: 'x' })).toBe('--fooBar: x;')
6478
})
6579

6680
it('supports nesting', () => {
6781
expect(
6882
toCustomPropertiesDeclarations({ colors: { primary: 'blue' } }),
69-
).toEqual({
70-
'--colors-primary': 'blue',
71-
})
83+
).toBe('--colors-primary: blue;')
7284
})
7385

7486
it('supports xstyled func references', () => {
7587
expect(
7688
toCustomPropertiesDeclarations({
7789
colors: { blue: '#0000FF', primary: th.color('blue') },
7890
}),
79-
).toEqual({
80-
'--colors-blue': '#0000FF',
81-
'--colors-primary': '#0000FF',
82-
})
91+
).toBe('--colors-blue: #0000FF;--colors-primary: #0000FF;')
8392
})
8493

8594
it('supports prefix', () => {
@@ -90,8 +99,6 @@ describe('#toCustomPropertiesDeclarations', () => {
9099
},
91100
'xstyled',
92101
),
93-
).toEqual({
94-
'--xstyled-foo': 'bar',
95-
})
102+
).toBe('--xstyled-foo: bar;')
96103
})
97104
})

0 commit comments

Comments
 (0)