< 解密>
npm i @accessible/disclosure
反应的无障碍和多功能披露部分
特征
- 样式不可识别 您可以使用您选择的样式库 。 这个
与CSS-in-JS,SASS,平原CSS,平原合作
style物体,任何东西! - 方便门户 当配置时, 披露目标将变成您选择的回放门户 这样做.
- a11y/aria达标 此组件与框外的屏幕阅读器一起工作并管理 专注给你.
快速启动
import * as React from 'react'
import * as Disclosure from '@accessible/disclosure'
const Component = () => (
<Disclosure.Disclosure>
<Disclosure.Trigger>
<button>Open me</button>
</Disclosure.Trigger>
<Disclosure.Target>
<div className='my-disclosure'>
<Disclosure.CloseButton>
<button>Close me</button>
</Disclosure.CloseButton>
<div>I've been disclosed!</div>
</div>
</Disclosure.Target>
</Disclosure.Disclosure>
)
API 密码
构成部分
| 构成部分 | 说明 |
|---|---|
<Disclosure> | 此组件为您的披露目标创建上下文并触发,并包含一些配置选项. |
<Target> | 此组件包装任何反应元素并将其变成披露目标. |
<Trigger> | 此组件将任何反应元素包裹起来并变成一个披露触发器. |
<CloseButton> | 这是一种方便组件,可以包入任何回放元素并添加一个 onClick处理器来关闭披露. |
钩子
| 钩子 | 说明 |
|---|---|
useDisclosure() | 这个钩子提供了披露的价值 披露内容。 。 。 . |
useA11yTarget() | 创建无头披露目标的反向钩 WAI-ARIA 撰写惯例。 。 。 . |
useA11yTrigger() | 创建无头披露触发器的反应钩 WAI-ARIA 撰写惯例。 。 。 . |
useA11yCloseButton() | 创建无头密钥的反向钩 WAI-ARIA 撰写惯例。 。 。 . |
< 解密>
此组件为您的披露目标和触发设定了上下文,并包含一些 配置选项.
道具
| Prop | 类型 | 默认 | 需要吗? | 说明 |
|---|---|---|---|---|
| 默认打开 | boolean | false | 没有 | 此设置了默认的公开状态 。 默认情况下,该披露是关闭的. |
| 打开 | boolean | undefined | 没有 | 这就形成了一个控制下披露部分,其中披露的开放状态由该财产控制. |
| 变化 | (open: boolean) => void | undefined | 没有 | 此调用在任何时间 open 披露状况发生变化. |
| 编号 | string | undefined | 没有 | 默认情况下, 此组件会为您创建一个独特的 id, 因为某些 aria 属性需要它 。 此处提供一个ID可以取代自动ID特性. |
| 儿童 | React.ReactNode | undefined | 没有 | 你的披露内容和任何其他孩子. |
使用 A11Y目标( 目标, 选项 )
创建无头披露目标的反向钩 WAI-ARIA 撰写惯例。 。 。 .
参数
| 参数 | 类型 | 需要吗? | 说明 |
|---|---|---|---|
| 目标 | React.RefObject<T> | T | null | 对 | 反应 ref 或 HTML 元素 |
| 选项 | UseA11yTargetOptions | 没有 | 配置选项 |
使用A11目标选项
export interface UseA11yTargetOptions {
/**
* Adds this class name to props when the disclosure is open
*/
openClass?: string
/**
* Adds this class name to props when the disclosure is closed
*/
closedClass?: string
/**
* Adds this style to props when the disclosure is open
*/
openStyle?: React.CSSProperties
/**
* Adds this style to props when the disclosure is closed
*/
closedStyle?: React.CSSProperties
/**
* Prevents the `window` from scrolling when the target is
* focused after opening.
*/
preventScroll?: boolean
/**
* When `true`, this closes the target element when the `Escape`
* key is pressed.
* @default true
*/
closeOnEscape?: boolean
}
回返
interface A11yProps {
readonly 'aria-hidden': boolean
readonly id: string | undefined
readonly className: string | undefined
readonly style: {
visibility: 'visible' | 'hidden'
} & React.CSSProperties
}
示例
import * as React from 'react'
import {useA11yTarget} from '@accessible/disclosure'
const MyTarget = () => {
const ref = React.useRef(null)
const a11yProps = useA11yTarget(ref, {preventScroll: true})
return (
<div ref={ref} {...a11yProps}>
I am the disclosure content
</div>
)
}
<目标>
此组件包装任何反应元素并将其变成披露目标.
道具
| Prop | 类型 | 默认 | 需要吗? | 说明 |
|---|---|---|---|---|
| 门户 | boolean | string | PortalizeProps | false | 没有 | 何时 true 这将让该信息被披露为带有ID的反应门户 #portals。您可以通过在此提供其查询选择器,例如将其制入任何门户网站. #foobar, (中文(简体) ), [data-portal=true],或 .foobar。 。 。 . |
| 关闭 | boolean | true | 没有 | 默认情况下,当 Escape 键已按下。 你可以通过提供 false 在这里. |
| 关闭分类 | string | undefined | 没有 | 当披露时,该类名将适用于儿童要素 closed。 。 。 . |
| 打开分类 | string | undefined | 没有 | 当披露时,该类名将适用于儿童要素 open。 。 。 . |
| 关闭样式 | React.CSSProperties | undefined | 没有 | 这些样式将适用于披露时的儿童要素 closed 除设定目标能见度的默认样式外. |
| 打开样式 | React.CSSProperties | undefined | 没有 | 这些样式名称将适用于披露时的儿童元素 open 除设定目标能见度的默认样式外. |
| 防止滚动 | boolean | false | 没有 | 何时 true 这将防止您的浏览器滚动文档,将新聚焦的标签带入视图. |
| 儿童 | React.ReactElement | undefined | 对 | 儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有aria属性. |
示例
<Target>
<div className='alert'>Alert</div>
</Target>
// <div
// class="alert"
// aria-hidden="true"
// id="🅰12"
// style="visibility: hidden;"
// >
// Alert
// </div>
使用 A11yTrigger( 目标, 选项) 吗
创建无头披露触发器的反应钩 WAI-ARIA 撰写惯例。 。 。 .
除了为您组件提供无障碍道具之外, 此钩子还会添加事件
用于实际要素与假要素之间的互操作性,例如:. 财务报告和审定财务报表
参数
| 参数 | 类型 | 需要吗? | 说明 |
|---|---|---|---|
| 目标 | React.RefObject<T> | T | null | 对 | 反应 ref 或 HTML 元素 |
| 选项 | UseA11yTriggerOptions | 没有 | 配置选项 |
使用A11yTrigger 选项
export interface UseA11yTriggerOptions {
/**
* Adds this class name to props when the disclosure is open
*/
openClass?: string
/**
* Adds this class name to props when the disclosure is closed
*/
closedClass?: string
/**
* Adds this style to props when the disclosure is open
*/
openStyle?: React.CSSProperties
/**
* Adds this style to props when the disclosure is closed
*/
closedStyle?: React.CSSProperties
/**
* Adds an onClick handler in addition to the default one that
* toggles the disclosure's open state.
*/
onClick?: (e: MouseEvent) => any
}
回返
interface A11yProps {
readonly 'aria-controls': string | undefined
readonly 'aria-expanded': boolean
readonly role: 'button'
readonly tabIndex: 0
readonly className: string | undefined
readonly style: React.CSSProperties | undefined
}
示例
import * as React from 'react'
import {useA11yTrigger} from '@accessible/disclosure'
const MyTrigger = () => {
const ref = React.useRef(null)
const a11yProps = useA11yTrigger(ref, {
openClass: 'open',
closedClass: 'closed',
})
return (
<button ref={ref} {...a11yProps}>
Clicking me toggles the disclosure content
</button>
)
}
调试器( T)
此组件将任何反应元素包起来并添加一个 onClick 切换打开状态的处理器
披露目标.
道具
| Prop | 类型 | 默认 | 需要吗? | 说明 |
|---|---|---|---|---|
| 关闭分类 | string | undefined | 没有 | 当披露时,该类名将适用于儿童要素 closed。 。 。 . |
| 打开分类 | string | undefined | 没有 | 当披露时,该类名将适用于儿童要素 open。 。 。 . |
| 关闭样式 | React.CSSProperties | undefined | 没有 | 这些样式将适用于披露时的儿童要素 closed。 。 。 . |
| 打开样式 | React.CSSProperties | undefined | 没有 | 这些样式名称将适用于披露时的儿童元素 open。 。 。 . |
| 儿童 | React.ReactElement | undefined | 对 | 儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有aria属性. |
<Trigger on='click'>
<button className='my-button'>Open me!</button>
</Trigger>
// <button
// class="my-button"
// aria-controls="🅰12"
// aria-expanded="false"
// >
// Open me!
// </button>
使用 A11y 关闭按钮( 目标, 选项 )
创建无头密钥的反向钩 WAI-ARIA 撰写惯例。 。 。 .
除了为您组件提供无障碍道具之外, 此钩子还会添加事件
用于实际要素与假要素之间的互操作性,例如:. 财务报告和审定财务报表
参数
| 参数 | 类型 | 需要吗? | 说明 |
|---|---|---|---|
| 目标 | React.RefObject<T> | T | null | 对 | 反应 ref 或 HTML 元素 |
| 选项 | UseA11yCloseButtonOptions | 没有 | 配置选项 |
使用 A11y 关闭按钮选项
export interface UseA11yCloseButtonOptions {
/**
* Adds an onClick handler in addition to the default one that
* closes the disclosure.
*/
onClick?: (e: MouseEvent) => any
}
回返
interface A11yProps {
readonly 'aria-controls': string | undefined
readonly 'aria-expanded': boolean
readonly 'aria-label': 'Close'
readonly role: 'button'
readonly tabIndex: 0
}
示例
import * as React from 'react'
import {useA11yCloseButton} from '@accessible/disclosure'
const MyTrigger = () => {
const ref = React.useRef(null)
const a11yProps = useA11yCloseButton(ref, {
onClick: () => console.log('Closing!'),
})
return (
<button ref={ref} {...a11yProps}>
Clicking me closes the disclosure content
</button>
)
}
< 关闭按钮>
这是一个方便组件,将任何反应元素包裹起来,并添加一个Click处理器来关闭披露.
道具
| Prop | 类型 | 默认 | 需要吗? | 说明 |
|---|---|---|---|---|
| 儿童 | React.ReactElement | undefined | 对 | 儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有aria属性. |
<CloseButton>
<button className='my-button'>Close me</button>
</CloseButton>
// <button
// class="my-button"
// aria-controls="disclosure--12"
// aria-expanded="false"
// >
// Close me
// </button>
使用解析 ()
这个钩子提供了披露的价值 披露内容
披露内容值
export interface DisclosureContextValue {
/**
* The open state of the disclosure
*/
isOpen: boolean
/**
* Opens the disclosure
*/
open: () => void
/**
* Closes the disclosure
*/
close: () => void
/**
* Toggles the open state of the disclosure
*/
toggle: () => void
/**
* A unique ID for the disclosure target
*/
id?: string
}
示例
const Component = () => {
const {open, close, toggle, isOpen} = useDisclosure()
return <button onClick={toggle}>Toggle the disclosure</button>
}
自由主义
麻省理工学院