@accessible/drawer

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

* * 用于反应的无障碍多功能抽屉组件

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

< 拖放器>

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i @accessible/drawer

用于反应的无障碍多功能抽屉组件

特征

  • 样式不可识别 您可以使用您选择的样式库 。 这个 与CSS-in-JS,SASS,平原CSS,平原合作 style 物体,任何东西!
  • 方便门户 抽屉目标在配置时会渲染成您选择的 React ports 这样做.
  • a11y/aria达标 此组件与框外的屏幕阅读器一起工作并管理 专注给你.

快速启动

检查一下这个例子 代码框

import * as React from 'react'
import * as Drawer from '@accessible/drawer'

const Component = () => (
  <Drawer.Drawer>
    <Drawer.Trigger>
      <button>Open me</button>
    </Drawer.Trigger>

    <Drawer.Target>
      <div className='my-drawer'>
        <Drawer.CloseButton>
          <button>Close me</button>
        </Drawer.CloseButton>

        <div>I've been revealed!</div>
      </div>
    </Drawer.Target>
  </Drawer.Drawer>
)

API 密码

构成部分

构成部分说明
<Drawer>此组件为您的抽屉目标创建上下文并触发并包含一些配置选项 .
<Target>此组件包入任何反应元素并将其变成抽屉目标 .
<Trigger>此组件将任意反应元素包裹起来, 并将其变成抽屉触发器 .
<CloseButton>这是一个方便组件,可以包入任何回放元素,并添加一个 onClick处理器来关闭抽屉.

钩子

钩子说明
useDrawer()此钩提供抽屉的值 拖取器串联 Vale 对象。 。 。 .
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 {
  /**
   * Sets the placement of the drawer menu
   * @default "left"
   */
  placement?: 'top' | 'right' | 'bottom' | 'left'
  /**
   * Adds this class name to props when the drawer is open
   */
  openClass?: string
  /**
   * Adds this class name to props when the drawer is closed
   */
  closedClass?: string
  /**
   * Adds this style to props when the drawer is open
   */
  openStyle?: React.CSSProperties
  /**
   * Adds this style to props when the drawer 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
}

回返

type A11yProps = {
  readonly 'aria-hidden': boolean
  readonly id: string | undefined
  readonly className: string | undefined
  readonly style: {
    readonly visibility: 'hidden' | 'visible'
  } & React.CSSProperties
} & {
  readonly style:
    | ({
        readonly visibility: 'hidden' | 'visible'
      } & React.CSSProperties & {
          readonly position: 'fixed'
          readonly top: 0
          readonly right: 0
          readonly bottom: 'auto'
          readonly left: 0
          readonly transform: 'translate3d(0, -100%, 0)'
        })
    | ({
        readonly visibility: 'hidden' | 'visible'
      } & React.CSSProperties & {
          readonly position: 'fixed'
          readonly top: 0
          readonly right: 0
          readonly bottom: 0
          readonly left: 'auto'
          readonly transform: 'translate3d(100%, 0, 0)'
        })
    | ({
        readonly visibility: 'hidden' | 'visible'
      } & React.CSSProperties & {
          readonly position: 'fixed'
          readonly top: 'auto'
          readonly right: 0
          readonly bottom: 0
          readonly left: 0
          readonly transform: 'translate3d(0, 100%, 0)'
        })
    | ({
        readonly visibility: 'hidden' | 'visible'
      } & React.CSSProperties & {
          readonly position: 'fixed'
          readonly top: 0
          readonly right: 'auto'
          readonly bottom: 0
          readonly left: 0
          readonly transform: 'translate3d(-100%, 0, 0)'
        })
}

示例

import * as React from 'react'
import {useA11yTarget} from '@accessible/drawer'

const MyTarget = () => {
  const ref = React.useRef(null)
  const a11yProps = useA11yTarget(ref, {preventScroll: true})

  return (
    <div ref={ref} {...a11yProps}>
      I am the drawer content
    </div>
  )
}

<目标>

此组件包入任何反应元素并将其变成抽屉目标 .

道具

Prop类型默认需要吗?说明
安置情况"top" | "right" | "bottom" | "left"false没有何时 true 这将让抽屉变成一个带有ID的反应门户 #portals。您可以通过在此提供其查询选择器,例如将其制入任何门户网站. #foobar, (中文(简体) ), [data-portal=true],或 .foobar。 。 。 .
门户boolean | string | PortalizePropsfalse没有何时 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 drawer is open
   */
  openClass?: string
  /**
   * Adds this class name to props when the drawer is closed
   */
  closedClass?: string
  /**
   * Adds this style to props when the drawer is open
   */
  openStyle?: React.CSSProperties
  /**
   * Adds this style to props when the drawer is closed
   */
  closedStyle?: React.CSSProperties
  /**
   * Adds an onClick handler in addition to the default one that
   * toggles the drawer's open state.
   */
  onClick?: (e: MouseEvent) => any
}

回返

interface A11yProps<E extends React.MouseEvent<any, MouseEvent>> {
  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/drawer'

const MyTrigger = () => {
  const ref = React.useRef(null)
  const a11yProps = useA11yTrigger(ref, {
    openClass: 'open',
    closedClass: 'closed',
  })

  return (
    <button ref={ref} {...a11yProps}>
      Clicking me toggles the drawer 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 drawer.
   */
  onClick?: (e: MouseEvent) => any
}

回返

interface A11yProps<E extends React.MouseEvent<any, MouseEvent>> {
  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/drawer'

const MyTrigger = () => {
  const ref = React.useRef(null)
  const a11yProps = useA11yCloseButton(ref, {
    onClick: () => console.log('Closing!'),
  })

  return (
    <button ref={ref} {...a11yProps}>
      Clicking me closes the drawer content
    </button>
  )
}

< 关闭按钮>

这是一个方便组件,它包住任何反应元素,并添加一个Click处理器,关闭抽屉.

道具

Prop类型默认需要吗?说明
儿童React.ReactElementundefined对儿童被这一组成部分所克隆,其道具中以及上面所定义的事件中带有aria属性.
<CloseButton>
  <button className='my-button'>Close me</button>
</CloseButton>

// <button
//   class="my-button"
//   aria-controls="drawer--12"
//   aria-expanded="false"
// >
//   Close me
// </button>

使用 Drawer () ()

此钩提供抽屉的值 拖取器串联 Vale 对象

绘图格式

export interface DrawerContextValue {
  /**
   * The open state of the drawer
   */
  isOpen: boolean
  /**
   * Opens the drawer
   */
  open: () => void
  /**
   * Closes the drawer
   */
  close: () => void
  /**
   * Toggles the open state of the drawer
   */
  toggle: () => void
  /**
   * A unique ID for the drawer target
   */
  id?: string
}

示例

const Component = () => {
  const {open, close, toggle, isOpen} = useDrawer()
  return <button onClick={toggle}>Toggle the drawer</button>
}

自由主义

麻省理工学院

版本与发布记录

@accessible/drawer 共发布 19 个版本,首次发布于 2019/12/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2020/08/116 年前
3.0.12020/08/116 年前
3.0.02020/07/206 年前
2.2.12020/05/306 年前
2.2.02020/05/306 年前
2.1.42020/01/176 年前

关于 @accessible/drawer 的常见问题

@accessible/drawer 是什么?

* * 用于反应的无障碍多功能抽屉组件

如何安装 @accessible/drawer?

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

@accessible/drawer 支持 TypeScript 吗?

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

@accessible/drawer 支持 ESM 还是 CommonJS?

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

@accessible/drawer 可以商用吗?

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

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

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

@accessible/drawer 还在维护吗?

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