Browser · @vueuse/core v15.0.0 · Reviewed

useWindowSize

Keep the viewport width and height reactive when the browser window is resized.

App.vueVue SFCOpen in Vue Playground ↗
Live preview

Edit the code and the preview updates as you type.

What it does

A preview or canvas needs current viewport dimensions for behavior that CSS media queries cannot express.

The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.

The code

Vue · Composition API
const { width, height } = useWindowSize()
// Use CSS for visual breakpoints; use these refs for JS behavior.

Where you'd use it

  • Sizing a canvas to the viewport
  • Calculating available space for a viewer
  • Adapting behavior at a custom threshold

In Nuxt (and other gotchas)

There is no window on the server, so the initial size uses a fallback until hydration. Prefer CSS for responsive layout to avoid server/client markup differences; use the refs for client-side behavior.

When not to use it

Do not replace CSS media queries with JavaScript just to hide or restyle ordinary layout elements. Avoid expensive work on each resize; throttle calculations if necessary.

Links

Need a hand with your Vue or Nuxt app?

Related functions