🎹 An accessible accordion in <1kb
- 🔬 Tiny (<1kb gzipped)
- 💕 Accessible by default
- ✨ Easy to style and animate
- ⚙️ Highly configurable
npm i sqzbx
Apply data-sqzbx-button
and data-sqzbx-panel
attributes as illustrated below. sqzbx
doesn't care about the structure of your markup as long as the following are true:
- There are an equal number of
data-sqzbx-button
elements anddata-sqzbx-panel
elements - All
data-sqzbx-button
elements are actually buttons - All
data-sqzbx-panel
elements contain exactly 1 child element
<div class="accordion">
<button data-sqzbx-button>Button 1</button>
<div data-sqzbx-panel>
<p>Panel 1</p>
</div>
<button data-sqzbx-button>Button 2</button>
<div data-sqzbx-panel>
<p>Panel 2</p>
</div>
<button data-sqzbx-button>Button 3</button>
<div data-sqzbx-panel>
<p>Panel 3</p>
</div>
</div>
Add some CSS. Below are the minimal recommended styles, but here is a more advanced example
[data-sqzbx-panel] {
overflow: hidden;
max-height: 0;
}
[data-sqzbx-panel][aria-hidden='true'] {
max-height: 0 !important;
}
Initialize and mount sqzbx
import sqzbx from 'sqzbx'
const element = document.querySelector('.accordion')
const accordion = sqzbx(element)
accordion.mount()
sqzbx
takes an options object as its second parameter
sqzbx(element, options)
default:
false
When multiple
is set to true
, any number of panels may be open at the same time. By default, when a user opens a new accordion item, the previously open item will collapse.
default:
false
When collapsible
is set to true
, panels that are already open may be collapsed. By default, open panels only collapse when a user opens a different one.
default:
null
Sets the initially expanded accordion item.
default:
true
In order to make animation easy, sqzbx
measures the height of the firstElementChild
for each panel and automatically updates these values on window resize. Set resize
to false
to remove this automatic resize behavior and call the .resize()
method manually.
Fired immediately after a user selects a new accordion item.
accordion.on('expand', ({ index, button, panel, open }) => {})
Fired immediately after an accordion item is collapsed.
accordion.on('collapse', ({ index, button, panel, open }) => {})
Fired on expanding panel transitionend
(which means that this event only fires if the panel has a CSS transition).
accordion.on('expanded', ({ index, button, panel, open }) => {})
Fired on collapsing panel transitionend
(which means that this event only fires if the panel has a CSS transition).
accordion.on('collapsed', ({ index, button, panel, open }) => {})
The callback receives an accordion item object containing index
of the item, button
element, panel
element, and an open
boolean. Returns a function to unsubscribe from the event.
const offExpand = accordion.on('expand', ({ index, button, panel, open }) => {})
offExpand() // removes expand event listener
Sets up all aria attributes and attaches event listeners. Must be called after initializing an instance of sqzbx
. Returns an unmount
function to remove all event listeners which is useful for sites with client-side navigation.
let unmount
function init() {
unmount = accordion.mount()
}
function destroy() {
unmount()
}
Manually update the panel height. Useful when paired with the resize
option set to false
to disable the automatic resizing behavior and manage this yourself.
import sqzbx from 'sqzbx'
const element = document.querySelector('.accordion')
const accordion = sqzbx(element, { resize: false })
window.addEventListener('resize', accordion.resize)
accordion.mount()