JSPM

  • Created
  • Published
  • Downloads 36930
  • Score
    100M100P100Q161192F
  • License Apache-2.0

Containers relating to accordions in the Garden Design System

Package Exports

  • @zendeskgarden/container-accordion
  • @zendeskgarden/container-accordion/dist/index.cjs.js
  • @zendeskgarden/container-accordion/dist/index.esm.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@zendeskgarden/container-accordion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@zendeskgarden/container-accordion npm version

This package includes containers relating to accordions in the Garden Design System.

Installation

npm install @zendeskgarden/container-accordion

Usage

This container implements the accordion design pattern and can be used to build an accordion component. Check out storybook for live examples.

useAccordion

The useAccordion hook manages toggle state and required accessibility attributes for a group of sections.

import { useAccordion } from '@zendeskgarden/container-accordion';

const Accordion = ({ sections = [0, 1, 2], expandable = true, collapsible = true } = {}) => {
  const { getHeaderProps, getTriggerProps, getPanelProps, expandedSections, disabledSections } =
    useAccordion({
      sections,
      expandedSections: [0],
      expandable,
      collapsible
    });

  return (
    <>
      {sections.map((section, index) => {
        const disabled = disabledSections.indexOf(index) !== -1;
        const hidden = expandedSections.indexOf(index) === -1;

        return (
          <div key={index}>
            <h2 {...getHeaderProps({ role: null, ariaLevel: null })}>
              <button
                {...getTriggerProps({
                  index,
                  role: null,
                  tabIndex: null,
                  disabled,
                  style: { width: '100%' }
                })}
              >
                {index}
              </button>
            </h2>
            <section
              {...getPanelProps({
                index,
                role: null,
                hidden
              })}
            >
              {section}
            </section>
          </div>
        );
      })}
    </>
  );
};

return <Accordion expandable={true} collapsible={true} />;

AccordionContainer

AccordionContainer is a render-prop wrapper for the useAccordion hook.

import { AccordionContainer } from '@zendeskgarden/container-accordion';

const Accordion = ({ sections = [0, 1, 2], expandable = true, collapsible = true } = {}) => (
  <AccordionContainer sections={sections} expandable={expandable} collapsible={collapsible}>
    {({ getHeaderProps, getTriggerProps, getPanelProps, expandedSections, disabledSections }) => (
      <>
        {sections.map((section, index) => {
          const disabled = disabledSections.indexOf(index) !== -1;
          const hidden = expandedSections.indexOf(index) === -1;

          return (
            <div key={index}>
              <h2 {...getHeaderProps({ role: null, ariaLevel: null })}>
                <button
                  {...getTriggerProps({
                    index,
                    role: null,
                    tabIndex: null,
                    disabled,
                    style: { width: '100%' }
                  })}
                >
                  {index}
                </button>
              </h2>
              <section
                {...getPanelProps({
                  index,
                  role: null,
                  hidden
                })}
              >
                {section}
              </section>
            </div>
          );
        })}
      </>
    )}
  </AccordionContainer>
);

return <Accordion expandable={true} collapsible={true} />;