'use client' import { useEffect, useRef, type KeyboardEvent } from 'react' import { Check, AlertTriangle } from 'lucide-react' import { cn } from '@/lib/utils' // ─── Types ────────────────────────────────────────────────────────────────── export type StepStatus = 'idle' | 'active' | 'done' | 'error' export interface StepBarItem { id: string label: string status: StepStatus } interface StepBarProps { steps: StepBarItem[] onGoTo: (index: number) => void showLabels?: boolean } // ─── Helper ────────────────────────────────────────────────────────────────── function DotIcon({ status, index }: { status: StepStatus; index: number }) { if (status === 'done') return if (status === 'error') return return ( {index + 1} ) } // ─── Component ─────────────────────────────────────────────────────────────── export function StepBar({ steps, onGoTo, showLabels }: StepBarProps) { const btnRefs = useRef>([]) const total = steps.length const activeIdx = steps.findIndex((s) => s.status === 'active') const fillPct = total > 1 ? (Math.max(0, activeIdx) / (total - 1)) * 100 : 0 useEffect(() => { btnRefs.current[activeIdx]?.scrollIntoView({ behavior: 'smooth', inline: 'center', block: 'nearest', }) }, [activeIdx]) const isReachable = (i: number) => { const s = steps[i].status if (s === 'done' || s === 'active' || s === 'error') return true return i > 0 && steps[i - 1].status === 'done' } const handleKey = (e: KeyboardEvent, i: number) => { let next = i if (e.key === 'ArrowRight') next = Math.min(total - 1, i + 1) else if (e.key === 'ArrowLeft') next = Math.max(0, i - 1) else if (e.key === 'Home') next = 0 else if (e.key === 'End') next = total - 1 else return e.preventDefault() const dir = next > i ? 1 : -1 while (next !== i && !isReachable(next)) { next += dir if (next < 0 || next >= total) return } btnRefs.current[next]?.focus() } return ( {/* * Track layer — z-index: 1 so it sits BELOW the dots (z-index: 2). * top: 32px = paddingTop(16px) + dotSize(32px)/2 */} {/* Steps row — z-index: 2, establishes stacking context above track */} {steps.map((step, i) => { const isActive = step.status === 'active' const isDone = step.status === 'done' const isError = step.status === 'error' const isIdle = step.status === 'idle' const reachable = isReachable(i) const displayLabel = showLabels ?? true return ( { btnRefs.current[i] = el }} type="button" role="tab" aria-selected={isActive} aria-current={isActive ? 'step' : undefined} aria-label={`${step.label}${isError ? ' — hata' : ''} (${i + 1} / ${total})`} onClick={() => reachable && onGoTo(i)} onKeyDown={(e) => handleKey(e, i)} disabled={!reachable} tabIndex={isActive ? 0 : -1} className={cn( 'group flex flex-1 flex-col items-center', 'pt-4 pb-3 gap-2', 'select-none outline-none rounded-xl', 'focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background', reachable ? 'cursor-pointer' : 'cursor-not-allowed opacity-40', )} > {/* * Dot wrapper — position: relative + z-index: 2 * This creates a stacking context that sits above the track (z:1). * Fixed 32×32 for ALL states so track top:32px never shifts. */} {/* Active pulse ring */} {isActive && ( )} {/* Label */} {displayLabel && ( 4 && 'hidden sm:block', isActive && 'font-semibold text-foreground', isDone && !isActive && 'text-muted-foreground', isError && !isActive && 'font-medium text-destructive', isIdle && 'text-muted-foreground', )} > {step.label} )} ) })} ) }