Skip to content

Commit

Permalink
Merge branch 'main' into mp/inactive-actionlist-item
Browse files Browse the repository at this point in the history
  • Loading branch information
mperrotti authored Dec 11, 2023
2 parents e48ddc9 + 21e75dd commit 9ef7b72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-hotels-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Sync theme in effect
9 changes: 8 additions & 1 deletion src/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,20 @@ function useSystemColorMode() {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const media = window?.matchMedia?.('(prefers-color-scheme: dark)')

function matchesMediaToColorMode(matches: boolean) {
return matches ? 'night' : 'day'
}

function handleChange(event: MediaQueryListEvent) {
const isNight = event.matches
setSystemColorMode(isNight ? 'night' : 'day')
setSystemColorMode(matchesMediaToColorMode(isNight))
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (media) {
// just in case the preference changed before the event listener was attached
const isNight = media.matches
setSystemColorMode(matchesMediaToColorMode(isNight))
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (media.addEventListener !== undefined) {
media.addEventListener('change', handleChange)
Expand Down

0 comments on commit 9ef7b72

Please sign in to comment.