d3ro-voice/packages/ui/src/components/ds/SegmentControl.tsx
2026-08-29 18:33:45 +09:00

123 lines
3.6 KiB
TypeScript

'use client'
// packages/ui/src/components/ds/SegmentControl.tsx
// Linear / Apple-tier segmented pill control with tactile active state
import React from 'react'
import { Box, Typography } from '@mui/material'
import { d3roPalette, d3roRadius, d3roShadow, typoSx } from '../../theme'
export interface SegmentOption<T extends string = string> {
value: T
label: string
icon?: React.ReactNode
badge?: string | number
}
export interface SegmentControlProps<T extends string = string> {
options?: SegmentOption<T>[]
/** Alias for options */
items?: SegmentOption<T>[]
value: T
onChange: (value: T) => void
size?: 'small' | 'medium'
sx?: object
}
export function SegmentControl<T extends string = string>({
options,
items,
value,
onChange,
size = 'medium',
sx,
}: SegmentControlProps<T>): React.ReactElement {
const isSmall = size === 'small'
const list = options || items || []
return (
<Box
sx={{
display: 'inline-flex',
alignItems: 'center',
p: '3px',
bgcolor: d3roPalette.bg.inset,
borderRadius: d3roRadius.pill,
border: `1px solid ${d3roPalette.glass.hairline}`,
gap: '2px',
...sx,
}}
>
{list.map((opt) => {
const isSelected = opt.value === value
return (
<Box
key={opt.value}
onClick={() => onChange(opt.value)}
sx={{
display: 'flex',
alignItems: 'center',
gap: 0.75,
px: isSmall ? 1.5 : 2,
py: isSmall ? 0.5 : 0.75,
borderRadius: d3roRadius.pill,
bgcolor: isSelected ? d3roPalette.glass.raised : 'transparent',
color: isSelected ? d3roPalette.text.primary : d3roPalette.text.secondary,
border: isSelected ? `1px solid ${d3roPalette.glass.hairlineStrong}` : '1px solid transparent',
boxShadow: isSelected ? d3roShadow.buttonRaised : 'none',
cursor: 'pointer',
userSelect: 'none',
transition: 'all 0.2s cubic-bezier(0.16, 1, 0.3, 1)',
'&:hover': {
color: d3roPalette.text.primary,
bgcolor: isSelected ? d3roPalette.glass.raised : d3roPalette.glass.surface,
},
'&:active': {
transform: 'scale(0.97)',
},
}}
>
{opt.icon && (
<Box
sx={{
display: 'flex',
alignItems: 'center',
color: isSelected ? d3roPalette.accent.light : 'inherit',
'& svg': { width: isSmall ? 14 : 16, height: isSmall ? 14 : 16 },
}}
>
{opt.icon}
</Box>
)}
<Typography
sx={{
...typoSx('compact'),
fontSize: isSmall ? '12px' : '13px',
fontWeight: isSelected ? 600 : 500,
lineHeight: 1,
}}
>
{opt.label}
</Typography>
{opt.badge !== undefined && (
<Box
sx={{
ml: 0.5,
px: 0.75,
py: 0.15,
borderRadius: d3roRadius.pill,
bgcolor: isSelected ? d3roPalette.accent.dim : d3roPalette.bg.chassis,
color: isSelected ? d3roPalette.accent.light : d3roPalette.text.dimLabel,
fontSize: '10px',
fontWeight: 500,
}}
>
{opt.badge}
</Box>
)}
</Box>
)
})}
</Box>
)
}