📄 useFocus¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 1 |
| 📦 Imports | 8 |
| 🟢 Vue Composition API | 3 |
| 📐 Interfaces | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/core/useFocus/index.ts
📦 Imports¶
| Name | Source |
|---|---|
WritableComputedRef |
vue |
ConfigurableWindow |
../_configurable |
MaybeElementRef |
../unrefElement |
computed |
vue |
shallowRef |
vue |
watch |
vue |
unrefElement |
../unrefElement |
useEventListener |
../useEventListener |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
computed |
computed | none | none |
computed |
computed | none | none |
watch |
watch | none | none |
Functions¶
useFocus(target: MaybeElementRef, options: UseFocusOptions): UseFocusReturn¶
Track or set the focus state of a DOM element.
Parameters:
targetany: The target element for the focus and blur events.optionsany: No description
See: https://vueuse.org/useFocus
Raw JSDoc
Calls:
shallowRef (from vue)computed (from vue)unrefElement (from ../unrefElement)useEventListener (from ../useEventListener)(event.target as HTMLElement).matchestargetElement.value?.blurtargetElement.value?.focuswatch (from vue)
Code
export function useFocus(target: MaybeElementRef, options: UseFocusOptions = {}): UseFocusReturn {
const { initialValue = false, focusVisible = false, preventScroll = false } = options
const innerFocused = shallowRef(false)
const targetElement = computed(() => unrefElement(target))
const listenerOptions = { passive: true }
useEventListener(targetElement, 'focus', (event) => {
if (!focusVisible || (event.target as HTMLElement).matches?.(':focus-visible'))
innerFocused.value = true
}, listenerOptions)
useEventListener(targetElement, 'blur', () => innerFocused.value = false, listenerOptions)
const focused = computed({
get: () => innerFocused.value,
set(value: boolean) {
if (!value && innerFocused.value)
targetElement.value?.blur()
else if (value && !innerFocused.value)
targetElement.value?.focus({ preventScroll })
},
})
watch(
targetElement,
() => {
focused.value = initialValue
},
{ immediate: true, flush: 'post' },
)
return { focused }
}
Interfaces¶
UseFocusOptions¶
Interface Code
export interface UseFocusOptions extends ConfigurableWindow {
/**
* Initial value. If set true, then focus will be set on the target
*
* @default false
*/
initialValue?: boolean
/**
* Replicate the :focus-visible behavior of CSS
*
* @default false
*/
focusVisible?: boolean
/**
* Prevent scrolling to the element when it is focused.
*
* @default false
*/
preventScroll?: boolean
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
initialValue |
boolean |
✓ | not shown |
focusVisible |
boolean |
✓ | not shown |
preventScroll |
boolean |
✓ | not shown |
UseFocusReturn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
focused |
WritableComputedRef<boolean> |
✗ | not shown |
Generated by Syntax Scribe