Skip to content

useWindowFocus

Category
Export Size
681 B
Last Changed
3 weeks ago

Reactively track window focus with window.onfocus and window.onblur events.

Demo

💡 Click somewhere outside of the document to unfocus.

Usage

vue
<script setup lang="ts">
import { 
useWindowFocus
} from '@vueuse/core'
const
focused
=
useWindowFocus
()
</script> <template> <
div
>{{
focused
}}</
div
>
</template>

Component Usage

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

vue
<template>
  <UseWindowFocus v-slot="{ 
focused
}">
Document Focus: {{
focused
}}
</UseWindowFocus> </template>

Type Declarations

ts
/**
 * Reactively track window focus with `window.onfocus` and `window.onblur`.
 *
 * @see https://vueuse.org/useWindowFocus
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useWindowFocus
(
options
?:
ConfigurableWindow
,
):
ShallowRef
<boolean>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
vaaski
SerKo
青椒肉丝
Fernando Fernández
Anthony Fu
vaakian X

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.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

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