Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Lightweight onboarding library for Next.js

NotificationsYou must be signed in to change notification settings

enszrlu/NextStep

Repository files navigation

NextStep

NextStep is a lightweight onboarding library for Next.js / React applications. It utilizesmotion for smooth animations and supports multiple React frameworks including Next.js, React Router, and Remix.

Some of the use cases:

  • Easier Onboarding: Guide new users with step-by-step tours
  • Engagement Boost: Make help docs interactive, so users learn bydoing.
  • Better Error Handling: Skip generic toasters—show users exactly what to fix with tailored tours.
  • Event-Based Tours: Trigger custom tours after key actions to keep users coming back.

The library allows users to use custom cards (tooltips) for easier integration.

If you like the project, please leave a star! ⭐️⭐️⭐️⭐️⭐️

Getting Started

# npmnpm i nextstepjs motion# pnpmpnpm add nextstepjs motion# yarnyarn add nextstepjs motion# bunbun add nextstepjs motion

Navigation Adapters (v2.0+)

NextStep 2.0 introduces a framework-agnostic routing system through navigation adapters. Each adapter is packaged separately to minimize bundle size - only the adapter you import will be included in your bundle.

Important: Make sure to import the adapter you need in your app in order to access full functionality. Without an adapter, navigation features likenextRoute andprevRoute may not work properly.

Built-in Adapters

Next.js

NextStep uses Next.js adapter as default, therefore you don't need to import it.

// app/layout.tsx or pages/_app.tsximport{NextStep,NextStepProvider}from'nextstepjs';exportdefaultfunctionLayout({ children}){return(<NextStepProvider><NextStepsteps={steps}>{children}</NextStep></NextStepProvider>);}
React Router as a Framework
//app/root.tsximport{NextStepProvider,NextStepReact,typeTour}from'nextstepjs';import{useReactRouterAdapter}from'nextstepjs/adapters/react-router';exportdefaultfunctionApp(){return(<NextStepProvider><NextStepReactnavigationAdapter={useReactRouterAdapter}steps={steps}><Outlet/></NextStepReact></NextStepProvider>);}
Remix
// root.tsximport{NextStepProvider,NextStepReact}from'nextstepjs';import{useRemixAdapter}from'nextstepjs/adapters/remix';exportdefaultfunctionApp(){return(<NextStepProvider><NextStepReactnavigationAdapter={useRemixAdapter}steps={steps}><Outlet/></NextStepReact></NextStepProvider>);}
Important Configuration for Vite (React Router or Remix)

If you're using Vite with React Router or Remix, add the following configuration to yourvite.config.ts:

exportdefaultdefineConfig({ssr:{noExternal:['nextstepjs','motion'],},});
Custom Navigation Adapter

You can create your own navigation adapter for any routing solution by implementing theNavigationAdapter interface:

import{NextStepReact}from'nextstepjs';importtype{NavigationAdapter}from'nextstepjs';constuseCustomAdapter=():NavigationAdapter=>{return{push:(path:string)=>{// Your navigation logic here// Example: history.push(path)},getCurrentPath:()=>{// Your path retrieval logic here// Example: window.location.pathnamereturnwindow.location.pathname;},};};constApp=()=>{return(<NextStepReactnavigationAdapter={useCustomAdapter}steps={steps}>{children}</NextStepReact>);};

Troubleshooting

If you encounter an error related to module exports when using the Pages Router, it is likely due to a mismatch between ES modules (which useexport statements) and CommonJS modules (which usemodule.exports). Thenextstepjs package uses ES module syntax, but your Next.js project might be set up to use CommonJS.

To resolve this issue, ensure that your Next.js project is configured to support ES modules. You can do this by updating yournext.config.js file to include the following configuration:

/**@type {import('next').NextConfig} */constnextConfig={reactStrictMode:true,experimental:{esmExternals:true,},transpilePackages:['nextstepjs'],};exportdefaultnextConfig;

Custom Card

You can create a custom card component for greater control over the design:

PropTypeDescription
stepObjectThe currentStep object from your steps array, including content, title, etc.
currentStepnumberThe index of the current step in the steps array.
totalStepsnumberThe total number of steps in the onboarding process.
nextStepA function to advance to the next step in the onboarding process.
prevStepA function to go back to the previous step in the onboarding process.
arrowReturns an SVG object, the orientation is controlled by the steps side prop
skipTourA function to skip the tour
'use client';importtype{CardComponentProps}from'nextstepjs';exportconstCustomCard=({  step,  currentStep,  totalSteps,  nextStep,  prevStep,  skipTour,  arrow,}:CardComponentProps)=>{return(<div><h1>{step.icon}{step.title}</h1><h2>{currentStep} of{totalSteps}</h2><p>{step.content}</p><buttononClick={prevStep}>Previous</button><buttononClick={nextStep}>Next</button><buttononClick={skipTour}>Skip</button>{arrow}</div>);};

Tours Array

NextStep supports multiple "tours", allowing you to create multiple product tours:

import{Tour}from'nextstepjs';conststeps:Tour[]=[{tour:'firstTour',steps:[// Step objects],},{tour:'secondTour',steps:[// Step objects],},];

Step Object

PropTypeDescription
iconReact.ReactNode,string,nullAn icon or element to display alongside the step title.
titlestringThe title of your step
contentReact.ReactNodeThe main content or body of the step.
selectorstringOptional. A string used to target anid that this step refers to. If not provided, card will be displayed in the center top of the document body.
side"top","bottom","left","right"Optional. Determines where the tooltip should appear relative to the selector.
showControlsbooleanOptional. Determines whether control buttons (next, prev) should be shown if using the default card.
showSkipbooleanOptional. Determines whether skip button should be shown if using the default card.
blockKeyboardControlbooleanOptional. Determines whether keyboard control should be blocked
pointerPaddingnumberOptional. The padding around the pointer (keyhole) highlighting the target element.
pointerRadiusnumberOptional. The border-radius of the pointer (keyhole) highlighting the target element.
nextRoutestringOptional. The route to navigate to when moving to the next step.
prevRoutestringOptional. The route to navigate to when moving to the previous step.
viewportIDstringOptional. The id of the viewport element to use for positioning. If not provided, the document body will be used.

NoteNextStep handles card cutoff from screen sides. If side is right or left and card is out of the viewport, side would be switched totop. If side is top or bottom and card is out of the viewport, then side would be flipped between top and bottom.

Target Anything

Target anything in your app using the element'sid attribute.

<divid="nextstep-step1">Onboard Step</div>

Routing During a Tour

NextStep allows you to navigate between different routes during a tour using thenextRoute andprevRoute properties in the step object. These properties enable seamless transitions between different pages or sections of your application.

  • nextRoute: Specifies the route to navigate to when the "Next" button is clicked.
  • prevRoute: Specifies the route to navigate to when the "Previous" button is clicked.

WhennextRoute orprevRoute is provided, NextStep will use Next.js'snext/navigation to navigate to the specified route.

Using NextStepViewport and viewportID

When a selector is in a scrollable area, it is best to wrap the content of the scrollable area withNextStepViewport. This component takeschildren and anid as prop. By providing theviewportID to the step, NextStep will target this element within the viewport. This ensures that the step is anchored to the element even if the container is scrollable.

Here's an example of how to useNextStepViewport:

<divclassName="relative overflow-auto h-64"><NextStepViewportid="scrollable-viewport">{children}</NextStepViewport></div>

Examplesteps

[{tour:'firsttour',steps:[{icon:<>👋</>,title:'Tour 1, Step 1',content:<>First tour, first step</>,selector:'#tour1-step1',side:'top',showControls:true,showSkip:true,pointerPadding:10,pointerRadius:10,nextRoute:'/foo',prevRoute:'/bar',},{icon:<>🎉</>,title:'Tour 1, Step 2',content:<>First tour, second step</>,selector:'#tour1-step2',side:'top',showControls:true,showSkip:true,pointerPadding:10,pointerRadius:10,viewportID:'scrollable-viewport',},],},{tour:'secondtour',steps:[{icon:<>🚀</>,title:'Second tour, Step 1',content:<>Second tour, first step!</>,selector:'#nextstep-step1',side:'top',showControls:true,showSkip:true,pointerPadding:10,pointerRadius:10,nextRoute:'/foo',prevRoute:'/bar',},],},];

NextStep & NextStepReact Props

PropertyTypeDescription
childrenReact.ReactNodeYour website or application content
stepsArray[]Array of Tour objects defining each step of the onboarding
navigationAdapterNavigationAdapterOptional. Router adapter for navigation (defaults to Next.js on NextStep and window adapter on NextStepReact)
showNextStepbooleanControls visibility of the onboarding overlay
shadowRgbstringRGB values for the shadow color surrounding the target area
shadowOpacitystringOpacity value for the shadow surrounding the target area
cardComponentReact.ComponentTypeCustom card component to replace the default one
cardTransitionTransitionMotion transition object for step transitions
onStart(tourName: string | null) => voidCallback function triggered when the tour starts
onStepChange(step: number, tourName: string | null) => voidCallback function triggered when the step changes
onComplete(tourName: string | null) => voidCallback function triggered when the tour completes
onSkip(step: number, tourName: string | null) => voidCallback function triggered when the user skips the tour
clickThroughOverlaybooleanOptional. If true, overlay background is clickable, default is false
disableConsoleLogsbooleanOptional. If true, console logs are disabled, default is false
scrollToTopbooleanOptional. If true, the page will scroll to the top when the tour ends, default is true
noInViewScrollbooleanOptional. If true, the page will not scroll to the target element when it is in view, default is false
<NextStepsteps={steps}showNextStep={true}shadowRgb="55,48,163"shadowOpacity="0.8"cardComponent={CustomCard}cardTransition={{duration:0.5,type:'spring'}}onStepChange={(step,tourName)=>console.log(`Step changed to${step} in${tourName}`)}onComplete={(tourName)=>console.log(`Tour completed:${tourName}`)}onSkip={(step,tourName)=>console.log(`Tour skipped:${step} in${tourName}`)}clickThroughOverlay={false}>{children}</NextStep>

useNextStep Hook

useNextStep hook allows you to control the tour from anywhere in your app.

import{useNextStep}from'nextstepjs';....const{ startNextStep, closeNextStep}=useNextStep();constonClickHandler=(tourName:string)=>{startNextStep(tourName);};

Keyboard Navigation

NextStep supports keyboard navigation:

  • Right Arrow: Next step
  • Left Arrow: Previous step
  • Escape: Skip tour

Localization

NextStep is a lightweight library and does not come with localization support. However, you can easily switch between languages by supplying thesteps array based on locale.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

Credits

  • Onborda for the inspiration and some code snippets.

[8]ページ先頭

©2009-2025 Movatter.jp