Segmented Control
Single-choice segmented control with explicit items, disabled states, and controlled or default selection.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add segmented-controlSource files
Expand a file to read the source the installer copies into your app.
components/ui/segmented-control/SegmentedControl.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import * as ToggleGroupPrimitive from '@rn-primitives/toggle-group'
import { cssInterop } from 'react-native-css-interop'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'
cssInterop(ToggleGroupPrimitive.Root, { className: 'style' })
cssInterop(ToggleGroupPrimitive.Item, { className: 'style' })
const SegmentedContext = React.createContext<{ value: string | undefined; disabled: boolean } | null>(null)
export interface SegmentedControlProps extends ViewProps {
className?: string
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
disabled?: boolean
}
export const SegmentedControl = React.forwardRef<React.ComponentRef<typeof View>, SegmentedControlProps>(
({ value, defaultValue, onValueChange, disabled = false, className, children, ...props }, ref) => {
const [internal, setInternal] = React.useState(defaultValue)
const resolved = value ?? internal
return (
<SegmentedContext.Provider value={{ value: resolved, disabled }}>
<ToggleGroupPrimitive.Root
ref={ref}
{...props}
type="single"
value={resolved}
disabled={disabled}
onValueChange={(next) => {
if (disabled || next === undefined || next === resolved) return
if (value === undefined) setInternal(next)
onValueChange?.(next)
}}
className={cn('flex-row flex-wrap gap-[4px] rounded-lg bg-muted p-[4px]', className)}
>
{children}
</ToggleGroupPrimitive.Root>
</SegmentedContext.Provider>
)
},
)
SegmentedControl.displayName = 'SegmentedControl'
export type SegmentedControlItemProps = Omit<ToggleGroupPrimitive.ItemProps, 'children'> & {
children?: React.ReactNode
className?: string
}
export const SegmentedControlItem = React.forwardRef<ToggleGroupPrimitive.ItemRef, SegmentedControlItemProps>(
({ className, children, disabled, ...props }, ref) => {
const group = React.useContext(SegmentedContext)
if (!group) throw new Error('SegmentedControlItem must be inside SegmentedControl')
const isDisabled = group.disabled || !!disabled
return (
<ToggleGroupPrimitive.Item
ref={ref}
{...props}
disabled={isDisabled}
className={cn(
'min-h-[48px] min-w-[80px] max-w-full shrink-0 grow items-center justify-center rounded-md px-3 py-2',
group.value === props.value && 'bg-card',
isDisabled && 'opacity-50',
className,
)}
>
{renderTextChildren(children)}
</ToggleGroupPrimitive.Item>
)
},
)
SegmentedControlItem.displayName = 'SegmentedControlItem'
components/ui/segmented-control/index.ts
export * from './SegmentedControl'
Dependencies
Registry items
npm packages
@rn-primitives/toggle-groupreact-native-css-interop