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

fix(color-modes): properly use print mode even if its name is initialColorModeName #2090

Merged
merged 1 commit into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/color-modes/src/custom-properties.ts
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
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: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a theme after mutation. Previous code assumed initial color mode is always at the root.

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