Skip to content

Instantly share code, notes, and snippets.

@lordzouga
Created August 4, 2023 23:10
Show Gist options
  • Save lordzouga/8263b3b2e2a098ee6e62265208313445 to your computer and use it in GitHub Desktop.
Save lordzouga/8263b3b2e2a098ee6e62265208313445 to your computer and use it in GitHub Desktop.
Get screen breakpoints with javascript while using tailwind in Nuxt3
import tailwindConfig from '~/tailwind.config';
import resolveConfig from 'tailwindcss/resolveConfig';
const { theme: { screens } } = resolveConfig(tailwindConfig);
/* returns the breakpoint in form of 'md', 'sm' ..etc */
const getActiveBreakpoint = () => {
/* Sort the breakpoints based on their dimensions in descending order */
const sorted = Object.entries(screens).sort((x, y) => parseInt(y[1]) - parseInt(x[1]));
/* Find the first instance where the current width is higher or equal to a breakpoint */
const bp = sorted.find((s) => window.innerWidth >= parseInt(s[1]));
/* if no breakpoint is found, it is a mobile screen */
if (!bp) return "mb"
else return bp[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment