Skip to content

useBreakpoints

Category
Export Size
919 B
Last Changed
3 weeks ago

Reactive viewport breakpoints.

Demo

Current breakpoints: [ "sm", "md", "lg", "xl" ]
Active breakpoint: xl
xs(<640px): false
xs(<=640px): false
sm: false
md: false
lg: false
xl: true
2xl: false
greaterThanBreakPoint: true

Usage

js
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'

const breakpoints = useBreakpoints(breakpointsTailwind)

const smAndLarger = breakpoints.greaterOrEqual('sm') // sm and larger
const largerThanSm = breakpoints.greater('sm') // only larger than sm
const lgAndSmaller = breakpoints.smallerOrEqual('lg') // lg and smaller
const smallerThanLg = breakpoints.smaller('lg') // only smaller than lg
vue
<script setup lang="ts">
import { useBreakpoints } from '@vueuse/core'

const breakpoints = useBreakpoints({
  mobile: 0, // optional
  tablet: 640,
  laptop: 1024,
  desktop: 1280,
})

// Can be 'mobile' or 'tablet' or 'laptop' or 'desktop'
const activeBreakpoint = breakpoints.active()

// true or false
const laptop = breakpoints.between('laptop', 'desktop')
</script>

<template>
  <div :class="activeBreakpoint">
    ...
  </div>
</template>

Server Side Rendering and Nuxt

If 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 mismatch

js
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'

const breakpoints = useBreakpoints(
  breakpointsTailwind,
  { ssrWidth: 768 } // Will enable SSR mode and render like if the screen was 768px wide
)

Alternatively you can set this up globally for your app using provideSSRWidth

Presets

  • Tailwind: breakpointsTailwind
  • Bootstrap v5: breakpointsBootstrapV5
  • Vuetify v2: breakpointsVuetifyV2 (deprecated: breakpointsVuetify)
  • Vuetify v3: breakpointsVuetifyV3
  • Ant Design: breakpointsAntDesign
  • Quasar v2: breakpointsQuasar
  • Sematic: breakpointsSematic
  • Master CSS: breakpointsMasterCss
  • Prime Flex: breakpointsPrimeFlex
  • ElementUI / ElementPlus: breakpointsElement

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:

js
import { breakpointsTailwind } from '@vueuse/core'
// and so on

Type Declarations

Show Type Declarations
typescript
export * from "./breakpoints"
export type Breakpoints<K extends string = string> = Record<
  K,
  MaybeRefOrGetter<number | string>
>
export interface UseBreakpointsOptions extends ConfigurableWindow {
  /**
   * The query strategy to use for the generated shortcut methods like `.lg`
   *
   * 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first)
   * 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first)
   *
   * @default "min-width"
   */
  strategy?: "min-width" | "max-width"
  ssrWidth?: number
}
/**
 * Reactively viewport breakpoints
 *
 * @see https://vueuse.org/useBreakpoints
 */
export declare function useBreakpoints<K extends string>(
  breakpoints: Breakpoints<K>,
  options?: UseBreakpointsOptions,
): Record<K, ComputedRef<boolean>> & {
  greaterOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
  smallerOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
  greater(k: MaybeRefOrGetter<K>): ComputedRef<boolean>
  smaller(k: MaybeRefOrGetter<K>): ComputedRef<boolean>
  between(a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>): ComputedRef<boolean>
  isGreater(k: MaybeRefOrGetter<K>): boolean
  isGreaterOrEqual(k: MaybeRefOrGetter<K>): boolean
  isSmaller(k: MaybeRefOrGetter<K>): boolean
  isSmallerOrEqual(k: MaybeRefOrGetter<K>): boolean
  isInBetween(a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>): boolean
  current: () => ComputedRef<K[]>
  active(): ComputedRef<"" | K>
}
export type UseBreakpointsReturn<K extends string = string> = ReturnType<
  typeof useBreakpoints<K>
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Doctor Wu
IlyaL
Adrien Foulon
AAABingBing
Stefano Bartoletti
Ruslan Makarov
jack-allocate
Ed
Adrian Rudnik
Inesh Bose
Imam Susanto
azaleta
webfansplz
Ege İliklier
Shinigami
Alex Kozack
taidaid
wheat
Azri Kahar

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.1.0 on
55965 - feat(useSSRWidth): add optional support for SSR in useMediaQuery and useBreakpoints (#4317)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.2.0 on
f71e5 - feat: add breakpoints for ElementUI/ElementPlus (#4238)
v10.8.0 on
3ae45 - feat: add active getter, add breakpoints for Vuetify v3 (#3687)
15fc0 - feat: add strategy option to allow desktop-first convention (#3783)
d3317 - feat: enable passing ref or getter to get breakpoints (#3621)
a086e - fix: stricter types
v10.7.0 on
dc624 - feat: make parameters reactivity (#3592)
v10.5.0 on
ec9a4 - fix: add missing breakpoint for the bootstrap (#3413)
v10.4.0 on
8b5ed - feat: add breakpoints for PrimeFlex (#3317)
v10.0.0-beta.3 on
e75a5 - feat: update deps
v10.0.0-beta.2 on
bbabd - feat: getting current breakpoints (#2906)

Released under the MIT License.