import { useEffect, useState } from 'react' import { useInView } from 'react-intersection-observer' import { StepHikeContext } from './StepHikeContext' const StepHike = ({ children }) => { const [activeStep, setActiveStep] = useState<{ titleId: string; step: number } | undefined>( undefined ) // check if there are any children if (!children) throw 'StepHike component requires children' const steps = children.filter((x: any) => { return x.type.name === 'Step' }) useEffect(() => { if (steps.length > 0) { setActiveStep({ titleId: steps[0].props.title.replaceAll(' ', '-').toLowerCase(), step: 0, }) } }, []) // check if there is at least 1 StepHike subcomponent if (steps.length === 0 || !steps) throw 'StepHike component needs at least 1 child' return (
{children}
) } const Step = ({ children, title, step }) => { const [show, setShow] = useState(false) useEffect(() => { setShow(true) }, []) const ChildrenRender = ({ active }) =>
{children}
const { ref } = useInView({ rootMargin: '10px 20px 30px 40px', threshold: 1, onChange: (inView, entry) => { if (window.scrollY === 0) console.log('out of view', title) if (inView) console.log('in view', title) // highlightSelectedTocItem(entry.target.id) }, }) return ( <> {({ activeStep, steps }) => { const cleanTitleId = title.replaceAll(' ', '-').toLowerCase() const active = cleanTitleId === activeStep?.titleId return (
{step}

{title}

Step {step} of {steps?.length}
) }}
) } StepHike.Step = Step export default StepHike