Accordion
<smart-accordion> is a collapsible section: a header button that toggles a body. The header and body are projected through the [accordionHeader] / [accordionBody] slots, and the open state is the two-way show model. The accordion has no injection token: <smart-accordion> always renders <smart-accordion-default>; for the styled look use <smart-accordion-preset> directly, and for a look of your own extend AccordionBaseComponent.
Usage
<smart-accordion [(show)]="open">
<span accordionHeader>What is your refund policy?</span>
<p accordionBody>You can request a full refund within 30 days of purchase.</p>
</smart-accordion>
The preview renders the code above in an application that registers every preset (provideSmartPresets(), see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Wrapper: <smart-accordion>
Inputs and models
| Input | Type | Default | Description |
|---|---|---|---|
show | ModelSignal<boolean> | false | Open state, two-way ([(show)]); showChange reports every change. |
options | IAccordionOptions | undefined | undefined | open (initial state, applied once) and disabled. |
class | string | '' | Classes appended to the container (the cssClass input, alias class). |
Content projection
| Selector | Description |
|---|---|
[accordionHeader] | Content of the header button. |
[accordionBody] | Content shown while the section is open. |
The wrapper passes both slots to <smart-accordion-default> as the headerTpl / bodyTpl templates.
IAccordionOptions
| Field | Type | Default | Description |
|---|---|---|---|
open | boolean | undefined | Initial open state. When it is true on the first render and show is not already true, AccordionBaseComponent.ngOnInit sets show to true once and emits showChange, in every variant. Later changes of options.open are ignored; toggles and [(show)] take over. |
disabled | boolean | false | toggle() does nothing and the header button is rendered disabled. |
animated | boolean | undefined | Deprecated: not read by the built-in implementations (no variant animates); available to a custom implementation. |
Bind [(show)] to a signal when you combine it with options.open: a plain class field updated during the first change detection raises NG0100 (ExpressionChangedAfterItHasBeenChecked) in dev mode.
Building blocks of the default rendering
<smart-accordion-default> (AccordionDefaultComponent) is a bordered card made of two exported components you can reuse:
| Component | Selector | Inputs |
|---|---|---|
AccordionHeaderComponent | smart-accordion-header | open (boolean, chevron up while true), disabled (boolean), cssClass (string) |
AccordionBodyComponent | smart-accordion-body | cssClass (string) |
AccordionPresetComponent (<smart-accordion-preset>)
A fully styled accordion based on the Preline bordered accordion. It extends AccordionBaseComponent, so it takes the same contract as AccordionDefaultComponent: the required headerTpl / bodyTpl template inputs, the show model, options and cssClass (bound as [cssClass]; the base input has no class alias).
Because there is no token, the preset cannot restyle <smart-accordion>, and provideSmartPresets() does not change it: render <smart-accordion-preset> where you want the styled look and supply the templates yourself.
Expand/collapse is driven by the inherited show model and @if; no Preline JS runtime is needed. The toggle button carries aria-expanded / aria-controls, the open body is a region, the chevron points down while collapsed and up while expanded, and the container border is visible only while open.
AccordionBaseComponent
The abstract directive every variant extends (AccordionDefaultComponent, AccordionPresetComponent, and yours).
Inputs
| Input | Type | Default | Description |
|---|---|---|---|
show | ModelSignal<boolean> | false | Two-way binding for open state |
options | InputSignal<IAccordionOptions | undefined> | undefined | Accordion configuration |
cssClass | InputSignal<string> | '' | External CSS classes |
headerTpl | InputSignal<TemplateRef<unknown>> | required | Header template |
bodyTpl | InputSignal<TemplateRef<unknown>> | required | Body template |
Computed properties
| Property | Type | Description |
|---|---|---|
sharedContainerClasses | Signal<string[]> | Divider, rounded, border classes with dark mode support |
Methods
| Method | Description |
|---|---|
toggle() | Toggles the show model (no-op while options.disabled) |
ngOnInit() | Applies options.open as the initial state; a subclass with its own ngOnInit calls super.ngOnInit() to keep it |
Extending the base class
import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
signal,
ViewEncapsulation,
} from '@angular/core';
import {
AccordionBaseComponent,
IAccordionOptions,
} from '@smartsoft001/angular';
@Component({
selector: 'docs-custom-accordion',
imports: [NgTemplateOutlet],
template: `
<div [class]="containerClasses()">
<button
type="button"
class="docs-accordion__header"
[disabled]="options()?.disabled ?? false"
[attr.aria-expanded]="show()"
(click)="toggle()"
>
<ng-container [ngTemplateOutlet]="headerTpl()" />
<span aria-hidden="true">{{ show() ? '-' : '+' }}</span>
</button>
@if (show()) {
<div class="docs-accordion__body">
<ng-container [ngTemplateOutlet]="bodyTpl()" />
</div>
}
</div>
`,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CustomAccordionComponent extends AccordionBaseComponent {
containerClasses = computed(() =>
['docs-accordion', ...this.sharedContainerClasses(), this.cssClass()]
.filter(Boolean)
.join(' '),
);
}
// The accordion has no standard-component token, so a custom implementation is
// rendered directly by its own selector instead of being injected into
// <smart-accordion>. The required headerTpl / bodyTpl inputs are <ng-template>
// references supplied by the host.
@Component({
selector: 'docs-accordion-custom-example',
imports: [CustomAccordionComponent],
template: `
<ng-template #headerTpl
>What is the best thing about Switzerland?</ng-template
>
<ng-template #bodyTpl
>I don't know, but the flag is a big plus.</ng-template
>
<docs-custom-accordion
[headerTpl]="headerTpl"
[bodyTpl]="bodyTpl"
[(show)]="open"
[options]="options"
/>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AccordionCustomExampleComponent {
open = signal(false);
options: IAccordionOptions = { disabled: false };
}
Source
The component lives in packages/shared/angular/src/lib/components/accordion and is documented for Claude Code by the angular-components-accordion skill.
SmartAccordion is a collapsible section: a header button that toggles a body. The header and body are ReactNode slots (accordionHeader, accordionBody). The open state is either controlled (show + onShowChange) or kept inside the component (defaultShow, or options.open applied once on the first render). The accordion has no registry key: SmartAccordion always renders SmartAccordionDefault; for the styled look render SmartAccordionPreset directly.
Usage
import { useState } from 'react';
import { SmartAccordion } from '@smartsoft001/react';
export function AccordionUsageExample() {
const [open, setOpen] = useState(false);
return (
<SmartAccordion
show={open}
onShowChange={setOpen}
accordionHeader={<span>What is your refund policy?</span>}
accordionBody={
<p>You can request a full refund within 30 days of purchase.</p>
}
/>
);
}
The preview renders the code above in an application that registers every preset (SMART_PRESET_COMPONENTS on SmartProvider, see Installation) and the English translations; without the presets, a component that has one renders its standard implementation instead, for most components unstyled markup.
Exports
All from @smartsoft001/react.
| Export | Kind | What it is |
|---|---|---|
SmartAccordion | component | SmartAccordionDefault fed with the accordionHeader / accordionBody slots. |
SmartAccordionBody | component | The padded content of an open accordion. |
SmartAccordionDefault | component | The default accordion rendering: a bordered card whose header toggles the body. |
SmartAccordionHeader | component | The header button of SmartAccordionDefault, with a chevron pointing up while open. |
SmartAccordionPreset | component | Styled accordion variation (preset), based on the Preline bordered accordion. |
useAccordion | hook | The behaviour every accordion variant shares: the open state, controlled through show + onShowChange or kept internally, and toggle(), which does nothing while options.disabled. |
The preset's class helpers (getAccordionPresetContainerClasses, getAccordionPresetToggleClasses, getAccordionPresetContentClasses, getAccordionPresetIconClasses) are exported too, for a custom implementation that wants the preset look.
Props and Types
SmartAccordionProps
Props of SmartAccordion. Extends SmartAccordionStateProps.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | — | Classes appended to the container. |
accordionHeader? | ReactNode | — | Content of the header button. |
accordionBody? | ReactNode | — | Content shown while open. |
SmartAccordionStateProps
The open state every accordion shares: show and options. Pass show + onShowChange to control it; leave show undefined to let the accordion keep its own state, starting from defaultShow (or options.open).
| Prop | Type | Default | Description |
|---|---|---|---|
show? | boolean | — | Controlled open state; undefined keeps the state internal. |
defaultShow? | boolean | false | Initial open state when uncontrolled. |
onShowChange? | (show: boolean) => void | — | Every change of show: toggles and the initial options.open. |
options? | IAccordionOptions | — | open (initial state, applied once) and disabled (the header ignores clicks). |
SmartAccordionBaseProps
Props of the accordion variations (SmartAccordionDefault, SmartAccordionPreset): the slots are headerTpl / bodyTpl instead of accordionHeader / accordionBody. Extends SmartAccordionStateProps.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | — | Classes appended to the container. |
headerTpl | ReactNode | required | Content of the header button. |
bodyTpl | ReactNode | required | Content shown while open. |
SmartAccordionHeaderProps
Props of SmartAccordionHeader, the header button of the default variation.
| Prop | Type | Default | Description |
|---|---|---|---|
open? | boolean | false | Chevron up while true. |
disabled? | boolean | false | Renders the button disabled. |
className? | string | — | Classes appended to the button. |
children? | ReactNode | — | Content of the button. |
SmartAccordionBodyProps
Props of SmartAccordionBody, the padded body of the default variation.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | — | Classes appended to the body. |
children? | ReactNode | — | Content of the body. |
IAccordionOptions
| Field | Type | Default | Description |
|---|---|---|---|
open? | boolean | — | Initial open state, applied once on the first render by every variant: an uncontrolled accordion starts open, and onShowChange(true) is reported unless show / defaultShow was already true. Later changes to this flag are ignored; toggles and the show prop take over. A controlled accordion stays as its show prop says until the parent applies the reported change. |
disabled? | boolean | false | Prevents toggle() from changing show (the header ignores clicks). |
animated? | boolean | — | @deprecated No accordion variant (default or preset) animates anything; this flag has no effect. |
Controlled and uncontrolled
- Uncontrolled: leave
showundefined. The accordion keeps its own state, starting fromdefaultShow(defaultfalse) oroptions.open.onShowChangestill reports every toggle. - Controlled: pass
showand update it fromonShowChange. A toggle only callsonShowChange(next); the accordion stays asshowsays until the parent changes it. options.openis read once, on the first render. When it istrueand the initial state is closed, the accordion reportsonShowChange(true)once. Later changes ofoptions.openare ignored.options.disabledmakes the header ignore clicks (the header is rendered disabled).
Replacing the Implementation
The accordion has no SmartProvider registry key, so it cannot be swapped application-wide. Pick the variation where you render it:
SmartAccordion(slotsaccordionHeader/accordionBody) rendersSmartAccordionDefault, a bordered card withSmartAccordionHeaderandSmartAccordionBody.SmartAccordionPreset(slotsheaderTpl/bodyTpl) is the styled variation, based on the Preline bordered accordion: the toggle button carriesaria-expanded/aria-controlsand the open body is aregion.- For a look of your own, write a component on
useAccordionand render it in place ofSmartAccordion.
The useAccordion hook
The behaviour every accordion variant shares: the open state, controlled through show + onShowChange or kept internally, and toggle(), which does nothing while options.disabled. options.open is applied once, on the first render: an uncontrolled accordion starts open, and onShowChange(true) is reported unless show / defaultShow was already true. A controlled accordion stays as its show prop says until the parent applies that change. Later changes of options.open are ignored.
| Returns | Type | Description |
|---|---|---|
show | boolean | The current open state (the show prop when controlled). |
toggle | () => void | Flips the state and reports it through onShowChange; does nothing while options.disabled. |
sharedContainerClasses | string[] | The border and divider classes (with smart:dark: variants) of the default container. |
import { useState } from 'react';
import {
IAccordionOptions,
SmartAccordionBaseProps,
useAccordion,
} from '@smartsoft001/react';
export function CustomAccordion(props: SmartAccordionBaseProps) {
const { options, className, headerTpl, bodyTpl } = props;
const { show, toggle, sharedContainerClasses } = useAccordion(props);
return (
<div
className={['docs-accordion', ...sharedContainerClasses, className]
.filter(Boolean)
.join(' ')}
>
<button
type="button"
className="docs-accordion__header"
disabled={options?.disabled ?? false}
aria-expanded={show}
onClick={toggle}
>
{headerTpl}
<span aria-hidden="true">{show ? '-' : '+'}</span>
</button>
{show && <div className="docs-accordion__body">{bodyTpl}</div>}
</div>
);
}
const options: IAccordionOptions = { disabled: false };
// The accordion has no SmartProvider registry key, so a custom implementation
// is rendered directly in place of <SmartAccordion>.
export function AccordionCustomExample() {
const [open, setOpen] = useState(false);
return (
<CustomAccordion
headerTpl="What is the best thing about Switzerland?"
bodyTpl="I don't know, but the flag is a big plus."
show={open}
onShowChange={setOpen}
options={options}
/>
);
}
Styling
- Tailwind 4 classes with the
smart:prefix; the default container usesuseAccordion().sharedContainerClassesand the preset its owngetAccordionPreset*Classes, both withsmart:dark:variants. classNameis appended to the container in both variations.options.animatedis deprecated and has no effect.
Source
The component lives in packages/shared/react/src/lib/components/accordion and is documented for Claude Code by the react-components-accordion skill.