import { Children, cloneElement, type ComponentPropsWithoutRef, Fragment, isValidElement, type ReactNode, useMemo, } from 'react'; import clsx from 'clsx'; import { useTranslation } from 'react-i18next'; import { Alert } from '../../components/Alert'; import { useCookiesConsent } from '../../hooks/useCookiesConsent'; import { useToast } from '../../hooks/useToast'; import { ButtonBeta as Button } from '../ButtonBeta'; import PageLayoutBreadcrumb from './components/PageLayoutBreadcrumb'; import PageLayoutContent from './components/PageLayoutContent'; import PageLayoutHeader from './components/PageLayoutHeader'; import PageLayoutHelpZone from './components/PageLayoutHelpZone'; import PageLayoutOverlay from './components/PageLayoutOverlay'; import PageLayoutSidebarLeft from './components/PageLayoutSidebarLeft'; import PageLayoutSidebarRight from './components/PageLayoutSidebarRight'; import { type NoPaddingConfig, PageLayoutContext, type PageLayoutScrollMode, type PageLayoutVariant, } from './PageLayoutContext'; export interface PageLayoutProps extends ComponentPropsWithoutRef<'div'> { children: ReactNode; /** Layout variant: "centered" (default) centers everything in a max-width container, * "fullpage" extends sidebars to screen edges with centered content */ variant?: PageLayoutVariant; /** Add a gap between sidebars and content */ withGap?: boolean; /** Scroll mode: "columns" — each column scrolls independently (header not fixed), * "page" — normal page scroll with sticky breadcrumb (header not fixed) */ scrollMode?: PageLayoutScrollMode; /** Disable padding on specific columns */ noPadding?: NoPaddingConfig; } /** * Children.toArray only flattens arrays, not fragments — a `<>...` * wrapper stays a single element, so recurse into fragments explicitly. * Each nested toArray restarts its keys at `.0`, so the fragment key is * prefixed to the keys of its children to keep them unique among siblings. */ function flattenFragments(children: ReactNode, keyPrefix = ''): ReactNode[] { return Children.toArray(children).flatMap((child) => { if (!isValidElement(child)) return [child]; const key = `${keyPrefix}${child.key ?? ''}`; if (child.type === Fragment) { return flattenFragments(child.props.children, key); } return [keyPrefix ? cloneElement(child, { key }) : child]; }); } /** * Detect which compound children are present. */ function analyzeChildren(children: ReactNode) { let hasLeftSidebar = false; let hasRightSidebar = false; let breadcrumb: ReactNode = null; let overlay: ReactNode = null; let helpZone: ReactNode = null; const headerChildren: ReactNode[] = []; const mainChildren: ReactNode[] = []; flattenFragments(children).forEach((child) => { if (!isValidElement(child)) return; switch (child.type) { case PageLayoutHeader: headerChildren.push(child); break; case PageLayoutBreadcrumb: breadcrumb = child; break; case PageLayoutOverlay: overlay = child; break; case PageLayoutHelpZone: helpZone = child; break; case PageLayoutSidebarLeft: hasLeftSidebar = true; mainChildren.push(child); break; case PageLayoutSidebarRight: hasRightSidebar = true; mainChildren.push(child); break; case PageLayoutContent: mainChildren.push(child); break; default: mainChildren.push(child); break; } }); return { hasLeftSidebar, hasRightSidebar, breadcrumb, overlay, helpZone, headerChildren, mainChildren, }; } const Root = ({ children, variant = 'centered', withGap = false, scrollMode, noPadding, className, ...props }: PageLayoutProps) => { const { t } = useTranslation(); const { showCookiesConsent, handleConsultCookies, handleCloseCookiesConsent, } = useCookiesConsent(); const contextValue = useMemo( () => ({ variant, scrollMode, noPadding }), [variant, scrollMode, noPadding], ); const toast = useToast(); const renderToaster = toast.renderToaster(); const { hasLeftSidebar, hasRightSidebar, breadcrumb, overlay, helpZone, headerChildren, mainChildren, } = analyzeChildren(children); const mainAreaClasses = clsx('pagelayout-mainarea', { 'has-both-sidebars': hasLeftSidebar && hasRightSidebar, 'has-left-sidebar-only': hasLeftSidebar && !hasRightSidebar, 'has-right-sidebar-only': !hasLeftSidebar && hasRightSidebar, 'has-gap': withGap, }); const isFullpage = variant === 'fullpage'; return (
{headerChildren} {headerChildren.length > 0 && (
)} {/* In centered mode, breadcrumb is outside main-area (full container width) */} {!isFullpage && breadcrumb}
{/* In fullpage mode, breadcrumb is inside main-area (content column width) */} {isFullpage && breadcrumb} {mainChildren}
{overlay} {showCookiesConsent && ( {t('rgpd.cookies.banner.button.consult')} } onClose={handleCloseCookiesConsent} > {t('rgpd.cookies.banner.text1')} )}
{helpZone} {renderToaster} ); }; Root.displayName = 'PageLayout'; const PageLayout = Object.assign(Root, { Header: PageLayoutHeader, Breadcrumb: PageLayoutBreadcrumb, SidebarLeft: PageLayoutSidebarLeft, Content: PageLayoutContent, SidebarRight: PageLayoutSidebarRight, Overlay: PageLayoutOverlay, HelpZone: PageLayoutHelpZone, }); export default PageLayout;