Slight refactor of your implemetation... to ensure the breakpoint gets calculated on load, if there is no resize.
import { onMounted, reactive } from 'vue' const screens = { xs: 320, sm: 640, md: 768, lg: 1024, xl: 1280 } const breakpoints = reactive({ w: 0, h: 0, is: 'xs' }) const xs = (val: number) => val >= screens.xs && val < screens.sm const sm = (val: number) => val >= screens.sm && val < screens.md const md = (val: number) => val >= screens.md && val < screens.lg const lg = (val: number) => val >= screens.lg && val < screens.xl const xl = (val: number) => val >= screens.xl const getBreakpoint = (w: number) => { if (xs(w)) return 'xs' else if (sm(w)) return 'sm' else if (md(w)) return 'md' else if (lg(w)) return 'lg' else if (xl(w)) return 'xl' else return 'all' } const setBreakpoint = () => { breakpoints.w = window.innerWidth breakpoints.h = window.innerHeight breakpoints.is = getBreakpoint(window.innerWidth) } const useBreakpoint = () => { onMounted(() => { setBreakpoint() window.addEventListener('resize', () => { setBreakpoint() }) }) return { breakpoints } } export default useBreakpoint
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Slight refactor of your implemetation... to ensure the breakpoint gets calculated on load, if there is no resize.