import { forwardRef, ReactNode } from 'react'; import classnames from 'classnames'; import { TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_ACTIVE, TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_BASE, TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_HOVER, TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_ACTIVE, TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_BASE, TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_HOVER, TOKEN_ROUNDING_0, } from '@swipebox/morphe-design-tokens'; import Box from '../Box'; import { useDefaultLabelContext } from '../contexts/DefaultLabelProvider'; import Flex from '../Flex'; import Icon from '../Icon'; import icons from '../icons/index'; import Indicator from '../Indicator'; import style from '../Tabs.css'; import TapAreaLink from '../TapAreaLink'; import TextUI from '../TextUI'; import useFocusVisible from '../useFocusVisible'; import useExperimentalTheme from '../utils/useExperimentalTheme'; import useInteractiveStates from '../utils/useInteractiveStates'; type TabType = { notificationAccessibilityLabel?: string; href: string; id?: string; indicator?: 'dot' | number; text: ReactNode; }; type TabProps = TabType & { bgColor: 'default' | 'transparent'; dataTestId?: string; index: number; icon?: keyof typeof icons; size?: 'sm' | 'lg'; isActive: boolean; onChange: (arg1: { event: React.MouseEvent | React.KeyboardEvent; readonly activeTabIndex: number; dangerouslyDisableOnNavigation: () => void; }) => void; }; const COLORS = Object.freeze({ default: { base: TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_BASE, hover: TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_HOVER, active: TOKEN_COLOR_BACKGROUND_TABS_DEFAULT_ACTIVE, }, transparent: { base: TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_BASE, hover: TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_HOVER, active: TOKEN_COLOR_BACKGROUND_TABS_TRANSPARENT_ACTIVE, }, }); const TabWithForwardRef = forwardRef(function Tab( { notificationAccessibilityLabel, bgColor, href, icon, size, indicator, id, index, isActive, onChange, text, dataTestId, }: TabProps, ref, ) { const { handleOnMouseEnter, handleOnMouseLeave, handleOnBlur, handleOnFocus, handleOnMouseDown, handleOnMouseUp, isFocused, isHovered, isActive: isPressed, } = useInteractiveStates(); const { isFocusVisible } = useFocusVisible(); const theme = useExperimentalTheme(); const { accessibilityNotificationLabel } = useDefaultLabelContext('Tabs'); const isRtl = typeof document === 'undefined' ? false : document?.dir === 'rtl'; let color = COLORS[bgColor].base; if (!isActive) { if (isPressed) { color = COLORS[bgColor].active; } else if (isHovered || isFocused) { color = COLORS[bgColor].hover; } } const underlineBottomPos = size === 'sm' ? 8 : 2; const paddingX = size === 'sm' ? 3 : 1.5; return (
| undefined' is not assignable to type 'LegacyRef | undefined'. ref={theme.MAIN ? ref : undefined} accessibilityCurrent={isActive ? 'page' : undefined} dataTestId={dataTestId} href={href} onBlur={handleOnBlur} onFocus={handleOnFocus} onMouseDown={handleOnMouseDown} onMouseEnter={handleOnMouseEnter} onMouseLeave={handleOnMouseLeave} onMouseUp={handleOnMouseUp} onTap={({ event, dangerouslyDisableOnNavigation }) => { onChange({ activeTabIndex: index, event, dangerouslyDisableOnNavigation, }); }} rounding={theme.MAIN ? 4 : 2} tapStyle={!isActive && !theme.MAIN ? 'compress' : 'none'} > {icon ? : null} {text} {indicator === 'dot' && ( )} {/* Number.isFinite will return false for a string or undefined */} {typeof indicator === 'number' && Number.isFinite(indicator) && ( )} {isActive && ( {/* Active tab underline */} )}
); }); TabWithForwardRef.displayName = 'Tab'; export default TabWithForwardRef;