Skip to content
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 numberDiff line numberDiff 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 numberDiff line numberDiff 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} />

@siddharthkpsiddharthkpApr 26, 2022

Copy link
Copy Markdown
MemberAuthor

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 numberDiff line numberDiff 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
Copy Markdown
MemberAuthor

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
Copy Markdown
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 🤔

@siddharthkpsiddharthkpApr 29, 2022

Copy link
Copy Markdown
MemberAuthor

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...

constresolvedColorMode=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 numberDiff line numberDiff 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 DownExpand 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>
)
}