Skip to content
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

feat: Default color mode to user's current system theme #3456

Merged
merged 5 commits into from
Oct 31, 2024
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
8 changes: 4 additions & 4 deletions src/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@
--color-code-operator: 219 178, 89;
}

/* Also used for prefers-color-scheme: light */
/* explicitly define it below if we want to change behavior */
body {
@apply lightMode;
}
Expand All @@ -261,7 +263,7 @@
@apply darkMode;
}

/* new body theme can be applied here
/* new body theme can be applied here */

@media (prefers-color-scheme: dark) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is uncommented now

body {
Expand All @@ -271,9 +273,7 @@
body.light {
@apply lightMode;
}

new body theme added above should be applied here as well to cover specificity
} */
}
}

#sentry-feedback {
Expand Down
1 change: 1 addition & 0 deletions src/shared/ThemeContext/ThemeContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Theme context', () => {
function setup() {
window.localStorage.__proto__.setItem = vi.fn()
window.localStorage.__proto__.getItem = vi.fn()
window.matchMedia = vi.fn().mockResolvedValue({ matches: false })

const user = userEvent.setup()

Expand Down
11 changes: 9 additions & 2 deletions src/shared/ThemeContext/ThemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ interface ThemeContextProviderProps {
export const ThemeContextProvider: FC<ThemeContextProviderProps> = ({
children,
}) => {
const currentTheme = (localStorage.getItem('theme') as Theme) || Theme.LIGHT
const [theme, setTheme] = useState<Theme>(currentTheme)
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only logic changes

Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on moving this logic to a <script> in the <head> of the index.html? That should mean the user doesn't have to wait for the JS to be downloaded, and executed to have the correct colour set, when they visit the site 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did consider that approach, though I ended up on preferring to have all the theme logic in one place


let systemTheme = Theme.LIGHT
if (prefersDark) {
systemTheme = Theme.DARK
}

const currentTheme = localStorage.getItem('theme') as Theme
const [theme, setTheme] = useState<Theme>(currentTheme ?? systemTheme)
const initialRender = useRef(true)

if (initialRender.current) {
Expand Down
2 changes: 2 additions & 0 deletions src/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ vi.mock('@sentry/react', async () => {
}
})

window.matchMedia = vi.fn().mockResolvedValue({ matches: false })

beforeAll(() => {
globalThis.jest = {
...globalThis.jest,
Expand Down
Loading