Giter Club home page Giter Club logo

tabs's Introduction


@accessible/tabs

๐Ÿ…ฐ An accessible and versatile tabs component for React with keyboard navigation and labeling features taught in w3.org's WAI-ARIA tabs example

npm i @accessible/tabs

Bundlephobia Types Code coverage Build status NPM Version MIT License


An accessible and versatile tabs component for React modeled after the WAI-ARIA example taught here.

Quick Start

Check out the example on CodeSandbox

import {Tabs, TabList, Tab, Panel} from '@accessible/tabs'

const Component = () => (
  <Tabs defaultActive={0} manualActivation>
    <TabList>
      <div aria-label='Some research thing'>
        <Tab>
          <button>Abstract</button>
        </Tab>
        <Tab>
          <button>References</button>
        </Tab>
      </div>
    </TabList>

    <Panel>
      <div>Abstract body</div>
    </Panel>
    <Panel>
      <div>References body</div>
    </Panel>
  </Tabs>
)

API

Components

Component Description
<Tabs> This component creates the context for your tabs and contains some configuration options. You'll need to add <Tab> and <Panel> as children in order to actually create tabs.
<TabList> The component adds role='tablist' to its child component.
<Tab> This component clones any React element and turns it into a tab that controls the visible state of a <Panel>. It must be a child of <Tabs> and all tabs must be adjacent in the tree. Each tab has a corresponding <Panel> that shares the same index.
<Panel> This component clones its child and turns it into a panel that corresponds to a <Tab> with the same index. All panels must be adjacent in the tree unless an index prop is defined.

Hooks

Hook Description
useTabs() This hook returns the value of the TabsContext object.
useTab() This hook returns the value of the TabContext object.

<Tabs>

This component creates the context for your tabs and contains some configuration options. You'll need to add <Tab> and <Panel> as children in order to actually create tabs.

Props

Prop Type Default Required? Description
defaultActive number 0 No The <Tab> index you want active by default.
active number undefined No Makes this a controlled component where the activate control has no effect. The tab index defined here is always the one that is active.
manualActivation boolean false No By default this component opens tabs automatically when using keyboard navigation to switch between tabs. By setting this to true, the user will have to use the space or enter key to activate the tab after the tab is focused.
preventScroll boolean false No When true this will prevent your browser from scrolling the document to bring the newly-focused tab into view.
onChange (active: number) => void undefined No Called each time the active tab changes. It provides the active tab index as its only argument.
children React.ReactNode[] undefined Yes You can define any children here with some caveats listed elsewhere.

<TabList>

Props

Prop Type Default Required? Description
children React.ReactElement undefined Yes The child is cloned by this component and given a property for role='tablist'.

<Tab>

This component clones any React element and turns it into a tab that controls the visible state of a <Panel>. It must be a child of <Tabs> and all tabs must be adjacent in the tree. Each tab has a corresponding <Panel> that shares the same index.

// YES
const MyTabs = () => (
  <Tabs>
    <TabList>
      {/* index: 0 */}
      <Tab>
        <div />
      </Tab>
      {/* index: 1 */}
      <Tab>
        <div />
      </Tab>
    </TabList>
    {/* index: 0 */}
    <Panel>
      <div />
    </Panel>
    {/* index: 1 */}
    <Panel>
      <div />
    </Panel>
  </Tabs>
)

// ABSOLUTELY NOT
const MyTabs = () => (
  <Tabs>
    <TabList>
      {/* The Tab components here are not adjacent in the tree. */}
      <div>
        <Tab>
          <div />
        </Tab>
      </div>
      <div>
        <Tab>
          <div />
        </Tab>
      </div>
    </TabList>
    <Panel>
      <div />
    </Panel>
    <Panel>
      <div />
    </Panel>
  </Tabs>
)

Props

Prop Type Default Required? Description
activeClass string undefined No Adds this class to the child component when the tab is in an active state.
inactiveClass string undefined No Adds this class to the child component when the tab is in an inactive state.
activeStyle React.CSSProperties undefined No Adds these styles to the child component when the tab is in an active state.
inactiveStyle React.CSSProperties undefined No Adds these styles to the child component when the tab is in an inactive state.
id string undefined No Defining an ID here overrides the auto id generated for aria attributes.
disabled boolean false No Setting this to true will prevent the tab from activating if it isn't already active.
index number undefined No Setting an index here overrides the default index created when this component mounts. Indexes are used to match tabs to their corresponding <Panel>. I would recommend not setting this property and letting the library handle it automatically.
onDelete (event: KeyboardEvent) => void undefined No This callback will fire if a user presses the Delete key on their keyboard when this tab (not the panel) is focused.
children React.ReactElement undefined Yes The child is cloned by this component and has aria attributes injected into its props as well as keyboard event handlers for navigating between tabs.

<Panel>

This component clones its child and turns it into a panel that corresponds to a <Tab> with the same index. All panels must be adjacent in the tree unless an index prop is defined. For example:

<Tabs>
  <TabList>
    [0] <Tab>
    [1] <Tab>
  </TabList>

  [0] <Panel>
  [1] <Panel>
</Tabs>

Props

Prop Type Default Required? Description
activeClass string undefined No Adds this class to the child component when the panel's <Tab> is in an active state.
inactiveClass string undefined No Adds this class to the child component when the panel's <Tab> is in an inactive state.
activeStyle React.CSSProperties undefined No Adds these styles to the child component when the panel's <Tab> is in an active state.
inactiveStyle React.CSSProperties undefined No Adds these styles to the child component when the panel's <Tab> is in an inactive state.
index number undefined No Setting an index here overrides the default index created when this component mounts. Indexes are used to match panels to their corresponding <Tab>. I would recommend not setting this property and letting the library handle it automatically.
children React.ReactElement undefined Yes The child is cloned by this component and has aria attributes injected into its props and will have its visible state controlled by the <Tab> component with the same index.

useTab(index: number)

Returns TabContext object for the <Tab> corresponding to the provided index. It must be used within a child of <Tabs>.

TabContextValue

interface TabContextValue {
  // The ID used for aria attributes
  id?: string
  // A ref to the tab's underlying element
  tabRef?: HTMLElement
  // The index of the tab
  index: number
  // Activates this tab unless `disabled` is `true`
  activate: () => void
  // Is this tab active?
  isActive: boolean
  // Is this tab disabled?
  disabled: boolean
}

useTabs()

This hook returns the value of the TabsContext object. This hook must be within a child of <Tabs>.

TabsContextValue

interface TabsContextValue {
  // An array of tabs that have been registered
  tabs: TabState[]
  // Registers a new tab
  registerTab: (
    index: number,
    element: HTMLElement,
    id?: string,
    disabled?: boolean
  ) => () => void
  // The tab that is currently active
  active: number | undefined
  // Activates the tab at `index`
  activate: (index: number | undefined) => void
  // Is manual activation configured?
  manualActivation: boolean
}

type TabState =
  | {
      element?: HTMLElement
      id?: string
      disabled?: boolean
    }
  | undefined

LICENSE

MIT

tabs's People

Contributors

jaredlunde avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

tabs's Issues

Add option to disable window scroll

Hey man, great library! All your components have been a great addition to some projects I have been working on.

I have a feature request for the tabs; would it be possible to pass the preventScroll property as a parameter to the tabs component? In my current usecase, my page jumps down when I switch tabs. There are ways to work around it in my case, but I was wondering if you would consider adding it.

https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus

Module build fails when transpiling with babel

Hey guys,

I've found an issue when compiling the dist to a ES5 version:
ERROR in ./node_modules/@accessible/tabs/dist/es/index.js Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: /node_modules/@accessible/tabs/dist/es/index.js: Unexpected token, expected ";" (306:9)

The line in question is this one: !manualActivation && activate() when I manually change it to: ;!manualActivation && activate() it works. The same code error is in the cjs dist.

Is there a way to get this fixed so babel doesn't fail? Currently im stuck on this and the project wont build.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.