Skip to content

Commit

Permalink
Merge pull request #2090 from system-ui/printmode-fix
Browse files Browse the repository at this point in the history
fix(color-modes): properly use print mode even if its name is `initialColorModeName`
  • Loading branch information
hasparus authored Mar 21, 2022
2 parents baec10c + 154a971 commit f20e2ba
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/color-modes/src/custom-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ export const __createColorStyles = (theme: Theme = {}) => {
const styles = __createColorProperties(colors, modes)

if (printColorModeName) {
const mode =
printColorModeName === 'initial' ||
printColorModeName === initialColorModeName
? colors
: modes[printColorModeName]
styles['@media print'] = __objectToVars('colors', mode)
let printMode = modes[printColorModeName]
if (!printMode && printColorModeName === initialColorModeName)
printMode = colors

if (printMode) {
styles['@media print'] = __objectToVars('colors', printMode)
} else {
console.error(
`Theme UI \`printColorModeName\` was not found in colors scale`,
{ colors, printColorModeName }
)
}
}

const colorToVarValue = (color: string) => toVarValue(`colors-${color}`)

return css({
Expand Down
26 changes: 26 additions & 0 deletions packages/color-modes/test/custom-properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ describe('__createColorStyles', () => {
},
},
})

expect(styles).toEqual({
color: 'var(--theme-ui-colors-text)',
backgroundColor: 'var(--theme-ui-colors-background)',
Expand All @@ -201,6 +202,31 @@ describe('__createColorStyles', () => {
})
})

test('creates styles for print color mode if its name is the same as initialColorModeName', () => {
const styles = __createColorStyles({
config: {
initialColorModeName: 'light',
useColorSchemeMediaQuery: true,
printColorModeName: 'light',
},
colors: {
text: '#fff',
background: '#000',
modes: {
light: {
text: '#000',
background: '#fff',
},
},
},
})

expect(styles['@media print']).toEqual({
'--theme-ui-colors-text': '#000',
'--theme-ui-colors-background': '#fff',
})
})

test('creates styles for initial print color mode', () => {
const styles = __createColorStyles({
config: {
Expand Down

0 comments on commit f20e2ba

Please sign in to comment.