HiDeoo / intro.js-react

Intro.js react wrapper
MIT License
409 stars 57 forks source link

Changing example to React functional components #110

Open helios2003 opened 8 months ago

helios2003 commented 8 months ago

Is your feature request related to a problem?

As of 2024, most developers have been using React's functional components instead of Class-based components. Hence understanding the documentation of the library would be easy if components were written using functions. Look at the example here: https://codesandbox.io/embed/o2A4gwXE3

Describe the solution you'd like

Rewriting the examples to functional components.

Describe alternatives you've considered

No response

Additional Context

No response

jcapogna commented 4 months ago

This is especially needed if you're using dynamic components as refs have changed.

An example of how to update dynamic components in a functional component:

const stepsRef = useRef<Steps>(null);

const onBeforeChange = async (nextStepIndex: number) => {
    if (nextStepIndex === 3) {
        await waitForElementToLoad()
        stepsRef.current?.updateStepElement(nextStepIndex)
    }
}

return (
    <Steps
        ...
        onBeforeChange={onBeforeChange}
        ref={stepsRef}
    />
)
jaimefps commented 3 months ago

@jcapogna I've been fighting this for hours. Happy I found your comment. Note that I had to also account for a transition that moves the element across the screen, so I also have a delay in place to account for both te element mounting and the transition being done, pseudo code looks like:

const onBeforeChange = async (nextStepIndex: number) => {
  if (nextStepIndex === 3) {
    induceUiChange()

    await waitForElementToLoad()

    await delay(1000) // wait for transition to complete

    stepsRef.current?.updateStepElement(nextStepIndex)
  }
}
jcapogna commented 3 months ago

@jaimefps Glad it helped. I had to delay for a transition in my use case as well. I trigger an element to appear, which then results in an animation happening.