diff --git a/src/app/[lang]/Menu.tsx b/src/app/[lang]/Menu.tsx index f993928..d5e0b2d 100644 --- a/src/app/[lang]/Menu.tsx +++ b/src/app/[lang]/Menu.tsx @@ -1,5 +1,6 @@ 'use client'; +import { CSSProperties, useLayoutEffect, useRef, useState } from 'react'; import { usePathname } from 'next/navigation'; import Link from '@/components/Link'; @@ -16,6 +17,8 @@ export type MenuProps = { }; function Menu({ menu }: MenuProps) { + const [offset, setOffset] = useState({ left: 0, width: 16 }); + const menuRef = useRef(null); const pathname = usePathname(); const locale = getLocale(pathname) || ''; const localePrefix = new RegExp(`^/${locale}/?([^/]*)/?`); @@ -24,11 +27,28 @@ function Menu({ menu }: MenuProps) { (item) => item.href === `/${rootPathname}` ); + const menuStyle = { + '--active-offset-w': (offset.width - 16) / 40, + '--active-offset-x': `${offset.left + 8}px`, + } as CSSProperties; + + useLayoutEffect(() => { + const itemElement = menuRef.current?.children[activeLinkIndex]; + + if (itemElement instanceof HTMLElement) { + setOffset({ + left: itemElement.offsetLeft, + width: itemElement.offsetWidth, + }); + } + }, [activeLinkIndex]); + return ( -