Skip to content

FIX : prevent tour from closing on left arrow key at first step #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { destroyHighlight, highlight } from "./highlight";
import { destroyEmitter, listen } from "./emitter";
import { getState, resetState, setState } from "./state";
import "./driver.css";
import { InvalidDriverActionError } from "./errors";
import { DRIVER_MARKER, isDriver } from "./utils";

export type DriveStep = {
element?: string | Element | (() => Element);
Expand Down Expand Up @@ -38,6 +40,7 @@ export interface Driver {
hasPreviousStep: () => boolean;
highlight: (step: DriveStep) => void;
destroy: () => void;
[DRIVER_MARKER]: true;
}

export function driver(options: Config = {}): Driver {
Expand Down Expand Up @@ -72,6 +75,10 @@ export function driver(options: Config = {}): Driver {
}

const nextStepIndex = activeIndex + 1;
// @ts-ignore
if (isDriver(this) && nextStepIndex >= steps.length) {
throw new InvalidDriverActionError("Cannot move to next step. Already at the last step.");
}
if (steps[nextStepIndex]) {
drive(nextStepIndex);
} else {
Expand All @@ -87,6 +94,9 @@ export function driver(options: Config = {}): Driver {
}

const previousStepIndex = activeIndex - 1;
if (previousStepIndex < 0) {
throw new InvalidDriverActionError("Cannot move to previous step. Already at the first step.");
}
if (steps[previousStepIndex]) {
drive(previousStepIndex);
} else {
Expand All @@ -97,6 +107,10 @@ export function driver(options: Config = {}): Driver {
function moveTo(index: number) {
const steps = getConfig("steps") || [];

if (index < 0 || index >= steps.length) {
throw new RangeError();
}

if (steps[index]) {
drive(index);
} else {
Expand Down Expand Up @@ -182,10 +196,11 @@ export function driver(options: Config = {}): Driver {
destroy();
return;
}

if (stepIndex < 0 || stepIndex >= steps.length) {
throw new RangeError();
}
if (!steps[stepIndex]) {
destroy();

return;
}

Expand Down Expand Up @@ -370,6 +385,7 @@ export function driver(options: Config = {}): Driver {
destroy: () => {
destroy(false);
},
[DRIVER_MARKER]: true,
};

setCurrentDriver(api);
Expand Down
12 changes: 12 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class InvalidDriverActionError extends Error {
constructor(errorMessage: string) {
super(errorMessage);
this.name = "InvalidDriverActionError";

Object.setPrototypeOf(this, InvalidDriverActionError.prototype);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, InvalidDriverActionError);
}
}
}
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getConfig } from "./config";
import { Driver } from "./driver";

export function easeInOutQuad(elapsed: number, initialValue: number, amountOfChange: number, duration: number): number {
if ((elapsed /= duration / 2) < 1) {
Expand Down Expand Up @@ -65,3 +66,9 @@ function isElementInView(element: Element) {
export function isElementVisible(el: HTMLElement) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
}

export const DRIVER_MARKER = Symbol("Driver");

export function isDriver(obj: any): obj is Driver {
return obj && obj[DRIVER_MARKER] === true;
}