- Responsive (using more button) - Full API (Open & Close & Select & Refresh & setOption & setTab, ...) - lazy loading and rendering - Customizable style - Return to last used tab when closing selected tab - PanelList can be rendered outside the TabList container - ARIA accessible - Customizable Tab component - Multiple themes - The core is about 23kb
`js
import React from 'react';
import 'react-dyn-tabs/style/react-dyn-tabs.css'; // Mandatory CSS required by the react-dyn-tabs
import 'react-dyn-tabs/themes/react-dyn-tabs-card.css'; // Optional Theme applied to the react-dyn-tabs
import useDynTabs from 'react-dyn-tabs';
const addTab3 = function () {
// use ready function to access the instance object
ready((instance) => {
// open tab 3
instance.open({id: '3', title: 'Tab 3', panelComponent: (props) =>
panel 3
}).then(() => {
console.log('tab 3 is open');
});
// switch to tab 3
instance.select('3').then(() => {
console.log('tab 3 is selected');
});
});
};
return (
);
};
`
ready function
The ready function in the react-dyn-tabs library is part of the array returned by the useDynTabs hook, alongside the TabList and PanelList components. This function allows developers to execute a callback when the TabList and PanelList components are fully mounted, providing access to the instance object for further manipulation.
$3
- Multiple Calls: Developers can invoke the ready function multiple times without any issues.
- Stable Identity: The reference to the ready function remains stable across component re-renders, ensuring consistent behavior.
- Immediate Execution: If the ready function is called after the tabs have already been mounted, the provided callback will be executed immediately.
Fires when the user clicks on the tab, but before select them.
This event should return boolean true or false, If the event returns false the tab is not selected.
fires when the user clicks on the close icon, but before close them.
This event should return boolean true or false, If the event return false the tab is not closed.
Attach a handler to an event. The handler is executed at most once.
Return value : instance object
Parameters:
- event Name : String (can be either of onFirstSelect|onSelect|onClose|onOpen|onInit|onChange|onDestroy) - handler : function
Example
`js
instance.one('onSelect', function ({currentSelectedTabId, previousSelectedTabId}) {});
`
$3
Remove an event handler.
Return value : instance object
Parameters:
- event Name : String (can be either of onFirstSelect|onSelect|onClose|onOpen|onInit|onChange|onDestroy) - handler : function (A handler function previously attached for the event)
- getCopyData function is an older version of getData function and it is enabled by default so that existing users do not have to change their code. You are free to use both conventions.
- getCopyPerviousData function is an older version of getPreviousData function and it is enabled by default so that existing users do not have to change their code. You are free to use both conventions.
- panel 1 is eagerly loaded and rendered.
- panel 2 is eagerly loaded but will not be rendered until tab 2 is activated.
- panel 3 will not be loaded and rendered until tab 3 is activated.
react-dyn-tabs does not include any style loading by default. Default stylesheets and themes are provided and can be included in your application if desired.
$3
`js
import 'react-dyn-tabs/style/react-dyn-tabs.css';
// or import 'react-dyn-tabs/style/react-dyn-tabs.min.css';
// or import 'react-dyn-tabs/style/scss/react-dyn-tabs.scss';
`
For rtl mode you should also import following file
`js
import 'react-dyn-tabs/style/react-dyn-tabs-rtl.css';
// or import 'react-dyn-tabs/style/react-dyn-tabs-rtl.min.css';
// or import 'react-dyn-tabs/style/scss/react-dyn-tabs-rtl.scss';
`
$3
Themes define how the Tabs looks. The library comes with Provided Themes such as card and bootstrap. To use a theme you need to 1) import the themes CSS and 2) apply the chosen theme name to the theme option of the react-dyn-tabs.
- card theme
`js
import 'react-dyn-tabs/themes/react-dyn-tabs-card.css';
// or import 'react-dyn-tabs/themes/scss/react-dyn-tabs-card.scss';
// or import 'react-dyn-tabs/themes/react-dyn-tabs-card.min.css';
...
useDynTabs({theme:'card'});
`
- bootstrap theme
`js
import 'react-dyn-tabs/themes/react-dyn-tabs-bootstrap.css';
// or import 'react-dyn-tabs/themes/scss/react-dyn-tabs-bootstrap.scss';
// or import 'react-dyn-tabs/themes/react-dyn-tabs-bootstrap.min.css';
...
useDynTabs({theme:'bootstrap'});
`
- basic theme
`js
import 'react-dyn-tabs/themes/react-dyn-tabs-basic.css';
// or import 'react-dyn-tabs/themes/scss/react-dyn-tabs-basic.scss';
// or import 'react-dyn-tabs/themes/react-dyn-tabs-basic.min.css';
...
useDynTabs({theme:'basic'});
`
- classic theme
`js
import 'react-dyn-tabs/themes/react-dyn-tabs-classic.css';
// or import 'react-dyn-tabs/themes/scss/react-dyn-tabs-classic.scss';
// or import 'react-dyn-tabs/themes/react-dyn-tabs-classic.min.css';
...
useDynTabs({theme:'classic'});
`
Caveats
- Some actions like open, select, close and refresh cause re-rendering, and using them immediately after calling useDynTabs hook will create an infinite loop and other bugs that most likely you don't want to cause. you should use them inside event listeners or subscriptions.
- Do not use setState inside the onInit callback because it leads to an infinite loop.