Skip to content

Commit

Permalink
feat(ThemeProvider): add dark, light alias for ThemeProvider (#2259)
Browse files Browse the repository at this point in the history
* feat(ThemeProvider): add dark, light alias for ThemeProvider

* Create cyan-singers-invite.md

* test(ThemeProvider): add alias tests for colorMode

* Update .changeset/cyan-singers-invite.md
  • Loading branch information
joshblack committed Aug 24, 2022
1 parent 88b8c0e commit 0383f1a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cyan-singers-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": minor
---

Add support for `'dark'` and `'light'` in `colorMode` for ThemeProvider
4 changes: 3 additions & 1 deletion src/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultNightScheme = 'dark'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Theme = {[key: string]: any}
type ColorMode = 'day' | 'night'
type ColorMode = 'day' | 'night' | 'light' | 'dark'
type ColorModeWithAuto = ColorMode | 'auto'

export type ThemeProviderProps = {
Expand Down Expand Up @@ -202,7 +202,9 @@ function resolveColorMode(colorMode: ColorModeWithAuto, systemColorMode: ColorMo
function chooseColorScheme(colorMode: ColorMode, dayScheme: string, nightScheme: string) {
switch (colorMode) {
case 'day':
case 'light':
return dayScheme
case 'dark':
case 'night':
return nightScheme
}
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/ThemeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ it('respects nightScheme prop', () => {
expect(screen.getByText('Hello')).toHaveStyleRule('color', 'gray')
})

it('respects nightScheme prop with colorMode="dark"', () => {
render(
<ThemeProvider theme={exampleTheme} colorMode="dark" nightScheme="dark_dimmed">
<Text color="text">Hello</Text>
</ThemeProvider>
)

expect(screen.getByText('Hello')).toHaveStyleRule('color', 'gray')
})

it('respects dayScheme prop', () => {
render(
<ThemeProvider theme={exampleTheme} colorMode="day" dayScheme="dark" nightScheme="dark_dimmed">
Expand All @@ -136,6 +146,16 @@ it('respects dayScheme prop', () => {
expect(screen.getByText('Hello')).toHaveStyleRule('color', 'white')
})

it('respects dayScheme prop with colorMode="light"', () => {
render(
<ThemeProvider theme={exampleTheme} colorMode="light" dayScheme="dark" nightScheme="dark_dimmed">
<Text color="text">Hello</Text>
</ThemeProvider>
)

expect(screen.getByText('Hello')).toHaveStyleRule('color', 'white')
})

it('works in auto mode', () => {
render(
<ThemeProvider theme={exampleTheme} colorMode="auto">
Expand Down

0 comments on commit 0383f1a

Please sign in to comment.