Skip to content

Instantly share code, notes, and snippets.

@todesstoss
Last active January 12, 2018 14:08
Show Gist options
  • Save todesstoss/e751d934f051ad5aaab790b0d787cb4c to your computer and use it in GitHub Desktop.
Save todesstoss/e751d934f051ad5aaab790b0d787cb4c to your computer and use it in GitHub Desktop.
class MediaQueryListener {
afterElement = window.getComputedStyle ? window.getComputedStyle(document.body, ':after') : false;
currentBreakpoint = '';
lastBreakpoint = '';
constructor() {
if(!this.afterElement) {
return;
}
this.resizeListener();
}
updateCurrentBreakpoint() {
this.currentBreakpoint = this.afterElement.getPropertyValue('content').replace(/"/g, '');
if (this.currentBreakpoint !== this.lastBreakpoint) {
const event = new CustomEvent(
'breakpoint-change',
{ detail: { currentBreakpoint: this.currentBreakpoint } }
);
window.dispatchEvent(event);
this.lastBreakpoint = this.currentBreakpoint;
}
}
resizeListener() {
window.addEventListener('resize', () => {
this.updateCurrentBreakpoint();
});
window.addEventListener('orientationchange', () => {
this.updateCurrentBreakpoint();
});
window.addEventListener('load', () => {
this.updateCurrentBreakpoint();
});
}
};
export default MediaQueryListener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment