Accordion
Build an accessible accordion component with smooth animations and keyboard support
What We're Building
Component Code
accordion.ts
import { bindable } from 'aurelia';
export class Accordion {
@bindable allowMultiple = false;
@bindable openPanels: number[] = [];
togglePanel(index: number) {
if (this.allowMultiple) {
// Multiple panels can be open
const panelIndex = this.openPanels.indexOf(index);
if (panelIndex > -1) {
this.openPanels.splice(panelIndex, 1);
} else {
this.openPanels.push(index);
}
} else {
// Only one panel can be open
if (this.isPanelOpen(index)) {
this.openPanels = [];
} else {
this.openPanels = [index];
}
}
}
isPanelOpen(index: number): boolean {
return this.openPanels.includes(index);
}
}accordion.html
accordion-panel.ts
accordion-panel.html
accordion.css
Usage Examples
Basic Accordion (Single Panel Open)
Multiple Panels Open
Controlled Open Panels
With Rich Content
Testing
Accessibility Features
Enhancements
1. Add Animation Callbacks
2. Add Icons
3. Add Lazy Loading
Best Practices
Summary
Last updated
Was this helpful?