nex_docus/frontend/src/components/ModeSwitch/ModeSwitch.jsx

48 lines
1.1 KiB
React
Raw Normal View History

2026-03-11 07:27:52 +00:00
import './ModeSwitch.css'
function ModeSwitch({
value = 'view',
onChange,
viewLabel = '浏览',
editLabel = '编辑',
options,
ariaLabel = '模式切换',
2026-05-09 02:45:30 +00:00
size = 'default',
2026-03-11 07:27:52 +00:00
}) {
const finalOptions = options || [
{ label: viewLabel, value: 'view' },
{ label: editLabel, value: 'edit' },
]
const activeIndex = Math.max(
0,
finalOptions.findIndex((item) => item.value === value)
)
return (
<div
2026-05-09 02:45:30 +00:00
className={`mode-switch mode-switch-${size}`}
2026-03-11 07:27:52 +00:00
role="tablist"
aria-label={ariaLabel}
style={{
'--option-count': String(finalOptions.length),
'--active-index': String(activeIndex),
}}
>
<span className="mode-switch-thumb" aria-hidden="true" />
{finalOptions.map((item) => (
<button
key={item.value}
type="button"
className={`mode-switch-option ${value === item.value ? 'active' : ''}`}
aria-selected={value === item.value}
onClick={() => onChange?.(item.value)}
>
{item.label}
</button>
))}
</div>
)
}
export default ModeSwitch