-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useWindowFocus): new function (#601)
* feat: useWindowFocus Reactively track window focus with `window.onfocus` and `window.onblur` events. * feat: useMouse supports drag and drop (#592) - Adds the `dragover` event to continue to track mouse position while dragging. * chore: update deps * fix: types * chore: update Co-authored-by: Marshall Thompson <marshall@creativeideal.net> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
297a265
commit 97328e6
Showing
9 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { defineComponent, reactive } from 'vue-demi' | ||
import { useWindowFocus } from '@vueuse/core' | ||
|
||
export const UseWindowFocus = defineComponent({ | ||
name: 'UseWindowFocus', | ||
setup(props, { slots }) { | ||
const data = reactive({ | ||
focused: useWindowFocus(), | ||
}) | ||
|
||
return () => { | ||
if (slots.default) | ||
return slots.default(data) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script setup lang="ts"> | ||
import { ref, watch } from 'vue-demi' | ||
import { useWindowFocus } from '.' | ||
const startMessage = '💡 Click somewhere outside of the document to unfocus.' | ||
const message = ref(startMessage) | ||
const focused = useWindowFocus() | ||
watch(focused, (current) => { | ||
if (current) | ||
message.value = 'ℹ Tab is unfocused' | ||
else | ||
message.value = startMessage | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div>{{ message }}</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# useWindowFocus | ||
|
||
Reactively track window focus with `window.onfocus` and `window.onblur` events. | ||
|
||
## Usage | ||
|
||
```js | ||
import { useWindowFocus } from '@vueuse/core' | ||
|
||
const focused = useWindowFocus() | ||
``` | ||
|
||
## Component | ||
```html | ||
<UseWindowFocus v-slot="{ focused }"> | ||
Document Focus: {{ focused }} | ||
</UseWindowFocus> | ||
``` | ||
|
||
<!--FOOTER_STARTS--> | ||
## Type Declarations | ||
|
||
```typescript | ||
/** | ||
* Reactively track window focus with `window.onfocus` and `window.onblur`. | ||
* | ||
* @see https://vueuse.org/useWindowFocus | ||
* @param options | ||
*/ | ||
export declare function useWindowFocus({ | ||
window, | ||
}?: ConfigurableWindow): Ref<boolean> | ||
``` | ||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/core/useWindowFocus/index.ts) • [Demo](https://github.com/vueuse/vueuse/blob/main/packages/core/useWindowFocus/demo.vue) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/core/useWindowFocus/index.md) | ||
|
||
|
||
<!--FOOTER_ENDS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Ref, ref } from 'vue-demi' | ||
import { useEventListener } from '../useEventListener' | ||
import { ConfigurableWindow, defaultWindow } from '../_configurable' | ||
|
||
/** | ||
* Reactively track window focus with `window.onfocus` and `window.onblur`. | ||
* | ||
* @see https://vueuse.org/useWindowFocus | ||
* @param options | ||
*/ | ||
export function useWindowFocus({ window = defaultWindow }: ConfigurableWindow = {}): Ref<boolean> { | ||
if (!window) | ||
return ref(false) | ||
|
||
const focused = ref(window.document.hasFocus()) | ||
|
||
useEventListener(window, 'blur', () => { | ||
focused.value = false | ||
}) | ||
|
||
useEventListener(window, 'focus', () => { | ||
focused.value = true | ||
}) | ||
|
||
return focused | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters