Skip to content

useWindowSize

Category
Export Size
1.49 kB
Last Changed
3 weeks ago

Reactive window size

Demo

Usage

vue
<script setup lang="ts">
import { 
useWindowSize
} from '@vueuse/core'
const {
width
,
height
} =
useWindowSize
()
</script> <template> <
div
>
Width: {{
width
}}
Height: {{
height
}}
</
div
>
</template>

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseWindowSize v-slot="{ 
width
,
height
}">
Width: {{
width
}}
Height: {{
height
}}
</UseWindowSize> </template>

Type Declarations

Show Type Declarations
ts
export interface UseWindowSizeOptions extends ConfigurableWindow {
  
initialWidth
?: number
initialHeight
?: number
/** * Listen to window `orientationchange` event * * @default true */
listenOrientation
?: boolean
/** * Whether the scrollbar should be included in the width and height * Only effective when `type` is `'inner'` * * @default true */
includeScrollbar
?: boolean
/** * Use `window.innerWidth` or `window.outerWidth` or `window.visualViewport` * visualViewport documentation from MDN(https://developer.mozilla.org/zh-CN/docs/Web/API/VisualViewport) * @default 'inner' */
type
?: "inner" | "outer" | "visual"
} /** * Reactive window size. * * @see https://vueuse.org/useWindowSize * @param options * * @__NO_SIDE_EFFECTS__ */ export declare function
useWindowSize
(
options
?: UseWindowSizeOptions): {
width
:
ShallowRef
<number, number>
height
:
ShallowRef
<number, number>
} export type
UseWindowSizeReturn
=
ReturnType
<typeof
useWindowSize
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Jelf
Antério Vieira
SerKo
青椒肉丝
IlyaL
Robin
Fernando Fernández
huiliangShen
Al-Khawarizmi
Indrek Ardel
Zhousg
vaakian X
vaakian X
Shinigami
wheat
Alex Kozack
Roman Pavlov

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.1.0 on
e78ef - feat: add type: visual option that leverage visualViewport (#4395)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.2 on
bfba3 - feat: add ability to get outer sizes of the window (#3999)
v10.0.0-beta.3 on
4e7b0 - fix!: use media query instead of orientationchange listener (#2919)

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August