Collapsible
Shows and hides one section of content.
View as MarkdownExample
- Preview
- Code
Installation
npx fizzy-ui add collapsible Copies collapsible.ts into src/app/ui/ and installs what it needs. Run init once first.
Keyboard
| Key | Action |
|---|---|
| Enter / Space | Open or close |
Accessibility
No axe-core violations axe-core 4.14.0 checks this example, closed and with its panels open, on every build; the site is not published otherwise.
- A disclosure: the trigger is a button with aria-expanded and aria-controls.
- Closed content is inert, so it is out of the tab order and hidden from screen readers.
- An icon-only trigger needs an aria-label.
Testing
FzCollapsibleHarness drives it in your tests, the way a user would, with the Angular CDK harness API. --harness also copies collapsible.harness.ts; its methods are documented in the file.
npx fizzy-ui add collapsible --harnessimport { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { FzCollapsibleHarness } from './ui/collapsible.harness';
const collapsible = await TestbedHarnessEnvironment.loader(fixture).getHarness(FzCollapsibleHarness);Source
The exact file add copies. It is yours to edit.
collapsible.ts
import { Component, Directive, booleanAttribute, inject, input, model } from '@angular/core';
let nextId = 0;
/**
* Shows and hides one section: `<div fzCollapsible [(open)]="open">` with your own
* `<button fzCollapsibleTrigger>` and `<fz-collapsible-content>`. A disclosure: the button has
* `aria-expanded` and `aria-controls`; closed content is inert. For several sections, use accordion.
*/
@Directive({ selector: '[fzCollapsible]', exportAs: 'fzCollapsible', host: { class: 'block' } })
export class FzCollapsible {
readonly open = model(false);
readonly disabled = input(false, { transform: booleanAttribute });
readonly contentId = `fz-collapsible-${nextId++}`;
toggle() {
if (!this.disabled()) this.open.update((o) => !o);
}
}
/** The button that opens and closes it. Style a chevron with `in-aria-expanded:rotate-180`. */
@Directive({
selector: 'button[fzCollapsibleTrigger]',
host: {
type: 'button',
'[attr.aria-expanded]': 'collapsible.open()',
'[attr.aria-controls]': 'collapsible.contentId',
'[disabled]': 'collapsible.disabled()',
'(click)': 'collapsible.toggle()',
},
})
export class FzCollapsibleTrigger {
protected readonly collapsible = inject(FzCollapsible);
}
@Component({
selector: 'fz-collapsible-content',
host: {
'[id]': 'collapsible.contentId',
'[inert]': '!collapsible.open()',
// Height animates through grid rows (0fr <-> 1fr), as in the accordion.
class:
'grid grid-rows-[1fr] transition-[grid-template-rows] duration-(--fz-duration-popup) ease-spring motion-reduce:transition-none ' +
'[&[inert]]:grid-rows-[0fr]',
},
// overflow-hidden clips during the animation: -mx-1 px-1 leaves room for focus rings.
template: `<div class="-mx-1 min-h-0 overflow-hidden px-1"><ng-content /></div>`,
})
export class FzCollapsibleContent {
protected readonly collapsible = inject(FzCollapsible);
}
export const FZ_COLLAPSIBLE = [FzCollapsible, FzCollapsibleTrigger, FzCollapsibleContent] as const;