ngx-hopscotch
An injectable Angular service that provides an opinionated abstraction of Hopscotch for adding product tours to Angular apps. ngx-hopscotch was built with Angular CLI and Nrwl Schematics and packaged by ng-packagr.
Installation
npm install ngx-hopscotch --save
Usage
Import NgxHopscotchModule.forRoot() in your AppModule and optionally provide initial properties for the tour and callout.
;
Configure your tour steps in your root AppComponent.
; ;
Optionally, customize your tour steps with StepOptions callbacks. There's two ways in which you can add these callbacks to your tour steps.
The first way is with the HopscotchService.step() function and this way can be placed in any of the Angular component lifecycle hooks as appropriate for your app.
this._hopscotchService.step1, ;
The other way to add the callbacks is with the HopscotchService.configure() function, and this way should only be done in the ngOnInit hook.
this._hopscotchService.configure;
Call HopscotchService.init() in your "main" component's ngAfterViewInit hook -- this component contains the target for your callout.
public ngAfterViewInit: void
Create a handler for your "Start Tour" button which calls the step() function to start the tour.
public startTour: void
Call HopscotchService.end() in your "main" component's ngOnDestroy hook -- again this should be the component that contains the target for your callout.
public ngOnDestroy: void
Don't forget to include the Hopscotch styles somewhere in your Angular project.
Considerations for Latent Targets
When configuring your tour steps, pay close attention to the steps with targets that aren't currently visible - such targets could be part of a lazy loaded component, a conditionally included template by way of the ngIf directive, etc. If such a step exists, then you must add the multipage property to the definition of its previous step. For example, consider steps 0 and 1 above - step 0 has its multipage property set because step 1's target appears at a different route, and step 1 has its multipage property set because step 2's target is conditioned on an ngIf.
In the ngAfterViewInit hook of the latent target's component, you'll need to call HopscotchService.step() with the appropriate index to resume the tour.
HopscotchService API
TourStep
-
stepIndex: number - Zero-based index step.
-
stepDef: any - Technically, this can be anything understood by Hopscotch, but for ngx-hopscotch the only callbacks that are supported are onPrev and onNext.
StepOptions
-
onPrev: () => void - Handler for a step's Back button.
-
onNext: () => void - Handler for a step's Next button.
Functions
-
configure(steps:TourStep[]) - Call this function in your root AppComponent's ngOnInit hook to define all the steps of your tour. You can also use this function to add StepOptions callbacks to your tour steps in any one of your component's lifecycle hooks as appropriate for your app.
-
init() - Call this function in the ngAfterViewInit hook of the component that contains the target for your callout. If you didn't specify a callout in NgxHopscotchModule.forRoot(), then this function will resume your tour at the current step.
-
step(index:number, options?:StepOptions) - Call this function with an index of 0 to start the underlying Hopscotch tour. You'll only need to call this function again for any latent steps in your tour.
-
end() - Stop the underlying Hopscotch tour and release resources.
Demo
git clone https://github.com/LoominusAI/ngx-hopscotch.gitcd ngx-hopscotchnpm install or yarn installnpm run build:libng serve --app demo-packaged
Open a browser to http://localhost:4200
.