Intro.js React Wrapper
intro.js-react is a lightweight and actively maintained React wrapper for the Intro.js library, designed to simplify the creation of step-by-step product tours and hints within React applications. Currently at version 1.0.0, this package provides two primary components: `Steps` for guided tours and `Hints` for contextual popovers. It abstracts away direct Intro.js imperative API calls, allowing developers to manage tours declaratively using React props for configuration, steps, and lifecycle callbacks. Key differentiators include its adherence to React's component model, handling Intro.js instance management, and providing a more idiomatic React experience compared to integrating Intro.js directly. While it requires `intro.js` and `react` as peer dependencies, it handles the synchronization between React state and the underlying Intro.js instance, offering callbacks for various tour events like `onStart`, `onChange`, and `onComplete`. The package also supports Intro.js options directly through a dedicated `options` prop.
Common errors
-
Module not found: Can't resolve 'intro.js/introjs.css'
cause The Intro.js CSS file is missing or the import path is incorrect.fixEnsure `intro.js` is installed and add `import 'intro.js/introjs.css';` to your project. -
Error: Cannot find module 'intro.js' or 'react'
cause The peer dependencies `intro.js` or `react` have not been installed.fixInstall the peer dependencies: `npm install intro.js react` or `yarn add intro.js react`. -
The prop `onExit` is marked as required in `Steps`, but its value is `undefined`.
cause The `onExit` callback prop was not provided to the `Steps` component.fixDefine and pass an `onExit` function to the `Steps` component, typically to set `enabled` to `false`.
Warnings
- gotcha Both `intro.js` and `react` are peer dependencies and must be installed separately alongside `intro.js-react`. Failure to do so will result in runtime errors.
- gotcha The `intro.js` CSS styles must be manually imported into your project. This wrapper does not automatically include them.
- gotcha Step indexes in `intro.js-react` (e.g., for `initialStep` and `onExit` callbacks) are 0-based, unlike the underlying `intro.js` library where they are 1-based. Be mindful of this difference when migrating or debugging.
- gotcha The `onExit` prop for the `Steps` component is required. This callback is crucial for updating your component's state when the Intro.js tour is dismissed by user action (e.g., pressing ESC or clicking 'Done'), ensuring the `enabled` prop remains synchronized.
Install
-
npm install intro.js-react -
yarn add intro.js-react -
pnpm add intro.js-react
Imports
- Steps
const Steps = require('intro.js-react').Steps;import { Steps } from 'intro.js-react'; - Hints
import Hints from 'intro.js-react/Hints';
import { Hints } from 'intro.js-react'; - intro.js/introjs.css
import 'intro.js-react/dist/introjs.css';
import 'intro.js/introjs.css';
Quickstart
import React, { useState } from 'react';
import { Steps } from 'intro.js-react';
import 'intro.js/introjs.css'; // Don't forget to import Intro.js CSS
// Simulate some elements for the tour
const MyComponent = () => {
const [stepsEnabled, setStepsEnabled] = useState(true);
const [initialStep, setInitialStep] = useState(0);
const steps = [
{
element: '.step-one-selector', // Make sure this selector exists in your DOM
intro: 'This is the first step of our tour!',
position: 'right',
},
{
element: '.step-two-selector',
intro: 'And here is the second important element.',
},
{
element: '.step-three-selector',
intro: 'Finally, this is the last piece of information.',
position: 'bottom',
},
];
const onExit = () => {
setStepsEnabled(false);
setInitialStep(0); // Reset for next time
};
return (
<div>
<h1 className="step-one-selector">Welcome to My App</h1>
<p className="step-two-selector">This is some content.</p>
<button className="step-three-selector" onClick={() => setStepsEnabled(true)}>
Start Tour
</button>
<Steps
enabled={stepsEnabled}
steps={steps}
initialStep={initialStep}
onExit={onExit}
// You can also pass Intro.js options directly
options={{
showButtons: true,
showStepNumbers: false,
exitOnOverlayClick: false,
}}
/>
</div>
);
};
export default MyComponent;