@accessible/disclosure

中文文档
3.0.2发布于 2020/08/11MITTypeScriptESM + CJS近 30 天下载 4453

* 反应的可获取和多功能披露部分

中文使用文档
文档目录15 节
  1. 特征
  2. 快速启动
  3. API 密码
  4. 构成部分
  5. 钩子
  6. < 解密>
  7. 使用 A11Y目标( 目标, 选项 )
  8. <目标>
  9. 使用 A11yTrigger( 目标, 选项) 吗
  10. 调试器( T)
  11. 使用 A11y 关闭按钮( 目标, 选项 )
  12. < 关闭按钮>
  13. 使用解析 ()
  14. 披露内容值
  15. 自由主义

< 解密>

Bundlephobia Types Code coverage Build status NPM Version MIT License

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类型默认需要吗?说明
默认打开booleanfalse没有此设置了默认的公开状态 。 默认情况下,该披露是关闭的.
打开booleanundefined没有这就形成了一个控制下披露部分,其中披露的开放状态由该财产控制.
变化(open: boolean) => voidundefined没有此调用在任何时间 open 披露状况发生变化.
编号stringundefined没有默认情况下, 此组件会为您创建一个独特的 id, 因为某些 aria 属性需要它 。 此处提供一个ID可以取代自动ID特性.
儿童React.ReactNodeundefined没有你的披露内容和任何其他孩子.

使用 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。 。 。 .
关闭booleantrue没有默认情况下,当 Escape 键已按下。 你可以通过提供 false 在这里.
关闭分类stringundefined没有当披露时,该类名将适用于儿童要素 closed。 。 。 .
打开分类stringundefined没有当披露时,该类名将适用于儿童要素 open。 。 。 .
关闭样式React.CSSPropertiesundefined没有这些样式将适用于披露时的儿童要素 closed 除设定目标能见度的默认样式外.
打开样式React.CSSPropertiesundefined没有这些样式名称将适用于披露时的儿童元素 open 除设定目标能见度的默认样式外.
防止滚动booleanfalse没有何时 true 这将防止您的浏览器滚动文档,将新聚焦的标签带入视图.
儿童React.ReactElementundefined对儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有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类型默认需要吗?说明
关闭分类stringundefined没有当披露时,该类名将适用于儿童要素 closed。 。 。 .
打开分类stringundefined没有当披露时,该类名将适用于儿童要素 open。 。 。 .
关闭样式React.CSSPropertiesundefined没有这些样式将适用于披露时的儿童要素 closed。 。 。 .
打开样式React.CSSPropertiesundefined没有这些样式名称将适用于披露时的儿童元素 open。 。 。 .
儿童React.ReactElementundefined对儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有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.ReactElementundefined对儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有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>
}

自由主义

麻省理工学院

版本与发布记录

@accessible/disclosure 共发布 17 个版本,首次发布于 2020/01/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2020/08/116 年前
3.0.12020/07/126 年前
3.0.02020/07/126 年前
2.0.22020/07/126 年前
2.0.12020/07/126 年前
2.0.02020/07/126 年前

关于 @accessible/disclosure 的常见问题

@accessible/disclosure 是什么?

* 反应的可获取和多功能披露部分

如何安装 @accessible/disclosure?

在项目目录运行 npm install @accessible/disclosure,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=16.8)、react-dom(>=16.8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@accessible/disclosure 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@accessible/disclosure 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@accessible/disclosure 可以商用吗?

@accessible/disclosure 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@accessible/disclosure 的下载量有多少?

根据 npm 官方下载统计,@accessible/disclosure 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,453 次,日均约 148 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@accessible/disclosure 还在维护吗?

最新版本 3.0.2 发布于 2020/08/11(6 年前);至今共发布 17 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/01/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。