"use client"; import { arc, useAnimate, useReducedMotion } from "motion/react"; import { type ReactNode, useCallback, useLayoutEffect, useRef, useState, } from "react"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; export interface BounceSidebarItem { /** Unique key, used as the sidebar's active value. */ id: string; /** Text shown in the row. */ label: ReactNode; /** Link address. Renders a link instead of a button when set. */ href?: string; /** Icon shown before the label. Omitted when not set. */ icon?: ReactNode; /** Dims the row and blocks selection. */ disabled?: boolean; /** Where the link opens. Only used with `href`. */ target?: "_blank" | "_self" | "_parent" | "_top"; /** Link `rel`. Default: "noreferrer noopener" when `target` is "_blank" */ rel?: string; } /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const bounceSidebarMessages = { label: "Sidebar navigation", }; export type BounceSidebarMessages = typeof bounceSidebarMessages; export interface BounceSidebarProps { /** Links or buttons listed in the sidebar, in order. */ items: BounceSidebarItem[]; /** Id of the active item, for controlled use. */ value?: string; /** The initial active item id when uncontrolled. Defaults to the first item. */ defaultValue?: string; /** Called with the item id when an item is picked. */ onValueChange?: (value: string) => void; /** Accessible name for the nav. Default: "Sidebar navigation" */ ariaLabel?: string; /** Override any built-in string. */ labels?: Partial; /** Classes merged onto the root nav element. */ className?: string; /** Classes merged onto the list element. */ listClassName?: string; /** Classes merged onto each item's link or button. */ itemClassName?: string; /** Classes merged onto the bouncing dot. */ indicatorClassName?: string; } const DOT_SIZE = 6; // A compact, lightly underdamped spring gives the dot a quick landing without // turning the sidebar into a playful toy. The sideways arc carries the bounce. const BOUNCE_SPRING = { type: "spring", stiffness: 280, damping: 18, mass: 0.3, } as const; // The dot hops sideways, away from the labels. arc() bends proportionally to // the distance travelled, so cap it: adjacent rows get a ~5px hop and a long // jump peaks near 20px instead of swinging out of the sidebar. const ARC_MAX_STRENGTH = 0.26; const ARC_MAX_BULGE = 40; export function BounceSidebar({ items, value, defaultValue, onValueChange, ariaLabel, labels, className, listClassName, itemClassName, indicatorClassName, }: BounceSidebarProps) { const reduce = useReducedMotion(); const t = useMessages("bounceSidebar", bounceSidebarMessages, labels); const [internalValue, setInternalValue] = useState( defaultValue ?? items[0]?.id ?? "", ); const requestedValue = value ?? internalValue; const selectedValue = items.some((item) => item.id === requestedValue) ? requestedValue : (items[0]?.id ?? ""); const selectedIndex = items.findIndex((item) => item.id === selectedValue); const listRef = useRef(null); const itemRefs = useRef(new Map()); const selectedValueRef = useRef(selectedValue); const previousIndexRef = useRef(selectedIndex); const hasPositionRef = useRef(false); const dotYRef = useRef(0); const [dot, animateDot] = useAnimate(); selectedValueRef.current = selectedValue; const placeDot = useCallback( (destinationY: number) => { if (!dot.current) return; animateDot(dot.current, { x: 0, y: destinationY }, { duration: 0 }); dotYRef.current = destinationY; hasPositionRef.current = true; }, [animateDot, dot], ); const snapIndicator = useCallback(() => { const selectedItem = itemRefs.current.get(selectedValueRef.current); if (!selectedItem) return; placeDot( selectedItem.offsetTop + (selectedItem.offsetHeight - DOT_SIZE) / 2, ); }, [placeDot]); const positionIndicator = useCallback( (shouldAnimate: boolean) => { const selectedItem = itemRefs.current.get(selectedValue); if (!selectedItem) return; const destinationY = selectedItem.offsetTop + (selectedItem.offsetHeight - DOT_SIZE) / 2; if (!hasPositionRef.current || reduce || !shouldAnimate || !dot.current) { placeDot(destinationY); previousIndexRef.current = selectedIndex; return; } const distance = destinationY - dotYRef.current; const travel = Math.abs(distance); const longJumpProgress = Math.min(1, Math.max(0, (travel - 48) / 120)); dotYRef.current = destinationY; animateDot( dot.current, { x: 0, y: destinationY }, { ...BOUNCE_SPRING, stiffness: BOUNCE_SPRING.stiffness - 60 * longJumpProgress, damping: BOUNCE_SPRING.damping + longJumpProgress, mass: BOUNCE_SPRING.mass + 0.15 * longJumpProgress, path: arc({ strength: Math.min(ARC_MAX_STRENGTH, ARC_MAX_BULGE / travel), // Relative to the direction of travel, so both directions // bulge to the screen left, away from the labels. direction: distance > 0 ? "ccw" : "cw", }), }, ); previousIndexRef.current = selectedIndex; }, [animateDot, dot, placeDot, reduce, selectedIndex, selectedValue], ); useLayoutEffect(() => { const shouldAnimate = hasPositionRef.current && previousIndexRef.current !== selectedIndex; positionIndicator(shouldAnimate); }, [positionIndicator, selectedIndex]); useLayoutEffect(() => { const list = listRef.current; if (!list || typeof ResizeObserver === "undefined") return; const observer = new ResizeObserver(snapIndicator); observer.observe(list); return () => observer.disconnect(); }, [snapIndicator]); const selectItem = (id: string) => { if (value === undefined) setInternalValue(id); onValueChange?.(id); }; return ( ); }