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

feat(color-modes): Warn when theme color keys have leading/trailing whitespace #2099

Merged
merged 1 commit into from Jan 29, 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
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