📄 useActiveElement¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 3 |
| 📦 Imports | 7 |
| 📐 Interfaces | 1 |
| 📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useActiveElement/index.ts
📦 Imports¶
| Name | Source |
|---|---|
ShallowRef |
vue |
ConfigurableDocumentOrShadowRoot |
../_configurable |
ConfigurableWindow |
../_configurable |
shallowRef |
vue |
defaultWindow |
../_configurable |
onElementRemoval |
../onElementRemoval |
useEventListener |
../useEventListener |
Functions¶
useActiveElement(options: UseActiveElementOptions): UseActiveElementReturn<T>¶
Reactive document.activeElement
Parameters:
optionsany: No description
See: https://vueuse.org/useActiveElement
Tags: @__NO_SIDE_EFFECTS__
Raw JSDoc
Calls:
shallowRef (from vue)getDeepActiveElementuseEventListener (from ../useEventListener)triggeronElementRemoval (from ../onElementRemoval)
Code
export function useActiveElement<T extends HTMLElement>(
options: UseActiveElementOptions = {},
): UseActiveElementReturn<T> {
const {
window = defaultWindow,
deep = true,
triggerOnRemoval = false,
} = options
const document = options.document ?? window?.document
const getDeepActiveElement = () => {
let element = document?.activeElement
if (deep) {
while (element?.shadowRoot)
element = element?.shadowRoot?.activeElement
}
return element
}
const activeElement = shallowRef<T | null | undefined>()
const trigger = () => {
activeElement.value = getDeepActiveElement() as T | null | undefined
}
if (window) {
const listenerOptions = {
capture: true,
passive: true,
}
useEventListener(
window,
'blur',
(event) => {
if (event.relatedTarget !== null)
return
trigger()
},
listenerOptions,
)
useEventListener(
window,
'focus',
trigger,
listenerOptions,
)
}
if (triggerOnRemoval) {
onElementRemoval(activeElement, trigger, { document })
}
trigger()
return activeElement
}
Internal helpers¶
Declared inside another function in this file.
getDeepActiveElement(): Element¶
Returns: Element
Code
trigger(): void¶
Returns: void
Calls:
getDeepActiveElement
Interfaces¶
UseActiveElementOptions¶
Interface Code
export interface UseActiveElementOptions extends ConfigurableWindow, ConfigurableDocumentOrShadowRoot {
/**
* Search active element deeply inside shadow dom
*
* @default true
*/
deep?: boolean
/**
* Track active element when it's removed from the DOM
* Using a MutationObserver under the hood
* @default false
*/
triggerOnRemoval?: boolean
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
deep |
boolean |
✓ | not shown |
triggerOnRemoval |
boolean |
✓ | not shown |
Type Aliases¶
UseActiveElementReturn<T extends HTMLElement = HTMLElement>¶
type UseActiveElementReturn<T extends HTMLElement = HTMLElement> = ShallowRef<T | null | undefined>;
Generated by Syntax Scribe