useFocus

Tags:

一个 React 钩子,允许获取聚焦状态。

演示

源码
Welcome, 🐵
`#el-focus-i` Focused:
true
`#el-focus-b` Focused:
false
`#el-focus-p` Focused:
false

Paragraph #el-focus-p

用法

请查看 API。

源码

点击下方链接跳转 GitHub 查看源代码。

API

const focus = useFocus(elementTarget, options)

元素目标 ElementTarget

ElementTarget 是一个联合类型,代表可以被定位的各种元素。

更多详情,请参见 ElementTargetElementTarget 类型

选项 Options

export type UseFocusOptions = {
  /**
   * 初始聚焦状态
   *
   * @defaultValue false
   */
  initialValue?: boolean
}

返回值

export type UseFocusReturns = readonly [
  /**
   * 聚焦状态
   */
  focused: boolean,
  /**
   * 聚焦动作
   */
  actions: {
    /**
     * 聚焦目标元素
     */
    focus(): void
    /**
     * 失焦目标元素
     */
    blur(): void
  },
]