layout redesign
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { useState } from 'react';
|
||||
import { deletePost } from '../../lib/api';
|
||||
|
||||
interface Props {
|
||||
slug: string;
|
||||
title: string;
|
||||
variant?: 'icon' | 'full';
|
||||
}
|
||||
|
||||
export default function DeletePostButton({ slug, title, variant = 'full' }: Props) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function handleClick() {
|
||||
if (busy) return;
|
||||
if (!window.confirm(`Delete "${title}"? This cannot be undone.`)) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
await deletePost(slug);
|
||||
window.location.href = '/';
|
||||
} catch (e) {
|
||||
window.alert(`Failed to delete: ${e instanceof Error ? e.message : 'unknown error'}`);
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (variant === 'icon') {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
disabled={busy}
|
||||
title="Delete post"
|
||||
aria-label="Delete post"
|
||||
className="p-2 rounded-md bg-surface0/80 hover:bg-red/20 text-subtext0 hover:text-red border border-surface1 transition-colors disabled:opacity-50"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M3 6h18" />
|
||||
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" />
|
||||
<path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
|
||||
<line x1="10" x2="10" y1="11" y2="17" />
|
||||
<line x1="14" x2="14" y1="11" y2="17" />
|
||||
</svg>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
disabled={busy}
|
||||
className="inline-flex items-center gap-2 bg-surface0 hover:bg-red/15 text-subtext1 hover:text-red px-3 py-1.5 md:px-4 md:py-2 rounded-md border border-surface1 hover:border-red/30 transition-colors text-sm disabled:opacity-50"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M3 6h18" />
|
||||
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" />
|
||||
<path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
|
||||
<line x1="10" x2="10" y1="11" y2="17" />
|
||||
<line x1="14" x2="14" y1="11" y2="17" />
|
||||
</svg>
|
||||
{busy ? 'Deleting…' : 'Delete'}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { useState } from 'react';
|
||||
import { logout as apiLogout } from '../../lib/api';
|
||||
|
||||
export default function LogoutButton() {
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function handleClick() {
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
await apiLogout();
|
||||
} catch {
|
||||
/* clear UI anyway */
|
||||
}
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleClick}
|
||||
disabled={busy}
|
||||
title="Sign out"
|
||||
aria-label="Sign out"
|
||||
className="text-subtext0 hover:text-red transition-colors p-1.5 rounded-md hover:bg-surface0/60 disabled:opacity-50"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" />
|
||||
<polyline points="16 17 21 12 16 7" />
|
||||
<line x1="21" x2="9" y1="12" y2="12" />
|
||||
</svg>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import { deletePost } from '../../lib/api';
|
||||
|
||||
interface Post {
|
||||
slug: string;
|
||||
@@ -12,6 +13,7 @@ interface Post {
|
||||
|
||||
interface Props {
|
||||
posts: Post[];
|
||||
isAdmin?: boolean;
|
||||
}
|
||||
|
||||
function formatSlug(slug: string) {
|
||||
@@ -30,178 +32,154 @@ function formatDate(date: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export default function PostList({ posts }: Props) {
|
||||
const [query, setQuery] = useState('');
|
||||
const [activeTags, setActiveTags] = useState<Set<string>>(new Set());
|
||||
export default function PostList({ posts: initialPosts, isAdmin = false }: Props) {
|
||||
const [posts, setPosts] = useState(initialPosts);
|
||||
const [deleting, setDeleting] = useState<string | null>(null);
|
||||
|
||||
const allTags = useMemo(() => {
|
||||
const set = new Set<string>();
|
||||
for (const p of posts) for (const t of p.tags || []) set.add(t);
|
||||
return Array.from(set).sort((a, b) => a.localeCompare(b));
|
||||
}, [posts]);
|
||||
async function handleDelete(slug: string, title: string) {
|
||||
if (deleting) return;
|
||||
if (!window.confirm(`Delete "${title}"? This cannot be undone.`)) return;
|
||||
setDeleting(slug);
|
||||
try {
|
||||
await deletePost(slug);
|
||||
setPosts(p => p.filter(x => x.slug !== slug));
|
||||
} catch (e) {
|
||||
window.alert(`Failed to delete: ${e instanceof Error ? e.message : 'unknown error'}`);
|
||||
} finally {
|
||||
setDeleting(null);
|
||||
}
|
||||
}
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const q = query.trim().toLowerCase();
|
||||
return posts.filter(p => {
|
||||
if (activeTags.size > 0) {
|
||||
for (const t of activeTags) if (!p.tags?.includes(t)) return false;
|
||||
}
|
||||
if (!q) return true;
|
||||
const hay = `${p.title || formatSlug(p.slug)} ${p.excerpt || ''}`.toLowerCase();
|
||||
return hay.includes(q);
|
||||
});
|
||||
}, [posts, query, activeTags]);
|
||||
|
||||
function toggleTag(tag: string) {
|
||||
setActiveTags(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(tag)) next.delete(tag);
|
||||
else next.add(tag);
|
||||
return next;
|
||||
});
|
||||
if (posts.length === 0) {
|
||||
return (
|
||||
<div className="glass p-8 md:p-12 text-center text-sm md:text-base text-subtext1">
|
||||
No posts yet.
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="glass px-4 py-3 md:px-5 md:py-4 flex flex-col gap-3 md:gap-4">
|
||||
<div className="relative">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className="absolute left-3 top-1/2 -translate-y-1/2 text-subtext0 pointer-events-none"
|
||||
aria-hidden="true"
|
||||
<div className="flex flex-col space-y-6">
|
||||
{posts.map(post => {
|
||||
const displayTitle = post.title || formatSlug(post.slug);
|
||||
const isDeleting = deleting === post.slug;
|
||||
return (
|
||||
<article
|
||||
key={post.slug}
|
||||
className={`glass p-5 md:p-8 transition-all hover:bg-surface0/80 group relative ${
|
||||
isDeleting ? 'opacity-50 pointer-events-none' : ''
|
||||
}`}
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<path d="m21 21-4.3-4.3" />
|
||||
</svg>
|
||||
<input
|
||||
type="search"
|
||||
value={query}
|
||||
onChange={e => setQuery(e.target.value)}
|
||||
placeholder="Search posts…"
|
||||
aria-label="Search posts"
|
||||
className="w-full bg-crust border border-surface1 rounded-lg pl-9 pr-3 py-2 text-sm md:text-base text-text placeholder:text-subtext0 focus:outline-none focus:border-mauve transition-colors"
|
||||
/>
|
||||
</div>
|
||||
{allTags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{allTags.map(tag => {
|
||||
const active = activeTags.has(tag);
|
||||
return (
|
||||
<button
|
||||
key={tag}
|
||||
type="button"
|
||||
onClick={() => toggleTag(tag)}
|
||||
className={`text-[10px] uppercase tracking-wider px-2.5 py-1 rounded-full border transition-colors ${
|
||||
active
|
||||
? 'bg-mauve/20 text-mauve border-mauve/30'
|
||||
: 'bg-surface0 text-subtext0 border-surface1 hover:text-text'
|
||||
}`}
|
||||
>
|
||||
{tag}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{activeTags.size > 0 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveTags(new Set())}
|
||||
className="text-[10px] uppercase tracking-wider px-2.5 py-1 rounded-full border border-transparent text-subtext0 hover:text-red transition-colors"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{filtered.length === 0 ? (
|
||||
<div className="glass p-8 md:p-12 text-center text-sm md:text-base text-subtext1">
|
||||
No posts match.
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col space-y-6">
|
||||
{filtered.map(post => {
|
||||
const displayTitle = post.title || formatSlug(post.slug);
|
||||
return (
|
||||
<article
|
||||
key={post.slug}
|
||||
className="glass p-5 md:p-8 transition-colors hover:bg-surface0/80 group"
|
||||
>
|
||||
<a href={`/posts/${encodeURIComponent(post.slug)}`} className="block">
|
||||
<div className="flex flex-col md:flex-row md:items-center gap-4 md:gap-6">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 mb-2 text-xs text-subtext0">
|
||||
<time dateTime={post.date}>{formatDate(post.date)}</time>
|
||||
<a href={`/posts/${encodeURIComponent(post.slug)}`} className="block">
|
||||
<div className="flex flex-col md:flex-row md:items-center gap-4 md:gap-6">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 mb-2 text-xs text-subtext0">
|
||||
<time dateTime={post.date}>{formatDate(post.date)}</time>
|
||||
<span className="opacity-50">·</span>
|
||||
<span>{post.reading_time} min read</span>
|
||||
{post.draft && (
|
||||
<>
|
||||
<span className="opacity-50">·</span>
|
||||
<span>{post.reading_time} min read</span>
|
||||
{post.draft && (
|
||||
<>
|
||||
<span className="opacity-50">·</span>
|
||||
<span className="text-peach uppercase tracking-wide font-semibold">
|
||||
Draft
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<h2 className="text-xl md:text-2xl font-semibold text-lavender group-hover:text-mauve transition-colors mb-2">
|
||||
{displayTitle}
|
||||
</h2>
|
||||
<p className="text-subtext1 text-sm md:text-base leading-relaxed line-clamp-2">
|
||||
{post.excerpt || `Read more about ${displayTitle}...`}
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-mauve opacity-0 group-hover:opacity-100 transition-opacity self-end md:self-auto shrink-0 hidden md:block">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M5 12h14" />
|
||||
<path d="m12 5 7 7-7 7" />
|
||||
</svg>
|
||||
</div>
|
||||
<span className="text-peach uppercase tracking-wide font-semibold">
|
||||
Draft
|
||||
</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<h2 className="text-xl md:text-2xl font-semibold text-lavender group-hover:text-mauve transition-colors mb-2 pr-20">
|
||||
{displayTitle}
|
||||
</h2>
|
||||
<p className="text-subtext1 text-sm md:text-base leading-relaxed line-clamp-2">
|
||||
{post.excerpt || `Read more about ${displayTitle}...`}
|
||||
</p>
|
||||
</div>
|
||||
<div className="text-mauve opacity-0 group-hover:opacity-100 transition-opacity self-end md:self-auto shrink-0 hidden md:block">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M5 12h14" />
|
||||
<path d="m12 5 7 7-7 7" />
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
{post.tags && post.tags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2 mt-3">
|
||||
{post.tags.map(tag => (
|
||||
<span
|
||||
key={tag}
|
||||
className="text-[10px] uppercase tracking-wider px-2 py-0.5 rounded-full bg-surface0 text-subtext0 border border-surface1"
|
||||
>
|
||||
{tag}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isAdmin && (
|
||||
<div className="absolute top-4 right-4 md:top-5 md:right-5 flex items-center gap-1.5">
|
||||
<a
|
||||
href={`/admin/editor?edit=${encodeURIComponent(post.slug)}`}
|
||||
onClick={e => e.stopPropagation()}
|
||||
title="Edit post"
|
||||
aria-label={`Edit ${displayTitle}`}
|
||||
className="p-1.5 rounded-md bg-surface0/80 hover:bg-blue/20 text-subtext0 hover:text-blue border border-surface1 transition-colors"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z" />
|
||||
<path d="m15 5 4 4" />
|
||||
</svg>
|
||||
</a>
|
||||
{post.tags && post.tags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2 mt-3">
|
||||
{post.tags.map(tag => {
|
||||
const active = activeTags.has(tag);
|
||||
return (
|
||||
<button
|
||||
key={tag}
|
||||
type="button"
|
||||
onClick={() => toggleTag(tag)}
|
||||
className={`text-[10px] uppercase tracking-wider px-2 py-0.5 rounded-full border transition-colors ${
|
||||
active
|
||||
? 'bg-mauve/20 text-mauve border-mauve/30'
|
||||
: 'bg-surface0 text-subtext0 border-surface1 hover:text-text'
|
||||
}`}
|
||||
>
|
||||
{tag}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
onClick={e => { e.preventDefault(); e.stopPropagation(); handleDelete(post.slug, displayTitle); }}
|
||||
disabled={isDeleting}
|
||||
title="Delete post"
|
||||
aria-label={`Delete ${displayTitle}`}
|
||||
className="p-1.5 rounded-md bg-surface0/80 hover:bg-red/20 text-subtext0 hover:text-red border border-surface1 transition-colors disabled:opacity-50"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="14"
|
||||
height="14"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<path d="M3 6h18" />
|
||||
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" />
|
||||
<path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />
|
||||
<line x1="10" x2="10" y1="11" y2="17" />
|
||||
<line x1="14" x2="14" y1="11" y2="17" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { getPosts } from '../../lib/api';
|
||||
|
||||
interface Post {
|
||||
slug: string;
|
||||
date: string;
|
||||
title?: string;
|
||||
excerpt?: string;
|
||||
tags: string[];
|
||||
draft: boolean;
|
||||
reading_time: number;
|
||||
}
|
||||
|
||||
function formatSlug(slug: string) {
|
||||
if (!slug) return '';
|
||||
return slug
|
||||
.split('-')
|
||||
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
function formatDate(date: string) {
|
||||
return new Date(date).toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
});
|
||||
}
|
||||
|
||||
export default function Search() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState('');
|
||||
const [posts, setPosts] = useState<Post[] | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [activeIdx, setActiveIdx] = useState(0);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const listRef = useRef<HTMLUListElement>(null);
|
||||
|
||||
// Global Cmd/Ctrl+K + Esc listener
|
||||
useEffect(() => {
|
||||
function onKey(e: KeyboardEvent) {
|
||||
const isMac = navigator.platform.toLowerCase().includes('mac');
|
||||
const mod = isMac ? e.metaKey : e.ctrlKey;
|
||||
if (mod && e.key.toLowerCase() === 'k') {
|
||||
e.preventDefault();
|
||||
setOpen(o => !o);
|
||||
} else if (e.key === 'Escape' && open) {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
window.addEventListener('keydown', onKey);
|
||||
return () => window.removeEventListener('keydown', onKey);
|
||||
}, [open]);
|
||||
|
||||
// Lazy fetch posts on first open
|
||||
useEffect(() => {
|
||||
if (!open || posts !== null || loading) return;
|
||||
setLoading(true);
|
||||
getPosts()
|
||||
.then(p => setPosts(p as Post[]))
|
||||
.catch(e => setError(e instanceof Error ? e.message : 'Failed to load'))
|
||||
.finally(() => setLoading(false));
|
||||
}, [open, posts, loading]);
|
||||
|
||||
// Focus input on open
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setQuery('');
|
||||
setActiveIdx(0);
|
||||
requestAnimationFrame(() => inputRef.current?.focus());
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
// Lock body scroll when open
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const prev = document.body.style.overflow;
|
||||
document.body.style.overflow = 'hidden';
|
||||
return () => { document.body.style.overflow = prev; };
|
||||
}, [open]);
|
||||
|
||||
const results = useMemo(() => {
|
||||
if (!posts) return [];
|
||||
const q = query.trim().toLowerCase();
|
||||
if (!q) return posts;
|
||||
return posts.filter(p => {
|
||||
const hay = `${p.title || formatSlug(p.slug)} ${p.excerpt || ''} ${p.tags?.join(' ') || ''}`.toLowerCase();
|
||||
return hay.includes(q);
|
||||
});
|
||||
}, [posts, query]);
|
||||
|
||||
// Reset active index when results change
|
||||
useEffect(() => {
|
||||
setActiveIdx(0);
|
||||
}, [query, posts]);
|
||||
|
||||
function navigate(slug: string) {
|
||||
window.location.href = `/posts/${encodeURIComponent(slug)}`;
|
||||
}
|
||||
|
||||
function onInputKey(e: React.KeyboardEvent<HTMLInputElement>) {
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault();
|
||||
setActiveIdx(i => Math.min(i + 1, Math.max(0, results.length - 1)));
|
||||
} else if (e.key === 'ArrowUp') {
|
||||
e.preventDefault();
|
||||
setActiveIdx(i => Math.max(i - 1, 0));
|
||||
} else if (e.key === 'Enter' && results[activeIdx]) {
|
||||
e.preventDefault();
|
||||
navigate(results[activeIdx].slug);
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll active result into view
|
||||
useEffect(() => {
|
||||
if (!open || !listRef.current) return;
|
||||
const el = listRef.current.children[activeIdx] as HTMLElement | undefined;
|
||||
el?.scrollIntoView({ block: 'nearest' });
|
||||
}, [activeIdx, open]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(true)}
|
||||
aria-label="Search posts (Ctrl+K)"
|
||||
title="Search (Ctrl+K)"
|
||||
className="text-subtext0 hover:text-text transition-colors flex items-center gap-2 px-2 py-1 rounded-md hover:bg-surface0/60"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<path d="m21 21-4.3-4.3" />
|
||||
</svg>
|
||||
<kbd className="hidden md:inline-flex items-center gap-0.5 text-[10px] font-mono text-subtext0 px-1.5 py-0.5 rounded border border-surface1 bg-surface0/60">
|
||||
<span>⌘</span><span>K</span>
|
||||
</kbd>
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div
|
||||
className="fixed inset-0 z-[200] flex items-start justify-center pt-[10vh] md:pt-[15vh] px-4"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label="Search posts"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
<div
|
||||
className="absolute inset-0 bg-crust/70 backdrop-blur-md"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<div
|
||||
className="relative w-full max-w-xl bg-mantle border border-surface1 rounded-2xl shadow-2xl overflow-hidden flex flex-col max-h-[70vh] animate-in fade-in slide-in-from-top-4 duration-200"
|
||||
onClick={e => e.stopPropagation()}
|
||||
>
|
||||
<div className="flex items-center gap-3 px-4 py-3 border-b border-surface1">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
className="text-subtext0 shrink-0"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<circle cx="11" cy="11" r="8" />
|
||||
<path d="m21 21-4.3-4.3" />
|
||||
</svg>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="search"
|
||||
value={query}
|
||||
onChange={e => setQuery(e.target.value)}
|
||||
onKeyDown={onInputKey}
|
||||
placeholder="Search posts…"
|
||||
aria-label="Search query"
|
||||
className="flex-1 bg-transparent outline-none text-base text-text placeholder:text-subtext0"
|
||||
/>
|
||||
<kbd className="text-[10px] font-mono text-subtext0 px-1.5 py-0.5 rounded border border-surface1 bg-surface0/60">
|
||||
Esc
|
||||
</kbd>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
{loading && (
|
||||
<div className="px-4 py-8 text-center text-sm text-subtext0">Loading…</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="px-4 py-8 text-center text-sm text-red">{error}</div>
|
||||
)}
|
||||
{!loading && !error && posts && results.length === 0 && (
|
||||
<div className="px-4 py-8 text-center text-sm text-subtext0">
|
||||
{query ? 'No posts match.' : 'No posts.'}
|
||||
</div>
|
||||
)}
|
||||
{!loading && !error && results.length > 0 && (
|
||||
<ul ref={listRef} className="py-1">
|
||||
{results.map((p, i) => {
|
||||
const title = p.title || formatSlug(p.slug);
|
||||
const active = i === activeIdx;
|
||||
return (
|
||||
<li
|
||||
key={p.slug}
|
||||
onMouseEnter={() => setActiveIdx(i)}
|
||||
onClick={() => navigate(p.slug)}
|
||||
className={`px-4 py-3 cursor-pointer border-l-2 transition-colors ${
|
||||
active
|
||||
? 'bg-surface0 border-mauve'
|
||||
: 'border-transparent hover:bg-surface0/60'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-baseline justify-between gap-3">
|
||||
<div className={`font-medium truncate ${active ? 'text-mauve' : 'text-lavender'}`}>
|
||||
{title}
|
||||
</div>
|
||||
<time className="text-[10px] text-subtext0 shrink-0" dateTime={p.date}>
|
||||
{formatDate(p.date)}
|
||||
</time>
|
||||
</div>
|
||||
{p.excerpt && (
|
||||
<div className="text-xs text-subtext1 line-clamp-1 mt-0.5">{p.excerpt}</div>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between px-4 py-2 border-t border-surface1 text-[10px] text-subtext0 bg-crust/40">
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="flex items-center gap-1">
|
||||
<kbd className="font-mono px-1 rounded border border-surface1 bg-surface0/60">↑↓</kbd>
|
||||
navigate
|
||||
</span>
|
||||
<span className="flex items-center gap-1">
|
||||
<kbd className="font-mono px-1 rounded border border-surface1 bg-surface0/60">↵</kbd>
|
||||
open
|
||||
</span>
|
||||
</div>
|
||||
<span>{results.length} {results.length === 1 ? 'result' : 'results'}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user