Skip to content

useFocusTrap

Category
Export Size
718 B
Package
@vueuse/integrations
Last Changed
last month

这是 focus-trap 的响应式封装。

有关可以传递的选项的更多信息,请参阅 focus-trap 文档中的 createOptions

Demo

Available in the @vueuse/integrations add-on.

安装

bash
npm i focus-trap@^7

使用

基本用法

vue
<script setup>
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import { ref } from 'vue'

const target = ref()
const { hasFocus, activate, deactivate } = useFocusTrap(target)
</script>

<template>
  <div>
    <button @click="activate()">
      激活
    </button>
    <div ref="target">
      <span>是否有焦点: {{ hasFocus }}</span>
      <input type="text">
      <button @click="deactivate()">
        停用
      </button>
    </div>
  </div>
</template>

多个 Ref 引用

vue
<script setup>
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import { ref } from 'vue'

const targetOne = ref()
const targetTwo = ref()
const { hasFocus, activate, deactivate } = useFocusTrap([targetOne, targetTwo])
</script>

<template>
  <div>
    <button @click="activate()">
      激活
    </button>
    <div ref="targetOne">
      <span>有焦点: {{ hasFocus }}</span>
      <input type="text">
    </div>
    ...
    <div ref="targetTwo">
      <p>另一个目标在这里</p>
      <input type="text">
      <button @click="deactivate()">
        取消激活
      </button>
    </div>
  </div>
</template>

自动聚焦

vue
<script setup>
import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
import { ref } from 'vue'

const target = ref()
const { hasFocus, activate, deactivate } = useFocusTrap(target, { immediate: true })
</script>

<template>
  <div>
    <div ref="target">
      ...
    </div>
  </div>
</template>

条件渲染

这个函数不能正确激活具有使用 v-if 进行条件渲染的元素上的焦点。这是因为它们在焦点激活时尚不存在于 DOM 中。要解决这个问题,您需要在下一个刻度上激活。

vue
<script setup>
import { nextTick, ref } from 'vue'

const target = ref()
const { activate, deactivate } = useFocusTrap(target, { immediate: true })

const show = ref(false)

async function reveal() {
  show.value = true

  await nextTick()
  activate()
}
</script>

<template>
  <div>
    <div v-if="show" ref="target">
      ...
    </div>

    <button @click="reveal">
      显示并聚焦
    </button>
  </div>
</template>

使用组件

通过 UseFocusTrap 组件,在挂载此组件时,焦点陷阱将自动激活,并在卸载时停用。

vue
<script setup>
import { UseFocusTrap } from '@vueuse/integrations/useFocusTrap/component'
import { ref } from 'vue'

const show = ref(false)
</script>

<template>
  <UseFocusTrap v-if="show" :options="{ immediate: true }">
    <div class="modal">
      ...
    </div>
  </UseFocusTrap>
</template>

类型声明

显示类型声明
typescript
export interface UseFocusTrapOptions extends Options {
  /**
   * Immediately activate the trap
   */
  immediate?: boolean
}
export interface UseFocusTrapReturn {
  /**
   * Indicates if the focus trap is currently active
   */
  hasFocus: Ref<boolean>
  /**
   * Indicates if the focus trap is currently paused
   */
  isPaused: Ref<boolean>
  /**
   * Activate the focus trap
   *
   * @see https://github.com/focus-trap/focus-trap#trapactivateactivateoptions
   * @param opts Activate focus trap options
   */
  activate: (opts?: ActivateOptions) => void
  /**
   * Deactivate the focus trap
   *
   * @see https://github.com/focus-trap/focus-trap#trapdeactivatedeactivateoptions
   * @param opts Deactivate focus trap options
   */
  deactivate: (opts?: DeactivateOptions) => void
  /**
   * Pause the focus trap
   *
   * @see https://github.com/focus-trap/focus-trap#trappause
   */
  pause: Fn
  /**
   * Unpauses the focus trap
   *
   * @see https://github.com/focus-trap/focus-trap#trapunpause
   */
  unpause: Fn
}
/**
 * Reactive focus-trap
 *
 * @see https://vueuse.org/useFocusTrap
 */
export declare function useFocusTrap(
  target: Arrayable<MaybeRefOrGetter<string> | MaybeComputedElementRef>,
  options?: UseFocusTrapOptions,
): UseFocusTrapReturn

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
Anthony Fu
Sma11X
Doctorwu
Soviut
vaakian X
azaleta
Agénor Debriat
Curt Grimes
Roman Harmyder
Alex Kozack
Jordy
wheat

变更日志

v11.0.0-beta.2 on 7/17/2024
83c41 - feat: support multiple refs (#4022)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.