- Plugins
- Tabs
Plugins
Tailwind CSS Tabs
Tabs make it easy to switch between different views.
Installation
To get started, install Tabs plugin via npm, else you can skip this step if you are already using Preline UI as a package.
npm i @preline/tabs
Example
Separate content into different panes where each pane is viewable one at a time.
This is the first item's tab body.
This is the second item's tab body.
This is the third item's tab body.
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<nav class="flex gap-x-2" aria-label="Tabs" role="tablist">
<button type="button" class="active" id="unstyled-tabs-item-1" aria-selected="true" data-hs-tab="#unstyled-tabs-1" aria-controls="unstyled-tabs-1" role="tab">
Tab 1
</button>
<button type="button" id="unstyled-tabs-item-2" aria-selected="false" data-hs-tab="#unstyled-tabs-2" aria-controls="unstyled-tabs-2" role="tab">
Tab 2
</button>
<button type="button" id="unstyled-tabs-item-3" aria-selected="false" data-hs-tab="#unstyled-tabs-3" aria-controls="unstyled-tabs-3" role="tab">
Tab 3
</button>
</nav>
<div class="mt-3">
<div id="unstyled-tabs-1" role="tabpanel" aria-labelledby="unstyled-tabs-item-1">
This is the <em>first</em> item's tab body.
</div>
<div id="unstyled-tabs-2" class="hidden" role="tabpanel" aria-labelledby="unstyled-tabs-item-2">
This is the <em>second</em> item's tab body.
</div>
<div id="unstyled-tabs-3" class="hidden" role="tabpanel" aria-labelledby="unstyled-tabs-item-3">
This is the <em>third</em> item's tab body.
</div>
</div>
Methods
The HSTabs
object is contained within the global window
object
Destroy instance (public method).
const { element } = HSTabs.getInstance('#tabs', true);
const destroyBtn = document.querySelector('#destroy-btn');
destroyBtn.addEventListener('click', () => {
element.destroy();
});
Open item (static method).
const openBtn = document.querySelector('#open-btn');
openBtn.addEventListener('click', () => {
HSTabs.open(document.querySelector('#tab'))
});
Events
An example where a function is run every time a tab changes.
const el = HSTabs.getInstance('#tab-1');
el.on('change', ({el, prev, current}) => {...});
Demo examples
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.