Skip to content
Ginga UI
Ginga UI
日本語

Getting started

  • Overview
  • Quick start
  • Theme generation
  • Custom rendering
  • All components

Forms

  • Button
  • Checkbox
  • Input
  • Radio
  • Questionnaire
  • SegmentedControl
  • Select
  • ComboBox
  • NumberField
  • SearchField
  • Slider
  • Switch
  • FormControl

Display

  • Accordion
  • Alert
  • AlertDialog
  • Badge
  • CodeBlock
  • Card
  • Dialog
  • Drawer
  • Image
  • List
  • Popover
  • Menu
  • Tooltip
  • Table
  • Tab
  • Toast
  • Trace

Typography

  • Heading
  • Paragraph

Navigation

  • Anchor

Layout

  • Box

Resources

  • Changelog
  • GitHub (opens in a new tab)

Logo: Twemoji (opens in a new tab) (CC-BY 4.0)

SegmentedControl

A row of buttons for choosing one option from a few. Suited to switching views or settings with two to five choices.

View in Storybook (opens in a new tab)

Installation

import { SegmentedControl } from "@ginga-ui/core";

Basic usage

Place SegmentedControlItem elements inside and give each an id. Specify the initial selection with defaultValue.

segmented-control.tsx
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";

export function BasicExample() {
  return (
    <SegmentedControl aria-label="Time range" defaultValue="week">
      <SegmentedControlItem id="day">Day</SegmentedControlItem>
      <SegmentedControlItem id="week">Week</SegmentedControlItem>
      <SegmentedControlItem id="month">Month</SegmentedControlItem>
    </SegmentedControl>
  );
}

Icons only

When an item shows only an icon, give it an aria-label.

segmented-control.tsx
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
import { LayoutGrid, List, Rows3 } from "lucide-react";

export function IconExample() {
  return (
    <SegmentedControl aria-label="View" defaultValue="grid">
      <SegmentedControlItem id="grid" aria-label="Grid">
        <LayoutGrid />
      </SegmentedControlItem>
      <SegmentedControlItem id="list" aria-label="List">
        <List />
      </SegmentedControlItem>
      <SegmentedControlItem id="compact" aria-label="Compact">
        <Rows3 />
      </SegmentedControlItem>
    </SegmentedControl>
  );
}

Size

Three sizes are available: sm, md, and lg.

segmented-control.tsx
segmented-control.module.css
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
import styles from "./segmented-control.module.css";

export function SizeExample() {
  return (
    <div className={styles.sizeRow}>
      <SegmentedControl aria-label="Sort order" defaultValue="new" size="sm">
        <SegmentedControlItem id="new">Newest</SegmentedControlItem>
        <SegmentedControlItem id="popular">Popular</SegmentedControlItem>
      </SegmentedControl>
      <SegmentedControl aria-label="Sort order" defaultValue="new" size="md">
        <SegmentedControlItem id="new">Newest</SegmentedControlItem>
        <SegmentedControlItem id="popular">Popular</SegmentedControlItem>
      </SegmentedControl>
      <SegmentedControl aria-label="Sort order" defaultValue="new" size="lg">
        <SegmentedControlItem id="new">Newest</SegmentedControlItem>
        <SegmentedControlItem id="popular">Popular</SegmentedControlItem>
      </SegmentedControl>
    </div>
  );
}

Controlled

Use value and onChange to manage the selection in state.

Selected: Monthly

segmented-control.tsx
import { useState } from "react";
import { Paragraph, SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";

export function ControlledExample() {
  const [plan, setPlan] = useState("monthly");

  return (
    <div>
      <SegmentedControl
        aria-label="Billing period"
        value={plan}
        onChange={setPlan}
      >
        <SegmentedControlItem id="monthly">Monthly</SegmentedControlItem>
        <SegmentedControlItem id="yearly">Yearly</SegmentedControlItem>
      </SegmentedControl>
      <Paragraph>
        Selected: {plan === "monthly" ? "Monthly" : "Yearly"}
      </Paragraph>
    </div>
  );
}

Props

SegmentedControl

selectionMode, disallowEmptySelection, selectedKeys, defaultSelectedKeys, and onSelectionChange cannot be passed. Use value and onChange instead.

NameTypeDefaultDescription
valuestring-id of the selected item (controlled).
defaultValuestring-id of the item selected initially (uncontrolled).
onChange(value: string) => void-Called with the id of the newly selected item.
size"sm" | "md" | "lg""md"Size of the control.
disabledbooleanfalseDisables every item.

Also accepts the props of React Aria's ToggleButtonGroup (opens in a new tab).

SegmentedControlItem

NameTypeDefaultDescription
idstring-Required. Value of the item, used by value and onChange.
disabledbooleanfalseDisables the item.

Also accepts the props of React Aria's ToggleButton (opens in a new tab).