import React, { useCallback, useEffect, useRef, useState } from 'react'; import { Link } from 'react-router-dom'; import { CaretLeftIcon, CaretRightIcon } from '@phosphor-icons/react'; interface ShelfProps { title: string; to?: string; kind?: 'wide' | 'tall'; children: React.ReactNode; } /** A horizontal row with scroll-snap; the title is a link and the arrows appear on hover. */ export const Shelf: React.FC = ({ title, to, kind, children }) => { const railRef = useRef(null); const [edges, setEdges] = useState({ start: true, end: false, fits: true }); const update = useCallback(() => { const rail = railRef.current; if (!rail) return; setEdges({ start: rail.scrollLeft < 4, end: rail.scrollLeft + rail.clientWidth >= rail.scrollWidth - 4, fits: rail.scrollWidth <= rail.clientWidth + 4, }); }, []); useEffect(() => { update(); const rail = railRef.current; if (!rail) return; const observer = new ResizeObserver(update); observer.observe(rail); return () => observer.disconnect(); }, [update, children]); const scroll = (direction: -1 | 1) => { const rail = railRef.current; if (rail) rail.scrollBy({ left: direction * rail.clientWidth * 0.9 }); }; return (
{to ?

{title}

:

{title}

} {!edges.fits && (
)}
{children}
); };