import React, { HTMLAttributes, KeyboardEvent, useCallback, useEffect, useId, useRef, useState } from 'react'; import { classNames } from '@/utils'; export type TabItem = { label: React.ReactNode; value: string; disabled?: boolean; }; export type AppTabVariant = 'default' | 'underline'; export type AppTabSize = 'sm' | 'md'; export type AppTabProps = Omit, 'onChange'> & { items: TabItem[]; value?: string; defaultValue?: string; onChange?: (value: string) => void; variant?: AppTabVariant; size?: AppTabSize; className?: string; fullWidth?: boolean; /** Id prefix used to generate aria-controls/id pairs. Defaults to a React-generated id. */ tabId?: string; }; const AppTab = ({ items, value: controlledValue, defaultValue, onChange, variant = 'default', size = 'md', className, fullWidth = false, tabId: tabIdProp, ...rest }: AppTabProps) => { const isControlled = controlledValue !== undefined; const firstEnabled = items.find((item) => !item.disabled)?.value ?? ''; const [internalValue, setInternalValue] = useState(defaultValue ?? firstEnabled); const activeValue = isControlled ? controlledValue : internalValue; const generatedId = useId(); const tabId = tabIdProp ?? generatedId; const containerRef = useRef(null); const tabRefs = useRef>(new Map()); const [indicatorStyle, setIndicatorStyle] = useState({}); const updateIndicator = useCallback(() => { const activeTab = tabRefs.current.get(activeValue); const container = containerRef.current; if (!activeTab || !container) { setIndicatorStyle({}); return; } const containerRect = container.getBoundingClientRect(); const tabRect = activeTab.getBoundingClientRect(); setIndicatorStyle({ width: tabRect.width, left: tabRect.left - containerRect.left, }); }, [activeValue]); useEffect(() => { updateIndicator(); }, [updateIndicator, items]); useEffect(() => { if (typeof window === 'undefined') return; if (typeof ResizeObserver === 'undefined') { window.addEventListener('resize', updateIndicator); return () => window.removeEventListener('resize', updateIndicator); } const observer = new ResizeObserver(updateIndicator); if (containerRef.current) observer.observe(containerRef.current); return () => observer.disconnect(); }, [updateIndicator]); const handleTabClick = (tabValue: string) => { if (!isControlled) { setInternalValue(tabValue); } onChange?.(tabValue); }; const setTabRef = (value: string) => (el: HTMLButtonElement | null) => { if (el) { tabRefs.current.set(value, el); } else { tabRefs.current.delete(value); } }; const enabledItems = items.filter((item) => !item.disabled); const handleKeyDown = (e: KeyboardEvent, currentValue: string) => { const currentIndex = enabledItems.findIndex((item) => item.value === currentValue); let nextIndex: number | null = null; if (e.key === 'ArrowRight' || e.key === 'ArrowDown') { nextIndex = (currentIndex + 1) % enabledItems.length; } else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { nextIndex = (currentIndex - 1 + enabledItems.length) % enabledItems.length; } else if (e.key === 'Home') { nextIndex = 0; } else if (e.key === 'End') { nextIndex = enabledItems.length - 1; } if (nextIndex !== null) { e.preventDefault(); const nextValue = enabledItems[nextIndex].value; tabRefs.current.get(nextValue)?.focus(); handleTabClick(nextValue); } }; const sizeClasses = { sm: 'px-3 py-1.5 text-xs', md: 'px-4 py-2 text-sm', }; const isDefault = variant === 'default'; const isUnderline = variant === 'underline'; return (
{/* Sliding indicator */}
); }; AppTab.displayName = 'AppTab'; export default AppTab;