@accessible/use-conditional-focus 中文文档教程

发布于 4年前 浏览 17 项目主页 更新于 3年前


useConditionalFocus()

Bundlephobia 代码覆盖率 构建状态 NPM 版本 MIT 许可证

npm i @accessible/use-conditional-focus

一个将有条件地聚焦元素的 React 钩子。 默认情况下,这将专注于第一个可聚焦的 提供的根元素的子元素,但您也可以选择包含根元素。

Quick Start

```jsx和谐 从“反应”导入*作为反应 import useConditionalFocus

从 '@accessible/use-conditional-focus' const Component = () => { const ref = React.useRef(null) const [visible, setVisible] = React.useState(false) // 聚焦 ref 元素中的第一个可聚焦子元素 // 可见是 true useConditionalFocus(ref, visible)

返回 (

// This button will be focused when visible is true
) } ```

API

useConditionalFocus(target, shouldFocus, options?)

Arguments

Prop Type Default Required? Description
target React.RefObject<T> | T | Window | Document | null Yes A React ref, element, window, or document
shouldFocus boolean false Yes Provide a true value here to focus the first focusable child in the element.
options UseConditionalFocusOptions {includeRoot: false, preventScroll: false} No See UseConditionalFocusOptions.

UseConditonalFocusOptions

Prop Type Default Required? Description
includeRoot boolean false No When true this will try to focus on the root element in addition to its children.
preventScroll boolean false No When true this will prevent your browser from scrolling the document to bring the newly-focused element into view.

Returns void

LICENSE

麻省理工学院


useConditionalFocus()

Bundlephobia Code coverage Build status NPM Version MIT License

npm i @accessible/use-conditional-focus

A React hook that will focus elements conditionally. By default this will focus on the first focusable child of the provided root element, but you can optionally include the root as well.

Quick Start

```jsx harmony import * as React from 'react' import useConditionalFocus from '@accessible/use-conditional-focus'

const Component = () => { const ref = React.useRef(null) const [visible, setVisible] = React.useState(false) // Focuses the first focusable child in the ref element when // visible is true useConditionalFocus(ref, visible)

return (

// This button will be focused when visible is true
) } ```

API

useConditionalFocus(target, shouldFocus, options?)

Arguments

Prop Type Default Required? Description
target React.RefObject<T> | T | Window | Document | null Yes A React ref, element, window, or document
shouldFocus boolean false Yes Provide a true value here to focus the first focusable child in the element.
options UseConditionalFocusOptions {includeRoot: false, preventScroll: false} No See UseConditionalFocusOptions.

UseConditonalFocusOptions

Prop Type Default Required? Description
includeRoot boolean false No When true this will try to focus on the root element in addition to its children.
preventScroll boolean false No When true this will prevent your browser from scrolling the document to bring the newly-focused element into view.

Returns void

LICENSE

MIT

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文