Skip to content

Commit

Permalink
fix: scroll highlighted tag into view (#25)
Browse files Browse the repository at this point in the history
* fix(tags): scroll highlighted tag into view

* chore: apply nit suggestions

---------

Co-authored-by: Andy Young <andyluyoung@gmail.com>
  • Loading branch information
uussaammaahh and ayoung19 authored Oct 9, 2024
1 parent 6e56384 commit 20227ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions popup/components/TagOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const TagOption = ({ entryId, tag, focused, onHover, onClose }: Props) =>
<Group
ref={groupRef}
align="center"
className="tag-option"
spacing="xs"
px={rem(4)}
py={rem(8)}
Expand Down
12 changes: 10 additions & 2 deletions popup/components/TagSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { useDisclosure, useHotkeys } from "@mantine/hooks";
import { IconTags } from "@tabler/icons-react";
import { useAtomValue } from "jotai";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";

import { allTagsAtom } from "~popup/states/atoms";
import { commonActionIconSx, defaultBorderColor, lightOrDark } from "~utils/sx";
Expand All @@ -32,6 +32,8 @@ export const TagSelect = ({ entryId }: Props) => {

const [focusedTagIndex, setFocusedTagIndex] = useState(0);

const scrollAreaRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (opened) {
setTagSearch("");
Expand Down Expand Up @@ -67,6 +69,12 @@ export const TagSelect = ({ entryId }: Props) => {
[],
);

useEffect(() => {
scrollAreaRef.current
?.querySelectorAll(".tag-option")
?.[focusedTagIndex]?.scrollIntoView({ block: "nearest" });
}, [focusedTagIndex]);

return (
<Popover
opened={opened}
Expand Down Expand Up @@ -108,7 +116,7 @@ export const TagSelect = ({ entryId }: Props) => {
/>
<Divider sx={(theme) => ({ borderColor: defaultBorderColor(theme) })} />
{/* https://github.com/creativetimofficial/material-tailwind/issues/528 */}
<ScrollArea.Autosize placeholder={undefined} mah={200} p={rem(4)}>
<ScrollArea.Autosize placeholder={undefined} mah={200} p={rem(4)} ref={scrollAreaRef}>
<Stack spacing={0}>
{matchedTags.map((tag, i) => (
<TagOption
Expand Down

0 comments on commit 20227ea

Please sign in to comment.