-
-
Save minedun6/2981bce3421635de8b015a1e3c6e1a29 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