Skip to content

Commit

Permalink
Merge pull request #2099 from system-ui/lachlanjc-warn-colorspace
Browse files Browse the repository at this point in the history
  • Loading branch information
hasparus committed Jan 29, 2022
2 parents 9b6ef87 + dc64d2a commit 9c69777
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/color-modes/src/index.tsx
Expand Up @@ -164,6 +164,24 @@ const TopLevelColorModeProvider = ({
' and cannot reference a key in `theme.colors.modes`.'
)
}
const allColorKeys: Array<string> = []
const flattenKeys = (obj: Record<string, any>) => {
Object.keys(obj).forEach((key) => {
allColorKeys.push(key)
if (typeof obj[key] === 'object') {
flattenKeys(obj[key])
}
})
return allColorKeys
}
flattenKeys(outerTheme.colors ?? {}).forEach((color) => {
if (color !== color.trim()) {
console.warn(
`[theme-ui] Key \`${color}\` in theme.colors contains leading/trailing ` +
'whitespace, which can cause bugs in your project.'
)
}
})
}

const newTheme = useThemeWithAppliedColorMode({ colorMode, outerTheme })
Expand Down
26 changes: 25 additions & 1 deletion packages/color-modes/test/index.tsx
Expand Up @@ -541,6 +541,30 @@ test('warns when initialColorModeName matches a key in theme.colors.modes', () =
restore()
})

test('warns when a key in theme.colors.modes has leading/trailing whitespace', () => {
const restore = mockConsole()
render(
<ThemeProvider
theme={{
colors: {
text: '#000',
background: '#fff',
modes: {
dark: {
' text ': '#fff',
background: '#000',
},
},
},
}}
>
<ColorModeProvider />
</ThemeProvider>
)
expect(console.warn).toBeCalled()
restore()
})

test('does not warn in production', () => {
const restore = mockConsole()
const init = process.env.NODE_ENV
Expand All @@ -557,7 +581,7 @@ test('does not warn in production', () => {
modes: {
dark: {
text: '#fff',
background: '#000',
' background': '#000',
},
},
},
Expand Down

0 comments on commit 9c69777

Please sign in to comment.