Skip to content

Instantly share code, notes, and snippets.

@mouadziani
Forked from sebastiandedeyne/breakpoint.js
Created April 11, 2022 03:05
Show Gist options
  • Save mouadziani/695095c82137f3a0c58a324e1377ee0e to your computer and use it in GitHub Desktop.
Save mouadziani/695095c82137f3a0c58a324e1377ee0e to your computer and use it in GitHub Desktop.
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