Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

4.6 KB
Raw Blame
import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';
import { Menu, X } from 'lucide-react';
import { useEffect, useRef, type ReactNode } from 'react';

export function AppLayout(props: {
  sidebar: ReactNode;
  mainHeader?: ReactNode;
  main: ReactNode;
  detail?: ReactNode;
  detailOpen?: boolean;
  immersive?: boolean;
  sidebarCollapsed?: boolean;
  mobileSidebarOpen?: boolean;
  onOpenMobileSidebar?(): void;
  onCloseMobileSidebar?(): void;
}) {
  const l = useLocalizedCopy();
  const sidebarRef = useRef<HTMLElement>(null);
  const mobileTriggerRef = useRef<HTMLButtonElement>(null);
  const onCloseMobileSidebarRef = useRef(props.onCloseMobileSidebar);
  const mobileSidebarOpen = props.mobileSidebarOpen === true;
  const shellClassName = [
    'opencreator-shell',
    props.detailOpen ? 'has-detail' : undefined,
    props.sidebarCollapsed ? 'sidebar-collapsed' : undefined,
    props.immersive ? 'is-immersive' : undefined
  ].filter(Boolean).join(' ');

  useEffect(() => {
    onCloseMobileSidebarRef.current = props.onCloseMobileSidebar;
  }, [props.onCloseMobileSidebar]);

  useEffect(() => {
    if (!mobileSidebarOpen) return;

    getFocusableElements(sidebarRef.current)[0]?.focus();

    function handleKeyDown(event: KeyboardEvent) {
      if (event.key === 'Escape') {
        event.preventDefault();
        onCloseMobileSidebarRef.current?.();
        return;
      }
      if (event.key !== 'Tab') return;

      const focusable = getFocusableElements(sidebarRef.current);
      if (focusable.length === 0) return;
      const first = focusable[0]!;
      const last = focusable[focusable.length - 1]!;
      if (event.shiftKey && document.activeElement === first) {
        event.preventDefault();
        last.focus();
      } else if (!event.shiftKey && document.activeElement === last) {
        event.preventDefault();
        first.focus();
      }
    }

    document.addEventListener('keydown', handleKeyDown);
    return () => {
      document.removeEventListener('keydown', handleKeyDown);
      window.setTimeout(() => mobileTriggerRef.current?.focus(), 0);
    };
  }, [mobileSidebarOpen]);

  return (
    <main className={shellClassName}>
      {mobileSidebarOpen ? (
        <button
          aria-label={l("关闭导航遮罩", "Turn off navigation mask")}
          className="mobile-navigation-backdrop"
          onClick={props.onCloseMobileSidebar}
          type="button"
        />
      ) : null}
      {props.immersive ? null : <aside
        className="opencreator-sidebar-pane"
        aria-label={l("OpenCreator 导航", "OpenCreator navigation")}
        data-collapsed={props.sidebarCollapsed ? 'true' : 'false'}
        data-mobile-open={mobileSidebarOpen ? 'true' : 'false'}
        ref={sidebarRef}
      >
        <button
          aria-label={l("关闭导航", "Close navigation")}
          className="mobile-navigation-close"
          onClick={props.onCloseMobileSidebar}
          title={l("关闭导航", "Close navigation")}
          type="button"
        >
          <X aria-hidden="true" size={18} />
        </button>
        {props.sidebar}
      </aside>}
      <section
        className="opencreator-main-pane"
        aria-label={l("OpenCreator 工作区", "OpenCreator workspace")}
        data-has-main-header={props.mainHeader === undefined ? undefined : 'true'}
      >
        {props.mainHeader === undefined ? null : (
          <div className="opencreator-main-titlebar">{props.mainHeader}</div>
        )}
        {props.immersive ? null : <div className="mobile-navigation-toolbar">
          <button
            aria-expanded={mobileSidebarOpen}
            aria-label={l("打开导航", "Open navigation")}
            className="mobile-navigation-trigger"
            onClick={props.onOpenMobileSidebar}
            ref={mobileTriggerRef}
            title={l("打开导航", "Open navigation")}
            type="button"
          >
            <Menu aria-hidden="true" size={20} />
          </button>
        </div>}
        <div className="opencreator-main-content">{props.main}</div>
      </section>
      {props.detailOpen ? (
        <aside className="opencreator-detail-pane" aria-label={l("详情", "Details")}>
          {props.detail}
        </aside>
      ) : null}
    </main>
  );
}

function getFocusableElements(root: HTMLElement | null): HTMLElement[] {
  if (root === null) return [];
  return Array.from(
    root.querySelectorAll<HTMLElement>(
      'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
    )
  ).filter(element => !element.hasAttribute('disabled'));
}