Skip to content

Commit

Permalink
ThemeProvider: Fix theme sync with system settings (#2048)
Browse files Browse the repository at this point in the history
* disable theme decorator for themeprovider story

* remove incorrect effect

* Create wicked-boats-allow.md

Co-authored-by: Mike Perrotti <mperrotti@github.com>
  • Loading branch information
siddharthkp and mperrotti committed May 11, 2022
1 parent ab30f14 commit 4e15985
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-boats-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

ThemeProvider: Bug fix, in `colorMode=auto`, the theme now syncs with system changes.
3 changes: 3 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ export const globalTypes = {

// context.globals.X references items in globalTypes
const withThemeProvider = (Story, context) => {
// used for testing ThemeProvider.stories.tsx
if (context.parameters.disableThemeDecorator) return <Story {...context} />

if (context.globals.colorMode === 'all') {
return (
<Wrapper>
Expand Down
4 changes: 0 additions & 4 deletions src/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({children, ...props}
setColorMode(props.colorMode ?? fallbackColorMode ?? defaultColorMode)
}, [props.colorMode, fallbackColorMode])

React.useEffect(() => {
setColorMode(resolvedColorMode)
}, [resolvedColorMode])

React.useEffect(() => {
setDayScheme(props.dayScheme ?? fallbackDayScheme ?? defaultDayScheme)
}, [props.dayScheme, fallbackDayScheme])
Expand Down
24 changes: 20 additions & 4 deletions src/stories/ThemeProvider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {createGlobalStyle} from 'styled-components'
export default {
title: 'Generic behaviors/ThemeProvider',
component: ThemeProvider,
parameters: {disableThemeDecorator: true},
argTypes: {
theme: {
table: {
Expand Down Expand Up @@ -97,8 +98,23 @@ export const Nested: Story<ThemeProviderProps> = args => {
)
}

Nested.args = {
colorMode: 'day',
dayScheme: 'light',
nightScheme: 'dark_dimmed'
const AutoContents = () => {
const {colorMode, resolvedColorMode} = useTheme()

return (
<Box sx={{padding: 10, backgroundColor: 'canvas.inset', color: 'fg.default'}}>
colorMode: {colorMode} <br />
resolvedColorMode: {resolvedColorMode} <br />
</Box>
)
}

export const Auto = () => {
return (
<ThemeProvider colorMode="auto">
<BaseStyles>
<AutoContents />
</BaseStyles>
</ThemeProvider>
)
}

0 comments on commit 4e15985

Please sign in to comment.