Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/elements/__tests__/suggestion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ describe("suggestion", () => {
expect(onClick).toHaveBeenCalledWith("Test suggestion");
});

it("centers the clicked suggestion", async () => {
const scrollIntoView = vi
.spyOn(Element.prototype, "scrollIntoView")
.mockImplementation(vi.fn());
const user = userEvent.setup();

render(<Suggestion suggestion="Test suggestion" />);

const button = screen.getByRole("button");
await user.click(button);

expect(scrollIntoView).toHaveBeenCalledWith({
behavior: "smooth",
block: "nearest",
inline: "center",
});
});

it("applies default variant and size", () => {
render(<Suggestion suggestion="Test" />);
const button = screen.getByRole("button");
Expand Down
17 changes: 13 additions & 4 deletions packages/elements/src/suggestion.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use client";

import type { ComponentProps, MouseEvent } from "react";

import { Button } from "@repo/shadcn-ui/components/ui/button";
import {
ScrollArea,
ScrollBar,
} from "@repo/shadcn-ui/components/ui/scroll-area";
import { cn } from "@repo/shadcn-ui/lib/utils";
import type { ComponentProps } from "react";
import { useCallback } from "react";

export type SuggestionsProps = ComponentProps<typeof ScrollArea>;
Expand Down Expand Up @@ -38,9 +39,17 @@ export const Suggestion = ({
children,
...props
}: SuggestionProps) => {
const handleClick = useCallback(() => {
onClick?.(suggestion);
}, [onClick, suggestion]);
const handleClick = useCallback(
(event: MouseEvent<HTMLButtonElement>) => {
event.currentTarget.scrollIntoView?.({
behavior: "smooth",
block: "nearest",
inline: "center",
});
onClick?.(suggestion);
},
[onClick, suggestion]
);

return (
<Button
Expand Down