项目作者: accessible-ui

项目描述 :
🅰 An accessible and versatile accordion for React with keyboard navigation and labeling features taught in w3.org's WAI-ARIA accordion best practices example
高级语言: TypeScript
项目地址: git://github.com/accessible-ui/accordion.git
创建时间: 2019-12-26T22:11:40Z
项目社区:https://github.com/accessible-ui/accordion

开源协议:MIT License

下载









Bundlephobia


Types


Code coverage


Build status


NPM Version


MIT License

  1. npm i @accessible/accordion


An accessible and versatile accordion for React with keyboard navigation and labeling features taught in
w3.org’s WAI-ARIA accordion best practices example.

Features

  • Style-agnostic You can use this component with the styling library of your choice. It
    works with CSS-in-JS, SASS, plain CSS, plain style objects, anything!
  • a11y/aria-compliant This component works with screen readers out of the box and manages
    focus for you.

Quick Start

Check out the example on CodeSandbox

```jsx harmony
import {Accordion, Section, Trigger, Panel} from ‘@accessible/accordion’

const Component = () => (








Section 1 content


  1. <Section>
  2. <h3>
  3. <Trigger>
  4. <button>Section 2</button>
  5. </Trigger>
  6. </h3>
  7. <Panel>
  8. <div className="panel">Section 2 content</div>
  9. </Panel>
  10. </Section>


)

  1. ## API
  2. ### Components
  3. | Component | Description |
  4. | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  5. | [`<Accordion>`](#accordion) | This component creates the context for your accordion section and contains some configuration options. |
  6. | [`<Section>`](#section) | This component creates the context for the accordion panel and trigger contained in this section. It must be a direct descendent of [`<Accordion>`](#accordion). |
  7. | [`<Trigger>`](#trigger) | This component clones any React element and turns it into a accordion trigger that controls the visible state of the panel. |
  8. | [`<Panel>`](#panel) | This component clones any React element and turns it into a accordion panel. |
  9. | [`<Close>`](#close) | This is a convenience component that clones any React element and adds an onClick handler to close its parent panel. | |
  10. ### Hooks
  11. | Hook | Description |
  12. | --------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
  13. | [`useAccordion()`](#useaccordion) | This hook returns the value of the accordion's [AccordionContext object](#accordioncontextvalue). |
  14. | [`useSection()`](#usesection) | This hook returns the value of the accordion [`<Section>`'s](#section) [SectionContext object](#sectioncontextvalue). |
  15. | [`useControls()`](#usecontrols) | This hook returns the accordion [`<Section>`'s](#section) `open`, `close`, and `toggle` functions. |
  16. | [`useDisabled()`](#usedisabled) | This hook returns the accordion [`<Section>`'s](#section) `disabled` value. |
  17. | [`useIsOpen()`](#useisopen) | This hook returns the accordion [`<Section>`'s](#section) `isOpen` value. |
  18. ### `<Accordion>`
  19. This component creates the context for your accordion section and contains some configuration options.
  20. [`<Section>`s](#section) are the only type of children allowed.
  21. #### Props
  22. | Prop | Type | Default | Required? | Description |
  23. | ----------------- | ----------------------------------------------------- | ----------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  24. | defaultOpen | <code>number | number[]</code> | `undefined` | No | The section by index (or sections if `allowMultipleOpen`) you want opened by default. |
  25. | open | <code>number | number[]</code> | `undefined` | No | Makes this a controlled component where `open`, `close`, `toggle`, controls have no effect. The sections defined here are always the ones that are open. |
  26. | allowMultipleOpen | `boolean` | `false` | No | Allows multiple sections to be opened at one time. |
  27. | allowAllClosed | `boolean` | `false` | No | Allows all of the sections to be closed. If `false`, you must define either the `open` or `defaultOpen` property. |
  28. | onChange | <code>(opened: number | number[]) => void</code> | `undefined` | No | Called each time the open sections change. If `allowMultipleOpen`, the argument will be an array, otherwise a single number. The number corresponds to the open section's index. |
  29. | children | `React.ReactElement<SectionProps>[]` | `undefined` | Yes | The only children allowed by this component are [`<Section>`s](#section). |
  30. ### `<Section>`
  31. This component creates the context for the accordion panel and trigger contained in this section. It must be a direct
  32. descendent of [`<Accordion>`](#accordion).
  33. #### Props
  34. | Prop | Type | Default | Required? | Description |
  35. | -------- | --------------------------------------------------------------------------------------- | ----------- | --------- | ---------------------------------------------------------------------------------------------------------------- |
  36. | id | `string` | `undefined` | No | Overrides the ID that is auto-generated by this component. |
  37. | disabled | `boolean` | `false` | No | `true` if the section should not be allowed to have its `open` state changed. |
  38. | children | <code>React.ReactNode | ((context: SectionContextValue) => React.ReactNode)</code> | `undefined` | Yes | Sections must include a [`<Trigger>`](#trigger) and a [`Panel`](#panel) in addition to anything else you'd like. |
  39. ### `<Trigger>`
  40. This component clones any React element and turns it into a accordion trigger that controls the visible state of
  41. the [`<Panel>`](#panel). It must be a child of [`<Section>`](#section).
  42. #### Props
  43. | Prop | Type | Default | Required? | Description |
  44. | ----------- | --------------------- | ----------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  45. | openClass | `string` | `undefined` | No | This class name will be applied to the child element when the section is `open`. |
  46. | closedClass | `string` | `undefined` | No | This class name will be applied to the child element when the section is `closed`. |
  47. | openStyle | `React.CSSProperties` | `undefined` | No | These styles will be applied to the child element when the section is `open`. |
  48. | closedStyle | `React.CSSProperties` | `undefined` | No | These styles will be applied to the child element when the section is `closed`. |
  49. | children | `React.ReactElement` | `undefined` | Yes | The child is cloned by this component and has aria attributes injected into its props as well as keyboard events for opening the section with `space` and `enter` keys and navigating between sections. |
  50. ### `<Panel>`
  51. This component clones any React element and turns it into a accordion section panel. It must be
  52. a child of [`<Section>`](#section).
  53. #### Props
  54. | Prop | Type | Default | Required? | Description |
  55. | ----------- | --------------------- | ----------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  56. | openClass | `string` | `undefined` | No | This class name will be applied to the child element when the section is `open`. |
  57. | closedClass | `string` | `undefined` | No | This class name will be applied to the child element when the section is `closed`. |
  58. | openStyle | `React.CSSProperties` | `undefined` | No | These styles will be applied to the child element when the section is `open`. |
  59. | closedStyle | `React.CSSProperties` | `undefined` | No | These styles will be applied to the child element when the section is `closed`. |
  60. | children | `React.ReactElement` | `undefined` | Yes | The child is cloned by this component and has aria attributes injected into its props as well as keyboard events for closing the section with the `escape` key. |
  61. ### `<Close>`
  62. This is a convenience component that clones any React element and adds an onClick handler to close its parent panel. It
  63. must be a child of [`<Section>`](#section).
  64. #### Props
  65. | Prop | Type | Default | Required? | Description |
  66. | -------- | -------------------- | ----------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
  67. | children | `React.ReactElement` | `undefined` | Yes | The child is cloned by this component and has aria attributes injected into its props as keyboard events to ensure it acts like a button even if it isn't a native `<button>`. |
  68. ### `useAccordion()`
  69. This hook returns the value of the accordion's [AccordionContext object](#accordioncontextvalue). This hook
  70. must be within a child of [`<Accordion>`](#accordion).
  71. ### `AccordionContextValue`
  72. ```typescript jsx
  73. interface AccordionContextValue {
  74. // DOM references to the accordion sections
  75. sections: (HTMLElement | undefined)[]
  76. // Registers a new accordion section
  77. registerSection: (index: number, trigger: HTMLElement) => () => void
  78. // The indexes of the open sections
  79. opened: number[]
  80. // Opens a section
  81. open: (section: number | undefined) => void
  82. // Closes a section
  83. close: (section: number | undefined) => void
  84. // Returns true if a section is open
  85. isOpen: (section: number | undefined) => boolean
  86. // Does the accordion allow all of its sections to be closed?
  87. allowAllClosed: boolean
  88. }

useSection()

This hook returns the value of the accordion sections’s SectionContextValue object. This hook
must be within a child of <Section>.

SectionContextValue

typescript jsx interface SectionContextValue { // Is this section open? isOpen: boolean // Opens this section if not disabled open: () => void // Closes this section if possible close: () => void // Toggles the visible state of this section if possible toggle: () => void // The id of this section id?: string // The index of this section index: number // Is the section disabled? disabled: boolean // The DOM reference to the section's <Trigger> triggerRef: React.MutableRefObject<HTMLElement | null> }

useControls()

This hook returns the accordion sections’s open, close, and toggle functions. This hook
must be within a child of <Section>.

useDisabled()

This hook returns the accordion sections’s disabled value. This hook
must be within a child of <Section>.

useIsOpen()

This hook returns the accordion sections’s isOpen value. This hook
must be within a child of <Section>.

LICENSE

MIT