Skip to content

Commit 2793ef4

Browse files
authored
Handle invalid color scheme names (#1286)
* Apply default scheme when colorScheme is undefined * Test ThemeProvider default color scheme * Create nervous-eagles-cover.md
1 parent 2f54845 commit 2793ef4

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.changeset/nervous-eagles-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/components": patch
3+
---
4+
5+
`ThemeProvider` now uses the first defined color scheme if passed an invalid color scheme name

src/ThemeProvider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ function applyColorScheme(theme: Theme, colorScheme: string) {
154154
if (!theme.colorSchemes[colorScheme]) {
155155
// eslint-disable-next-line no-console
156156
console.error(`\`${colorScheme}\` scheme not defined in \`theme.colorSchemes\``)
157-
return theme
157+
158+
// Apply the first defined color scheme
159+
const defaultColorScheme = Object.keys(theme.colorSchemes)[0]
160+
return deepmerge(theme, theme.colorSchemes[defaultColorScheme])
158161
}
159162

160163
return deepmerge(theme, theme.colorSchemes[colorScheme])

src/__tests__/ThemeProvider.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ it('defaults to dark color scheme in night mode', () => {
106106
expect(screen.getByText('Hello')).toHaveStyleRule('color', 'white')
107107
})
108108

109+
it('defaults to first color scheme when passed an invalid color scheme name', () => {
110+
render(
111+
<ThemeProvider theme={exampleTheme} dayScheme="foo">
112+
<Text color="text">Hello</Text>
113+
</ThemeProvider>
114+
)
115+
116+
expect(screen.getByText('Hello')).toHaveStyleRule('color', 'black')
117+
})
118+
109119
it('respects nightScheme prop', () => {
110120
render(
111121
<ThemeProvider theme={exampleTheme} colorMode="night" nightScheme="dark_dimmed">

0 commit comments

Comments
 (0)