mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Allow easy navigating in address bar
This commit is contained in:
3 files changed
+162
-22
No files matched your search
@@ -665,6 +665,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
currentPath={currentPath}
|
||||
onNavigate={hasBuiltProject ? navigateIframe : undefined}
|
||||
onRefresh={hasBuiltProject ? refreshIframe : undefined}
|
||||
navigationHistory={navigationHistory}
|
||||
leftContent={(
|
||||
<Button
|
||||
variant="ghost"
|
||||
|
||||
@@ -81,4 +81,80 @@ describe('BrowserAddressBar', () => {
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { RefreshCw } from 'lucide-react';
|
||||
@@ -12,6 +12,7 @@ interface BrowserAddressBarProps {
|
||||
className?: string;
|
||||
extraContent?: ReactNode;
|
||||
leftContent?: ReactNode;
|
||||
navigationHistory?: string[];
|
||||
}
|
||||
|
||||
export function BrowserAddressBar({
|
||||
@@ -21,14 +22,35 @@ export function BrowserAddressBar({
|
||||
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) => {
|
||||
if (
|
||||
dropdownRef.current &&
|
||||
!dropdownRef.current.contains(event.target as Node) &&
|
||||
inputRef.current &&
|
||||
!inputRef.current.contains(event.target as Node)
|
||||
) {
|
||||
setShowHistory(false);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside);
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside);
|
||||
}, []);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!onNavigate) return;
|
||||
@@ -41,12 +63,30 @@ 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 = Array.from(new Set([...navigationHistory].reverse()));
|
||||
|
||||
return (
|
||||
<div className={cn(
|
||||
"flex items-center gap-2 p-2 border-b bg-background w-full",
|
||||
@@ -56,27 +96,50 @@ export function BrowserAddressBar({
|
||||
{leftContent}
|
||||
|
||||
{/* Address bar with refresh button inside */}
|
||||
<form onSubmit={handleSubmit} className="flex-1 mx-auto px-6 max-w-80 relative">
|
||||
<Input
|
||||
type="text"
|
||||
value={inputValue}
|
||||
onChange={handleInputChange}
|
||||
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-7 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>
|
||||
)}
|
||||
<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="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"
|
||||
>
|
||||
<span className="truncate">{path}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{/* Extra content */}
|
||||
|
||||
Reference in new issue
Block a user