Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

3.5 KB
Raw Blame
import React from 'react';
import { useI18n } from '../../i18n';

export interface ToggleSwitchProps {
  checked: boolean;
  onChange: (checked: boolean) => void;
  label?: string;
  description?: string;
  disabled?: boolean;
  size?: 'xs' | 'sm';
  className?: string;
  title?: string;
  'aria-label'?: string;
  'data-testid'?: string;
}

const ToggleSwitchInner: React.FC<ToggleSwitchProps> = ({
  checked,
  onChange,
  label,
  description,
  disabled = false,
  size = 'sm',
  className = '',
  title,
  'aria-label': ariaLabel,
  'data-testid': dataTestId,
}) => {
  const { t } = useI18n();
  const isXs = size === 'xs';

  const trackDimensions = isXs ? 'w-[24px] h-[14px]' : 'w-[28px] h-[16px]';
  const thumbDimensions = isXs ? 'w-[10px] h-[10px]' : 'w-[12px] h-[12px]';
  const thumbTranslate = isXs
    ? checked
      ? 'translate-x-[11px]'
      : 'translate-x-[1px]'
    : checked
      ? 'translate-x-[13px]'
      : 'translate-x-[1px]';

  const switchTrack = (
    <span
      aria-hidden="true"
      className={`relative inline-flex items-center shrink-0 rounded-full border transition-colors ${trackDimensions} ${
        checked
          ? 'bg-ui-accent border-transparent'
          : 'bg-ui-input border-ui-default group-hover:border-ui-strong'
      }`}
    >
      <span
        className={`block rounded-full transition-transform duration-100 ${thumbDimensions} ${thumbTranslate} ${
          checked ? 'bg-ui-accent-contrast' : 'bg-ui-icon-muted group-hover:bg-ui-primary'
        }`}
      />
    </span>
  );

  const handleToggle = () => {
    if (disabled) return;
    onChange(!checked);
  };

  if (!label && !description) {
    return (
      <button
        type="button"
        role="switch"
        aria-checked={checked}
        aria-label={ariaLabel ?? title ?? t('Toggle')}
        disabled={disabled}
        data-testid={dataTestId}
        data-checked={checked ? 'true' : 'false'}
        title={title}
        onClick={handleToggle}
        className={`group inline-flex items-center justify-center rounded-full focus:outline-none focus-visible:ring-1 focus-visible:ring-ui-focus ${
          disabled ? 'opacity-45 cursor-not-allowed' : 'cursor-pointer'
        } ${className}`}
      >
        {switchTrack}
      </button>
    );
  }

  return (
    <button
      type="button"
      role="switch"
      aria-checked={checked}
      aria-label={ariaLabel ?? label}
      disabled={disabled}
      data-testid={dataTestId}
      data-checked={checked ? 'true' : 'false'}
      title={title}
      onClick={handleToggle}
      className={`group w-full flex items-center justify-between gap-2 text-left select-none rounded-ui-xs focus:outline-none focus-visible:ring-1 focus-visible:ring-ui-focus ${
        description ? 'py-1' : 'py-0.5'
      } ${disabled ? 'opacity-45 cursor-not-allowed' : 'cursor-pointer'} ${className}`}
    >
      <span className="min-w-0 flex-1">
        <span
          className={`block text-[11px] leading-tight truncate transition-colors ${
            disabled
              ? 'text-ui-disabled'
              : checked
                ? 'text-ui-primary'
                : 'text-ui-secondary group-hover:text-ui-primary'
          }`}
        >
          {label}
        </span>
        {description && (
          <span className="block text-[10px] text-ui-muted leading-snug truncate mt-0.5">
            {description}
          </span>
        )}
      </span>
      {switchTrack}
    </button>
  );
};

export const ToggleSwitch = React.memo(ToggleSwitchInner);
ToggleSwitch.displayName = 'ToggleSwitch';