auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

mobile-nav.tsx (3644B)


      1 import m from 'mithril';
      2 import { Nav, type NavItem } from '@component/nav';
      3 import { AccountActions, type UserMenuAttrs } from '@component/user-menu';
      4 
      5 export interface MobileNavToggleAttrs {
      6   expanded: boolean;
      7   onToggle: () => void;
      8 }
      9 
     10 export const MobileNavToggle: m.Component<MobileNavToggleAttrs> = {
     11   view({ attrs }) {
     12     const label = attrs.expanded ? 'Close navigation' : 'Open navigation';
     13 
     14     return (
     15       <button
     16         id="mobile-nav-toggle"
     17         class={`mobile-nav-toggle${attrs.expanded ? ' is-open' : ''}`}
     18         type="button"
     19         aria-controls="mobile-navigation"
     20         aria-expanded={attrs.expanded ? 'true' : 'false'}
     21         aria-label={label}
     22         onclick={attrs.onToggle}
     23       >
     24         <span class="mobile-nav-toggle-bars" aria-hidden="true"></span>
     25       </button>
     26     );
     27   },
     28 };
     29 
     30 export const MobileNavAccount: m.Component<UserMenuAttrs> = {
     31   view({ attrs }) {
     32     return (
     33       <section class="mobile-nav-account" aria-label="Account">
     34         <div class="menu-heading">
     35           <span class="menu-heading-title">{attrs.name}</span>
     36           <span class="menu-heading-meta">{attrs.meta}</span>
     37         </div>
     38         <AccountActions
     39           accountHref={attrs.accountHref}
     40           theme={attrs.theme}
     41           onCycleTheme={attrs.onCycleTheme}
     42           onSignOut={attrs.onSignOut}
     43         />
     44       </section>
     45     );
     46   },
     47 };
     48 
     49 export interface MobileNavAttrs {
     50   label: string;
     51   ariaLabel: string;
     52   items: NavItem[];
     53   accountPanel?: m.Children;
     54   open: boolean;
     55   onClose: () => void;
     56 }
     57 
     58 export const MobileNav: m.Component<MobileNavAttrs> = {
     59   oncreate(this: any, vnode: m.Vnode<MobileNavAttrs>) {
     60     this.attrs = vnode.attrs;
     61     this.open = Boolean(vnode.attrs.open);
     62     this.applyState(this.open);
     63     this.onKeydown = (event: KeyboardEvent) => {
     64       if (event.key === 'Escape' && this.open) {
     65         this.attrs.onClose();
     66         m.redraw();
     67       }
     68     };
     69     window.addEventListener('keydown', this.onKeydown);
     70   },
     71 
     72   onupdate(this: any, vnode: m.Vnode<MobileNavAttrs>) {
     73     this.attrs = vnode.attrs;
     74     const open = Boolean(vnode.attrs.open);
     75     if (open === this.open) return;
     76 
     77     if (open) {
     78       this.focusBefore = document.activeElement;
     79     }
     80 
     81     this.applyState(open);
     82 
     83     if (!open) {
     84       (this.focusBefore as HTMLElement | null)?.focus?.();
     85       this.focusBefore = null;
     86     }
     87   },
     88 
     89   onremove(this: any) {
     90     window.removeEventListener('keydown', this.onKeydown);
     91     this.applyState(false);
     92   },
     93 
     94   applyState(this: any, open: boolean) {
     95     this.open = open;
     96     document.documentElement.classList.toggle('mobile-nav-open', open);
     97 
     98     const main = document.querySelector('.main') as HTMLElement | null;
     99     if (main) (main as any).inert = open;
    100   },
    101 
    102   view({ attrs }) {
    103     const items = attrs.items.map((item) => ({
    104       ...item,
    105       onclick: (event: Event) => {
    106         attrs.onClose();
    107         item.onclick?.(event);
    108       },
    109     }));
    110 
    111     return (
    112       <div
    113         id="mobile-navigation"
    114         class={`mobile-nav${attrs.open ? ' is-open' : ''}`}
    115         aria-hidden={attrs.open ? null : 'true'}
    116         onclick={(event: MouseEvent) => {
    117           const target = event.target as Element | null;
    118           if (!target?.closest('.nav-item')) {
    119             if (!target?.closest('.menu-item, button, a')) attrs.onClose();
    120           }
    121         }}
    122       >
    123         <Nav label={attrs.label} ariaLabel={attrs.ariaLabel} items={items} />
    124         {attrs.accountPanel ? (
    125           <>
    126             <div class="mobile-nav-divider" aria-hidden="true"></div>
    127             {attrs.accountPanel}
    128           </>
    129         ) : null}
    130       </div>
    131     );
    132   },
    133 };