Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useBreakpoints.md
1---2category: Browser3---45# useBreakpoints67Reactive viewport breakpoints.89## Usage1011```ts12import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'1314const breakpoints = useBreakpoints(breakpointsTailwind)1516const smAndLarger = breakpoints.greaterOrEqual('sm') // sm and larger17const largerThanSm = breakpoints.greater('sm') // only larger than sm18const lgAndSmaller = breakpoints.smallerOrEqual('lg') // lg and smaller19const smallerThanLg = breakpoints.smaller('lg') // only smaller than lg20```2122```vue23<script setup lang="ts">24import { useBreakpoints } from '@vueuse/core'2526const breakpoints = useBreakpoints({27mobile: 0, // optional28tablet: 640,29laptop: 1024,30desktop: 1280,31})3233// Can be 'mobile' or 'tablet' or 'laptop' or 'desktop'34const activeBreakpoint = breakpoints.active()3536// true or false37const laptop = breakpoints.between('laptop', 'desktop')38</script>3940<template>41<div :class="activeBreakpoint">42...43</div>44</template>45```4647### Shortcut Methods4849You can access breakpoints directly as properties on the returned object. These return reactive refs.5051```ts52const breakpoints = useBreakpoints({53tablet: 640,54laptop: 1024,55})5657// Equivalent to breakpoints.greaterOrEqual('tablet') with min-width strategy58const isTablet = breakpoints.tablet59```6061### Strategy6263The `strategy` option controls how the shortcut properties behave:6465- `min-width` (default, mobile-first): `breakpoints.lg` is `true` when viewport is `>= lg`66- `max-width` (desktop-first): `breakpoints.lg` is `true` when viewport is `< xl`6768```ts69const breakpoints = useBreakpoints(breakpointsTailwind, {70strategy: 'max-width', // desktop-first71})72```7374### Available Methods7576| Method | Description |77| --------------------- | -------------------------------------------------------- |78| `greaterOrEqual(k)` | Reactive: viewport width >= breakpoint |79| `greater(k)` | Reactive: viewport width > breakpoint |80| `smallerOrEqual(k)` | Reactive: viewport width <= breakpoint |81| `smaller(k)` | Reactive: viewport width < breakpoint |82| `between(a, b)` | Reactive: viewport width between a and b |83| `isGreaterOrEqual(k)` | Non-reactive: returns boolean immediately |84| `isGreater(k)` | Non-reactive: returns boolean immediately |85| `isSmallerOrEqual(k)` | Non-reactive: returns boolean immediately |86| `isSmaller(k)` | Non-reactive: returns boolean immediately |87| `isInBetween(a, b)` | Non-reactive: returns boolean immediately |88| `current()` | Returns computed array of all matching breakpoints |89| `active()` | Returns computed string of the current active breakpoint |9091#### Server Side Rendering and Nuxt9293If you are using `useBreakpoints` with SSR enabled, then you need to specify which screen size you would like to render on the server and before hydration to avoid an hydration mismatch9495```ts96import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'9798const breakpoints = useBreakpoints(breakpointsTailwind, {99ssrWidth: 768 // Will enable SSR mode and render like if the screen was 768px wide100})101```102103Alternatively you can set this up globally for your app using [`provideSSRWidth`](../useSSRWidth/index.md)104105## Presets106107- Tailwind: `breakpointsTailwind`108- Bootstrap v5: `breakpointsBootstrapV5`109- Vuetify v2: `breakpointsVuetifyV2` (deprecated: `breakpointsVuetify`)110- Vuetify v3: `breakpointsVuetifyV3`111- Ant Design: `breakpointsAntDesign`112- Quasar v2: `breakpointsQuasar`113- Sematic: `breakpointsSematic`114- Master CSS: `breakpointsMasterCss`115- Prime Flex: `breakpointsPrimeFlex`116- ElementUI / ElementPlus: `breakpointsElement`117118_Breakpoint presets are deliberately not auto-imported, as they do not start with `use` to have the scope of VueUse. They have to be explicitly imported:_119120```js121import { breakpointsTailwind } from '@vueuse/core'122// and so on123```124125## Type Declarations126127```ts128export * from "./breakpoints"129export type Breakpoints<K extends string = string> = Record<130K,131MaybeRefOrGetter<number | string>132>133export interface UseBreakpointsOptions extends ConfigurableWindow {134/**135* The query strategy to use for the generated shortcut methods like `.lg`136*137* 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first)138* 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first)139*140* @default "min-width"141*/142strategy?: "min-width" | "max-width"143ssrWidth?: number144}145export type UseBreakpointReturn<K extends string = string> = Record<146K,147ComputedRef<boolean>148> & {149greaterOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>150smallerOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>151greater: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>152smaller: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>153between: (154a: MaybeRefOrGetter<K>,155b: MaybeRefOrGetter<K>,156) => ComputedRef<boolean>157isGreater: (k: MaybeRefOrGetter<K>) => boolean158isGreaterOrEqual: (k: MaybeRefOrGetter<K>) => boolean159isSmaller: (k: MaybeRefOrGetter<K>) => boolean160isSmallerOrEqual: (k: MaybeRefOrGetter<K>) => boolean161isInBetween: (a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>) => boolean162current: () => ComputedRef<K[]>163active: () => ComputedRef<K | "">164}165/**166* Reactively viewport breakpoints167*168* @see https://vueuse.org/useBreakpoints169*170* @__NO_SIDE_EFFECTS__171*/172export declare function useBreakpoints<K extends string>(173breakpoints: Breakpoints<K>,174options?: UseBreakpointsOptions,175): UseBreakpointReturn<K>176```177