Components
Forms & Input
Toggle Group

Toggle Group

Use ToggleGroup to build consistent, accessible interfaces aligned with the shared design system.

Installation

1. Copy the source code:

  • Source in this monorepo: packages/ui/src/components/toggle-group.tsx
  • Place in your project: components/ui/toggle-group.tsx
  • Required utility: lib/utils.ts (for cn helper)

2. Update the import paths to match your project structure.


Usage

import { ToggleGroup, ToggleGroupItem } from "@hoag/ui/components/toggle-group";
 
export default function Demo() {
  return (
    <ToggleGroup
      type="single"
      defaultValue="center"
      aria-label="Text alignment"
    >
      <ToggleGroupItem value="left" aria-label="Left aligned">
        Left
      </ToggleGroupItem>
      <ToggleGroupItem value="center" aria-label="Center aligned">
        Center
      </ToggleGroupItem>
      <ToggleGroupItem value="right" aria-label="Right aligned">
        Right
      </ToggleGroupItem>
    </ToggleGroup>
  );
}

Import

import { ToggleGroup, ToggleGroupItem } from "@hoag/ui/components/toggle-group";

Basic Example

import { ToggleGroup, ToggleGroupItem } from "@hoag/ui/components/toggle-group";
 
export function ToggleGroupBasicExample() {
  return (
    <ToggleGroup
      type="single"
      defaultValue="center"
      aria-label="Text alignment"
    >
      <ToggleGroupItem value="left" aria-label="Left aligned">
        Left
      </ToggleGroupItem>
      <ToggleGroupItem value="center" aria-label="Center aligned">
        Center
      </ToggleGroupItem>
      <ToggleGroupItem value="right" aria-label="Right aligned">
        Right
      </ToggleGroupItem>
    </ToggleGroup>
  );
}

Composition Pattern

import { ToggleGroup, ToggleGroupItem } from "@hoag/ui/components/toggle-group";
 
export function ToggleGroupSection() {
  return (
    <ToggleGroup
      type="multiple"
      defaultValue={["bold"]}
      aria-label="Text styles"
    >
      <ToggleGroupItem value="bold" aria-label="Toggle bold">
        Bold
      </ToggleGroupItem>
      <ToggleGroupItem value="italic" aria-label="Toggle italic">
        Italic
      </ToggleGroupItem>
      <ToggleGroupItem value="underline" aria-label="Toggle underline">
        Underline
      </ToggleGroupItem>
    </ToggleGroup>
  );
}

Accessibility Checklist

  • Verify semantic roles and ARIA attributes for interactive behavior.
  • Keep keyboard navigation and focus visibility consistent in all states.
  • Ensure color contrast meets accessibility requirements in light and dark themes.
  • Provide screen reader context for dynamic state or content changes.

Testing Checklist

  • Add render tests for default and key variant states.
  • Add interaction tests for callbacks and state transitions.
  • Add visual regression checks for responsive and theme variations.
  • Cover edge states such as loading, empty, disabled, and error.

Production Tips

  • Wrap component logic in feature-level abstractions for domain behavior.
  • Keep visual variants centralized to avoid style drift across screens.
  • Reuse a single import path strategy for simpler refactors.
  • Mirror documented states in Storybook and QA checklists.

Named Exports

  • ToggleGroup
  • ToggleGroupItem

API Reference

For authoritative prop signatures and implementation details, inspect:

  • packages/ui/src/components/toggle-group.tsx

MIT 2026 © @hoag/ui