All files / client/src/components/features mobile-layout.tsx

85.71% Statements 12/14
80.55% Branches 29/36
71.42% Functions 5/7
84.61% Lines 11/13

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                            2x                       5x       5x   3x 3x             5x                                         1x                             1x                                                                                           1x                                   5x                                 2x  
import React from "react";
import ReactDOM from "react-dom";
import { Button } from "@/components/ui/button";
import { Cpu, Wrench, Terminal, Monitor, MessageCircleQuestion } from "lucide-react";
import clsx from "clsx";
 
export type MobilePanel = "code" | "compile" | "serial" | "board" | "tutor";
 
interface MobileLayoutProps {
  readonly isMobile: boolean;
  readonly isTablet?: boolean;
  readonly showTutor?: boolean;
  readonly mobilePanel: MobilePanel;
  readonly setMobilePanel: React.Dispatch<React.SetStateAction<MobilePanel>>;
  readonly overlayZ: number;
 
  readonly portalContainer?: HTMLElement | null;
  readonly className?: string;
  readonly testId?: string;
  readonly onOpenPanel?: (panel: MobilePanel) => void;
}
 
export const MobileLayout = React.memo(function MobileLayout({
  isMobile,
  isTablet = false,
  showTutor = false,
  mobilePanel,
  setMobilePanel,
  overlayZ,
  portalContainer = typeof document === "undefined" ? null : document.body,
  className,
  testId = "mobile-layout",
  onOpenPanel,
}: MobileLayoutProps) {
  const showCompactNavigation = isMobile || (isTablet && showTutor);
  // The panel surfaces themselves stay mounted in the responsive workspace.
  // This component owns navigation only, so selecting a surface never moves
  // or unmounts the Monaco editor.
  const handleToggle = React.useCallback(
    (panel: MobilePanel) => {
      setMobilePanel(panel);
      if (panel !== mobilePanel) onOpenPanel?.(panel);
    },
    [mobilePanel, setMobilePanel, onOpenPanel],
  );
 
  // render fab bar via portal
  const fabBar = (
    <div
      className="fixed inset-0 pointer-events-none"
      style={{ zIndex: overlayZ }}
      data-testid="mobile-fab-container"
    >
      <div
        className="absolute inset-0 flex items-end justify-end p-8"
        style={{
          paddingBottom: "env(safe-area-inset-bottom, var(--ui-safe-area-bottom))",
          paddingRight: "env(safe-area-inset-right, var(--ui-safe-area-right))",
        }}
      >
        <div className="pointer-events-auto sticky mr-4 mb-4" style={{ alignSelf: "flex-end" }}>
          <div
            className="bg-black/95 rounded-full shadow-lg p-2 flex flex-col items-center space-y-3"
            data-mobile-fab-toolbar
          >
            {isMobile && <Button
              variant="ghost"
              size="icon"
              aria-label="Code Editor"
              onClick={() => handleToggle("code")}
              className={clsx(
                "w-[var(--ui-button-height)] h-[var(--ui-button-height)] rounded-full",
                mobilePanel === "code"
                  ? "bg-blue-600 text-white hover:bg-blue-700"
                  : "bg-transparent text-muted-foreground",
              )}
              data-mobile-fab-button
            >
              <Cpu className="!w-10 !h-10" />
            </Button>}
            {isMobile && <Button
              variant="ghost"
              size="icon"
              aria-label="Compilation Output"
              onClick={() => handleToggle("compile")}
              className={clsx(
                "w-[var(--ui-button-height)] h-[var(--ui-button-height)] rounded-full",
                mobilePanel === "compile"
                  ? "bg-green-600 text-white hover:bg-green-700"
                  : "bg-transparent text-muted-foreground",
              )}
              data-mobile-fab-button
            >
              <Wrench className="!w-10 !h-10 opacity-80" />
            </Button>}
            {isMobile && <Button
              variant="ghost"
              size="icon"
              aria-label="Serial Output"
              onClick={() => handleToggle("serial")}
              className={clsx(
                "w-[var(--ui-button-height)] h-[var(--ui-button-height)] rounded-full",
                mobilePanel === "serial"
                  ? "bg-amber-600 text-white hover:bg-amber-700"
                  : "bg-transparent text-muted-foreground",
              )}
              data-mobile-fab-button
            >
              <Terminal className="!w-10 !h-10" />
            </Button>}
            {isMobile && <Button
              variant="ghost"
              size="icon"
              aria-label="Arduino Board"
              onClick={() => handleToggle("board")}
              className={clsx(
                "w-[var(--ui-button-height)] h-[var(--ui-button-height)] rounded-full",
                mobilePanel === "board"
                  ? "bg-sky-600 text-white hover:bg-sky-700"
                  : "bg-transparent text-muted-foreground",
              )}
              data-mobile-fab-button
            >
              <Monitor className="!w-10 !h-10" />
            </Button>}
            {showTutor && (
              <Button
                variant="ghost"
                size="icon"
                aria-label="Tutor"
                onClick={() => handleToggle("tutor")}
                className={clsx(
                  "w-[var(--ui-button-height)] h-[var(--ui-button-height)] rounded-full",
                  mobilePanel === "tutor"
                    ? "bg-violet-600 text-white hover:bg-violet-700"
                    : "bg-transparent text-muted-foreground",
                )}
                data-mobile-fab-button
              >
                <MessageCircleQuestion className="!w-10 !h-10" />
              </Button>
            )}
          </div>
        </div>
      </div>
    </div>
  );
 
  return (
    <>
      {showCompactNavigation && portalContainer && ReactDOM.createPortal(fabBar, portalContainer)}
      {showCompactNavigation && (
        <div
          className={clsx("sr-only", className)}
          style={{ zIndex: overlayZ }}
          data-testid={testId}
          aria-live="polite"
        >
          Active mobile panel: {mobilePanel}
        </div>
      )}
    </>
  );
});
 
MobileLayout.displayName = "MobileLayout";