Skip to content

Commit c117fa7

Browse files
authored
Improve mobile search experience (#9)
1 parent a8415c8 commit c117fa7

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

src/components/Search.astro

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ import SearchDialogWrapper from './react/SearchDialogWrapper';
5252
.search-trigger {
5353
display: flex;
5454
align-items: center;
55+
justify-content: center;
5556
gap: 0.5rem;
56-
padding: 0.375rem 0.75rem;
57-
min-width: 12rem;
58-
background: var(--sl-color-gray-6);
59-
border: 1px solid var(--sl-color-hairline);
60-
border-radius: 0.375rem;
57+
padding: 0.5rem;
58+
background: transparent;
59+
border: none;
6160
color: var(--sl-color-gray-2);
6261
font-size: 0.875rem;
6362
cursor: pointer;
@@ -66,16 +65,38 @@ import SearchDialogWrapper from './react/SearchDialogWrapper';
6665
}
6766

6867
.search-trigger:hover {
69-
background: var(--sl-color-gray-5);
70-
border-color: var(--sl-color-gray-4);
68+
color: var(--sl-color-gray-1);
69+
}
70+
71+
@media (min-width: 640px) {
72+
.search-trigger {
73+
padding: 0.375rem 0.75rem;
74+
min-width: 12rem;
75+
justify-content: flex-start;
76+
background: var(--sl-color-gray-6);
77+
border: 1px solid var(--sl-color-hairline);
78+
border-radius: 0.375rem;
79+
}
80+
81+
.search-trigger:hover {
82+
background: var(--sl-color-gray-5);
83+
border-color: var(--sl-color-gray-4);
84+
}
7185
}
7286

7387
.search-icon {
74-
width: 1rem;
75-
height: 1rem;
88+
width: 1.25rem;
89+
height: 1.25rem;
7690
opacity: 0.7;
7791
}
7892

93+
@media (min-width: 640px) {
94+
.search-icon {
95+
width: 1rem;
96+
height: 1rem;
97+
}
98+
}
99+
79100
.search-label {
80101
display: none;
81102
}

src/components/react/SearchDialog.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,29 @@ export const SearchDialog: React.FC<SearchDialogProps> = ({
281281

282282
// Focus input and blur page content when dialog opens
283283
useEffect(() => {
284+
const unlockScroll = () => {
285+
const scrollY = document.body.style.top;
286+
document.body.classList.remove('search-dialog-open');
287+
document.body.style.top = '';
288+
if (scrollY) {
289+
window.scrollTo(0, Number.parseInt(scrollY, 10) * -1);
290+
}
291+
};
292+
284293
if (isOpen) {
294+
// Save scroll position before locking
295+
const scrollY = window.scrollY;
296+
document.body.style.top = `-${scrollY}px`;
285297
document.body.classList.add('search-dialog-open');
286298
setTimeout(() => inputRef.current?.focus(), 50);
287299
setQuery('');
288300
setResults([]);
289301
setSelectedIndex(0);
290302
} else {
291-
document.body.classList.remove('search-dialog-open');
303+
unlockScroll();
292304
}
293305

294-
return () => {
295-
document.body.classList.remove('search-dialog-open');
296-
};
306+
return unlockScroll;
297307
}, [isOpen]);
298308

299309
// Search effect with manual debouncing
@@ -455,7 +465,7 @@ export const SearchDialog: React.FC<SearchDialogProps> = ({
455465
if (!isOpen) return null;
456466

457467
return createPortal(
458-
<div className="fixed inset-0 z-50 flex items-start justify-center pt-[10vh]">
468+
<div className="fixed inset-0 z-50 flex items-start justify-center pt-4 px-6 sm:pt-[10vh] sm:px-4">
459469
{/* Backdrop */}
460470
<motion.div
461471
initial={{ opacity: 0 }}
@@ -472,7 +482,7 @@ export const SearchDialog: React.FC<SearchDialogProps> = ({
472482
animate={{ opacity: 1, scale: 1, y: 0 }}
473483
exit={{ opacity: 0, scale: 0.96, y: -10 }}
474484
transition={{ duration: 0.15 }}
475-
className="relative w-full max-w-2xl mx-4 bg-popover border border-border rounded-lg shadow-2xl overflow-hidden"
485+
className="relative w-full max-w-2xl bg-popover border border-border rounded-lg shadow-2xl overflow-hidden"
476486
>
477487
{/* Search input */}
478488
<div className="flex items-center gap-3 px-4 py-3 border-b border-border">

src/styles/custom.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ starlight-tabs [role="tab"][aria-selected="true"] {
480480
/* Search dialog open state - blur page content and prevent scroll */
481481
body.search-dialog-open {
482482
overflow: hidden;
483+
position: fixed;
484+
inset: 0;
485+
touch-action: none;
483486
}
484487

485488
body.search-dialog-open .page {

0 commit comments

Comments
 (0)