Skip to content

⬅️ Back to Table of Contents

📄 index.ts

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 9
📊 Variables & Constants 4
🟢 Vue Composition API 2
📐 Interfaces 1

📚 Table of Contents

🛠️ File Location:

📂 packages/core/useFocusWithin/index.ts

📦 Imports

Name Source
ComputedRef vue
ConfigurableWindow ../_configurable
MaybeElementRef ../unrefElement
computed vue
shallowRef vue
defaultWindow ../_configurable
unrefElement ../unrefElement
useActiveElement ../useActiveElement
useEventListener ../useEventListener

Variables & Constants

Name Type Kind Value Exported
EVENT_FOCUS_IN "focusin" const 'focusin'
EVENT_FOCUS_OUT "focusout" const 'focusout'
PSEUDO_CLASS_FOCUS_WITHIN ":focus-within" const ':focus-within'
listenerOptions { passive: boolean; } const { passive: true }

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none
computed computed none none

Functions

useFocusWithin(target: MaybeElementRef, options: ConfigurableWindow): UseFocusWithinReturn

Code
export function useFocusWithin(target: MaybeElementRef, options: ConfigurableWindow = {}): UseFocusWithinReturn {
  const { window = defaultWindow } = options
  const targetElement = computed(() => unrefElement(target))
  const _focused = shallowRef(false)
  const focused = computed(() => _focused.value)
  const activeElement = useActiveElement(options)

  if (!window || !activeElement.value) {
    return { focused }
  }

  const listenerOptions = { passive: true }
  useEventListener(targetElement, EVENT_FOCUS_IN, () => _focused.value = true, listenerOptions)
  useEventListener(targetElement, EVENT_FOCUS_OUT, () =>
    _focused.value = targetElement.value?.matches?.(PSEUDO_CLASS_FOCUS_WITHIN) ?? false, listenerOptions)

  return { focused }
}
  • JSDoc:

    /**
     * Track if focus is contained within the target element
     *
     * @see https://vueuse.org/useFocusWithin
     * @param target The target element to track
     * @param options Focus within options
     */
    

  • Parameters:

  • target: MaybeElementRef
  • options: ConfigurableWindow
  • Return Type: UseFocusWithinReturn
  • Calls:
  • computed (from vue)
  • unrefElement (from ../unrefElement)
  • shallowRef (from vue)
  • useActiveElement (from ../useActiveElement)
  • useEventListener (from ../useEventListener)
  • targetElement.value?.matches

Interfaces

UseFocusWithinReturn

Interface Code
export interface UseFocusWithinReturn {
  /**
   * True if the element or any of its descendants are focused
   */
  focused: ComputedRef<boolean>
}

Properties

Name Type Optional Description
focused ComputedRef<boolean>