React
Segmented Control
A component used to select one of multiple options to immediately filter or switch related content.
Available since@seed-design/react@0.0.1, @seed-design/css@0.0.1
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
export default function SegmentedControlPreview() {
return (
<SegmentedControl defaultValue="Hot" aria-label="Sort by">
<SegmentedControlItem value="Hot">Hot</SegmentedControlItem>
<SegmentedControlItem value="New">New</SegmentedControlItem>
</SegmentedControl>
);
}Installation
npx @seed-design/cli@latest add ui:segmented-controlpnpm dlx @seed-design/cli@latest add ui:segmented-controlyarn dlx @seed-design/cli@latest add ui:segmented-controlbun x @seed-design/cli@latest add ui:segmented-controlInstall dependencies
npm install @seed-design/reactyarn add @seed-design/reactpnpm add @seed-design/reactbun add @seed-design/reactCopy the code below and paste it in
/**
* @file ui:segmented-control
* @requires @seed-design/react@^2.0.0
* @requires @seed-design/css@^2.0.0
**/
"use client";
import { HStack, SegmentedControl as SeedSegmentedControl } from "@seed-design/react";
import { NotificationBadgePositioner, NotificationBadge } from "@seed-design/react";
import * as React from "react";
export interface SegmentedControlProps extends SeedSegmentedControl.RootProps {}
/**
* @see https://crispy.carrot-soft.tech/react/components/segmented-control
*/
export const SegmentedControl = React.forwardRef<HTMLDivElement, SegmentedControlProps>(
({ children, ...otherProps }, ref) => {
if (!otherProps["aria-label"] && !otherProps["aria-labelledby"]) {
console.warn(
"SegmentedControl component requires either an `aria-label` or `aria-labelledby` attribute.",
);
}
if (otherProps.value === undefined && otherProps.defaultValue === undefined) {
console.warn(
"SegmentedControl component requires either a `value` or `defaultValue` attribute.",
);
}
return (
<SeedSegmentedControl.Root ref={ref} {...otherProps}>
{children}
<SeedSegmentedControl.Indicator />
</SeedSegmentedControl.Root>
);
},
);
SegmentedControl.displayName = "SegmentedControl";
export interface SegmentedControlItemProps extends SeedSegmentedControl.ItemProps {
inputProps?: React.InputHTMLAttributes<HTMLInputElement>;
rootRef?: React.Ref<HTMLLabelElement>;
notification?: boolean;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/segmented-control#segmentedcontrolitem
*/
export const SegmentedControlItem = React.forwardRef<HTMLInputElement, SegmentedControlItemProps>(
({ children, inputProps, rootRef, notification, ...otherProps }, ref) => {
return (
<SeedSegmentedControl.Item ref={rootRef} {...otherProps}>
<SeedSegmentedControl.ItemHiddenInput ref={ref} {...inputProps} />
{notification ? (
<HStack position="relative" align="flex-start">
{children}
<NotificationBadgePositioner size="small" attach="text">
<NotificationBadge />
</NotificationBadgePositioner>
</HStack>
) : (
children
)}
</SeedSegmentedControl.Item>
);
},
);
SegmentedControlItem.displayName = "SegmentedControlItem";
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
Props
SegmentedControl
You must provide at least one of value or defaultValue.
Prop
Type
disabled?boolean | undefinedname?string | undefinedform?string | undefinedvalue?string | undefineddefaultValue?string | undefinedonValueChange?((value: string) => void) | undefinedSegmentedControlItem
Prop
Type
inputProps?React.InputHTMLAttributes<HTMLInputElement> | undefinedrootRef?React.Ref<HTMLLabelElement> | undefinednotification?boolean | undefinedvaluestringdisabled?boolean | undefinedinvalid?boolean | undefinedExamples
Disabled
import { VStack } from "@seed-design/react";
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
export default function SegmentedControlPreview() {
return (
<VStack align="center" gap="spacingY.componentDefault">
<SegmentedControl defaultValue="Hot" disabled aria-label="Sort by">
<SegmentedControlItem value="Hot">Hot</SegmentedControlItem>
<SegmentedControlItem value="New">New</SegmentedControlItem>
</SegmentedControl>
<SegmentedControl defaultValue="Marinara" aria-label="Pasta">
<SegmentedControlItem value="Marinara">Marinara</SegmentedControlItem>
<SegmentedControlItem value="Alfredo" disabled>
Alfredo
</SegmentedControlItem>
<SegmentedControlItem value="Pesto" disabled>
Pesto
</SegmentedControlItem>
<SegmentedControlItem value="Carbonara">Carbonara</SegmentedControlItem>
<SegmentedControlItem value="Bolognese">Bolognese</SegmentedControlItem>
</SegmentedControl>
</VStack>
);
}Notification
import { ActionButton, VStack } from "@seed-design/react";
import { useState } from "react";
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
export default function SegmentedControlNotification() {
const [sortBy, setSortBy] = useState("monthly");
const [hasSeenAnnual, setHasSeenAnnual] = useState(false);
return (
<VStack align="center" gap="spacingY.componentDefault">
<SegmentedControl
aria-label="Billing Method"
value={sortBy}
onValueChange={(value) => {
setSortBy(value);
if (value === "annual") setHasSeenAnnual(true);
}}
>
<SegmentedControlItem value="monthly">Monthly</SegmentedControlItem>
<SegmentedControlItem value="annual" notification={!hasSeenAnnual}>
Annual
</SegmentedControlItem>
<SegmentedControlItem value="enterprise">Enterprise Custom</SegmentedControlItem>
</SegmentedControl>
<ActionButton
size="xsmall"
variant="neutralSolid"
disabled={!hasSeenAnnual}
onClick={() => setHasSeenAnnual(false)}
>
Reset Notification
</ActionButton>
</VStack>
);
}Long Label
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
export default function SegmentedControlLongLabel() {
return (
<SegmentedControl defaultValue="price" aria-label="정렬 기준">
<SegmentedControlItem value="price">가격 높은 순</SegmentedControlItem>
<SegmentedControlItem value="discount">할인율 높은 순</SegmentedControlItem>
<SegmentedControlItem value="popularity">인기 많은 순</SegmentedControlItem>
</SegmentedControl>
);
}Fixed Width
You can directly set the width by providing width to the style prop of SegmentedControl.
import { VStack } from "@seed-design/react";
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
export default function SegmentedControlFixedWidth() {
return (
<VStack align="center" gap="spacingY.componentDefault">
<SegmentedControl defaultValue="new" style={{ width: "500px" }} aria-label="Sort by">
<SegmentedControlItem value="new">New</SegmentedControlItem>
<SegmentedControlItem value="hot">Hot</SegmentedControlItem>
</SegmentedControl>
<SegmentedControl defaultValue="oneway" style={{ width: "400px" }} aria-label="Trip Type">
<SegmentedControlItem value="oneway">One Way Trip</SegmentedControlItem>
<SegmentedControlItem notification value="round">
Round Trip
</SegmentedControlItem>
<SegmentedControlItem notification value="multi">
Multi-City Journey
</SegmentedControlItem>
</SegmentedControl>
</VStack>
);
}Listening to Value Changes
You can detect selection value changes using the onValueChange prop of SegmentedControl.
If you need to use events, you can add event handlers directly to the internal <input> element through the inputProps of SegmentedControlItem.
import { VStack, Text } from "@seed-design/react";
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control";
import { useState } from "react";
export default function SegmentedControlValueChanges() {
const [count, setCount] = useState(0);
const [lastValue, setLastValue] = useState<string | null>(null);
return (
<VStack gap="x4" align="center">
<SegmentedControl
defaultValue="hot"
aria-label="Sort by"
onValueChange={(value) => {
setCount((prev) => prev + 1);
setLastValue(value);
}}
>
<SegmentedControlItem value="hot">Hot</SegmentedControlItem>
<SegmentedControlItem value="new">New</SegmentedControlItem>
</SegmentedControl>
<Text>
onValueChange called: {count} times, last value: {lastValue ?? "-"}
</Text>
</VStack>
);
}