Add device toggle to preview bar

This commit is contained in:
Alex Gleason committed 2025-12-20 18:57:16 -06:00
1 parent f3d2f40c13
commit 8c7c2694c4
4 files changed
+173 -127

No files matched your search

@@ -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'];
+72 -67
View File
@@ -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>
);
}
+41
View File
@@ -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>
);
}