Last active
October 4, 2022 19:01
-
-
Save pichfl/8c4e48e30970a64c295c707339480082 to your computer and use it in GitHub Desktop.
Media query helper for Ember.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Helper from '@ember/component/helper'; | |
export default class MatchMedia extends Helper { | |
_mediaQueryList = null; | |
_mediaQueryListener = null; | |
compute([mediaQueryString]) { | |
return this._attachMatchMedia(mediaQueryString); | |
} | |
willDestroy() { | |
this._detachMatchMedia(); | |
} | |
_attachMatchMedia(mediaQueryString) { | |
this._detachMatchMedia(); | |
this._mediaQueryListener = (event) => { | |
this.recompute(event.matches); | |
}; | |
this._mediaQueryList = window.matchMedia(mediaQueryString); | |
this._mediaQueryList.addListener(this._mediaQueryListener); | |
return this._mediaQueryList.matches; | |
} | |
_detachMatchMedia() { | |
if (this._mediaQueryListener) { | |
this._mediaQueryList.removeListener(this._mediaQueryListener); | |
this._mediaQueryList = null; | |
this._mediaQueryListener = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment