Bedform UIControls

Toggles

Choose from a group of tabs.

Toggles

*
boolean
*
string

Usage

import { TabButton, Toggles } from "@bedform/ui/toggles";
import { useEffect, useRef, useState } from "react";

const options = ["Day", "Night", "Always"];

export function DaylightToggles() {
  const [value, setValue] = useState("day");
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const toggles = ref.current;
    const listener = (event: Event) => {
      setValue((event.target as HTMLButtonElement).value);
    };

    toggles?.addEventListener("change", listener);
    return () => toggles?.removeEventListener("change", listener);
  }, []);

  return (
    <Toggles ref={ref} aria-label="Daylight cycle">
      {options.map((option) => (
        <TabButton
          key={option}
          type="button"
          value={option.toLowerCase()}
          selected={value === option.toLowerCase()}
        >
          {option}
        </TabButton>
      ))}
    </Toggles>
  );
}

A tab button dispatches a native change event when a click or the arrow, Home or End keys select it, and the event bubbles to Toggles. React's onChange does not fire for buttons, so listen through a ref as above; onClick alone misses keyboard selection.

Behavior

Toggles sets role="tablist", lays out its children, and applies the toggle palette to direct and nested Tab Buttons.
For more information, see Tab Button.

Accessibility

Give the tab group an accessible name. Arrow keys move through enabled options. Home and End select the first and last option.

On this page