Skip to content

Commit 00eb6be

Browse files
authored
feat: support pageup/pagedown to navigate search results (#920)
* feat: support pageup/pagedown to navigate search results * docs: add release note
1 parent 95dc7a8 commit 00eb6be

4 files changed

Lines changed: 59 additions & 9 deletions

File tree

docs/content.en/docs/release-notes/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Information about release notes of Coco App is provided here.
1616
feat: support switching groups via keyboard shortcuts #911
1717
feat: support opening logs from about page #915
1818
feat: support moving cursor with home and end keys #918
19+
feat: support pageup/pagedown to navigate search results #920
1920

2021
### 🐛 Bug fix
2122

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { useEventListener } from "ahooks";
2+
import clsx from "clsx";
3+
import {
4+
forwardRef,
5+
HTMLAttributes,
6+
useImperativeHandle,
7+
useRef,
8+
} from "react";
9+
10+
const Scrollbar = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(
11+
(props, ref) => {
12+
const { children, className, ...rest } = props;
13+
const containerRef = useRef<HTMLDivElement>(null);
14+
15+
useImperativeHandle(ref, () => containerRef.current as HTMLDivElement);
16+
17+
useEventListener("keydown", (event) => {
18+
const { key } = event;
19+
20+
if (key !== "PageDown" && key !== "PageUp") return;
21+
22+
if (!containerRef.current) return;
23+
24+
event.preventDefault();
25+
26+
const delta = key === "PageDown" ? 1 : -1;
27+
const el = containerRef.current;
28+
29+
el.scrollBy({
30+
top: delta * el.clientHeight * 0.9,
31+
behavior: "smooth",
32+
});
33+
});
34+
35+
return (
36+
<div
37+
{...rest}
38+
ref={containerRef}
39+
className={clsx("custom-scrollbar", className)}
40+
>
41+
{children}
42+
</div>
43+
);
44+
}
45+
);
46+
47+
export default Scrollbar;

src/components/Search/DocumentList.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React, { useState, useRef, useEffect, useCallback } from "react";
22
import { useInfiniteScroll } from "ahooks";
33
import { useTranslation } from "react-i18next";
4+
import { Data } from "ahooks/lib/useInfiniteScroll/types";
5+
import { nanoid } from "nanoid";
6+
import { isNil } from "lodash-es";
47

58
import { useSearchStore } from "@/stores/searchStore";
69
import { SearchHeader } from "./SearchHeader";
@@ -11,9 +14,7 @@ import { Get } from "@/api/axiosRequest";
1114
import { useAppStore } from "@/stores/appStore";
1215
import { useConnectStore } from "@/stores/connectStore";
1316
import SearchEmpty from "../Common/SearchEmpty";
14-
import { Data } from "ahooks/lib/useInfiniteScroll/types";
15-
import { nanoid } from "nanoid";
16-
import { isNil } from "lodash-es";
17+
import Scrollbar from "@/components/Common/Scrollbar";
1718

1819
interface DocumentListProps {
1920
onSelectDocument: (id: string) => void;
@@ -297,8 +298,8 @@ export const DocumentList: React.FC<DocumentListProps> = ({
297298
/>
298299
</div>
299300

300-
<div
301-
className="flex-1 overflow-auto custom-scrollbar pr-0.5"
301+
<Scrollbar
302+
className="flex-1 overflow-auto pr-0.5"
302303
ref={containerRef}
303304
>
304305
{data?.list && data.list.length > 0 && (
@@ -334,7 +335,7 @@ export const DocumentList: React.FC<DocumentListProps> = ({
334335
<SearchEmpty />
335336
</div>
336337
)}
337-
</div>
338+
</Scrollbar>
338339
</div>
339340
);
340341
};

src/components/Search/DropdownList.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useKeyboardNavigation } from "@/hooks/useKeyboardNavigation";
1616
import { SearchSource } from "./SearchSource";
1717
import DropdownListItem from "./DropdownListItem";
1818
import platformAdapter from "@/utils/platformAdapter";
19+
import Scrollbar from "@/components/Common/Scrollbar";
1920

2021
type ISearchData = Record<string, QueryHits[]>;
2122

@@ -149,10 +150,10 @@ function DropdownList({
149150
});
150151

151152
return (
152-
<div
153+
<Scrollbar
153154
ref={containerRef}
154155
data-tauri-drag-region
155-
className="h-full w-full p-2 flex flex-col overflow-y-auto custom-scrollbar focus:outline-none"
156+
className="h-full w-full p-2 flex flex-col overflow-y-auto focus:outline-none"
156157
tabIndex={0}
157158
role="listbox"
158159
aria-label={t("search.header.results")}
@@ -189,7 +190,7 @@ function DropdownList({
189190
})}
190191
</div>
191192
))}
192-
</div>
193+
</Scrollbar>
193194
);
194195
}
195196

0 commit comments

Comments
 (0)