Merge branch 'fullscreenPreview' into 'main'

Fullscreen Preview

See merge request soapbox-pub/shakespeare!87
This commit is contained in:
Alex Gleason committed 2025-12-20 22:50:35 +00:00
commit 7a252e0037
5 files changed
+245 -145

No files matched your search

+31 -35
View File
@@ -10,7 +10,7 @@ import { useBuildProject } from '@/hooks/useBuildProject';
import { Tabs, TabsContent } from '@/components/ui/tabs';
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
import { Button } from '@/components/ui/button';
import { FolderOpen, ArrowLeft, Bug, Copy, Check, Play, Loader2, MenuIcon, Code, Rocket, X, Terminal } from 'lucide-react';
import { FolderOpen, ArrowLeft, Bug, Copy, Check, Play, Loader2, MenuIcon, Code, Rocket, X, Terminal, Expand, Shrink } from 'lucide-react';
import { useIsMobile } from '@/hooks/useIsMobile';
import { GitStatusIndicator } from '@/components/GitStatusIndicator';
import { BrowserAddressBar } from '@/components/ui/browser-address-bar';
@@ -89,6 +89,8 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
const [historyIndex, setHistoryIndex] = useState(0);
const [deployDialogOpen, setDeployDialogOpen] = useState(false);
const [isFullscreen, setIsFullscreen] = useState(false);
const previewContainerRef = useRef<HTMLDivElement>(null);
const iframeRef = useRef<HTMLIFrameElement>(null);
const { fs } = useFS();
const { projectsPath } = useFSPaths();
@@ -291,33 +293,9 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
sendNavigationCommand('refresh');
}, [sendNavigationCommand]);
const goBackIframe = useCallback(() => {
if (historyIndex > 0) {
// Move back in history
const newIndex = historyIndex - 1;
const targetPath = navigationHistory[newIndex];
setHistoryIndex(newIndex);
setCurrentPath(targetPath);
// Tell iframe to navigate to this path
sendNavigationCommand('navigate', { url: targetPath });
}
}, [historyIndex, navigationHistory, sendNavigationCommand]);
const goForwardIframe = useCallback(() => {
if (historyIndex < navigationHistory.length - 1) {
// Move forward in history
const newIndex = historyIndex + 1;
const targetPath = navigationHistory[newIndex];
setHistoryIndex(newIndex);
setCurrentPath(targetPath);
// Tell iframe to navigate to this path
sendNavigationCommand('navigate', { url: targetPath });
}
}, [historyIndex, navigationHistory, sendNavigationCommand]);
const toggleFullscreen = useCallback(() => {
setIsFullscreen(prev => !prev);
}, []);
const handleFetch = useCallback(async (request: JSONRPCRequest) => {
const { params, id } = request;
@@ -563,7 +541,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
</PopoverTrigger>
<PopoverContent
align="end"
className="w-[calc(100vw-1rem)] max-w-[480px] max-h-[512px] overflow-hidden shadow-lg border rounded-lg bg-background p-0"
className="w-[calc(100vw-1rem)] max-w-[480px] max-h-[512px] overflow-hidden shadow-lg border rounded-lg bg-background p-0 z-[9999]"
sideOffset={4}
>
@@ -643,7 +621,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
<span className="sr-only">Open menu</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48 sm:w-48">
<DropdownMenuContent align="end" className="w-48 sm:w-48 z-[9999]">
<DropdownMenuItem
onClick={handleBuildProject}
disabled={isBuildLoading}
@@ -674,17 +652,35 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
<Tabs value={activeTab} className="h-full">
{isPreviewable && (
<TabsContent value="preview" className="h-full mt-0">
<div className="h-full w-full flex flex-col relative">
<div
ref={previewContainerRef}
className={cn(
"h-full w-full flex flex-col relative",
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}
onBack={hasBuiltProject ? goBackIframe : undefined}
onForward={hasBuiltProject ? goForwardIframe : undefined}
canGoBack={hasBuiltProject && historyIndex > 0}
canGoForward={hasBuiltProject && historyIndex < navigationHistory.length - 1}
navigationHistory={navigationHistory}
leftContent={(
<Button
variant="ghost"
size="sm"
onClick={toggleFullscreen}
className="h-8 w-8 p-0 ml-1"
title={isFullscreen ? 'Exit immersive view' : 'Enter immersive view'}
>
{isFullscreen ? (
<Shrink className="h-4 w-4" />
) : (
<Expand className="h-4 w-4" />
)}
</Button>
)}
extraContent={(
<div className="flex items-center">
{(!isMobile && onToggleView && isPreviewable) && (
+112 -53
View File
@@ -3,11 +3,9 @@ import { render, screen, fireEvent } from '@testing-library/react';
import { BrowserAddressBar } from './browser-address-bar';
describe('BrowserAddressBar', () => {
it('renders navigation buttons and address input', () => {
render(<BrowserAddressBar />);
it('renders address input and refresh button', () => {
render(<BrowserAddressBar onRefresh={vi.fn()} />);
expect(screen.getByTitle('Go back')).toBeInTheDocument();
expect(screen.getByTitle('Go forward')).toBeInTheDocument();
expect(screen.getByTitle('Refresh')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Enter path (e.g., /, /about)')).toBeInTheDocument();
});
@@ -23,65 +21,30 @@ describe('BrowserAddressBar', () => {
expect(onNavigate).toHaveBeenCalledWith('/about');
});
it('disables back/forward buttons based on canGoBack/canGoForward props and handlers', () => {
// Test with handlers provided
const onBack = vi.fn();
const onForward = vi.fn();
it('disables input when no onNavigate handler is provided', () => {
render(<BrowserAddressBar />);
render(
<BrowserAddressBar
onBack={onBack}
onForward={onForward}
canGoBack={false}
canGoForward={true}
/>
);
expect(screen.getByTitle('Go back')).toBeDisabled(); // disabled because canGoBack=false
expect(screen.getByTitle('Go forward')).not.toBeDisabled(); // enabled because canGoForward=true and handler provided
});
it('disables buttons when no handlers are provided', () => {
render(
<BrowserAddressBar
canGoBack={true}
canGoForward={true}
/>
);
// All buttons should be disabled when no handlers are provided
expect(screen.getByTitle('Go back')).toBeDisabled();
expect(screen.getByTitle('Go forward')).toBeDisabled();
expect(screen.getByTitle('Refresh')).toBeDisabled();
// Input should also be disabled when no onNavigate handler is provided
// Input should be disabled when no onNavigate handler is provided
expect(screen.getByPlaceholderText('Enter path (e.g., /, /about)')).toBeDisabled();
});
it('calls navigation handlers when buttons are clicked', () => {
const onBack = vi.fn();
const onForward = vi.fn();
it('shows refresh button inside input when onRefresh handler is provided', () => {
const onRefresh = vi.fn();
render(<BrowserAddressBar onRefresh={onRefresh} />);
render(
<BrowserAddressBar
onBack={onBack}
onForward={onForward}
onRefresh={onRefresh}
canGoBack={true}
canGoForward={true}
/>
);
const refreshButton = screen.getByTitle('Refresh');
expect(refreshButton).toBeInTheDocument();
fireEvent.click(screen.getByTitle('Go back'));
fireEvent.click(screen.getByTitle('Go forward'));
fireEvent.click(screen.getByTitle('Refresh'));
expect(onBack).toHaveBeenCalled();
expect(onForward).toHaveBeenCalled();
fireEvent.click(refreshButton);
expect(onRefresh).toHaveBeenCalled();
});
it('does not show refresh button when no onRefresh handler is provided', () => {
render(<BrowserAddressBar />);
expect(screen.queryByTitle('Refresh')).not.toBeInTheDocument();
});
it('updates input value when currentPath prop changes', () => {
const { rerender } = render(<BrowserAddressBar currentPath="/" />);
@@ -98,4 +61,100 @@ 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'];
render(
<BrowserAddressBar
navigationHistory={navigationHistory}
onNavigate={vi.fn()}
/>
);
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
fireEvent.focus(input);
// History should be shown in reverse order (most recent first)
expect(screen.getByText('/contact')).toBeInTheDocument();
expect(screen.getByText('/about')).toBeInTheDocument();
expect(screen.getByText('/')).toBeInTheDocument();
});
it('does not show history dropdown when navigationHistory is empty', () => {
render(
<BrowserAddressBar
navigationHistory={[]}
onNavigate={vi.fn()}
/>
);
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
fireEvent.focus(input);
// No history items should be visible
expect(screen.queryByRole('button', { name: /\// })).not.toBeInTheDocument();
});
it('navigates when clicking a history item', () => {
const onNavigate = vi.fn();
const navigationHistory = ['/', '/about', '/contact'];
render(
<BrowserAddressBar
navigationHistory={navigationHistory}
onNavigate={onNavigate}
/>
);
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
fireEvent.focus(input);
const aboutButton = screen.getByText('/about');
fireEvent.click(aboutButton);
expect(onNavigate).toHaveBeenCalledWith('/about');
});
it('removes duplicate paths from history', () => {
const navigationHistory = ['/', '/about', '/', '/contact', '/about'];
render(
<BrowserAddressBar
navigationHistory={navigationHistory}
onNavigate={vi.fn()}
/>
);
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
fireEvent.focus(input);
// Should only show unique paths
const allButtons = screen.getAllByRole('button');
// Filter out the refresh button if present, only count history items
const historyButtons = allButtons.filter(btn =>
btn.textContent === '/' ||
btn.textContent === '/about' ||
btn.textContent === '/contact'
);
expect(historyButtons).toHaveLength(3);
});
});
+99 -54
View File
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useRef, useMemo } from 'react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { ChevronLeft, ChevronRight, RefreshCw } from 'lucide-react';
import { CheckIcon, RefreshCw } from 'lucide-react';
import { cn } from '@/lib/utils';
import { ReactNode } from 'react';
@@ -9,32 +9,47 @@ interface BrowserAddressBarProps {
currentPath?: string;
onNavigate?: (path: string) => void;
onRefresh?: () => void;
onBack?: () => void;
onForward?: () => void;
canGoBack?: boolean;
canGoForward?: boolean;
className?: string;
extraContent?: ReactNode;
leftContent?: ReactNode;
navigationHistory?: string[];
}
export function BrowserAddressBar({
currentPath = '/',
onNavigate,
onRefresh,
onBack,
onForward,
canGoBack = false,
canGoForward = false,
className,
extraContent,
leftContent,
navigationHistory = [],
}: BrowserAddressBarProps) {
const [inputValue, setInputValue] = useState(currentPath);
const [showHistory, setShowHistory] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const dropdownRef = useRef<HTMLDivElement>(null);
// Update input value when currentPath changes (e.g., when iframe navigates)
useEffect(() => {
setInputValue(currentPath);
}, [currentPath]);
// Close dropdown when clicking outside
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
const target = event.target as Node | null;
const dropdownElement = dropdownRef.current;
const inputElement = inputRef.current;
if (target && !dropdownElement?.contains(target) && !inputElement?.contains(target)) {
setShowHistory(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!onNavigate) return;
@@ -47,61 +62,91 @@ export function BrowserAddressBar({
}
onNavigate(path);
setShowHistory(false);
};
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(e.target.value);
};
const handleInputFocus = () => {
if (navigationHistory.length > 0) {
setShowHistory(true);
}
};
const handleHistoryItemClick = (path: string) => {
if (onNavigate) {
onNavigate(path);
setShowHistory(false);
inputRef.current?.blur();
}
};
// Get unique history items (reverse to show most recent first, remove duplicates)
const uniqueHistory = useMemo(() => Array.from(new Set([...navigationHistory].reverse())), [navigationHistory]);
return (
<div className={cn(
"flex items-center gap-1 p-2 border-b bg-background w-full",
"flex items-center gap-2 p-2 border-b bg-background w-full",
className
)}>
{/* Navigation buttons */}
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
onClick={onBack}
disabled={!canGoBack || !onBack}
className="h-8 w-8 p-0"
title="Go back"
>
<ChevronLeft className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={onForward}
disabled={!canGoForward || !onForward}
className="h-8 w-8 p-0"
title="Go forward"
>
<ChevronRight className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
onClick={onRefresh}
disabled={!onRefresh}
className="h-8 w-8 p-0"
title="Refresh"
>
<RefreshCw className="h-4 w-4" />
</Button>
</div>
{/* Left content (e.g., maximize button) */}
{leftContent}
{/* Address bar */}
<form onSubmit={handleSubmit} className="flex-1 mx-auto px-4 max-w-2xl">
<Input
type="text"
value={inputValue}
onChange={handleInputChange}
placeholder="Enter path (e.g., /, /about)"
className="h-8 bg-muted/50 border-muted-foreground/20 focus:bg-background"
disabled={!onNavigate}
/>
{/* 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}
/>
{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 */}
+1 -1
View File
@@ -1,7 +1,7 @@
import { cva } from "class-variance-authority"
export const buttonVariants = cva(
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
{
variants: {
variant: {