Listbox
An inline list of options with single or multiple selection.
View as MarkdownExample
- Preview
- Code
Installation
npx fizzy-ui add listbox Copies listbox.ts into src/app/ui/ and installs what it needs. Run init once first.
Keyboard
| Key | Action |
|---|---|
| ↓ / ↑ | Move through options |
| Home / End | First or last option |
| Space | Toggle the option (explicit mode) |
| Type | Jump to an option by label |
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.
- Give the list an aria-label or aria-labelledby.
Testing
FzListboxHarness drives it in your tests, the way a user would, with the Angular CDK harness API. --harness also copies listbox.harness.ts; its methods are documented in the file.
npx fizzy-ui add listbox --harnessimport { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { FzListboxHarness } from './ui/listbox.harness';
const listbox = await TestbedHarnessEnvironment.loader(fixture).getHarness(FzListboxHarness);Source
The exact file add copies. It is yours to edit.
listbox.ts
import { Directive } from '@angular/core';
import { Listbox, Option } from '@angular/aria/listbox';
@Directive({
selector: '[fzListbox]',
hostDirectives: [
{
directive: Listbox,
inputs: ['value', 'multi', 'orientation', 'selectionMode', 'disabled', 'readonly'],
outputs: ['valueChange'],
},
],
host: {
class: 'flex flex-col gap-0.5 rounded-md border bg-background p-1 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring',
},
})
export class FzListbox {}
@Directive({
selector: '[fzOption]',
hostDirectives: [{ directive: Option, inputs: ['value', 'label', 'disabled'] }],
host: {
class:
'flex cursor-default select-none items-center justify-between rounded-sm px-2 py-1.5 outline-none ' +
'data-[active=true]:bg-accent data-[active=true]:text-accent-foreground aria-disabled:opacity-50 ' +
"aria-selected:font-medium aria-selected:after:content-['✓']",
},
})
export class FzOption {}
export const FZ_LISTBOX = [FzListbox, FzOption] as const;