Toggle group example

Single and multiple choices in a form.

Pressed buttons keep stable values. Native submission includes each selected value; reset restores the uncontrolled defaults.

Complete React client module · MIT · Next.js and Vite

Interactive example

Single and multiple choices in a form

Pressed buttons keep stable values. Native submission includes each selected value; reset restores the uncontrolled defaults.

Keyboard

  • Arrow keys move focus inside each group and skip disabled options.
  • Space or Enter toggles the focused option. Single groups hold at most one value.
  • Submit with no required alignment to reveal validation and focus the first choice.

States covered

  • Single selection
  • Multiple selection
  • Required group validation
  • Disabled choice
  • Native form values and reset

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-toggle-formatting.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, ToggleGroup } from '@bld8/ui';
import { AlignLeft, AlignCenter, AlignRight, Bold, Italic, Underline } from 'lucide-react';

export function FormattingChoicesExample() {
  const [result, setResult] = useState('Submit to inspect the form values.');
  return <form style={{ display: 'flex', flexDirection: 'column', gap: 20, width: '100%' }} onReset={() => setResult('Submit to inspect the form values.')} onSubmit={event => {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    setResult(JSON.stringify({ alignment: data.getAll('alignment'), format: data.getAll('format') }));
  }}>
    <ToggleGroup label="Text alignment" name="alignment" required defaultValue={['left']} options={[
      { value: 'left', label: 'Align left', icon: AlignLeft },
      { value: 'center', label: 'Align center', icon: AlignCenter },
      { value: 'right', label: 'Align right', icon: AlignRight },
    ]} hint="Choose one alignment." />
    <ToggleGroup label="Text formatting" name="format" multiple defaultValue={['bold']} options={[
      { value: 'bold', label: 'Bold text', icon: Bold },
      { value: 'italic', label: 'Italic text', icon: Italic },
      { value: 'underline', label: 'Underline text', icon: Underline, disabled: true },
    ]} hint="Choose any supported formatting options." />
    <div style={{ display: 'flex', alignItems: 'center', flexWrap: 'wrap', gap: 12 }}><Button type="submit">Apply formatting</Button><Button type="reset" variant="secondary">Reset formatting</Button></div>
    <output aria-label="Formatting form values" className="bui-hint">{result}</output>
  </form>;
}