react-joyride icon indicating copy to clipboard operation
react-joyride copied to clipboard

Title: External stepIndex changes in controlled mode corrupt internal lifecycle — overlay close breaks

Open rakhmanov opened this issue 8 months ago • 1 comments

Describe the bug

react-joyride version: 3.0.2 React version: 19

Description In controlled mode, changing stepIndex via external state (e.g., a useEffect reacting to app state changes) is the documented API for programmatic navigation. However, when stepIndex is updated outside of joyride's own onEvent flow, the internal lifecycle state machine does not fully synchronize. Subsequent overlay close actions (overlayClickAction: "close") produce inconsistent behavior — the overlay may get stuck, or the step transition may be incorrect.

Steps to reproduce Set up a controlled tour with stepIndex and overlayClickAction: "close" Add a collapsible sidebar with a tour step targeting a nav item inside it Use a useEffect to update stepIndex when the sidebar opens (a common pattern for conditional steps):

const targetStep = sidebarOpen ? 1 : 0; useEffect(() => { if (!run) return; setStepIndex(targetStep); }, [run, targetStep]); Start the tour — tooltip appears on sidebar toggle (step 0) Click the sidebar toggle button directly (not via joyride's Next button) — sidebar opens The useEffect sets stepIndex to 1 — tooltip correctly shows on the nav item Click the dark overlay to dismiss Result: Overlay behavior is broken — it either gets stuck or advances incorrectly. The internal state machine was not expecting the stepIndex change since it didn't originate from onEvent.

Expected behavior Externally changing stepIndex should fully synchronize joyride's internal lifecycle state. The overlay close behavior should work identically whether the step was reached via onEvent advancement or via an external stepIndex update.

Context This pattern (external stepIndex changes driven by app state) is common in real applications:

Conditional steps that appear/disappear based on UI state (sidebar open/closed) Steps that auto-advance when the user performs an action outside the tour (e.g., navigates via the actual UI instead of clicking the highlighted element) SPA page transitions where the step list is dynamically filtered

react-joyride version

3.0.2

Reproduction link

https://stackblitz.com/edit/react-joyride-zrtppwyk?file=src%2FApp.tsx

Screenshots or videos

No response

Additional context

No response

rakhmanov avatar Apr 04 '26 13:04 rakhmanov

Hey @rakhmanov

Pulled the Stackblitz repro — overlay click fires step:after with ACTIONS.CLOSE and your onEvent advances to the next step. The beacon shows on the next target. That's working as designed, not a corrupt state.

The pattern you describe is the anti-pattern: driving stepIndex from a useEffect that watches app state.

// don't do this
useEffect(() => {
  if (!run) return;
  setStepIndex(sidebarOpen ? 1 : 0);
}, [run, sidebarOpen]);

External stepIndex writes that don't originate from onEvent race the lifecycle state machine. The lifecycle has phases (init → ready → *_before → beacon/tooltip → complete); an external index jump mid-phase can resolve a different target than the one the lifecycle is currently transitioning into. Result: stuck overlays, beacons on the wrong step, etc.

Use a before step hook instead — the documented pattern for conditional UI:

const steps = [
  {
    target: '.sidebar-toggle',
    content: 'Open the sidebar.',
  },
  {
    target: '.sidebar-nav-item',
    content: 'Tweak settings here.',
    before: async () => {
      setSidebarOpen(true);
      await new Promise(r => setTimeout(r, 300)); // open animation
    },
    after: () => setSidebarOpen(false), // optional cleanup
  },
];

before runs before the step's lifecycle starts → state change and target resolution happen in the right order. No external stepIndex writes.

Full recipe: Conditional / Dynamic Steps.

For the "dismiss tooltip → show beacon on same step" use case (related to #1205), use the new 'replay' action shipped in 3.1.0:

{ overlayClickAction: 'replay' }

The Controlled mode warning in the docs (How It Works → Controlled) covers this verbatim. Reach for stepIndex only when an external system owns the index (persistence, multi-tour orchestrator). For everything else — uncontrolled + before hooks.

gilbarbara avatar Apr 29 '26 03:04 gilbarbara