Skip to content

ThemeProvider: Fix theme sync with system settings #2048

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

Merged
merged 5 commits into from
May 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} />
Copy link
Member Author

@siddharthkp siddharthkp Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to introduce a config to disable the theme decorator so that I can actually test ThemeProvider in an isolated story 😅


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])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split this effect in #1936, now this effect was just syncing state to colorMode 🤔 which is a bug

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand this bug. Everything seems to be working as expected 🤔

Copy link
Member Author

@siddharthkp siddharthkp Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug was that it set colorMode to the same value as resolvedColorMode overwriting the value of colorMode passed to ThemeProvider by the developer...

const resolvedColorMode = resolveColorMode(colorMode, systemColorMode)

so when the colorMode = auto and systemColorMode = dark, resolvedColorMode would be dark it, which is good.

Then this effect came around and set setColorMode(resolvedColorMode) which overwrote colorMode to be dark instead of auto, which means it will no longer respond to changes in systemColorMode

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>
)
}