A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
import React, { useCallback } from 'react';
export interface SegmentedOption<T extends string> {
value: T;
label?: string;
icon?: React.ReactNode;
title?: string;
disabled?: boolean;
/** Optional accent styling when this specific option is active (e.g., B/W or Split preview mode). */
accentOnActive?: boolean;
'data-testid'?: string;
'aria-label'?: string;
className?: string;
}
export interface SegmentedControlProps<T extends string> {
value: T;
options: ReadonlyArray<SegmentedOption<T>>;
onChange: (value: T) => void;
size?: 'xs' | 'sm' | 'md';
fullWidth?: boolean;
wrap?: boolean;
/** Use tinted accent surface for the active option instead of neutral machined elevation. */
accentActive?: boolean;
/** ARIA role pattern: 'radio' uses role="radiogroup"+role="radio"+aria-checked; 'button' uses role="group"+aria-pressed. */
selectionRole?: 'radio' | 'button';
disabled?: boolean;
className?: string;
'aria-label'?: string;
'data-testid'?: string;
}
const OPTION_SIZE_CLASSES = {
xs: 'h-[18px] px-1.5 text-[9.5px] gap-0.5',
sm: 'min-h-[calc(var(--ui-control-height)-4px)] px-2 py-0.5 text-[10px] gap-1',
md: 'h-6 px-2.5 text-[11px] gap-1.5',
} as const;
function SegmentedControlInner<T extends string>({
value,
options,
onChange,
size = 'sm',
fullWidth = false,
wrap = false,
accentActive = false,
selectionRole = 'radio',
disabled = false,
className = '',
'aria-label': ariaLabel,
'data-testid': dataTestId,
}: SegmentedControlProps<T>) {
const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
if (disabled) return;
const enabledOptions = options.filter((opt) => !opt.disabled);
if (enabledOptions.length === 0) return;
const currentIndex = enabledOptions.findIndex((opt) => opt.value === value);
let nextIndex = -1;
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
nextIndex = currentIndex >= 0 ? (currentIndex + 1) % enabledOptions.length : 0;
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
nextIndex =
currentIndex >= 0
? (currentIndex - 1 + enabledOptions.length) % enabledOptions.length
: enabledOptions.length - 1;
} else if (e.key === 'Home') {
nextIndex = 0;
} else if (e.key === 'End') {
nextIndex = enabledOptions.length - 1;
}
if (nextIndex >= 0) {
e.preventDefault();
e.stopPropagation();
const nextOpt = enabledOptions[nextIndex];
if (nextOpt && nextOpt.value !== value) {
onChange(nextOpt.value);
}
}
},
[disabled, onChange, options, value]
);
return (
<div
role={selectionRole === 'radio' ? 'radiogroup' : 'group'}
aria-label={ariaLabel}
data-testid={dataTestId}
onKeyDown={handleKeyDown}
className={`ui-icon-stroke ${
fullWidth ? 'flex w-full' : 'inline-flex'
} ${
wrap ? 'flex-wrap gap-[2px]' : 'items-center gap-[1px]'
} rounded-ui-sm bg-ui-input border border-ui-subtle ${
size === 'xs' ? 'p-[1px]' : 'p-[2px]'
} select-none ${disabled ? 'opacity-50' : ''} ${className}`}
>
{options.map((opt) => {
const isActive = opt.value === value;
const isOptDisabled = disabled || Boolean(opt.disabled);
const useAccent = isActive && (accentActive || opt.accentOnActive);
return (
<button
key={opt.value}
type="button"
disabled={isOptDisabled}
role={selectionRole === 'radio' ? 'radio' : undefined}
aria-checked={selectionRole === 'radio' ? isActive : undefined}
aria-pressed={selectionRole === 'button' ? isActive : undefined}
aria-label={opt['aria-label'] ?? (!opt.label ? opt.title : undefined)}
data-testid={opt['data-testid']}
data-active={isActive ? 'true' : 'false'}
title={opt.title}
onClick={() => {
if (!isOptDisabled && opt.value !== value) {
onChange(opt.value);
}
}}
className={`${
fullWidth || wrap ? 'flex-1' : ''
} ${
wrap ? 'min-w-[54px]' : 'min-w-0'
} flex items-center justify-center rounded-ui-xs border font-medium transition-colors truncate focus:outline-none focus-visible:border-ui-focus ${
OPTION_SIZE_CLASSES[size]
} ${
isOptDisabled
? 'text-ui-disabled border-transparent cursor-not-allowed'
: useAccent
? 'bg-ui-accent-subtle text-ui-accent border-ui-accent-border cursor-pointer'
: isActive
? 'bg-ui-control-active text-ui-primary border-ui-default cursor-pointer'
: 'text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover/65 border-transparent cursor-pointer'
} ${opt.className ?? ''}`}
>
{opt.icon && <span className="shrink-0 flex items-center">{opt.icon}</span>}
{opt.label && <span className="truncate">{opt.label}</span>}
</button>
);
})}
</div>
);
}
export const SegmentedControl = React.memo(
SegmentedControlInner
) as typeof SegmentedControlInner;