3.0.8 • Published 4 months ago

@zendeskgarden/container-accordion v3.0.8

Weekly downloads
11,707
License
Apache-2.0
Repository
github
Last release
4 months ago

@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} />;
3.0.8

4 months ago

3.0.7

4 months ago

3.0.6

4 months ago

3.0.5

4 months ago

2.0.7

10 months ago

2.0.8

10 months ago

3.0.4

7 months ago

3.0.3

8 months ago

3.0.2

9 months ago

3.0.1

9 months ago

3.0.0

9 months ago

2.0.6

11 months ago

2.0.5

1 year ago

2.0.4

1 year ago

1.1.3

2 years ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago