mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Use a browser-like UI in the project preview
This commit is contained in:
3 files changed
+216
-12
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
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 />);
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
it('calls onNavigate with proper path when form is submitted', () => {
|
||||
const onNavigate = vi.fn();
|
||||
render(<BrowserAddressBar onNavigate={onNavigate} />);
|
||||
|
||||
const input = screen.getByPlaceholderText('Enter path (e.g., /, /about)');
|
||||
fireEvent.change(input, { target: { value: 'about' } });
|
||||
fireEvent.submit(input.closest('form')!);
|
||||
|
||||
expect(onNavigate).toHaveBeenCalledWith('/about');
|
||||
});
|
||||
|
||||
it('disables back/forward buttons based on canGoBack/canGoForward props', () => {
|
||||
render(
|
||||
<BrowserAddressBar
|
||||
canGoBack={false}
|
||||
canGoForward={true}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByTitle('Go back')).toBeDisabled();
|
||||
expect(screen.getByTitle('Go forward')).not.toBeDisabled();
|
||||
});
|
||||
|
||||
it('calls navigation handlers when buttons are clicked', () => {
|
||||
const onBack = vi.fn();
|
||||
const onForward = vi.fn();
|
||||
const onRefresh = vi.fn();
|
||||
|
||||
render(
|
||||
<BrowserAddressBar
|
||||
onBack={onBack}
|
||||
onForward={onForward}
|
||||
onRefresh={onRefresh}
|
||||
canGoBack={true}
|
||||
canGoForward={true}
|
||||
/>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByTitle('Go back'));
|
||||
fireEvent.click(screen.getByTitle('Go forward'));
|
||||
fireEvent.click(screen.getByTitle('Refresh'));
|
||||
|
||||
expect(onBack).toHaveBeenCalled();
|
||||
expect(onForward).toHaveBeenCalled();
|
||||
expect(onRefresh).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
import { useState } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { ChevronLeft, ChevronRight, RefreshCw } from 'lucide-react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface BrowserAddressBarProps {
|
||||
currentPath?: string;
|
||||
onNavigate?: (path: string) => void;
|
||||
onRefresh?: () => void;
|
||||
onBack?: () => void;
|
||||
onForward?: () => void;
|
||||
canGoBack?: boolean;
|
||||
canGoForward?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function BrowserAddressBar({
|
||||
currentPath = '/',
|
||||
onNavigate,
|
||||
onRefresh,
|
||||
onBack,
|
||||
onForward,
|
||||
canGoBack = false,
|
||||
canGoForward = false,
|
||||
className,
|
||||
}: BrowserAddressBarProps) {
|
||||
const [inputValue, setInputValue] = useState(currentPath);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
let path = inputValue.trim();
|
||||
|
||||
// Ensure path starts with /
|
||||
if (!path.startsWith('/')) {
|
||||
path = '/' + path;
|
||||
}
|
||||
|
||||
onNavigate?.(path);
|
||||
};
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setInputValue(e.target.value);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn(
|
||||
"flex items-center gap-1 p-2 border-b bg-background",
|
||||
className
|
||||
)}>
|
||||
{/* Navigation buttons */}
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onBack}
|
||||
disabled={!canGoBack}
|
||||
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}
|
||||
className="h-8 w-8 p-0"
|
||||
title="Go forward"
|
||||
>
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onRefresh}
|
||||
className="h-8 w-8 p-0"
|
||||
title="Refresh"
|
||||
>
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Address bar */}
|
||||
<form onSubmit={handleSubmit} className="flex-1 mx-2">
|
||||
<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"
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user