From 8a4c64c42f68ef9ba7d254eca36af4f7edf943b3 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 11 Jun 2025 17:34:16 +0200 Subject: [PATCH] Scroll control into view Signed-off-by: Bryan Frimin Signed-off-by: Sacha Al Himdani --- .../ui/src/Atoms/ControlItem/ControlItem.tsx | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/ui/src/Atoms/ControlItem/ControlItem.tsx b/packages/ui/src/Atoms/ControlItem/ControlItem.tsx index 69c5ea89d..329b21cb7 100644 --- a/packages/ui/src/Atoms/ControlItem/ControlItem.tsx +++ b/packages/ui/src/Atoms/ControlItem/ControlItem.tsx @@ -1,4 +1,4 @@ -import type { HTMLAttributes } from "react"; +import { useEffect, useRef, type HTMLAttributes } from "react"; import { Link } from "react-router"; import { tv } from "tailwind-variants"; @@ -6,7 +6,7 @@ type Props = { active?: boolean; id: string; description?: string; - to?: string; + to: string; } & HTMLAttributes; const classNames = tv({ @@ -40,12 +40,20 @@ export function ControlItem({ active, id, description, to, ...props }: Props) { } = classNames({ active, }); - if (to) { - return ( - -
{id}
-
{description}
- - ); - } + + const ref = useRef(null); + + // Make the active element scroll into view when selected + useEffect(() => { + if (ref.current && active) { + ref.current.scrollIntoView({ behavior: "smooth", block: "center" }); + } + }, [active]); + + return ( + +
{id}
+
{description}
+ + ); }