Skip to content

Radio group

Styled native radios in a labelled group.

View as Markdown

Example

Installation

npx fizzy-ui add radio-group

Copies radio-group.ts into src/app/ui/ and installs what it needs. Run init once first.

Keyboard

KeyAction
← → ↑ ↓Move to and select the next radio

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.

  • Radios sharing a name form one Tab stop. Label the group with aria-label.

Source

The exact file add copies. It is yours to edit.

radio-group.ts
import { Directive } from '@angular/core';

/**
 * `<div fzRadioGroup aria-label="…">` around native radios sharing a `name`:
 * `<label><input type="radio" fzRadio name="plan" value="pro"> Pro</label>`. The browser gives
 * one Tab stop, arrows that move the selection, and forms support.
 */
@Directive({
  selector: '[fzRadioGroup]',
  host: { role: 'radiogroup', class: 'grid gap-2 text-sm' },
})
export class FzRadioGroup {}

@Directive({
  selector: 'input[type=radio][fzRadio]',
  host: {
    class:
      'inline-grid size-4 shrink-0 cursor-pointer appearance-none place-content-center rounded-full border border-primary bg-background shadow-xs outline-none ' +
      'focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 ring-offset-background ' +
      'disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive ' +
      "before:size-2 before:scale-0 before:rounded-full before:bg-primary before:content-[''] checked:before:scale-100",
  },
})
export class FzRadio {}

export const FZ_RADIO_GROUP = [FzRadioGroup, FzRadio] as const;