Tailwind CSS Accordion Plugin API
Headless, unstyled accordion plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
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.
-
Install the plugin
Install
@preline/accordionvia npmTerminalnpm i @preline/accordion -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/accordion */ /* [!code highlight:3] */ @source "../node_modules/@preline/accordion/*.js"; @import "./node_modules/@preline/accordion/variants.css"; @import "./node_modules/@preline/accordion/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/accordion";JavaScript (non-auto)import HSAccordion from "@preline/accordion/non-auto"; HSAccordion.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#accordion"); if (el) new HSAccordion(el);
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!');
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Accordion
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Accordion examples