import React, { useMemo, useState } from 'react'
import Dropdown from 'react-bootstrap/Dropdown'
import FormControl from 'react-bootstrap/FormControl'
import TocIcon from '@/svgs/list-unordered.svg'
import { useRouter } from 'next/router'
import { extractHeadings } from '@/lib/toc'
export default function Toc ({ text }) {
const router = useRouter()
if (!text || text.length === 0) {
return null
}
const toc = useMemo(() => extractHeadings(text), [text])
if (toc.length === 0) {
return null
}
return (
{toc.map(v => {
return (
router.events.emit('hashChangeStart', `#${v.slug}`, { shallow: true })}
>{v.heading}
)
})}
)
}
const CustomToggle = React.forwardRef(({ children, onClick }, ref) => (
{
e.preventDefault()
onClick(e)
}}
>
{children}
))
// forwardRef again here!
// Dropdown needs access to the DOM of the Menu to measure it
const CustomMenu = React.forwardRef(
({ children, style, className, 'aria-labelledby': labeledBy }, ref) => {
const [value, setValue] = useState('')
return (
setValue(e.target.value)}
value={value}
/>
{React.Children.toArray(children).filter(
(child) =>
!value || child.props.children.toLowerCase().includes(value)
)}
)
}
)