Accordion
Accordions are vertically stacked expandable items. They reduce clutter by showing only essential information initially and revealing details on demand.
When to use
- To organize and present large amounts of content in a compact way
- For FAQs (Frequently Asked Questions)
- To create collapsible sections in forms or settings
- When you need to reduce cognitive load by hiding secondary information
Anatomy
- Container: Wraps all accordion items
- Item: Individual collapsible section
- Trigger: Clickable header that toggles content
- Content: Hidden/shown area containing detailed information
- Icon: Visual indicator showing open/closed state
Basic Accordion
PM7 UI is a modern, accessible component library designed for building beautiful user interfaces. It provides a comprehensive set of customizable components that work seamlessly across different frameworks.
You can install PM7 UI using npm or yarn:
npm install @pm7/core
# or
yarn add @pm7/core
Yes! PM7 UI is built with accessibility in mind. All components follow WAI-ARIA guidelines and are keyboard navigable, screen reader friendly, and support reduced motion preferences.
Multiple Open Items
- Modern design system
- Fully customizable
- Dark mode support
- TypeScript support
- Faster development
- Consistent UI/UX
- Accessible by default
- Great developer experience
Accordion Variants
No Separator
This accordion doesn't have a separator line between the header and content when expanded.
The content flows directly from the header without visual separation.
Flush Variant
Manage your account preferences, security settings, and profile information.
Control your privacy settings, manage data sharing, and configure security options.
Size Variants
Ultra-compact design with minimal padding and smallest font size. Perfect for very tight spaces or mobile interfaces.
Minimal visual footprint for maximum content density.
Compact design with smaller padding, font size, and icon. Perfect for sidebars or dense information layouts.
Less visual weight for secondary content.
Standard size with balanced padding and typography. This is the default size used when no size modifier is specified.
Ideal for most use cases and content types.
Spacious design with larger padding, font size, and icon. Great for important content or when you need more visual emphasis.
More prominent presentation for primary content areas.
Icon Start Position
Text Alignment
This accordion has center-aligned text in the header.
This accordion has right-aligned text in the header.
Height Variants
This accordion has a maximum height of 200px. When content exceeds this height, it becomes scrollable with a custom scrollbar.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.
This accordion has a maximum height of 400px. Perfect for medium-sized content that needs scrolling.
- Feature 1: Lorem ipsum dolor sit amet
- Feature 2: Consectetur adipiscing elit
- Feature 3: Sed do eiusmod tempor incididunt
- Feature 4: Ut labore et dolore magna aliqua
- Feature 5: Ut enim ad minim veniam
- Feature 6: Quis nostrud exercitation ullamco
- Feature 7: Laboris nisi ut aliquip ex ea
- Feature 8: Commodo consequat duis aute irure
- Feature 9: Dolor in reprehenderit in voluptate
- Feature 10: Velit esse cillum dolore eu fugiat
Additional content to demonstrate scrolling behavior in medium height accordions.
This accordion has a maximum height of 600px. Perfect for large content sections that still need a reasonable limit.
Complete Feature List
- Core Features: Modern design system, fully customizable components, comprehensive theming
- Developer Experience: TypeScript support, tree-shaking ready, zero dependencies
- Performance: Optimized bundle size, lazy loading support, efficient rendering
- Accessibility: WCAG 2.1 compliant, keyboard navigation, screen reader support
- Customization: CSS variables, theme inheritance, component composition
- Documentation: Comprehensive guides, live examples, API reference
- Testing: Unit tests, integration tests, visual regression tests
- Browser Support: Chrome, Firefox, Safari, Edge (latest 2 versions)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod, nisl eget consectetur sagittis, nisi nisl aliquam est, eget consectetur nisl nisl sit amet nisl.
Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum.
This accordion has a fixed height of 250px. You can set any custom height using the data-fixed-height
attribute.
// Example configuration
const accordion = new PM7Accordion(element, {
height: 'fixed',
fixedHeight: 250
});
This is useful when you need precise control over the accordion's expanded height, regardless of content size.
The content will scroll if it exceeds the fixed height, maintaining a consistent layout.
Installation
npm install @pm7/core
CSS Classes
Class | Description |
---|---|
Base Classes | |
.pm7-accordion |
Container for accordion items |
.pm7-accordion-item |
Individual accordion section |
.pm7-accordion-trigger |
Clickable header button |
.pm7-accordion-content |
Collapsible content area |
.pm7-accordion-content-inner |
Inner wrapper for content padding |
.pm7-accordion-icon |
Chevron icon for open/close state |
Variants | |
.pm7-accordion--flush |
Removes borders and background |
Size Modifiers | |
.pm7-accordion--xs |
Extra small size with minimal padding and 14px icon |
.pm7-accordion--sm |
Small size with reduced padding, font size, and 16px icon |
.pm7-accordion--md |
Medium size (default) with balanced proportions |
.pm7-accordion--lg |
Large size with increased padding, font size, and icon |
.pm7-accordion--compact |
Legacy: Same as .pm7-accordion--sm (deprecated) |
.pm7-accordion--separated |
Adds spacing between items |
.pm7-accordion--icon-start |
Positions the expand/collapse icon at the start |
.pm7-accordion--text-left |
Left-aligns the header text (default) |
.pm7-accordion--text-center |
Center-aligns the header text |
.pm7-accordion--text-right |
Right-aligns the header text |
.pm7-accordion--no-separator |
Removes the separator line between header and content |
Height Variants | |
.pm7-accordion--height-sm |
Small height limit (200px max) with scrollbar |
.pm7-accordion--height-md |
Medium height limit (400px max) with scrollbar |
.pm7-accordion--height-lg |
Large height limit (600px max) with scrollbar |
.pm7-accordion--height-fixed |
Fixed height with custom value via CSS variable |
Data Attributes
Attribute | Type | Default | Description |
---|---|---|---|
data-pm7-accordion |
Boolean | - | Enables automatic initialization |
data-allow-multiple |
Boolean | false | Allow multiple items open simultaneously |
data-default-open |
Number|"all" | null | Index of item(s) to open by default |
data-state |
"open"|"closed" | "closed" | Current state of accordion item |
data-icon-position |
"start"|"end" | "end" | Position of the expand/collapse icon |
data-text-align |
"left"|"center"|"right" | "left" | Text alignment in the header |
data-height |
"sm"|"md"|"lg"|"fixed" | null | Height limit for accordion content |
data-fixed-height |
Number | 300 | Custom height in pixels when using "fixed" height |
Basic Usage
<!-- Default accordion -->
<div class="pm7-accordion" data-pm7-accordion>
<div class="pm7-accordion-item">
<button class="pm7-accordion-trigger">
<span>Accordion Header</span>
<svg class="pm7-accordion-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M19 9l-7 7-7-7"/>
</svg>
</button>
<div class="pm7-accordion-content">
<div class="pm7-accordion-content-inner">
<p>Your content here</p>
</div>
</div>
</div>
</div>
<!-- Accordion with icon at start -->
<div class="pm7-accordion" data-pm7-accordion data-icon-position="start">
<!-- Accordion items -->
</div>
<!-- Or using CSS class directly -->
<div class="pm7-accordion pm7-accordion--icon-start" data-pm7-accordion>
<!-- Accordion items -->
</div>
<!-- Accordion with height limit -->
<div class="pm7-accordion" data-pm7-accordion data-height="sm">
<!-- Accordion items with small height limit (200px) -->
</div>
<!-- Accordion with fixed custom height -->
<div class="pm7-accordion" data-pm7-accordion data-height="fixed" data-fixed-height="350">
<!-- Accordion items with fixed height of 350px -->
</div>
JavaScript API
import { PM7Accordion } from '@pm7/core';
// Manual initialization
const accordionElement = document.querySelector('.pm7-accordion');
const accordion = new PM7Accordion(accordionElement, {
allowMultiple: true,
defaultOpen: 0, // Open first item by default
iconPosition: 'start', // Position icon at the start
textAlign: 'center', // Center align header text
height: 'md', // Set height limit ('sm', 'md', 'lg', 'fixed', or null)
fixedHeight: 350 // Custom height when using 'fixed' option
});
// Methods
accordion.open(0); // Open specific item
accordion.close(0); // Close specific item
accordion.openAll(); // Open all items
accordion.closeAll(); // Close all items
accordion.toggle(0); // Toggle specific item
React Usage
import { Accordion } from '@pm7/react';
function App() {
return (
<Accordion allowMultiple defaultOpen={0}>
<Accordion.Item>
<Accordion.Trigger>Section 1</Accordion.Trigger>
<Accordion.Content>
Content for section 1
</Accordion.Content>
</Accordion.Item>
<Accordion.Item>
<Accordion.Trigger>Section 2</Accordion.Trigger>
<Accordion.Content>
Content for section 2
</Accordion.Content>
</Accordion.Item>
</Accordion>
);
}
Accessibility
- Keyboard navigation with Tab, Enter, and Space keys
- Proper ARIA attributes (aria-expanded)
- Focus management and visible focus indicators
- Screen reader announcements for state changes
Copy this link that can be used as documentation for working with this component:
🤖 AI-Optimized Documentation
This link contains complete Markdown documentation that is perfectly readable by AI assistants, developers, and other automated systems. Directly accessible on GitHub - including all essential PM7 Accordion implementation guidance.
Perfect for ChatGPT, Claude, and other AI code assistants