Segmented Control A component used when selecting one of several options to immediately filter or switch related content.
@seed-design/lynx-react@0.6.0, @seed-design/lynx-css@0.10.0
import "./styles" ;
import { useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl
className = "segmented-control-example__control"
defaultValue = "Hot"
accessibility-label = "Sort by"
>
< SegmentedControlItem value = "Hot" >Hot</ SegmentedControlItem >
< SegmentedControlItem value = "New" >New</ SegmentedControlItem >
</ SegmentedControl >
</ view >
</ view >
);
}
npx @seed-design/cli add ui:segmented-control pnpm dlx @seed-design/cli add ui:segmented-control yarn dlx @seed-design/cli add ui:segmented-control bun x @seed-design/cli add ui:segmented-control
Install dependencies
npm install @seed-design/lynx-react yarn add @seed-design/lynx-react pnpm add @seed-design/lynx-react bun add @seed-design/lynx-react Copy the code below and paste it in /**
* @file ui:segmented-control
* @requires @seed-design/lynx-react@>=0.6.0 <1.0.0
* @requires @seed-design/lynx-css@>=0.10.0 <1.0.0
**/
import * as React from "@lynx-js/react" ;
import {
NotificationBadge,
NotificationBadgePositioner,
SegmentedControl as SeedSegmentedControl,
} from "@seed-design/lynx-react" ;
export interface SegmentedControlProps extends SeedSegmentedControl . RootProps {}
/**
* @see https://crispy.carrot-soft.tech/lynx/components/segmented-control
*/
export const SegmentedControl = React. forwardRef < unknown , SegmentedControlProps >(
({ children , ... otherProps }, ref ) => {
return (
< SeedSegmentedControl.Root ref = {ref} { ... otherProps}>
{children}
< SeedSegmentedControl.Indicator />
</ SeedSegmentedControl.Root >
);
},
);
SegmentedControl.displayName = "SegmentedControl" ;
export interface SegmentedControlItemProps
extends Omit < SeedSegmentedControl . ItemProps , "notification" > {
notification ?: boolean ;
}
/**
* @see https://crispy.carrot-soft.tech/lynx/components/segmented-control#segmentedcontrolitem
*/
export const SegmentedControlItem = React. forwardRef < unknown , SegmentedControlItemProps >(
({ children , notification , ... otherProps }, ref ) => {
return (
< SeedSegmentedControl.Item
ref = {ref}
{ ... otherProps}
notification = {
notification ? (
< NotificationBadgePositioner size = "small" attach = "text" >
< NotificationBadge />
</ NotificationBadgePositioner >
) : undefined
}
>
{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.
*/
value?string | undefined
defaultValue?string | undefined
disabled?boolean | undefined
onValueChange?(( value : string ) => void ) | undefined
style?CSSProperties | undefined
children?React.ReactNode
className?string | undefined
notification?boolean | undefined
bindtap?EventHandler < BaseTouchEvent < Target >> | undefined
main-thread:bindtap?EventHandler < BaseTouchEvent < Element >> | undefined
className?string | undefined
disabled?boolean | undefined
childrenstring | number
style?CSSProperties | undefined
valuestring
import {
SegmentedControl,
SegmentedControlItem,
} from "@/components/ui/segmented-control" ;
export function App () {
return (
< SegmentedControl defaultValue = "hot" accessibility-label = "Sort criteria" >
< SegmentedControlItem value = "hot" >Most popular</ SegmentedControlItem >
< SegmentedControlItem value = "new" >Most recent</ SegmentedControlItem >
</ SegmentedControl >
);
}
The Registry SegmentedControl automatically adds a selection Indicator.
The disabled prop on SegmentedControl disables all items. Individual SegmentedControlItem elements can also be disabled separately.
import "./styles" ;
import { useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl defaultValue = "Hot" disabled accessibility-label = "Sort by" >
< SegmentedControlItem value = "Hot" >Hot</ SegmentedControlItem >
< SegmentedControlItem value = "New" >New</ SegmentedControlItem >
</ SegmentedControl >
< SegmentedControl defaultValue = "Marinara" accessibility-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 >
</ view >
</ view >
);
}
import "./styles" ;
import { useState } from "@lynx-js/react" ;
import { ActionButton, useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
const [ sortBy , setSortBy ] = useState ( "monthly" );
const [ hasSeenAnnual , setHasSeenAnnual ] = useState ( false );
function handleValueChange ( value : string ) {
"background only" ;
setSortBy (value);
if (value === "annual" ) setHasSeenAnnual ( true );
}
function handleResetNotification () {
"background only" ;
setHasSeenAnnual ( false );
}
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl
accessibility-label = "Billing Method"
value = {sortBy}
onValueChange = {handleValueChange}
>
< 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}
bindtap = {handleResetNotification}
>
Reset Notification
</ ActionButton >
</ view >
</ view >
);
}
import "./styles" ;
import { useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl defaultValue = "price" accessibility-label = "정렬 기준" >
< SegmentedControlItem value = "price" >가격 높은 순</ SegmentedControlItem >
< SegmentedControlItem value = "discount" >할인율 높은 순</ SegmentedControlItem >
< SegmentedControlItem value = "popular" >인기 많은 순</ SegmentedControlItem >
</ SegmentedControl >
</ view >
</ view >
);
}
You can specify the width using style or className on SegmentedControl.
import "./styles" ;
import { useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl
className = "segmented-control-example__fixed-wide"
defaultValue = "new"
accessibility-label = "Sort by"
>
< SegmentedControlItem value = "new" >New</ SegmentedControlItem >
< SegmentedControlItem value = "hot" >Hot</ SegmentedControlItem >
</ SegmentedControl >
< SegmentedControl
className = "segmented-control-example__fixed-narrow"
defaultValue = "oneway"
accessibility-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 >
</ view >
</ view >
);
}
You can detect selection value changes using onValueChange. In Controlled mode, use it together with value.
import "./styles" ;
import { useState } from "@lynx-js/react" ;
import { useSeedClassName } from "@seed-design/lynx-react" ;
import { SegmentedControl, SegmentedControlItem } from "@/components/ui/segmented-control" ;
export default function Example () {
const seedClassName = useSeedClassName ({ colorMode: "system" });
const [ count , setCount ] = useState ( 0 );
const [ lastValue , setLastValue ] = useState < string | null >( null );
function handleValueChange ( nextValue : string ) {
"background only" ;
setCount (( previous ) => previous + 1 );
setLastValue (nextValue);
}
return (
< view className = { `${ seedClassName } docs-lynx-segmented-control-root` }>
< view className = "segmented-control-example" >
< SegmentedControl
defaultValue = "hot"
onValueChange = {handleValueChange}
accessibility-label = "Sort by"
>
< SegmentedControlItem value = "hot" >Hot</ SegmentedControlItem >
< SegmentedControlItem value = "new" >New</ SegmentedControlItem >
</ SegmentedControl >
< text className = "segmented-control-example__status" >
onValueChange called: {count} times, last value: {lastValue ?? "-" }
</ text >
</ view >
</ view >
);
}
Item React Web Lynx Rendering Elements HTML label, input Native view, text Selection Event input change bindtap, exposed state event is onValueChangeAccessibility ARIA radiogroup, radio Lynx accessibility-* attributes Indicator CSS grid and custom property Grid layout and custom property
ItemHiddenInput, name, form, inputProps for HTML form submission
Keyboard focus and focus-visible interactions
invalid state on SegmentedControlItem
Lynx Registry and package do not provide the invalid state. This difference is intentional. If you need error states, display error text near the SegmentedControl in your app and include error content in the accessibility-label.