@zendeskgarden/container-tabs
TypeScript icon, indicating that this package has built-in type declarations

2.0.10 • Public • Published

@zendeskgarden/container-tabs npm version

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

Installation

npm install @zendeskgarden/container-tabs

Usage

This container implements the tabs design pattern and can be used to build a tabbed interface. Check out storybook for live examples.

useTabs

import { useTabs } from '@zendeskgarden/container-tabs';

const tabs = [{ value: 'Tab 1' }, { value: 'Tab 2' }, { value: 'Tab 3' }];

const Tabs = () => {
  const { selectedValue, getTabProps, getTabListProps, getTabPanelProps } = useTabs({
    tabs
  });
  const tabComponents = [];
  const tabPanels = [];

  tabs.forEach(({ value }, index) => {
    tabComponents.push(
      <li
        {...getTabProps({
          value,
          key: value,
          style: {
            borderBottom: `3px solid ${value === selectedValue ? '#1f73b7' : 'transparent'}`
          }
        })}
      >
        {value}
      </li>
    );

    tabPanels.push(
      <div
        {...getTabPanelProps({
          value,
          key: value,
          style: {
            padding: '10px 0',
            borderTop: '1px solid'
          }
        })}
      >
        {value} Content
      </div>
    );
  });

  return (
    <>
      <ul
        {...getTabListProps({
          style: { display: 'flex' }
        })}
      >
        {tabComponents}
      </ul>
      {tabPanels}
    </>
  );
};

TabsContainer

import { TabsContainer } from '@zendeskgarden/container-tabs';

const tabs = [{ value: 'Tab 1' }, { value: 'Tab 2' }, { value: 'Tab 3' }];

const Tabs = () => {
  const tabComponents = [];
  const tabPanels = [];

  return (
    <TabsContainer tabs={tabs}>
      {({ selectedValue, getTabProps, getTabListProps, getTabPanelProps }) => {
        tabs.forEach(({ value }, index) => {
          tabComponents.push(
            <li
              {...getTabProps({
                value,
                key: value,
                style: {
                  borderBottom: `3px solid ${value === selectedValue ? '#1f73b7' : 'transparent'}`
                }
              })}
            >
              {value}
            </li>
          );

          tabPanels.push(
            <div
              {...getTabPanelProps({
                value,
                key: value,
                style: {
                  padding: '10px 0',
                  borderTop: '1px solid'
                }
              })}
            >
              {value} Content
            </div>
          );
        });

        return (
          <>
            <ul
              {...getTabListProps({
                style: { display: 'flex' }
              })}
            >
              {tabComponents}
            </ul>
            {tabPanels}
          </>
        );
      }}
      }
    </TabsContainer>
  );
};

Package Sidebar

Install

npm i @zendeskgarden/container-tabs

Weekly Downloads

2,345

Version

2.0.10

License

Apache-2.0

Unpacked Size

24.9 kB

Total Files

9

Last publish

Collaborators

  • zendesk-garden