progress.tsx 1.16 KB
import * as React from 'react';
import { cn } from '@/lib/utils';

interface ProgressProps extends React.HTMLAttributes<HTMLDivElement> {
  value?: number; // 0-100
  indeterminate?: boolean;
  tone?: 'brand' | 'success' | 'info' | 'warning' | 'danger' | 'neutral';
}

const toneClass: Record<NonNullable<ProgressProps['tone']>, string> = {
  brand: 'bg-primary',
  success: 'bg-emerald-500',
  info: 'bg-slate-500',
  warning: 'bg-amber-500',
  danger: 'bg-rose-500',
  neutral: 'bg-slate-400/70',
};

function Progress({
  value = 0,
  indeterminate = false,
  tone = 'brand',
  className,
  ...props
}: ProgressProps) {
  return (
    <div
      className={cn(
        'h-1.5 w-full overflow-hidden rounded-full bg-muted',
        className,
      )}
      {...props}
    >
      {indeterminate ? (
        <div className={cn('h-full w-1/3 rounded-full', toneClass[tone], 'shimmer')} />
      ) : (
        <div
          className={cn(
            'h-full rounded-full transition-[width] duration-500 ease-out',
            toneClass[tone],
          )}
          style={{ width: `${Math.min(100, Math.max(0, value))}%` }}
        />
      )}
    </div>
  );
}

export { Progress };