Skip to content

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

3.0 KB
Raw Blame
import React from 'react';

export type IconButtonSize = 'xs' | 'sm' | 'md';
export type IconButtonVariant = 'ghost' | 'subtle' | 'danger' | 'warning';

export interface IconButtonProps
  extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'children'> {
  icon?: React.ReactNode;
  /** Optional concise text label rendered alongside or instead of `icon`. */
  label?: React.ReactNode;
  active?: boolean;
  size?: IconButtonSize;
  variant?: IconButtonVariant;
  'data-testid'?: string;
}

const SQUARE_SIZE_CLASSES: Record<IconButtonSize, string> = {
  xs: 'w-5 h-5 text-[10px]',
  sm: 'w-[var(--ui-control-height)] h-[var(--ui-control-height)] text-[11px]',
  md: 'w-7 h-7 text-[11px]',
};

const LABELED_SIZE_CLASSES: Record<IconButtonSize, string> = {
  xs: 'h-5 px-1.5 gap-1 text-[10px]',
  sm: 'h-[var(--ui-control-height)] px-2 gap-1 text-[10.5px]',
  md: 'h-7 px-2.5 gap-1.5 text-[11px]',
};

const IconButtonInner: React.FC<IconButtonProps> = ({
  icon,
  label,
  active,
  size = 'sm',
  variant = 'ghost',
  disabled = false,
  className = '',
  title,
  'aria-label': ariaLabel,
  'aria-pressed': ariaPressed,
  'data-testid': dataTestId,
  type = 'button',
  ...rest
}) => {
  const hasLabel = label !== undefined && label !== null;
  const sizeClass = hasLabel ? LABELED_SIZE_CLASSES[size] : SQUARE_SIZE_CLASSES[size];

  let toneClass: string;
  if (disabled) {
    toneClass = 'text-ui-disabled border-transparent cursor-not-allowed';
  } else if (active) {
    toneClass =
      variant === 'warning'
        ? 'bg-ui-warning/15 text-ui-warning border-ui-warning/40 cursor-pointer'
        : 'bg-ui-accent-subtle text-ui-accent border-ui-accent-border cursor-pointer';
  } else if (variant === 'subtle') {
    toneClass =
      'bg-ui-control text-ui-secondary hover:text-ui-primary hover:bg-ui-control-hover border-ui-subtle cursor-pointer';
  } else if (variant === 'danger') {
    toneClass =
      'text-ui-icon-muted hover:text-ui-danger hover:bg-ui-control-hover border-transparent cursor-pointer';
  } else {
    toneClass =
      'text-ui-icon-muted hover:text-ui-primary hover:bg-ui-control-hover border-transparent cursor-pointer';
  }

  return (
    <button
      {...rest}
      type={type}
      disabled={disabled}
      title={title}
      aria-label={ariaLabel ?? (typeof title === 'string' && !hasLabel ? title : undefined)}
      aria-pressed={ariaPressed !== undefined ? ariaPressed : active}
      data-testid={dataTestId}
      data-active={active ? 'true' : undefined}
      className={`ui-icon-stroke inline-flex items-center justify-center shrink-0 rounded-ui-xs border transition-colors select-none focus:outline-none focus-visible:border-ui-focus ${sizeClass} ${toneClass} ${className}`}
    >
      {icon && <span className="shrink-0 flex items-center justify-center">{icon}</span>}
      {hasLabel && <span className="truncate leading-none">{label}</span>}
    </button>
  );
};

export const IconButton = React.memo(IconButtonInner);
IconButton.displayName = 'IconButton';