import { Children, cloneElement, isValidElement, useEffect, useState, type ReactElement, type ReactNode, type SyntheticEvent, } from 'react'; import { AppBar as MuiAppBar, Toolbar as MuiToolbar, type AppBarProps, IconButton, Box, InputBase, Drawer, Tabs, Tab, } from '@mui/material'; import RdsButton from '../rds-button/rds-button'; import ClearIcon from '@mui/icons-material/Clear'; import { Dehaze as DehazeIcon } from '@mui/icons-material'; import LogoutIcon from '@mui/icons-material/Logout'; import Person from '@mui/icons-material/Person'; import Close from '@mui/icons-material/Close'; import { ProfileMenu } from './ProfileMenu'; import { useRdsTokens } from '../shared/hooks/useRdsTokens'; import clsx from 'clsx'; import "./rds-app-bar.scss"; export type RdsAppBarSize = 'small' | 'medium' | 'large'; export interface RdsAppBarProps extends Omit { title?: string; rightActions?: ReactNode; centerContent?: ReactNode; size?: RdsAppBarSize; logo?: ReactNode; showLogo?: boolean; onMenuClick?: () => void; showMenuButton?: boolean; actions?: ReactNode; userName?: string; userEmail?: string; searchValue?: string; onSearchChange?: (value: string) => void; searchPlaceholder?: string; tabs?: Array; tabValue?: number; onTabChange?: (value: number) => void; subHeader?: ReactNode; showSearch?: boolean; variantStyle?: string; overflowContent?: ReactNode; } const toolbarHeights: Record = { small: 55, medium: 64, large: 80, }; const RdsAppBar = ({ title, rightActions, centerContent, children, size = 'medium', logo, showLogo = true, onMenuClick, showMenuButton = false, actions, userName, userEmail, searchValue, onSearchChange, searchPlaceholder, tabs, tabValue, onTabChange, subHeader, showSearch = true, variantStyle, overflowContent, ...props }:RdsAppBarProps) => { const tokens = useRdsTokens(); const [overflowOpen, setOverflowOpen] = useState(false); const [isSmallScreen, setIsSmallScreen] = useState(false); const [localBottomActive, setLocalBottomActive] = useState(0); useEffect(() => { const variantLower = variantStyle ? String(variantStyle).toLowerCase() : ''; const tabletBottomNavVariants = ['withmenubutton', 'withactions', 'withtabs', 'withnotificationbadge', 'withlogoandtabs']; const needsTabletBottomNav = tabletBottomNavVariants.includes(variantLower); const threshold = variantLower === 'withnotificationbadge' ? 1280 : needsTabletBottomNav ? 840 : 420; // Storybook viewport addon can report the full manager width on the first paint // before the iframe is resized — prefer layout width and keep observing. const getViewportWidth = () => Math.min( window.innerWidth || Number.POSITIVE_INFINITY, document.documentElement?.clientWidth || Number.POSITIVE_INFINITY, document.body?.clientWidth || Number.POSITIVE_INFINITY, ); const checkScreenSize = () => { setIsSmallScreen(getViewportWidth() <= threshold); }; checkScreenSize(); window.addEventListener('resize', checkScreenSize); const resizeObserver = typeof ResizeObserver !== 'undefined' ? new ResizeObserver(() => checkScreenSize()) : null; resizeObserver?.observe(document.documentElement); // Catch delayed Storybook iframe viewport application after reload. const rafId = window.requestAnimationFrame(checkScreenSize); const timeoutId = window.setTimeout(checkScreenSize, 100); return () => { window.removeEventListener('resize', checkScreenSize); resizeObserver?.disconnect(); window.cancelAnimationFrame(rafId); window.clearTimeout(timeoutId); }; }, [variantStyle]); const colorClass = props.color === 'primary' ? ' rds-header--primary' : props.color === 'secondary' ? ' rds-header--secondary' : props.color === 'transparent' ? ' rds-header--transparent' : ''; const variantClass = variantStyle ? ` rds-header--variant-${String(variantStyle).toLowerCase().replace(/[^a-z0-9]+/g, '')}` : ''; return ( {showMenuButton && ( )} {showLogo && logo && {logo}} {centerContent && {centerContent}} {Array.isArray(tabs) && typeof tabValue === 'number' && typeof onTabChange === 'function' && ( onTabChange(v)} textColor="primary" indicatorColor="primary" > {tabs.map((tab, idx) => typeof tab === 'string' ? ( ) : ( )} key={tab.label || 'tab-item'} /> ) )} )} {title} {showSearch && typeof searchValue === 'string' && typeof onSearchChange === 'function' && (
onSearchChange(e.target.value)} /> {searchValue && ( onSearchChange('')} > )}
)} {rightActions && {rightActions}} {userName && userEmail ? ( }, { label: 'Logout', icon: , sx: { color: 'var(--rds-error-main)' } }, ]} /> ) : ( actions && {actions} )} {children} {overflowContent && !isSmallScreen ? ( <> setOverflowOpen(true)} size="small" > setOverflowOpen(false)} slotProps={{ paper: { sx: { width: tokens.space(40) } } }}> setOverflowOpen(false)} aria-label="close"> {overflowContent} ) : null} {isSmallScreen && ( (variantStyle && variantStyle.toLowerCase() === 'withmenubutton') ? ( {Array.isArray(tabs) && tabs.map((t, i) => { const label = typeof t === 'string' ? t : t.label || String(i); const tabKey = typeof t === 'string' ? t : String((t as { label?: string }).label || 'tab-item'); const isActive = tabValue === i; return ( typeof onTabChange === 'function' && onTabChange(i)} className={`rds-bottom-nav-tab ${isActive ? 'rds-bottom-nav-tab--active' : ''}`} style="transparent" size="small" layout="text-only" text={label} /> ); })} 28 Days Left ) : ( (Array.isArray(tabs) && typeof tabValue === 'number' && typeof onTabChange === 'function') ? ( {tabs.map((t, i) => { const label = typeof t === 'string' ? t : t.label || String(i); const tabKey = typeof t === 'string' ? t : String((t as { label?: string }).label || 'tab-item'); const isActive = tabValue === i; return ( onTabChange(i)} className={`rds-bottom-nav-tab ${isActive ? 'rds-bottom-nav-tab--active' : ''}`} style="transparent" size="small" layout="text-only" text={label} /> ); })} ) : ( overflowContent ? ( {isValidElement<{ children?: ReactNode }>(overflowContent) && overflowContent.props.children ? Children.toArray(overflowContent.props.children).map((child, i) => { if (isValidElement(child)) { const childProps = (child as ReactElement<{ onClick?: (e: SyntheticEvent) => void; className?: string }>).props; const existingOnClick = childProps.onClick; const childClassName = `${childProps.className ? `${childProps.className} ` : ''}rds-bottom-nav-tab`; const childKey = (child as ReactElement).key != null ? String((child as ReactElement).key) : String(childProps.className || 'overflow-item'); return cloneElement( child as ReactElement>, { key: childKey, onClick: (e: SyntheticEvent) => { existingOnClick?.(e); setLocalBottomActive(i); }, className: localBottomActive === i ? `${childClassName} rds-bottom-nav-tab--active` : childClassName, }, ); } return {child}; }) : overflowContent} ) : null ) ) )}
{subHeader &&
{subHeader}
}
); }; RdsAppBar.displayName = 'RdsAppBar'; export default RdsAppBar;