Skip to content

Instantly share code, notes, and snippets.

@bpolaszek
Created March 25, 2022 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpolaszek/7f3a2c2481db7d72a49427df4237b87d to your computer and use it in GitHub Desktop.
Save bpolaszek/7f3a2c2481db7d72a49427df4237b87d to your computer and use it in GitHub Desktop.
Tailwind + Vue3 breakpoint debugger
<template>
<div v-if="show" class="absolute inset-0 h-screen w-screen bg-white bg-opacity-75 text-black z-500 flex items-center overflow-hidden">
<div class="mx-auto text-5xl uppercase">{{ currentBreakpoint }}</div>
</div>
</template>
<script setup>
import { refAutoReset, useMediaQuery } from '@vueuse/core';
import { computed, reactive, watch } from 'vue';
const breakpoints = {
xs: 0,
sm: 640,
md: 768,
lg: 1024,
xl: 1280,
'2xl': 1536,
};
const useCurrentBreakpoint = () => {
const matchingBreakpoints = reactive({});
for (const breakpoint in breakpoints) {
const valueInPixels = breakpoints[breakpoint];
matchingBreakpoints[breakpoint] = useMediaQuery(`(min-width: ${valueInPixels}px)`);
}
return computed(() => {
const entries = Object.entries(matchingBreakpoints).filter(([key, value]) => value);
return Object.keys(Object.fromEntries(entries))[entries.length - 1];
});
};
const show = refAutoReset(false, 200);
const currentBreakpoint = useCurrentBreakpoint();
watch(currentBreakpoint, () => show.value = true);
</script>
@bpolaszek
Copy link
Author

bpolaszek commented Mar 25, 2022

Quickly display the current breakpoint name when resizing the window.

Usage in your main component (parent node or body must have the relative class):

<template>
  <div class="relative">
    <DebugBreakpoint v-if="IS_DEV_MODE" />
    <RouterView />
  </div>
</template>

<script setup>
const IS_DEV_MODE = import?.meta?.env?.DEV || 'development' === process?.env?.NODE_ENV;
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment