Skip to content

Commit

Permalink
Fix different signatures of addEventListener and addListener (#155)
Browse files Browse the repository at this point in the history
Co-authored-by: Kostiantyn Ko <[email protected]>
Co-authored-by: Rogin Farrer <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2023
1 parent 263c05d commit 2a098e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-insects-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-collapsed": patch
---

Account for different signatures of addEventListener and addListener, fix #152
14 changes: 11 additions & 3 deletions packages/react-collapsed/src/utils/usePrefersReducedMotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ export function usePrefersReducedMotion() {
}

// Fallback to addListener/removeListener for older browsers, #152
(mediaQueryList.addEventListener ?? mediaQueryList.addListener)('change', listener)
return () => {
(mediaQueryList.removeEventListener ?? mediaQueryList.removeListener)('change', listener)
if (mediaQueryList.addEventListener) {
mediaQueryList.addEventListener('change', listener)
return () => {
mediaQueryList.removeEventListener('change', listener)
}
} else if (mediaQueryList.addListener) {
mediaQueryList.addListener(listener)
return () => {
mediaQueryList.removeListener(listener)
}
}
return undefined
}, [])
return prefersReducedMotion
}

0 comments on commit 2a098e1

Please sign in to comment.