Skip to content

Commit

Permalink
Merge pull request #868 from kyle-w-20230331/main
Browse files Browse the repository at this point in the history
[fix] matchMedia memory leak
  • Loading branch information
EtienneLem authored Jan 6, 2024
2 parents c57635f + 43abe6b commit 21a2708
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/emoji-mart/src/components/Picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export default class Picker extends Component {

unregister() {
document.removeEventListener('click', this.handleClickOutside)
this.darkMedia?.removeEventListener('change', this.darkMediaCallback)
this.unobserve()
}

Expand Down Expand Up @@ -194,17 +195,19 @@ export default class Picker extends Component {
}
}

darkMediaCallback = () => {
if (this.props.theme != 'auto') return
this.setState({ theme: this.darkMedia.matches ? 'dark' : 'light' })
}

initTheme(theme) {
if (theme != 'auto') return theme

if (!this.darkMedia) {
this.darkMedia = matchMedia('(prefers-color-scheme: dark)')
if (this.darkMedia.media.match(/^not/)) return 'light'

this.darkMedia.addListener(() => {
if (this.props.theme != 'auto') return
this.setState({ theme: this.darkMedia.matches ? 'dark' : 'light' })
})
this.darkMedia.addEventListener('change', this.darkMediaCallback)
}

return this.darkMedia.matches ? 'dark' : 'light'
Expand Down

0 comments on commit 21a2708

Please sign in to comment.