'use client' import { createRef, useEffect } from 'react' import ProjectType from '../../types/project' import { Project } from './Project' import styles from './index.module.css' type Props = { projects: ProjectType[] currentSlug: string } export default function ProjectNav({ projects, currentSlug }: Props) { // Always keep the scroll position centered // to currently viewed project on mount. const scrollContainer = createRef() const currentItem = createRef() useEffect(() => { function scrollToCurrent() { const activeItem = currentItem.current const scrollRect = scrollContainer.current?.getBoundingClientRect() const activeRect = activeItem && activeItem.getBoundingClientRect() const newScrollLeftPosition = activeRect && activeRect.left - scrollRect.left - scrollRect.width / 2 + activeRect.width / 2 scrollContainer.current.scrollLeft += newScrollLeftPosition } scrollToCurrent() }, [scrollContainer, currentItem]) return ( ) }