import { Menu } from '@headlessui/react'; import { ChevronDownIcon } from '@heroicons/react/24/solid'; import cn from 'clsx'; import { AnimatePresence, motion } from 'framer-motion'; import Link from 'next/link'; import React, { Fragment, ReactElement, useEffect, useRef, useState, } from 'react'; import scrollIntoView from 'scroll-into-view-if-needed'; import { useScrolled } from '@/lib'; import clsxm from '@/lib/clsxm'; import { ActiveAnchor, useActiveAnchor } from '../contexts'; export function getHeadingText(heading: any) { return heading?.text ? heading.text : ''; } function OrdedListItem({ heading, text, slug, activeAnchor, }: { heading: any; text: string; slug: string; activeAnchor: ActiveAnchor; }): ReactElement { const state = activeAnchor[slug]; const ref = useRef(null); useEffect(() => { const el = ref.current; // @ts-ignore const [toc] = document.getElementsByClassName('fluid-toc'); if (state?.isActive && el && toc) { scrollIntoView(el, { behavior: 'smooth', block: 'center', inline: 'center', scrollMode: 'always', boundary: toc, }); } }, [state?.isActive]); return (
  • {text}
  • ); } const ListItem = ({ heading, slug, text, activeAnchor, ...props }: { heading: any; slug: string; text: string; activeAnchor: ActiveAnchor; }) => { const state = activeAnchor[`${slug}`]; return ( {({ active }) => ( {text} )} ); }; const Desktop = () => { const anchors = useActiveAnchor(); const headings = Object.keys(anchors).map((anchor) => ({ index: anchors[anchor].index, depth: anchors[anchor].depth, value: anchor, text: anchors[anchor]?.text, })); const hasHeadings = headings.length > 0; return ( <>
    {hasHeadings && (

      On this page

      {headings.map((heading) => { return ( ); })}
    )}
    ); }; const Mobile = ({hasScrolled, isWithinTop}) => { const anchors = useActiveAnchor(); const headings = Object.keys(anchors).map((anchor) => ({ index: anchors[anchor].index, depth: anchors[anchor].depth, value: anchor, text: anchors[anchor]?.text, })); const hasHeadings = headings.length > 0; const activeHeading = (hasHeadings && headings.find((heading) => { return anchors[heading.value]?.isActive; })) || { index: 0, depth: 1, text: 'On this page', }; if (headings.length === 0) { return null; } return ( {({ open }) => ( Expand section list

    {getHeadingText(activeHeading)} On this page

    {open && (
    {headings.map((heading) => { return ( ); })}
    )}
    )}
    ); }; Desktop.displayName = 'TOCDesktop'; Mobile.displayName = 'TOCMobile'; export const TOC = Object.assign({}, { Desktop, Mobile });