Created
January 3, 2022 13:46
-
-
Save sebastiandedeyne/c70b267af7037879be9429042689a5da to your computer and use it in GitHub Desktop.
This file contains hidden or 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
document.addEventListener('alpine:init', () => { | |
Alpine.store('breakpoint', { | |
listeners: {}, | |
init() { | |
const breakpoints = { | |
sm: 640, | |
md: 768, | |
lg: 1024, | |
xl: 1280, | |
'2xl': 1535, | |
}; | |
Object.entries(breakpoints).forEach(([breakpoint, width]) => { | |
const mediaQuery = window.matchMedia(`(min-width: ${width}px)`); | |
this[breakpoint] = mediaQuery.matches; | |
this.listeners[breakpoint] = []; | |
mediaQuery.addEventListener('change', (event) => { | |
this[breakpoint] = event.matches; | |
this.listeners[breakpoint].forEach((listener) => { | |
listener(event.matches); | |
}); | |
}); | |
}); | |
}, | |
listen(breakpoint, callback) { | |
this.listeners[breakpoint].push(callback); | |
}, | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment