| import React, { useState, useEffect, useRef, useCallback, forwardRef, useImperativeHandle, useMemo } from 'react'; |
| import './TabContainer.css'; |
|
|
| |
| export interface TabItem { |
| id: string; |
| title: string; |
| tooltip?: string; |
| data?: any; |
| disabled?: boolean; |
| icon?: React.ReactNode; |
| icon_url?: string; |
| } |
|
|
| |
| export type TabContentRenderer = (tabItem: TabItem, isActive: boolean) => React.ReactNode; |
|
|
| |
| export interface TabContainerProps { |
| tabs: TabItem[]; |
| activeTabId?: string; |
| onTabChange?: (tabId: string, tabItem: TabItem) => void; |
| contentRenderer: TabContentRenderer; |
| className?: string; |
| showStatusBar?: boolean; |
| getStatusText?: (tabItem: TabItem) => string; |
| |
| enableScrollSwitch?: boolean; |
| scrollSwitchThreshold?: number; |
| scrollSwitchDelay?: number; |
| |
| tabPosition?: 'top' | 'bottom' | 'left' | 'right'; |
| } |
|
|
| |
| export interface TabContainerRef { |
| focus: () => void; |
| blur: () => void; |
| setActiveTab: (tabId: string) => void; |
| getActiveTab: () => TabItem | undefined; |
| getActiveTabIndex: () => number; |
| canSwitchNext: () => boolean; |
| canSwitchPrev: () => boolean; |
| switchToNext: () => void; |
| switchToPrev: () => void; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export const TabContainer = forwardRef<TabContainerRef, TabContainerProps>(({ |
| tabs, |
| activeTabId, |
| onTabChange, |
| contentRenderer, |
| className, |
| showStatusBar = false, |
| getStatusText, |
| enableScrollSwitch = false, |
| scrollSwitchThreshold = 120, |
| scrollSwitchDelay = 100, |
| tabPosition = 'top' |
| }, ref) => { |
| |
| const [currentActiveTabId, setCurrentActiveTabId] = useState<string>(''); |
| const isInitialized = useRef<boolean>(false); |
| |
| |
| const effectiveActiveTabId = useMemo(() => { |
| if (!tabs.length) return ''; |
| if (currentActiveTabId && tabs.find(tab => tab.id === currentActiveTabId)) { |
| return currentActiveTabId; |
| } |
| return tabs[0].id; |
| }, [tabs, currentActiveTabId]); |
|
|
| const containerRef = useRef<HTMLDivElement>(null); |
| const contentRef = useRef<HTMLDivElement>(null); |
| |
| |
| const scrollTimeoutRef = useRef<NodeJS.Timeout | null>(null); |
| const [isAtBoundary, setIsAtBoundary] = useState<'none' | 'top' | 'bottom'>('none'); |
| const lastScrollTop = useRef<number>(0); |
| const boundaryScrollStartRef = useRef<number>(0); |
| const overScrollDistanceRef = useRef<number>(0); |
|
|
| |
| useEffect(() => { |
| if (tabs.length > 0 && !isInitialized.current) { |
| const initialTabId = activeTabId && tabs.find(tab => tab.id === activeTabId) |
| ? activeTabId |
| : tabs[0].id; |
| |
| setCurrentActiveTabId(initialTabId); |
| isInitialized.current = true; |
| |
| |
| const initialTabItem = tabs.find(tab => tab.id === initialTabId); |
| if (initialTabItem) { |
| onTabChange?.(initialTabId, initialTabItem); |
| } |
| } |
| }, [tabs, activeTabId, onTabChange]); |
|
|
| |
| useEffect(() => { |
| if (effectiveActiveTabId && effectiveActiveTabId !== currentActiveTabId) { |
| setCurrentActiveTabId(effectiveActiveTabId); |
| |
| |
| const syncTabItem = tabs.find(tab => tab.id === effectiveActiveTabId); |
| if (syncTabItem) { |
| onTabChange?.(effectiveActiveTabId, syncTabItem); |
| } |
| } |
| }, [effectiveActiveTabId, currentActiveTabId, tabs, onTabChange]); |
|
|
| |
| useEffect(() => { |
| if (isInitialized.current && activeTabId && activeTabId !== currentActiveTabId) { |
| const tabExists = tabs.find(tab => tab.id === activeTabId); |
| if (tabExists) { |
| setCurrentActiveTabId(activeTabId); |
| } |
| } |
| }, [activeTabId, currentActiveTabId, tabs]); |
|
|
| |
| const activeTabItem = useMemo(() => { |
| if (!tabs.length || !effectiveActiveTabId) return undefined; |
| return tabs.find(tab => tab.id === effectiveActiveTabId); |
| }, [tabs, effectiveActiveTabId]); |
|
|
| const activeTabIndex = useMemo(() => { |
| if (!effectiveActiveTabId || !tabs.length) return 0; |
| const index = tabs.findIndex(tab => tab.id === effectiveActiveTabId); |
| return index >= 0 ? index : 0; |
| }, [tabs, effectiveActiveTabId]); |
|
|
| |
| const focusActiveTabContent = useCallback(() => { |
| if (contentRef.current) { |
| |
| const focusableElement = contentRef.current.querySelector('[tabindex="0"], input, textarea, button, [contenteditable="true"]') as HTMLElement; |
| if (focusableElement) { |
| focusableElement.focus(); |
| } |
| } |
| }, []); |
|
|
| |
| const handleTabActivation = useCallback((tabId: string, resetScroll = true) => { |
| const tabItem = tabs.find(tab => tab.id === tabId); |
| if (!tabItem || tabId === currentActiveTabId) return; |
|
|
| setCurrentActiveTabId(tabId); |
| onTabChange?.(tabId, tabItem); |
|
|
| |
| if (enableScrollSwitch) { |
| setIsAtBoundary('none'); |
| overScrollDistanceRef.current = 0; |
| } |
|
|
| |
| if (resetScroll) { |
| setTimeout(() => { |
| if (contentRef.current) { |
| contentRef.current.scrollTop = 0; |
| } |
| focusActiveTabContent(); |
| }, 50); |
| } else { |
| setTimeout(() => { |
| focusActiveTabContent(); |
| }, 100); |
| } |
| }, [tabs, currentActiveTabId, onTabChange, focusActiveTabContent, enableScrollSwitch]); |
|
|
| |
| useImperativeHandle(ref, () => ({ |
| focus: () => { |
| containerRef.current?.focus(); |
| }, |
| blur: () => { |
| containerRef.current?.blur(); |
| }, |
| setActiveTab: (tabId: string) => { |
| handleTabActivation(tabId); |
| }, |
| getActiveTab: () => activeTabItem, |
| getActiveTabIndex: () => activeTabIndex, |
| canSwitchNext: () => activeTabIndex < tabs.length - 1, |
| canSwitchPrev: () => activeTabIndex > 0, |
| switchToNext: () => { |
| if (activeTabIndex < tabs.length - 1) { |
| const nextTabId = tabs[activeTabIndex + 1].id; |
| handleTabActivation(nextTabId); |
| } |
| }, |
| switchToPrev: () => { |
| if (activeTabIndex > 0) { |
| const prevTabId = tabs[activeTabIndex - 1].id; |
| handleTabActivation(prevTabId); |
| } |
| } |
| }), [activeTabItem, activeTabIndex, tabs, handleTabActivation]); |
|
|
| |
| const handleTabClick = (tabId: string) => { |
| handleTabActivation(tabId); |
| }; |
|
|
| |
| useEffect(() => { |
| const handleKeyDown = (event: KeyboardEvent) => { |
| |
| const focused = document.activeElement; |
| const isOurElement = focused === containerRef.current || |
| containerRef.current?.contains(focused); |
| |
| if (!isOurElement) return; |
|
|
| switch (event.key) { |
| case 'ArrowLeft': |
| event.preventDefault(); |
| if (activeTabIndex > 0) { |
| const prevTabId = tabs[activeTabIndex - 1].id; |
| handleTabActivation(prevTabId); |
| } |
| break; |
| case 'ArrowRight': |
| event.preventDefault(); |
| if (activeTabIndex < tabs.length - 1) { |
| const nextTabId = tabs[activeTabIndex + 1].id; |
| handleTabActivation(nextTabId); |
| } |
| break; |
| default: |
| |
| break; |
| } |
| }; |
|
|
| const container = containerRef.current; |
| if (container) { |
| container.addEventListener('keydown', handleKeyDown); |
| return () => { |
| container.removeEventListener('keydown', handleKeyDown); |
| }; |
| } |
| }, [activeTabIndex, tabs, handleTabActivation]); |
|
|
| |
| useEffect(() => { |
| if (activeTabItem) { |
| |
| const delay = isInitialized.current ? 150 : 250; |
| const timeoutId = setTimeout(() => { |
| focusActiveTabContent(); |
| }, delay); |
| |
| return () => clearTimeout(timeoutId); |
| } |
| }, [activeTabItem, focusActiveTabContent]); |
|
|
| |
| const handleScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => { |
| if (!enableScrollSwitch || tabs.length <= 1) return; |
|
|
| const container = event.currentTarget; |
| const { scrollTop, scrollHeight, clientHeight } = container; |
| const currentScrollTop = scrollTop; |
| const scrollDirection = currentScrollTop > lastScrollTop.current ? 'down' : 'up'; |
| |
| |
| const isAtBottom = scrollTop + clientHeight >= scrollHeight - 5; |
| |
| const isAtTop = scrollTop <= 5; |
| |
| |
| if (isAtBottom && activeTabIndex < tabs.length - 1) { |
| if (isAtBoundary !== 'bottom') { |
| |
| setIsAtBoundary('bottom'); |
| boundaryScrollStartRef.current = currentScrollTop; |
| overScrollDistanceRef.current = 0; |
| } else if (scrollDirection === 'down') { |
| |
| overScrollDistanceRef.current += Math.abs(currentScrollTop - lastScrollTop.current) * 2; |
| |
| |
| if (overScrollDistanceRef.current >= scrollSwitchThreshold) { |
| if (scrollTimeoutRef.current) { |
| clearTimeout(scrollTimeoutRef.current); |
| } |
| |
| scrollTimeoutRef.current = setTimeout(() => { |
| const nextTabId = tabs[activeTabIndex + 1]?.id; |
| if (nextTabId) { |
| handleTabActivation(nextTabId, true); |
| } |
| setIsAtBoundary('none'); |
| overScrollDistanceRef.current = 0; |
| }, scrollSwitchDelay); |
| } |
| } |
| } |
| |
| else if (isAtTop && activeTabIndex > 0) { |
| if (isAtBoundary !== 'top') { |
| setIsAtBoundary('top'); |
| boundaryScrollStartRef.current = currentScrollTop; |
| overScrollDistanceRef.current = 0; |
| } else if (scrollDirection === 'up') { |
| overScrollDistanceRef.current += Math.abs(lastScrollTop.current - currentScrollTop) * 2; |
| |
| if (overScrollDistanceRef.current >= scrollSwitchThreshold) { |
| if (scrollTimeoutRef.current) { |
| clearTimeout(scrollTimeoutRef.current); |
| } |
| |
| scrollTimeoutRef.current = setTimeout(() => { |
| const prevTabId = tabs[activeTabIndex - 1]?.id; |
| if (prevTabId) { |
| handleTabActivation(prevTabId, true); |
| } |
| setIsAtBoundary('none'); |
| overScrollDistanceRef.current = 0; |
| }, scrollSwitchDelay); |
| } |
| } |
| } |
| |
| else if (!isAtBottom && !isAtTop) { |
| if (isAtBoundary !== 'none') { |
| setIsAtBoundary('none'); |
| overScrollDistanceRef.current = 0; |
| } |
| } |
| |
| |
| lastScrollTop.current = currentScrollTop; |
| }, [enableScrollSwitch, tabs, activeTabIndex, isAtBoundary, scrollSwitchThreshold, scrollSwitchDelay, handleTabActivation]); |
|
|
| |
| useEffect(() => { |
| return () => { |
| if (scrollTimeoutRef.current) { |
| clearTimeout(scrollTimeoutRef.current); |
| } |
| }; |
| }, []); |
|
|
| |
| if (!tabs.length) { |
| return ( |
| <div className={`tab-container-empty ${className || ''}`}> |
| No tabs available |
| </div> |
| ); |
| } |
|
|
| |
| const tabBar = ( |
| <div className={`tab-bar tab-bar-${tabPosition}`}> |
| <div className="tab-list"> |
| {tabs.map((tabItem, index) => ( |
| <div |
| key={tabItem.id} |
| className={`tab-item ${activeTabIndex === index ? 'tab-item-active' : ''} ${tabItem.disabled ? 'tab-item-disabled' : ''}`} |
| onClick={() => !tabItem.disabled && handleTabClick(tabItem.id)} |
| title={tabItem.tooltip || tabItem.title} |
| role="tab" |
| aria-selected={activeTabIndex === index} |
| tabIndex={tabItem.disabled ? -1 : 0} |
| > |
| <div className="tab-label"> |
| {tabItem.icon_url && ( |
| <img |
| src={tabItem.icon_url} |
| alt="" |
| className="tab-icon" |
| onError={(e) => { |
| (e.target as HTMLImageElement).style.display = 'none'; |
| }} |
| /> |
| )} |
| {tabItem.icon && ( |
| <div className="tab-icon-container"> |
| {tabItem.icon} |
| </div> |
| )} |
| <div className="tab-title"> |
| {tabItem.title} |
| </div> |
| </div> |
| </div> |
| ))} |
| </div> |
| </div> |
| ); |
|
|
| |
| const contentArea = ( |
| <div |
| ref={contentRef} |
| className="tab-content" |
| onScroll={enableScrollSwitch ? handleScroll : undefined} |
| > |
| {activeTabItem && contentRenderer(activeTabItem, true)} |
| </div> |
| ); |
|
|
| |
| const statusBar = showStatusBar && activeTabItem && getStatusText && ( |
| <div className="tab-status-bar"> |
| {getStatusText(activeTabItem)} |
| </div> |
| ); |
|
|
| return ( |
| <div |
| ref={containerRef} |
| className={`tab-container ${className || ''}`} |
| tabIndex={0} |
| > |
| {/* Render components in correct order based on tab position */} |
| {(tabPosition === 'top' || tabPosition === 'left') && tabBar} |
| {contentArea} |
| {(tabPosition === 'bottom' || tabPosition === 'right') && tabBar} |
| {statusBar} |
| </div> |
| ); |
| }); |
|
|
| |
| TabContainer.displayName = 'TabContainer'; |
|
|
| export default TabContainer; |
|
|