Ir al contenido
Español

Cómo funciona

A recessed track holds the toggles. In single mode one raised thumb is measured under the pressed toggle and glides to the next with a soft spring while the labels only change colour, so nothing reflows. In multiple mode each pressed toggle rises on its own and pressed neighbours drop their inner corners, fusing into one continuous chip that reads as one active span.

Úsalo para

  • Text formatting such as bold, italic and underline (multiple).
  • Switching a view mode or alignment among two to five options (single).

Busca otra opción cuando

  • Switching between page sections: use tabs.
  • Options in a form that is submitted later: use radio buttons or checkboxes.

Uso

import { useState } from 'react'
import { ToggleGroup } from './ToggleGroup.jsx'

export function FormatBar() {
  const [marks, setMarks] = useState(['bold'])
  const [view, setView] = useState('edit')
  return (
    <>
      <ToggleGroup
        label="View mode"
        value={view}
        onValueChange={setView}
        items={[
          { value: 'edit', label: 'Edit' },
          { value: 'preview', label: 'Preview' },
        ]}
      />
      <ToggleGroup
        type="multiple"
        iconOnly
        label="Text formatting"
        value={marks}
        onValueChange={setMarks}
        items={[
          { value: 'bold', label: 'Bold', icon: <BoldIcon /> },
          { value: 'italic', label: 'Italic', icon: <ItalicIcon /> },
        ]}
      />
    </>
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
itemsArray<{ value: string, label: string, icon?: ReactNode, disabled?: boolean }>—The toggles. With iconOnly the label becomes the accessible name and tooltip.
type'single' | 'multiple''single'One pressed at a time, or any number.
valuestring | null | string[]—Controlled value: a string for single, an array for multiple.
defaultValuestring | string[]—Initial value when uncontrolled.
onValueChange(value) => void—Called with the new value.
allowEmptybooleanfalseSingle mode: pressing the pressed toggle again clears the choice.
iconOnlybooleanfalseSquare toggles showing only the icon.
size'sm' | 'md' | 'lg''md'Toggle height 32, 36 or 44px.
labelstring—Accessible name of the group.
disabledbooleanfalseDisables every toggle.

Accesibilidad

  • The container is role="group" with an accessible name from label; every toggle is a native button with aria-pressed.
  • One toggle is in the tab order (roving tabindex); arrow keys move focus and wrap, Home and End jump, disabled toggles are skipped, and left/right follow the reading direction.
  • Space or Enter presses the focused toggle; icon-only toggles get their label as aria-label and title.
  • Pressed state is shown by a raised surface and darker text, not by colour alone; toggles are 32px or taller.
  • Reduced motion stops the thumb gliding and the press scale; only colours change.

Movimiento

The single-mode thumb glides in 360ms with a soft spring (slight overshoot). Pressing scales a toggle to 96% for 100ms; corners and surfaces change in 160ms.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

ToggleGroup.jsxtoggle-group.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Botones
Botón de borde aurora viene con Pro. La vista previa aparece al desbloquearlo.
Botón con mordiscos viene con Pro. La vista previa aparece al desbloquearlo.