Primitives
Display a series of panels that can be expanded or collapsed.
import { Component } from "@angular/core";
import { NgIcon, provideIcons } from "@ng-icons/core";
import { heroChevronDownMini } from "@ng-icons/heroicons/mini";
import {
NgpAccordion,
NgpAccordionContent,
NgpAccordionItem,
NgpAccordionTrigger,
} from "ng-primitives/accordion";
import { NgpButton } from "ng-primitives/button";
@Component({
selector: "app-accordion",
imports: [
NgpButton,
NgIcon,
NgpAccordion,
NgpAccordionItem,
NgpAccordionContent,
NgpAccordionTrigger,
],
providers: [provideIcons({ heroChevronDownMini })],
styles: `
:host {
display: flex;
justify-content: center;
width: 100%;
}
[ngpAccordion] {
width: 100%;
max-width: 24rem;
border-radius: 0.75rem;
border: 1px solid var(--ngp-border);
background-color: var(--ngp-background);
box-shadow: var(--ngp-shadow);
}
[ngpAccordionItem]:has(+ [ngpAccordionItem]) {
border-bottom: 1px solid var(--ngp-border);
}
[ngpAccordionTrigger] {
display: flex;
padding-left: 1rem;
padding-right: 1rem;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 500;
justify-content: space-between;
align-items: center;
width: 100%;
height: 2.75rem;
border-radius: 0.75rem;
outline: none;
color: var(--ngp-text-primary);
background-color: var(--ngp-background);
border: none;
}
[ngpAccordionTrigger][data-focus-visible] {
outline: 2px solid var(--ngp-focus-ring);
}
[ngpAccordionContent] {
font-size: 0.875rem;
color: var(--ngp-text-secondary);
overflow: hidden;
}
[ngpAccordionContent][data-open] {
animation: slideDown 0.2s ease-in-out forwards;
}
[ngpAccordionContent][data-closed] {
animation: slideUp 0.2s ease-in-out forwards;
}
.accordion-content {
padding: 0 16px 16px;
}
ng-icon {
font-size: 1.25rem;
color: var(--ngp-text-secondary);
}
[ngpAccordionTrigger][data-open] ng-icon {
transform: rotate(180deg);
}
@keyframes slideDown {
from {
height: 0;
}
to {
height: var(--ngp-accordion-content-height);
}
}
@keyframes slideUp {
from {
height: var(--ngp-accordion-content-height);
}
to {
height: 0;
}
}
`,
template: `
<div ngpAccordion ngpAccordionType="single" ngpAccordionCollapsible>
<div
#panel1="ngpAccordionItem"
ngpAccordionItem
ngpAccordionItemValue="item-1"
>
<button ngpAccordionTrigger ngpButton>
Would you like to learn more?
<ng-icon
[attr.data-open]="panel1.open()"
name="heroChevronDownMini"
/>
</button>
<div ngpAccordionContent>
<div class="accordion-content">
If you would like to learn more please reach out to us on GitHub.
</div>
</div>
</div>
<div
#panel2="ngpAccordionItem"
ngpAccordionItem
ngpAccordionItemValue="item-2"
>
<button ngpAccordionTrigger ngpButton>
Can I use this in my project?
<ng-icon
[attr.data-open]="panel2.open()"
name="heroChevronDownMini"
/>
</button>
<div ngpAccordionContent>
<div class="accordion-content">
Yes, this is open source and you can use it in your project.
</div>
</div>
</div>
</div>
`,
})
export default class AccordionExample {}
Import the Accordion primitives from ng-primitives/accordion
.
import {
NgpAccordion,
NgpAccordionItem,
NgpAccordionTrigger,
NgpAccordionContent,
} from 'ng-primitives/accordion';
Assemble the accordion directives in your template.
<div ngpAccordion ngpAccordionType="single" ngpAccordionCollapsible>
<div ngpAccordionItem ngpAccordionItemValue="item-1">
<button ngpAccordionTrigger ngpButton>Would you like to learn more?</button>
<div ngpAccordionContent>If you would like to learn more please reach out to us on GitHub.</div>
</div>
<div ngpAccordionItem ngpAccordionItemValue="item-2">
<button ngpAccordionTrigger ngpButton>Can I use this in my project?</button>
<div ngpAccordionContent>Yes, this is open source and you can use it in your project.</div>
</div>
</div>
Create reusable components that uses the NgpAccordion
and NgpAccordionItem
directives.
import { Component } from '@angular/core';
import { Accordion } from './accordion';
import { AccordionItem } from './accordion-item';
@Component({
selector: 'app-root',
imports: [Accordion, AccordionItem],
template: `
<app-accordion>
<app-accordion-item heading="Accordion 1" value="accordion-item-1">
<p>Accordion item 1</p>
</app-accordion-item>
<app-accordion-item heading="Accordion 2" value="accordion-item-2">
<p>Accordion item 2</p>
</app-accordion-item>
<app-accordion-item heading="Accordion 3" value="accordion-item-3">
<p>Accordion item 3</p>
</app-accordion-item>
</app-accordion>
`,
})
export default class App {}
Generate a reusable accordion component using the Angular CLI.
ng g ng-primitives:primitive accordion
path
: The path at which to create the component file.prefix
: The prefix to apply to the generated component selector.componentSuffix
: The suffix to apply to the generated component class name.fileSuffix
: The suffix to apply to the generated component file name. Defaults to component
.exampleStyles
: Whether to include example styles in the generated component file. Defaults to true
.The following directives are available to import from the ng-primitives/accordion
package:
Apply the `ngpAccordion` directive to an element that represents the group of accordion items. The type of the accordion.
Whether the accordion is collapsible.
The value of the accordion.
Whether the accordion is disabled.
The accordion orientation.
Event emitted when the accordion value changes.
[ngpAccordion]
ngpAccordion
The following data attributes are applied to the ngpAccordion
directive:
Attribute | Description | Value |
---|---|---|
data-orientation |
The orientation of the accordion. | horizontal | vertical |
data-disabled |
Applied when the element is disabled. | - |
Apply the `ngpAccordionItem` directive to an element that represents an accordion item. The value of the accordion item.
Whether the accordion item is disabled.
[ngpAccordionItem]
ngpAccordionItem
The following data attributes are applied to the ngpAccordionItem
directive:
Attribute | Description | Value |
---|---|---|
data-orientation |
The orientation of the accordion. | horizontal | vertical |
data-open |
Applied when the accordion item is open. | - |
data-disabled |
Applied when the accordion item is disabled. | - |
Apply the `ngpAccordionTrigger` directive to an element that represents the trigger for an accordion item, such as a button.[ngpAccordionTrigger]
ngpAccordionTrigger
The following data attributes are applied to the ngpAccordionTrigger
directive:
Attribute | Description | Value |
---|---|---|
data-orientation |
The orientation of the accordion. | horizontal | vertical |
data-open |
Applied when the accordion item is open. | - |
data-disabled |
Applied when the accordion item is disabled. | - |
Apply the `ngpAccordionContent` directive to an element that represents the content of an accordion item.[ngpAccordionContent]
ngpAccordionContent
The following data attributes are applied to the ngpAccordionContent
directive:
Attribute | Description | Value |
---|---|---|
data-orientation |
The orientation of the accordion. | horizontal | vertical |
data-open |
Applied when the accordion item is open. | - |
The following CSS custom properties are applied to the ngpAccordionContent
directive:
Property | Description |
---|---|
--ngp-accordion-content-width |
The width of the accordion content. |
--ngp-accordion-content-height |
The height of the accordion content. |
The ngpAccordionContent
primitive adds several CSS custom properties --ngp-accordion-content-width
and --ngp-accordion-content-height
to the element that can be used to animate the accordion content on open and close.
You can configure the default options for all accordions in your application by using the provideAccordionConfig
function in a providers array.
import { provideAccordionConfig } from 'ng-primitives/accordion';
bootstrapApplication(AppComponent, {
providers: [
provideAccordionConfig({
type: 'multiple',
collapsible: true,
orientation: 'horizontal',
}),
],
});
Define whether only one or multiple accordion items can be open at a time.
Define an accordion can be collapsed. This is only applicable when `type` is set to `single`.
Define the orientation of the accordion.
Adheres to the Accordion WAI-ARIA design pattern.
Copyright © 2025 Angular Primitives