Skip to content

Instantly share code, notes, and snippets.

@gokhantaskan
Last active January 17, 2023 12:29
Show Gist options
  • Save gokhantaskan/3aab881fa88a47fc45c390d1e0afade1 to your computer and use it in GitHub Desktop.
Save gokhantaskan/3aab881fa88a47fc45c390d1e0afade1 to your computer and use it in GitHub Desktop.
Vue breakpoint composable
import { computed, onMounted, onUnmounted, ref } from 'vue';
export default function useBreakpoints() {
const windowWidth = ref(window.innerWidth);
const onWidthChange = () => (windowWidth.value = window.innerWidth);
onMounted(() => window.addEventListener('resize', onWidthChange));
onUnmounted(() => window.removeEventListener('resize', onWidthChange));
const bp = computed(() => {
if (windowWidth.value < 440) return 'xs';
if (windowWidth.value < 640) return 'sm';
if (windowWidth.value < 748) return 'md';
if (windowWidth.value < 1024) return 'lg';
if (windowWidth.value < 1280) return 'xl';
return '2xl';
});
const width = computed(() => windowWidth.value);
const upToLargeBreakpoint = computed(() =>
['xs', 'sm', 'md', 'lg'].includes(bp.value)
);
const upToMediumBreakpoint = computed(() =>
['xs', 'sm', 'md'].includes(bp.value)
);
const isMobile = computed(() => ['xs', 'sm', 'md', 'lg'].includes(bp.value));
const isDesktop = computed(() => ['xl', '2xl'].includes(bp.value));
const upToSmallBreakpoint = computed(() => ['xs', 'sm'].includes(bp.value));
return {
width,
bp,
isMobile,
isDesktop,
upToLargeBreakpoint,
upToMediumBreakpoint,
upToSmallBreakpoint,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment