SPB Git

Repository Public

MobileNav.tsx 3.9 KB 116 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : agentilab-web8 * File             : components/layout/MobileNav.tsx9 * Module           : User Interface10 * Repository       : https://git.spboucher.ai/agentilab-web11 * Project Website  : Not specified12 * Created          : 2026-12-0513 * Last Modified    : 2026-12-0514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : Proprietary34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243'use client';4445import { useEffect, useRef } from 'react';46import type { ReactNode } from 'react';4748interface MobileNavProps {49  menuLabel: string;50  openLabel: string;51  closeLabel: string;52  children: ReactNode;53}5455/**56 * Menu mobile. Le balisage est un <details> : il s’ouvre sans JavaScript.57 * Ce composant y ajoute la fermeture par Échap avec restitution du focus,58 * le blocage du défilement et la fermeture après navigation.59 */60export function MobileNav({ menuLabel, openLabel, closeLabel, children }: MobileNavProps) {61  const detailsRef = useRef<HTMLDetailsElement>(null);62  const summaryRef = useRef<HTMLElement>(null);6364  useEffect(() => {65    const details = detailsRef.current;66    const summary = summaryRef.current;67    if (!details || !summary) return;6869    const sync = () => {70      document.documentElement.classList.toggle('nav-open', details.open);71      summary.setAttribute('aria-label', details.open ? closeLabel : openLabel);72    };73    const close = (restoreFocus: boolean) => {74      if (!details.open) return;75      details.open = false;76      if (restoreFocus) summary.focus();77    };78    const onKey = (event: KeyboardEvent) => {79      if (event.key === 'Escape' && details.open) {80        event.preventDefault();81        close(true);82      }83    };84    const onClick = (event: MouseEvent) => {85      if ((event.target as HTMLElement).closest('a')) close(false);86    };87    const wide = window.matchMedia('(min-width: 900px)');88    const onWide = () => {89      if (wide.matches) close(false);90    };9192    sync();93    details.addEventListener('toggle', sync);94    details.addEventListener('click', onClick);95    document.addEventListener('keydown', onKey);96    wide.addEventListener('change', onWide);97    return () => {98      details.removeEventListener('toggle', sync);99      details.removeEventListener('click', onClick);100      document.removeEventListener('keydown', onKey);101      wide.removeEventListener('change', onWide);102      document.documentElement.classList.remove('nav-open');103    };104  }, [openLabel, closeLabel]);105106  return (107    <details className="mnav" ref={detailsRef}>108      <summary ref={summaryRef} aria-label={openLabel}>109        <span className="mnav__icon" aria-hidden="true" />110        <span aria-hidden="true">{menuLabel}</span>111      </summary>112      <div className="mnav__panel">{children}</div>113    </details>114  );115}116
Files main
Menu

Theme