Scroll control into view

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Jonathan
2025-06-11 17:34:16 +02:00
committed by Sacha Al Himdani
parent 20be4c5752
commit 8a4c64c42f

View File

@@ -1,4 +1,4 @@
import type { HTMLAttributes } from "react"; import { useEffect, useRef, type HTMLAttributes } from "react";
import { Link } from "react-router"; import { Link } from "react-router";
import { tv } from "tailwind-variants"; import { tv } from "tailwind-variants";
@@ -6,7 +6,7 @@ type Props = {
active?: boolean; active?: boolean;
id: string; id: string;
description?: string; description?: string;
to?: string; to: string;
} & HTMLAttributes<HTMLAnchorElement>; } & HTMLAttributes<HTMLAnchorElement>;
const classNames = tv({ const classNames = tv({
@@ -40,12 +40,20 @@ export function ControlItem({ active, id, description, to, ...props }: Props) {
} = classNames({ } = classNames({
active, active,
}); });
if (to) {
const ref = useRef<HTMLAnchorElement>(null);
// Make the active element scroll into view when selected
useEffect(() => {
if (ref.current && active) {
ref.current.scrollIntoView({ behavior: "smooth", block: "center" });
}
}, [active]);
return ( return (
<Link className={wrapper()} to={to} {...props}> <Link className={wrapper()} to={to} {...props} ref={ref}>
<div className={idCls()}>{id}</div> <div className={idCls()}>{id}</div>
<div className={descriptionCls()}>{description}</div> <div className={descriptionCls()}>{description}</div>
</Link> </Link>
); );
}
} }