Security News
Python Overtakes JavaScript as Top Programming Language on GitHub
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
react-tabs
Advanced tools
The react-tabs package is a simple and flexible tab component for React applications. It allows developers to create tabbed interfaces with ease, providing a straightforward API and customizable components.
Basic Tabs
This feature allows you to create a basic tabbed interface with multiple tabs and corresponding content panels.
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
function App() {
return (
<Tabs>
<TabList>
<Tab>Tab 1</Tab>
<Tab>Tab 2</Tab>
<Tab>Tab 3</Tab>
</TabList>
<TabPanel>
<h2>Content for Tab 1</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 2</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 3</h2>
</TabPanel>
</Tabs>
);
}
Custom Styling
This feature demonstrates how to apply custom styling to the tabs by using a custom CSS file.
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
import './custom-tabs.css';
function App() {
return (
<Tabs>
<TabList>
<Tab className="custom-tab">Tab 1</Tab>
<Tab className="custom-tab">Tab 2</Tab>
<Tab className="custom-tab">Tab 3</Tab>
</TabList>
<TabPanel>
<h2>Content for Tab 1</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 2</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 3</h2>
</TabPanel>
</Tabs>
);
}
Dynamic Tabs
This feature shows how to create dynamic tabs that can be added or removed at runtime.
import { useState } from 'react';
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
function App() {
const [tabs, setTabs] = useState([
{ title: 'Tab 1', content: 'Content for Tab 1' },
{ title: 'Tab 2', content: 'Content for Tab 2' }
]);
const addTab = () => {
setTabs([...tabs, { title: `Tab ${tabs.length + 1}`, content: `Content for Tab ${tabs.length + 1}` }]);
};
return (
<div>
<button onClick={addTab}>Add Tab</button>
<Tabs>
<TabList>
{tabs.map((tab, index) => (
<Tab key={index}>{tab.title}</Tab>
))}
</TabList>
{tabs.map((tab, index) => (
<TabPanel key={index}>
<h2>{tab.content}</h2>
</TabPanel>
))}
</Tabs>
</div>
);
}
React-Bootstrap is a popular library that provides Bootstrap components as React components. It includes a Tabs component that offers similar functionality to react-tabs but with the added benefit of Bootstrap's styling and theming capabilities.
Material-UI is a comprehensive library of React components that implement Google's Material Design. It includes a Tabs component that provides similar functionality to react-tabs, along with a wide range of other UI components and customization options.
Semantic UI React is the official React integration for Semantic UI. It includes a Tab component that offers similar functionality to react-tabs, with the added benefit of Semantic UI's declarative syntax and extensive theming options.
An accessible and easy tab component for ReactJS.
https://reactcommunity.org/react-tabs/
Supports React 16.3.0 or newer
react-tabs was tested on real mobile devices and browsers with
yarn add react-tabs
or
npm install --save react-tabs
You can also use react-tabs directly as UMD build in an HTML document by adding
<script src="https://unpkg.com/react-tabs/dist/react-tabs.development.js" />
<!-- or -->
<script src="https://unpkg.com/react-tabs/dist/react-tabs.production.min.js" />
For example usages of the UMD builds have a look at the old_examples/umd
folder.
The development UMD build also needs the package prop-types
being loaded besides react
.
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
export default () => (
<Tabs>
<TabList>
<Tab>Title 1</Tab>
<Tab>Title 2</Tab>
</TabList>
<TabPanel>
<h2>Any content 1</h2>
</TabPanel>
<TabPanel>
<h2>Any content 2</h2>
</TabPanel>
</Tabs>
);
Allows reseting the internal id counter which is used to generate unique id's for tabs and tab panels.
You should never need to use this in the browser. Only if you are running an isomorphic react app that is rendered on the server you should call resetIdCounter()
before every page render so that the ids that get generated on the server match the ids generated in the browser.
import { resetIdCounter } from 'react-tabs';
resetIdCounter();
ReactDOMServer.renderToString(...);
react-tabs consists of 4 components which all need to be used together.
If you specify additional props on the <Tabs />
component they will be forwarded to the rendered <div />
.
string | Array<string> | { [string]: boolean }
default:
"react-tabs"
Provide a custom class name for the outer <div />
of the tabs.
You can also supply an array of class names or an object where the class names are the key and the value is a boolean indicating if the name should be added. See the docs of classnames on how to supply different class names.
boolean
default:
false
If set to true
the tabs will be focused on initial render. This allows immediate use of keyboard keys to switch tabs after the first render.
number
default:
0
This allows changing the tab that should be open on initial render. This is a zero-based index, so first tab is 0
, second tab is 1
, ...
This can only be used in uncontrolled mode when react-tabs handles the current selected tab internally and for this reason cannot be used together with
selectedIndex
. See here for more info on modes.
string
default:
"react-tabs__tab--disabled"
Provide a custom class name for disabled tabs.
This option can also be set directly at the
<Tab />
component.
(node: ?HTMLElement) => void
default:
null
Register a callback that will receive the underlying DOM node for every mount. It will also receive null on unmount.
boolean
default:
false
By default only the current active tab will be rendered to DOM. If set to true
all tabs will be rendered to the DOM always.
This can also be enabled for each individual
<TabPanel />
component with its propforceRender
.
(index: number, lastIndex: number, event: Event) => ?boolean
default:
undefined
This event handler is called every time a tab is about to change. It will be called with the index
that it will be changed to, the lastIndex
which was selected before and the underlying event
which is usually either a keydown
or click
event. When index
and lastIndex
are equal it means the user clicked on the currently active tab.
The callback can optionally return false
to cancel the change to the new tab.
Returning
false
when the change to the new tab should be canceled is also important in controlled mode, as react-tabs still internally handles the focus of the tabs.
In controlled mode the
onSelect
handler is a required prop.
number
default:
null
Set the currently selected tab. This is a zero-based index, so first tab is 0
, second tab is 1
, ...
This enables controlled mode, which also requires onSelect
to be set. See here for more info on modes.
string
default:
"react-tabs__tab--selected"
Provide a custom class name for the active tab.
This option can also be set directly at the
<Tab />
component.
string
default:
"react-tabs__tab-panel--selected"
Provide a custom class name for the active tab panel.
This option can also be set directly at the
<TabPanel />
component.
string
default:
"ltr"
Provide the direction of the component, can be either rtl or ltr.
If you specify additional props on the <TabList />
component they will be forwarded to the rendered <ul />
.
string | Array<string> | { [string]: boolean }
default:
"react-tabs__tab-list"
Provide a custom class name for the <ul />
.
You can also supply an array of class names or an object where the class names are the key and the value is a boolean indicating if the name should be added. See the docs of classnames on how to supply different class names.
If you specify additional props on the <Tab />
component they will be forwarded to the rendered <li />
.
string | Array<string> | { [string]: boolean }
default:
"react-tabs__tab"
Provide a custom class name for the <li />
.
You can also supply an array of class names or an object where the class names are the key and the value is a boolean indicating if the name should be added. See the docs of classnames on how to supply different class names.
boolean
default:
false
Disable this tab which will make it not do anything when clicked. Also a disabled class name will be added (see disabledClassName
)
string
default:
"react-tabs__tab--disabled"
Provide a custom class name for disabled tabs.
This option can also be set for all
<Tab />
components with the propdisabledTabClassName
on<Tabs />
.
string
default:
"react-tabs__tab--selected"
Provide a custom class name for the active tab.
This option can also be set for all
<Tab />
components with the propselectedTabClassName
on<Tabs />
.
string
default: if selected
"0"
otherwisenull
Overrides the tabIndex to enabled tabbing between tabs.
If you specify additional props on the <TabPanel />
component they will be forwarded to the rendered <div />
.
string | Array<string> | { [string]: boolean }
default:
"react-tabs__tab-panel"
Provide a custom class name for the <div />
containing the tab content.
You can also supply an array of class names or an object where the class names are the key and the value is a boolean indicating if the name should be added. See the docs of classnames on how to supply different class names.
boolean
default:
false
By default the tab content will only be rendered when the tab is active. If set to true
the tab will also be rendered if inactive.
This can also be enabled for all
<TabPanel />
components with the propforceRenderTabPanel
on<Tabs />
.
string
default:
"react-tabs__tab-panel--selected"
Provide a custom class name for the active tab panel.
This option can also be set for all
<TabPanel />
components with the propselectedTabPanelClassName
on<Tabs />
.
React tabs has two different modes it can operate in, which change the way how much you need to take care about the state yourself.
This is the default mode of react-tabs and makes the react-tabs components handle its state internally. You can change the starting tab with defaultIndex
and you can listen for changes with onSelect
.
In this mode you cannot force a tab change during runtime.
<Tabs defaultIndex={1} onSelect={index => console.log(index)}>
<TabList>
<Tab>Title 1</Tab>
<Tab>Title 2</Tab>
</TabList>
<TabPanel></TabPanel>
<TabPanel></TabPanel>
</Tabs>
This mode has to be enabled by supplying selectedIndex
to the <Tabs />
component.
In this mode react-tabs does not handle any tab selection state internally and leaves all the state management up to the outer application.
This mode also enforces you to set a handler for onSelect
. defaultIndex
does not have any effect and will therefore throw an error.
class App extends Component {
constructor() {
super();
this.state = { tabIndex: 0 };
}
render() {
return (
<Tabs selectedIndex={this.state.tabIndex} onSelect={tabIndex => this.setState({ tabIndex })}>
<TabList>
<Tab>Title 1</Tab>
<Tab>Title 2</Tab>
</TabList>
<TabPanel></TabPanel>
<TabPanel></TabPanel>
</Tabs>
);
}
}
react-tabs does not include any style loading by default. Default stylesheets are provided and can be included in your application if desired.
When using webpack and an appropriate loader (css-loader
, sass-loader
, less-loader
or style-loader
) you can simply import the default stylesheet.
import 'react-tabs/style/react-tabs.css';
// or
import 'react-tabs/style/react-tabs.scss';
// or
import 'react-tabs/style/react-tabs.less';
When using SASS you can easily import the default styles
@import '../../path/to/node_modules/react-tabs/style/react-tabs.scss';
When using LESS you can easily import the default styles
@import '../../path/to/node_modules/react-tabs/style/react-tabs.less';
When using the UMD version of react-tabs you can easily use the default styles by adding
<html>
<head>
...
<link rel="stylesheet" href="https://unpkg.com/react-tabs/style/react-tabs.css">
</head>
...
</html>
You can also always just simply copy the default style to your own css/scss/less and modify it to your own needs. The changelog will always tell you when classes change and we also consider changes that break the styling as semver major.
In case you want to create your own component wrapping the ones that the library provides, you have to set its tabsRole
. This value is used inside react-tabs to check the role of a component inside <Tabs />
.
Possible values for tabsRole are:
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
const CustomTab = ({ children }) => (
<Tab>
<h1>{children}</h1>
</Tab>
);
CustomTab.tabsRole = 'Tab'; // Required field to use your custom Tab
const App = () => (
<Tabs>
<TabList>
<CustomTab>Custom Tab 1</CustomTab>
<CustomTab>Custom Tab 2</CustomTab>
</TabList>
<TabPanel>Panel 1</TabPanel>
<TabPanel>Panel 2</TabPanel>
</Tabs>
);
MIT
FAQs
An accessible and easy tab component for ReactJS
The npm package react-tabs receives a total of 729,713 weekly downloads. As such, react-tabs popularity was classified as popular.
We found that react-tabs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
Security News
Dutch National Police and FBI dismantle Redline and Meta infostealer malware-as-a-service operations in Operation Magnus, seizing servers and source code.
Research
Security News
Socket is tracking a new trend where malicious actors are now exploiting the popularity of LLM research to spread malware through seemingly useful open source packages.