Skip to content

Commit

Permalink
Only add a media query event listener where supported (#1387)
Browse files Browse the repository at this point in the history
* Only add a media query event listener where supported

* Fall back to media.addListener if possible

* Changeset

* Changeset patch -> minor

* Update .changeset/tasty-humans-carry.md

Co-authored-by: Cole Bemis <colebemis@github.com>
  • Loading branch information
jfuchs and colebemis authored Aug 25, 2021
1 parent 8ee008d commit 6b4d52d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-humans-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/components': patch
---

Guard against MediaQueryList.addEventListener calls where unavailable and possibly fall back to .addListener
18 changes: 14 additions & 4 deletions src/ThemeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@ function useSystemColorMode() {
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
media?.addEventListener('change', handleChange)

return function cleanup() {
if (media) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (media.addEventListener !== undefined) {
media.addEventListener('change', handleChange)
return function cleanup() {
media.removeEventListener('change', handleChange)
}
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
media?.removeEventListener('change', handleChange)
else if (media.addListener !== undefined) {
media.addListener(handleChange)
return function cleanup() {
media.removeListener(handleChange)
}
}
}
}, [])

Expand Down

0 comments on commit 6b4d52d

Please sign in to comment.