Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@react-aria/tabs

Package Overview
Dependencies
Maintainers
2
Versions
779
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-aria/tabs

Spectrum UI components in React

  • 3.9.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is @react-aria/tabs?

@react-aria/tabs is a library that provides accessible tab components for React applications. It is part of the React Aria collection, which focuses on providing high-quality, accessible UI components. The package helps developers create tabbed interfaces that are compliant with WAI-ARIA standards, ensuring that they are usable by people with disabilities.

What are @react-aria/tabs's main functionalities?

Basic Tab Implementation

This code demonstrates a basic implementation of a tabbed interface using @react-aria/tabs. It sets up a tab list with two tabs and their corresponding content.

```jsx
import { useTabListState } from '@react-stately/tabs';
import { useTab, useTabList, useTabPanel } from '@react-aria/tabs';
import { TabList, Tab, TabPanel } from '@react-spectrum/tabs';

function MyTabs() {
  let state = useTabListState({
    defaultSelectedKey: 'tab1',
    children: [
      { key: 'tab1', title: 'Tab 1', children: 'Content 1' },
      { key: 'tab2', title: 'Tab 2', children: 'Content 2' }
    ]
  });

  let { tabListProps } = useTabList(state);

  return (
    <div>
      <TabList {...tabListProps}>
        {state.collection.map((item) => (
          <Tab key={item.key} item={item} state={state} />
        ))}
      </TabList>
      <TabPanel key={state.selectedItem.key} state={state} />
    </div>
  );
}
```

Customizable Tabs

This example shows how to customize the appearance of the tabs and tab panels using inline styles. The tabs have a blue color, and the tab list has a light gray background.

```jsx
import { useTabListState } from '@react-stately/tabs';
import { useTab, useTabList, useTabPanel } from '@react-aria/tabs';
import { TabList, Tab, TabPanel } from '@react-spectrum/tabs';

function CustomTabs() {
  let state = useTabListState({
    defaultSelectedKey: 'tab1',
    children: [
      { key: 'tab1', title: 'Tab 1', children: 'Content 1' },
      { key: 'tab2', title: 'Tab 2', children: 'Content 2' }
    ]
  });

  let { tabListProps } = useTabList(state);

  return (
    <div>
      <TabList {...tabListProps} style={{ backgroundColor: 'lightgray' }}>
        {state.collection.map((item) => (
          <Tab key={item.key} item={item} state={state} style={{ color: 'blue' }} />
        ))}
      </TabList>
      <TabPanel key={state.selectedItem.key} state={state} style={{ padding: '10px' }} />
    </div>
  );
}
```

Dynamic Tabs

This example demonstrates how to create dynamic tabs that can be added or removed at runtime. The `addTab` function adds a new tab to the state, and the tab list updates accordingly.

```jsx
import { useState } from 'react';
import { useTabListState } from '@react-stately/tabs';
import { useTab, useTabList, useTabPanel } from '@react-aria/tabs';
import { TabList, Tab, TabPanel } from '@react-spectrum/tabs';

function DynamicTabs() {
  const [tabs, setTabs] = useState([
    { key: 'tab1', title: 'Tab 1', children: 'Content 1' },
    { key: 'tab2', title: 'Tab 2', children: 'Content 2' }
  ]);

  let state = useTabListState({
    defaultSelectedKey: 'tab1',
    children: tabs
  });

  let { tabListProps } = useTabList(state);

  const addTab = () => {
    const newTab = { key: `tab${tabs.length + 1}`, title: `Tab ${tabs.length + 1}`, children: `Content ${tabs.length + 1}` };
    setTabs([...tabs, newTab]);
  };

  return (
    <div>
      <button onClick={addTab}>Add Tab</button>
      <TabList {...tabListProps}>
        {state.collection.map((item) => (
          <Tab key={item.key} item={item} state={state} />
        ))}
      </TabList>
      <TabPanel key={state.selectedItem.key} state={state} />
    </div>
  );
}
```

Other packages similar to @react-aria/tabs

FAQs

Package last updated on 14 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc