Skip to content

Commit 4b33f0f

Browse files
committed
sync theme once
1 parent 9bfa1b1 commit 4b33f0f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

.changeset/fresh-hotels-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
Sync theme in effect

src/ThemeProvider.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,20 @@ function useSystemColorMode() {
160160
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
161161
const media = window?.matchMedia?.('(prefers-color-scheme: dark)')
162162

163+
function matchesMediaToColorMode(matches: boolean) {
164+
return matches ? 'night' : 'day'
165+
}
166+
163167
function handleChange(event: MediaQueryListEvent) {
164168
const isNight = event.matches
165-
setSystemColorMode(isNight ? 'night' : 'day')
169+
setSystemColorMode(matchesMediaToColorMode(isNight))
166170
}
167171

168172
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
169173
if (media) {
170174
// just in case the preference changed before the event listener was attached
171-
setSystemColorMode(media.matches ? 'night' : 'day')
175+
const isNight = media.matches
176+
setSystemColorMode(matchesMediaToColorMode(isNight))
172177
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
173178
if (media.addEventListener !== undefined) {
174179
media.addEventListener('change', handleChange)

0 commit comments

Comments
 (0)