- Plugins
- Accordion
Plugins
Tailwind CSS Accordion
Build vertically collapsing accordions in combination with Accordion JavaScript plugin.
Installation
To get started, install Accordion plugin via npm, else you can skip this step if you are already using Preline UI as a package.
npm i @preline/accordion
Example
Click the accordions below to expand/collapse the accordion content.
It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element.
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div class="hs-accordion-group">
<div class="hs-accordion active" id="hs-unstyled-heading-one">
<button class="hs-accordion-toggle" aria-expanded="true" aria-controls="hs-unstyled-collapse-one">
Accordion #1
</button>
<div id="hs-unstyled-collapse-one" class="hs-accordion-content overflow-hidden transition-[height] duration-300" role="region" aria-labelledby="hs-unstyled-heading-one">
It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element.
</div>
</div>
<div class="hs-accordion" id="hs-unstyled-heading-two">
<button class="hs-accordion-toggle" aria-expanded="false" aria-controls="hs-unstyled-collapse-two">
Accordion #2
</button>
<div id="hs-unstyled-collapse-two" class="hs-accordion-content hidden overflow-hidden transition-[height] duration-300" role="region" aria-labelledby="hs-unstyled-heading-two">
It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element.
</div>
</div>
<div class="hs-accordion" id="hs-unstyled-heading-three">
<button class="hs-accordion-toggle" aria-expanded="false" aria-controls="hs-unstyled-collapse-three">
Accordion #3
</button>
<div id="hs-unstyled-collapse-three" class="hs-accordion-content hidden overflow-hidden transition-[height] duration-300" role="region" aria-labelledby="hs-unstyled-heading-three">
It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element.
</div>
</div>
</div>
Methods
The HSAccordion
object is contained within the global window
object
Open item (public method).
const accordion = new HSAccordion(document.querySelector('#accordion'));
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
accordion.show();
});
Open item (static method).
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
HSAccordion.show('#accordion');
});
Open item (mixed).
const { element } = HSAccordion.getInstance('#accordion', true);
const showBtn = document.querySelector('#show-btn');
showBtn.addEventListener('click', () => {
element.show();
});
Events
When item opens event example.
const { element } = HSAccordion.getInstance('#accordion', true);
element.on('open', () => {
console.log('Opened!');
});
element.on('close', () => {
console.log('Closed!');
});
Demo examples
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.