Skip to content

Commit 76a8ad2

Browse files
committed
feat: search container use scroll-area
1 parent 69c90fa commit 76a8ad2

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

components/layouts/page-header/doc-search.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22
import Link from 'next/link';
3-
import { useState } from 'react';
3+
import { useEffect, useState } from 'react';
44
import debounce from 'lodash.debounce';
55
import { File, Heading, Search } from 'lucide-react';
66
import { EnrichedDocumentSearchResultSetUnitResultUnit } from 'flexsearch';
@@ -14,13 +14,19 @@ import {
1414
DialogTrigger,
1515
} from '~/components/ui/dialog';
1616
import { Input } from '~/components/ui/input';
17+
import { ScrollArea } from '~/components/ui/scroll-area';
1718
import searchDoc, { IndexItem } from './search';
1819

1920
export default function DocSearch() {
21+
const [open, setOpen] = useState(false);
2022
const [searchList, setSearchList] = useState<
2123
EnrichedDocumentSearchResultSetUnitResultUnit<IndexItem>[]
2224
>([]);
2325

26+
useEffect(() => {
27+
if (!open) setSearchList([]);
28+
}, [open]);
29+
2430
const onSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
2531
const result = searchDoc(e.target.value);
2632

@@ -39,7 +45,7 @@ export default function DocSearch() {
3945
};
4046

4147
return (
42-
<Dialog>
48+
<Dialog onOpenChange={setOpen}>
4349
<DialogTrigger asChild>
4450
<div className="hidden md:block">
4551
<Button
@@ -77,7 +83,7 @@ export default function DocSearch() {
7783
</div>
7884
)}
7985
{searchList.length > 0 && (
80-
<div className="max-h-[300px] overflow-y-auto overflow-x-hidden px-2 py-1 text-foreground md:max-h-[calc(100vh-300px)]">
86+
<ScrollArea className="max-h-[300px] overflow-y-auto overflow-x-hidden px-2 py-1 text-foreground md:max-h-[calc(100vh-300px)]">
8187
{searchList.map((item) => {
8288
if (item.doc.type == 'heading') {
8389
return (
@@ -98,7 +104,7 @@ export default function DocSearch() {
98104
</Line>
99105
);
100106
})}
101-
</div>
107+
</ScrollArea>
102108
)}
103109
</DialogContent>
104110
</Dialog>

components/ui/scroll-area.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use client';
2+
3+
import * as React from 'react';
4+
import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area';
5+
6+
import { cn } from '~/lib/utils';
7+
8+
const ScrollArea = React.forwardRef<
9+
React.ElementRef<typeof ScrollAreaPrimitive.Root>,
10+
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
11+
>(({ className, children, ...props }, ref) => (
12+
<ScrollAreaPrimitive.Root
13+
ref={ref}
14+
className={cn('relative overflow-hidden', className)}
15+
{...props}
16+
>
17+
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
18+
{children}
19+
</ScrollAreaPrimitive.Viewport>
20+
<ScrollBar />
21+
<ScrollAreaPrimitive.Corner />
22+
</ScrollAreaPrimitive.Root>
23+
));
24+
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;
25+
26+
const ScrollBar = React.forwardRef<
27+
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
28+
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
29+
>(({ className, orientation = 'vertical', ...props }, ref) => (
30+
<ScrollAreaPrimitive.ScrollAreaScrollbar
31+
ref={ref}
32+
orientation={orientation}
33+
className={cn(
34+
'flex touch-none select-none transition-colors',
35+
orientation === 'vertical' && 'h-full w-2.5 border-l border-l-transparent p-[1px]',
36+
orientation === 'horizontal' && 'h-2.5 flex-col border-t border-t-transparent p-[1px]',
37+
className,
38+
)}
39+
{...props}
40+
>
41+
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" />
42+
</ScrollAreaPrimitive.ScrollAreaScrollbar>
43+
));
44+
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;
45+
46+
export { ScrollArea, ScrollBar };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@radix-ui/react-avatar": "^1.1.3",
2323
"@radix-ui/react-dialog": "^1.1.6",
2424
"@radix-ui/react-dropdown-menu": "^2.1.6",
25+
"@radix-ui/react-scroll-area": "^1.2.3",
2526
"@radix-ui/react-slot": "^1.1.2",
2627
"class-variance-authority": "^0.7.1",
2728
"clsx": "^2.1.1",

pnpm-lock.yaml

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)