Skip to content

Commit

Permalink
[fix] matchMedia memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-go committed Sep 26, 2023
1 parent d29728f commit 5c8e4c2
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 @@ -106,6 +106,7 @@ export default class Picker extends Component {

componentWillUnmount() {
this.unregister()
this.darkMedia?.removeEventListener(this.darkMediaCallback);
}

async reset(nextState = {}) {
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.addListener(this.darkMediaCallback);
}

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

0 comments on commit 5c8e4c2

Please sign in to comment.