- Plugins
- Collapse
Plugins
Tailwind CSS Collapse
Toggle the visibility of content.
Installation
To get started, install Collapse plugin via npm, else you can skip this step if you are already using Preline UI as a package.
npm i @preline/collapse
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<button type="button" class="hs-collapse-toggle" id="hs-unstyled-collapse" aria-expanded="false" aria-controls="#hs-unstyled-collapse-heading" data-hs-collapse="#hs-unstyled-collapse-heading">
Collapse
</button>
<div id="hs-unstyled-collapse-heading" class="hs-collapse hidden w-full overflow-hidden transition-[height] duration-300" aria-labelledby="hs-unstyled-collapse">
This is a collapse body. It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions.
</div>
Methods
The HSCollapse
object is contained within the global window
object
Open item (public method).
const collapse = new HSCollapse(document.querySelector('#collapse'));
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
collapse.show();
});
Open item (static method).
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
HSCollapse.show('#collapse');
});
Open item (mixed).
const { element } = HSCollapse.getInstance('#collapse', true);
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
element.show();
});
Events
Open any item event example.
const el = HSCollapse.getInstance('#collapse');
el.on('open', (el) => {...});
Demo examples
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Check out Preline UI Components