All files / apps / web/src / features / shell / components / AppShell index.tsx

100% Statements 25/25
92.85% Branches 13/14
100% Functions 8/8
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                            19x               14x   14x 14x   4x   14x     2x     14x 14x 14x   14x 4x 4x 4x       14x         19x 14x 14x 14x     14x   14x                                                                                            
import { cn } from 'cn'
import { useEffect, useMemo, useRef, useState, type ReactElement, type ReactNode } from 'react'
import { MEDIA_QUERY } from '@/constants/media'
import { ShellDrawerContext, type ShellDrawer } from '../../hooks/use-shell-drawer'
 
interface AppShellProps {
  /** The narrow icon strip; left out on screens that have none. */
  rail?: ReactNode
  sidebar: ReactNode
  children: ReactNode
  /** The side panel (a thread, channel details). Left out, the main pane takes its room. */
  aside?: ReactNode
}
 
const PANEL = 'border bg-card shadow-soft backdrop-blur-xl md:rounded-2xl'
 
// While the drawer is open it behaves as a dialog: focus moves in, Escape closes it, and focus
// goes back to the control that opened it. Widening to desktop closes it, where it is no drawer.
function useDrawerFocus(
  isOpen: boolean,
  close: () => void,
): React.RefObject<HTMLDivElement | null> {
  const drawerRef = useRef<HTMLDivElement>(null)
 
  useEffect(() => {
    if (!isOpen) return
 
    const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null
    const desktop =
      typeof window.matchMedia === 'function' ? window.matchMedia(MEDIA_QUERY.desktop) : null
 
    function onKeyDown(event: KeyboardEvent): void {
      if (event.key === 'Escape') close()
    }
 
    drawerRef.current?.focus()
    document.addEventListener('keydown', onKeyDown)
    desktop?.addEventListener('change', close)
 
    return () => {
      document.removeEventListener('keydown', onKeyDown)
      desktop?.removeEventListener('change', close)
      opener?.focus()
    }
  }, [isOpen, close])
 
  return drawerRef
}
 
// The frame of design 2.4, drawn as in the prototype: rounded glass panels floating on the
// page background. Under 768px the rail and sidebar become a drawer (useShellDrawer).
export const AppShell = ({ rail, sidebar, children, aside }: AppShellProps): ReactElement => {
  const [isOpen, setIsOpen] = useState(false)
  const drawer = useMemo<ShellDrawer>(
    () => ({ isOpen, open: () => setIsOpen(true), close: () => setIsOpen(false) }),
    [isOpen],
  )
  const drawerRef = useDrawerFocus(isOpen, drawer.close)
 
  return (
    <ShellDrawerContext.Provider value={drawer}>
      <div className="relative flex h-dvh text-foreground md:gap-3 md:p-3">
        {isOpen ? (
          <button
            type="button"
            aria-label="Close menu"
            onClick={drawer.close}
            className="absolute inset-0 z-30 bg-black/40 md:hidden"
          />
        ) : null}
        <div
          ref={drawerRef}
          tabIndex={-1}
          className={cn(
            'flex shrink-0 overflow-hidden border bg-sidebar text-sidebar-foreground shadow-soft outline-none backdrop-blur-xl md:rounded-2xl',
            // The cap leaves 44px of backdrop to tap on a 320px screen.
            'max-md:fixed max-md:inset-y-0 max-md:left-0 max-md:z-40 max-md:max-w-[calc(100vw-2.75rem)] max-md:transition-transform',
            isOpen ? 'max-md:shadow-float' : 'max-md:invisible max-md:-translate-x-full',
          )}
        >
          {rail}
          <nav aria-label="Sidebar" className="flex w-64 min-w-0 flex-col">
            {sidebar}
          </nav>
        </div>
        {/* Behind the open drawer the page cannot be reached by Tab or a screen reader. */}
        <main inert={isOpen} className={cn(PANEL, 'flex min-w-0 flex-1 flex-col overflow-hidden')}>
          {children}
        </main>
        {aside ? (
          <aside
            aria-label="Side panel"
            inert={isOpen}
            className={cn(
              PANEL,
              'w-96 shrink-0 overflow-y-auto max-md:fixed max-md:inset-0 max-md:z-20 max-md:w-auto',
            )}
          >
            {aside}
          </aside>
        ) : null}
      </div>
    </ShellDrawerContext.Provider>
  )
}