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 | 2x 3x 2x 2x 1x 1x 3x 2x 3x 2x | import React from "react";
import ReactDOM from "react-dom";
import { Button } from "@/components/ui/button";
import { Cpu, Wrench, Terminal, Monitor } from "lucide-react";
import clsx from "clsx";
export type MobilePanel = "code" | "compile" | "serial" | "board" | null;
export interface MobileLayoutProps {
isMobile: boolean;
mobilePanel: MobilePanel;
setMobilePanel: React.Dispatch<React.SetStateAction<MobilePanel>>;
headerHeight: number;
overlayZ: number;
// slots
codeSlot?: React.ReactNode;
compileSlot?: React.ReactNode;
serialSlot?: React.ReactNode;
boardSlot?: React.ReactNode;
portalContainer?: HTMLElement | null;
className?: string;
testId?: string;
onOpenPanel?: (panel: MobilePanel) => void;
onClosePanel?: () => void;
}
export const MobileLayout = React.memo(function MobileLayout({
isMobile,
mobilePanel,
setMobilePanel,
headerHeight,
overlayZ,
codeSlot,
compileSlot,
serialSlot,
boardSlot,
portalContainer = typeof document !== "undefined" ? document.body : null,
className,
testId = "mobile-layout",
onOpenPanel,
onClosePanel,
}: MobileLayoutProps) {
// helpers
const handleToggle = React.useCallback(
(panel: MobilePanel) => {
setMobilePanel((prev: MobilePanel) => (prev === panel ? null : panel));
if (panel === mobilePanel) {
onClosePanel?.();
} else {
onOpenPanel?.(panel);
}
},
[mobilePanel, setMobilePanel, onOpenPanel, onClosePanel],
);
// 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, 32px)",
paddingRight: "env(safe-area-inset-right, 32px)",
}}
>
<div className="pointer-events-auto sticky mr-4 mb-4" style={{ alignSelf: "flex-end" }}>
<div className="bg-black/95 rounded-full shadow-lg p-1 flex flex-col items-center space-y-2">
<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",
)}
>
<Cpu className="w-5 h-5" />
</Button>
<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",
)}
>
<Wrench className="w-5 h-5 opacity-80" />
</Button>
<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",
)}
>
<Terminal className="w-5 h-5" />
</Button>
<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",
)}
>
<Monitor className="w-5 h-5" />
</Button>
</div>
</div>
</div>
</div>
);
return (
<>
{isMobile && portalContainer && ReactDOM.createPortal(fabBar, portalContainer)}
{mobilePanel && (
<div
className={clsx("fixed left-0 right-0 bottom-0 bg-card p-0 flex flex-col w-screen", className)}
style={{
top: `${headerHeight}px`,
height: `calc(100vh - ${headerHeight}px)`,
zIndex: overlayZ,
}}
data-testid={testId}
>
<div className="flex-1 overflow-auto w-screen h-full">
{mobilePanel === "code" && codeSlot}
{mobilePanel === "compile" && compileSlot}
{mobilePanel === "serial" && serialSlot}
{mobilePanel === "board" && boardSlot}
</div>
</div>
)}
</>
);
});
MobileLayout.displayName = "MobileLayout";
|