rt-accordion is a lightweight JavaScript utility that creates accessible, smooth, and fully responsive accordion components with:
- Zero-config defaults (works out of the box)
- Attribute-driven configuration (supports both
rt-accordion-*anddata-rt-accordion-*) - Support for multiple instances
- A clean global API under
window.rtAccordion - Defensive fallbacks to avoid runtime crashes
Zero dependency (100% Vanilla JS)
- 1. Installation
- 2. Quick Start
- 3. Activation Rules
- 4. Configuration (HTML Attributes)
- 5. Multiple Instances
- 6. Global API
- 7. Troubleshooting
- 8. License
<script src="https://cdn.jsdelivr.net/npm/@rethink-js/rt-accordion@latest/dist/index.min.js"></script>npm install @rethink-js/rt-accordionThen bundle or load dist/index.min.js as appropriate for your build setup.
Add the script to your page. With no configuration provided, rt-accordion will:
- Auto-initialize itself when applicable
- Apply safe, accessible defaults (ARIA roles, tab-indexing, keyboard navigation)
- Expose the global API
Example HTML structure:
<div rt-accordion>
<div rt-accordion-item>
<button rt-accordion-trigger>Toggle Item</button>
<div rt-accordion-content>
<p>Your accordion content goes here.</p>
</div>
</div>
</div>Note: If you do not provide any
rt-accordion-modeorrt-accordion-default-openattributes, the library runs using its internal defaults (singlemode,firstitem open).
The library activates automatically when it detects the rt-accordion (or data-rt-accordion) attribute on a wrapper element in the DOM. If this attribute is not found, the script remains completely dormant to save resources.
You can use either standard rt-accordion-* attributes or strict HTML5 data-rt-accordion-* attributes.
<div rt-accordion rt-accordion-mode="multiple" rt-accordion-default-open="all">
...
</div>| Attribute | Description |
|---|---|
rt-accordion |
Enables accordion behavior on the wrapper. |
rt-accordion-id |
Optional unique identifier (auto-generated if missing). |
rt-accordion-mode |
Defines toggle behavior. Values: single (default) or multiple. |
rt-accordion-default-open |
Defines initial state. Values: first (default), all, or none. |
<div rt-accordion-item rt-accordion-open>
<button rt-accordion-trigger>
Read More <span rt-accordion-expanded-text="Read Less"></span>
</button>
<div rt-accordion-content>...</div>
</div>| Attribute | Description |
|---|---|
rt-accordion-item |
Marks the wrapper for an individual accordion row/item. |
rt-accordion-trigger |
Marks the clickable button that toggles the content. |
rt-accordion-content |
Marks the collapsible content container. |
rt-accordion-open |
Forces a specific item to be open on initialization. |
rt-accordion-expanded-text |
Swaps text inside the trigger when the item is open. |
<div rt-accordion rt-accordion-options-json='{"key":"value"}'></div>Used for advanced configuration that doesn’t warrant individual attributes. (Reserved for future extendability).
rt-accordion supports multiple independent instances on the same page.
Each instance:
- Has its own configuration (e.g., one can be
singlemode, anothermultiple). - Is registered internally.
- Can be controlled individually via the API.
Once initialized, you can interact with the accordions programmatically:
window.rtAccordion;| Method | Description |
|---|---|
ids() |
Returns an array of all active accordion instance IDs. |
get(id) |
Returns the specific accordion instance object. |
refresh() |
Forces a recalculation of all active accordion heights (Resize-safe). |
destroy(id?) |
Removes all inline styles, ARIA tags, and event listeners. |
- Ensure the correct
rt-accordionand internalrt-accordion-item/rt-accordion-trigger/rt-accordion-contentattributes exist. - Confirm the script loaded successfully.
- Ensure
rt-accordion-contentelements do not have fixed heights in your CSS. - The script uses
ResizeObserverto smoothly handle inner content changes; ensure your CSS isn't forcing conflicting transitions.
- Check for conflicting scripts.
- Verify attribute spelling (e.g.,
rt-accordion-trigger, notrt-accordion-btn). - Confirm instance isolation if nesting accordions.
MIT License
Package: @rethink-js/rt-accordion
GitHub: https://github.com/Rethink-JS/rt-accordion
by Rethink JS
https://github.com/Rethink-JS