Created
August 4, 2023 23:10
-
-
Save lordzouga/8263b3b2e2a098ee6e62265208313445 to your computer and use it in GitHub Desktop.
Get screen breakpoints with javascript while using tailwind in Nuxt3
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
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