Last active
January 12, 2018 14:08
-
-
Save todesstoss/e751d934f051ad5aaab790b0d787cb4c to your computer and use it in GitHub Desktop.
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
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