Skip to content

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

3.9 KB
Raw Blame
import React, { useCallback } from 'react';

export interface HairlineTabItem<T extends string> {
  value: T;
  label: string;
  icon?: React.ReactNode;
  badge?: React.ReactNode;
  disabled?: boolean;
  title?: string;
  'data-testid'?: string;
}

export interface HairlineTabsProps<T extends string> {
  value: T;
  tabs: ReadonlyArray<HairlineTabItem<T>>;
  onChange: (value: T) => void;
  size?: 'xs' | 'sm';
  fullWidth?: boolean;
  /** Render the bottom hairline border under the tab strip (default: true). */
  bordered?: boolean;
  className?: string;
  'aria-label'?: string;
  'data-testid'?: string;
}

function HairlineTabsInner<T extends string>({
  value,
  tabs,
  onChange,
  size = 'sm',
  fullWidth = false,
  bordered = true,
  className = '',
  'aria-label': ariaLabel,
  'data-testid': dataTestId,
}: HairlineTabsProps<T>) {
  const handleKeyDown = useCallback(
    (e: React.KeyboardEvent<HTMLDivElement>) => {
      const enabled = tabs.filter((t) => !t.disabled);
      if (enabled.length === 0) return;
      const idx = enabled.findIndex((t) => t.value === value);
      let nextIdx = -1;

      if (e.key === 'ArrowRight') {
        nextIdx = idx >= 0 ? (idx + 1) % enabled.length : 0;
      } else if (e.key === 'ArrowLeft') {
        nextIdx = idx >= 0 ? (idx - 1 + enabled.length) % enabled.length : enabled.length - 1;
      } else if (e.key === 'Home') {
        nextIdx = 0;
      } else if (e.key === 'End') {
        nextIdx = enabled.length - 1;
      }

      if (nextIdx >= 0) {
        e.preventDefault();
        e.stopPropagation();
        const next = enabled[nextIdx];
        if (next && next.value !== value) {
          onChange(next.value);
        }
      }
    },
    [onChange, tabs, value]
  );

  return (
    <div
      role="tablist"
      aria-label={ariaLabel}
      data-testid={dataTestId}
      onKeyDown={handleKeyDown}
      className={`ui-icon-stroke flex items-stretch select-none ${
        fullWidth ? 'w-full' : ''
      } ${bordered ? 'border-b border-ui-subtle' : ''} ${
        size === 'xs' ? 'gap-2' : 'gap-3'
      } ${className}`}
    >
      {tabs.map((tab) => {
        const isActive = tab.value === value;
        const isDisabled = Boolean(tab.disabled);
        return (
          <button
            key={tab.value}
            type="button"
            role="tab"
            disabled={isDisabled}
            aria-selected={isActive}
            data-testid={tab['data-testid']}
            data-active={isActive ? 'true' : 'false'}
            title={tab.title}
            onClick={() => {
              if (!isDisabled && tab.value !== value) {
                onChange(tab.value);
              }
            }}
            className={`relative flex items-center justify-center gap-1 transition-colors focus:outline-none focus-visible:text-ui-primary ${
              fullWidth ? 'flex-1' : 'shrink-0'
            } ${
              size === 'xs' ? 'h-6 px-1 text-[11px]' : 'h-7 px-1.5 text-[11px]'
            } ${
              isDisabled
                ? 'text-ui-disabled cursor-not-allowed'
                : isActive
                  ? 'text-ui-primary font-medium cursor-pointer'
                  : 'text-ui-muted hover:text-ui-secondary cursor-pointer'
            }`}
          >
            {tab.icon && <span className="shrink-0 flex items-center">{tab.icon}</span>}
            <span className="truncate">{tab.label}</span>
            {tab.badge !== undefined && (
              <span className="font-ui-mono tabular-nums text-[9.5px] text-ui-muted">
                {tab.badge}
              </span>
            )}
            {isActive && (
              <span
                aria-hidden="true"
                className="pointer-events-none absolute left-0 right-0 bottom-[-1px] h-[2px] bg-ui-accent"
              />
            )}
          </button>
        );
      })}
    </div>
  );
}

export const HairlineTabs = React.memo(HairlineTabsInner) as typeof HairlineTabsInner;