mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add device toggle to preview bar
This commit is contained in:
4 files changed
+173
-127
No files matched your search
@@ -14,6 +14,7 @@ import { FolderOpen, ArrowLeft, Bug, Copy, Check, Play, Loader2, MenuIcon, Code,
|
||||
import { useIsMobile } from '@/hooks/useIsMobile';
|
||||
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
|
||||
import { BrowserAddressBar } from '@/components/ui/browser-address-bar';
|
||||
import { type DeviceMode } from '@/components/ui/device-toggle';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
@@ -90,6 +91,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
|
||||
const [deployDialogOpen, setDeployDialogOpen] = useState(false);
|
||||
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||
const [deviceMode, setDeviceMode] = useState<DeviceMode>('laptop');
|
||||
const previewContainerRef = useRef<HTMLDivElement>(null);
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
const { fs } = useFS();
|
||||
@@ -659,58 +661,74 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
isFullscreen && "fixed inset-0 z-[100] bg-background"
|
||||
)}
|
||||
>
|
||||
{/* Always show browser address bar */}
|
||||
<div className="h-12 flex items-center w-full">
|
||||
<BrowserAddressBar
|
||||
currentPath={currentPath}
|
||||
onNavigate={hasBuiltProject ? navigateIframe : undefined}
|
||||
onRefresh={hasBuiltProject ? refreshIframe : undefined}
|
||||
navigationHistory={navigationHistory}
|
||||
leftContent={(
|
||||
{/* Top navigation bar */}
|
||||
<div className="h-12 flex items-center gap-2 p-2 border-b bg-background w-full">
|
||||
{/* Left side - fullscreen toggle */}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={toggleFullscreen}
|
||||
className="h-8 w-8 p-0"
|
||||
title={isFullscreen ? 'Exit immersive view' : 'Enter immersive view'}
|
||||
>
|
||||
{isFullscreen ? (
|
||||
<Shrink className="h-4 w-4" />
|
||||
) : (
|
||||
<Expand className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
|
||||
{/* Center - address bar with device toggle */}
|
||||
<div className="flex-1 px-6">
|
||||
<div className="relative max-w-64 mx-auto">
|
||||
<BrowserAddressBar
|
||||
currentPath={currentPath}
|
||||
onNavigate={hasBuiltProject ? navigateIframe : undefined}
|
||||
onRefresh={hasBuiltProject ? refreshIframe : undefined}
|
||||
navigationHistory={navigationHistory}
|
||||
deviceMode={deviceMode}
|
||||
onDeviceModeChange={setDeviceMode}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Right side - actions */}
|
||||
<div className="flex items-center">
|
||||
{(!isMobile && onToggleView && isPreviewable) && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={toggleFullscreen}
|
||||
className="h-8 w-8 p-0 ml-1"
|
||||
title={isFullscreen ? 'Exit immersive view' : 'Enter immersive view'}
|
||||
onClick={onToggleView}
|
||||
className="h-8 gap-2"
|
||||
>
|
||||
{isFullscreen ? (
|
||||
<Shrink className="h-4 w-4" />
|
||||
) : (
|
||||
<Expand className="h-4 w-4" />
|
||||
)}
|
||||
<Code className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
extraContent={(
|
||||
<div className="flex items-center">
|
||||
{(!isMobile && onToggleView && isPreviewable) && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onToggleView}
|
||||
className="h-8 gap-2"
|
||||
>
|
||||
<Code className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
<ConsoleDropdown />
|
||||
<Menu />
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<ConsoleDropdown />
|
||||
<Menu />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Content area */}
|
||||
<div className="flex-1">
|
||||
<div className="flex-1 flex items-center justify-center bg-muted/30">
|
||||
{hasBuiltProject ? (
|
||||
<iframe
|
||||
key={projectId}
|
||||
ref={iframeRef}
|
||||
src={`https://${projectId}.${previewDomain}/`}
|
||||
className="w-full h-full border-0"
|
||||
title="Project Preview"
|
||||
sandbox="allow-scripts allow-same-origin allow-forms"
|
||||
/>
|
||||
<div
|
||||
className={cn(
|
||||
"h-full transition-all duration-300 ease-in-out bg-background",
|
||||
deviceMode === 'laptop' && "w-full",
|
||||
deviceMode === 'tablet' && "w-full max-w-3xl shadow-lg",
|
||||
deviceMode === 'phone' && "w-full max-w-sm shadow-lg"
|
||||
)}
|
||||
>
|
||||
<iframe
|
||||
key={projectId}
|
||||
ref={iframeRef}
|
||||
src={`https://${projectId}.${previewDomain}/`}
|
||||
className="w-full h-full border-0"
|
||||
title="Project Preview"
|
||||
sandbox="allow-scripts allow-same-origin allow-forms"
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="h-full flex items-center justify-center bg-muted">
|
||||
<div className="text-center">
|
||||
|
||||
@@ -62,25 +62,7 @@ describe('BrowserAddressBar', () => {
|
||||
expect(input).toHaveValue('/contact');
|
||||
});
|
||||
|
||||
it('renders leftContent when provided', () => {
|
||||
render(
|
||||
<BrowserAddressBar
|
||||
leftContent={<button data-testid="left-button">Left</button>}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('left-button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders extraContent when provided', () => {
|
||||
render(
|
||||
<BrowserAddressBar
|
||||
extraContent={<button data-testid="extra-button">Extra</button>}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByTestId('extra-button')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows navigation history dropdown when input is focused', () => {
|
||||
const navigationHistory = ['/', '/about', '/contact'];
|
||||
|
||||
@@ -3,16 +3,16 @@ import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { CheckIcon, RefreshCw } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { ReactNode } from 'react';
|
||||
import { DeviceToggle, type DeviceMode } from '@/components/ui/device-toggle';
|
||||
|
||||
interface BrowserAddressBarProps {
|
||||
currentPath?: string;
|
||||
onNavigate?: (path: string) => void;
|
||||
onRefresh?: () => void;
|
||||
className?: string;
|
||||
extraContent?: ReactNode;
|
||||
leftContent?: ReactNode;
|
||||
navigationHistory?: string[];
|
||||
deviceMode?: DeviceMode;
|
||||
onDeviceModeChange?: (mode: DeviceMode) => void;
|
||||
}
|
||||
|
||||
export function BrowserAddressBar({
|
||||
@@ -20,9 +20,9 @@ export function BrowserAddressBar({
|
||||
onNavigate,
|
||||
onRefresh,
|
||||
className,
|
||||
extraContent,
|
||||
leftContent,
|
||||
navigationHistory = [],
|
||||
deviceMode,
|
||||
onDeviceModeChange,
|
||||
}: BrowserAddressBarProps) {
|
||||
const [inputValue, setInputValue] = useState(currentPath);
|
||||
const [showHistory, setShowHistory] = useState(false);
|
||||
@@ -87,70 +87,75 @@ export function BrowserAddressBar({
|
||||
const uniqueHistory = useMemo(() => Array.from(new Set([...navigationHistory].reverse())), [navigationHistory]);
|
||||
|
||||
return (
|
||||
<div className={cn(
|
||||
"flex items-center gap-2 p-2 border-b bg-background w-full",
|
||||
className
|
||||
)}>
|
||||
{/* Left content (e.g., maximize button) */}
|
||||
{leftContent}
|
||||
|
||||
{/* Address bar with refresh button inside */}
|
||||
<form onSubmit={handleSubmit} className="flex-1 px-6">
|
||||
<div className="relative max-w-64 mx-auto">
|
||||
<Input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={inputValue}
|
||||
onChange={handleInputChange}
|
||||
onFocus={handleInputFocus}
|
||||
placeholder="Enter path (e.g., /, /about)"
|
||||
className="h-7 text-xs md:text-xs bg-muted/50 border-muted-foreground/20 focus:bg-background pr-9 rounded-full"
|
||||
disabled={!onNavigate}
|
||||
<form onSubmit={handleSubmit} className={className}>
|
||||
{/* Device toggle on left side - only visible on large screens */}
|
||||
{deviceMode && onDeviceModeChange && (
|
||||
<div className="hidden md:block absolute left-1 top-1/2 -translate-y-1/2 z-10">
|
||||
<DeviceToggle
|
||||
mode={deviceMode}
|
||||
onModeChange={onDeviceModeChange}
|
||||
className="size-6 p-0 hover:bg-muted/50 rounded-full transition-colors group"
|
||||
/>
|
||||
{onRefresh && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onRefresh}
|
||||
className="size-6 absolute right-1 top-1/2 -translate-y-1/2 hover:bg-muted/50 rounded-full transition-colors group"
|
||||
title="Refresh"
|
||||
>
|
||||
<RefreshCw className="size-3 text-muted-foreground transition-colors group-hover:text-foreground" />
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{/* Navigation history dropdown */}
|
||||
{showHistory && uniqueHistory.length > 0 && (
|
||||
<div
|
||||
ref={dropdownRef}
|
||||
className="absolute top-full left-1/2 -translate-x-1/2 mt-1 bg-background border rounded-lg shadow-lg overflow-hidden z-50 max-h-64 w-full overflow-y-auto"
|
||||
>
|
||||
{uniqueHistory.map((path, index) => (
|
||||
<button
|
||||
key={index}
|
||||
type="button"
|
||||
onClick={() => handleHistoryItemClick(path)}
|
||||
className={cn(
|
||||
"w-full p-3 text-left text-xs hover:bg-muted/50 transition-colors flex items-center gap-2 border-b last:border-b-0",
|
||||
{
|
||||
"bg-muted/30 font-medium": path === currentPath,
|
||||
},
|
||||
)}
|
||||
>
|
||||
<span className="truncate">{path}</span>
|
||||
{path === currentPath && (
|
||||
<CheckIcon className="size-3 text-muted-foreground ml-auto" />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{/* Extra content */}
|
||||
{extraContent}
|
||||
</div>
|
||||
<Input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={inputValue}
|
||||
onChange={handleInputChange}
|
||||
onFocus={handleInputFocus}
|
||||
placeholder="Enter path (e.g., /, /about)"
|
||||
className={cn(
|
||||
"h-7 text-xs md:text-xs bg-muted/50 border-muted-foreground/20 focus:bg-background rounded-full",
|
||||
{
|
||||
"md:pl-9": deviceMode && onDeviceModeChange,
|
||||
"pr-9": onRefresh,
|
||||
}
|
||||
)}
|
||||
disabled={!onNavigate}
|
||||
/>
|
||||
|
||||
{/* Refresh button on right side */}
|
||||
{onRefresh && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onRefresh}
|
||||
className="size-6 absolute right-1 top-1/2 -translate-y-1/2 hover:bg-muted/50 rounded-full transition-colors group"
|
||||
title="Refresh"
|
||||
>
|
||||
<RefreshCw className="size-3 text-muted-foreground transition-colors group-hover:text-foreground" />
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{/* Navigation history dropdown */}
|
||||
{showHistory && uniqueHistory.length > 0 && (
|
||||
<div
|
||||
ref={dropdownRef}
|
||||
className="absolute top-full left-1/2 -translate-x-1/2 mt-1 bg-background border rounded-lg shadow-lg overflow-hidden z-50 max-h-64 w-full overflow-y-auto"
|
||||
>
|
||||
{uniqueHistory.map((path, index) => (
|
||||
<button
|
||||
key={index}
|
||||
type="button"
|
||||
onClick={() => handleHistoryItemClick(path)}
|
||||
className={cn(
|
||||
"w-full p-3 text-left text-xs hover:bg-muted/50 transition-colors flex items-center gap-2 border-b last:border-b-0",
|
||||
{
|
||||
"bg-muted/30 font-medium": path === currentPath,
|
||||
},
|
||||
)}
|
||||
>
|
||||
<span className="truncate">{path}</span>
|
||||
{path === currentPath && (
|
||||
<CheckIcon className="size-3 text-muted-foreground ml-auto" />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Laptop, Tablet, Smartphone } from 'lucide-react';
|
||||
|
||||
export type DeviceMode = 'laptop' | 'tablet' | 'phone';
|
||||
|
||||
interface DeviceToggleProps {
|
||||
mode: DeviceMode;
|
||||
onModeChange: (mode: DeviceMode) => void;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
const devices: DeviceMode[] = ['laptop', 'tablet', 'phone'];
|
||||
|
||||
const deviceIcons = {
|
||||
laptop: Laptop,
|
||||
tablet: Tablet,
|
||||
phone: Smartphone,
|
||||
};
|
||||
|
||||
export function DeviceToggle({ mode, onModeChange, className }: DeviceToggleProps) {
|
||||
const handleClick = () => {
|
||||
const currentIndex = devices.indexOf(mode);
|
||||
const nextIndex = (currentIndex + 1) % devices.length;
|
||||
onModeChange(devices[nextIndex]);
|
||||
};
|
||||
|
||||
const Icon = deviceIcons[mode];
|
||||
|
||||
return (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={handleClick}
|
||||
className={className}
|
||||
title={`Current: ${mode} (click to toggle)`}
|
||||
>
|
||||
<Icon className="size-3 text-muted-foreground transition-colors group-hover:text-foreground" />
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user