Skip to content

Commit

Permalink
fix: allow disabling scrollIntoView via "no-scroll" option (step-lvl …
Browse files Browse the repository at this point in the history
…and/or top-lvl)
  • Loading branch information
josias-r committed Jan 4, 2023
1 parent 35a7e47 commit e78e2dc
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ <h4>Boarding Definition</h4>
prevBtnText: 'Previous', // Previous button text for this step
showButtons: false, // Do not show control buttons in footer
keyboardControl: true, // Allow controlling through keyboard (escape to close, arrow keys to move)
scrollIntoViewOptions: {}, // We use `scrollIntoView()` when possible, pass here the options for it if you want any
scrollIntoViewOptions: {
behaviour: "smooth",
}, // We use `scrollIntoView()` when possible, pass here the options for it if you want any. Alternatively, you can also disable this functionallity by setting scrollIntoViewOptions to "no-scroll"
onBeforeHighlighted: (Element) {}, // Called when element is about to be highlighted
onHighlighted: (Element) {}, // Called when element is fully highlighted
onDeselected: (Element) {}, // Called when element has been deselected
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ const boarding = new Boarding({
prevBtnText: "Previous", // Previous button text for this step
showButtons: false, // Do not show control buttons in footer
keyboardControl: true, // Allow controlling through keyboard (escape to close, arrow keys to move)
scrollIntoViewOptions: {}, // We use `scrollIntoView()` when possible, pass here the options for it if you want any
scrollIntoViewOptions: {
behaviour: "smooth",
}, // We use `scrollIntoView()` when possible, pass here the options for it if you want any. Alternatively, you can also disable this functionallity by setting scrollIntoViewOptions to "no-scroll"
onBeforeHighlighted: (HighlightElement) => {}, // Called when element is about to be highlighted
onHighlighted: (HighlightElement) => {}, // Called when element is fully highlighted
onDeselected: (HighlightElement) => {}, // Called when element has been deselected
Expand Down
2 changes: 1 addition & 1 deletion src/lib/boarding-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface BoardingSharedOptions {
* Options to be passed to scrollIntoView if supported by browser
* @default { behavior: 'instant', block: 'center' }
*/
scrollIntoViewOptions: ScrollIntoViewOptions;
scrollIntoViewOptions: ScrollIntoViewOptions | "no-scroll";
/**
* Distance of elements corner from the edges of the overlay
* @default 10
Expand Down
10 changes: 8 additions & 2 deletions src/lib/boarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,10 @@ class Boarding {
padding: this.options.padding,
offset: this.options.offset,
animate: this.options.animate,
scrollIntoViewOptions: this.options.scrollIntoViewOptions,
scrollIntoViewOptions:
currentStep.scrollIntoViewOptions === undefined
? this.options.scrollIntoViewOptions
: currentStep.scrollIntoViewOptions,
// popover options
title: currentStep.popover.title,
description: currentStep.popover.description,
Expand Down Expand Up @@ -693,7 +696,10 @@ class Boarding {
return new HighlightElement({
highlightDomElement: domElement,
options: {
scrollIntoViewOptions: this.options.scrollIntoViewOptions,
scrollIntoViewOptions:
currentStep.scrollIntoViewOptions === undefined
? this.options.scrollIntoViewOptions
: currentStep.scrollIntoViewOptions,
onBeforeHighlighted:
currentStep.onBeforeHighlighted || this.options.onBeforeHighlighted,
onHighlighted: currentStep.onHighlighted || this.options.onHighlighted,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/cutout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function createSvgCutout({
svg.setAttribute("viewBox", `0 0 ${windowX} ${windowY}`);
svg.setAttribute("xmlSpace", "preserve");
svg.setAttribute("xmlnsXlink", "http://www.w3.org/1999/xlink");
svg.setAttribute("version", "version");
svg.setAttribute("version", "1.1");
svg.style.fillRule = "evenodd";
svg.style.clipRule = "evenodd";
svg.style.strokeLinejoin = "round";
Expand Down
19 changes: 8 additions & 11 deletions src/lib/core/highlight-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import { CLASS_ACTIVE_HIGHLIGHTED_ELEMENT } from "../common/constants";
import { bringInView } from "../common/utils";
import Popover from "./popover";

/** The top-level options that are shared between multiple classes that popover supports */
type HighlightElementSupportedSharedOptions = Pick<
BoardingSharedOptions,
"scrollIntoViewOptions"
>;

/** The options of popover that will come from the top-level but can also be overwritten */
export interface HighlightElementHybridOptions
extends Partial<
Pick<BoardingSharedOptions, "padding" | "radius" | "strictClickHandling">
Pick<
BoardingSharedOptions,
"padding" | "radius" | "strictClickHandling" | "scrollIntoViewOptions"
>
> {
/**
* Callback to be called when element is about to be highlighted
Expand All @@ -35,9 +32,7 @@ export interface HighlightElementHybridOptions
*/
onPrevious?: (element: HighlightElement) => void;
}
interface HighlightElementOptions
extends HighlightElementHybridOptions,
HighlightElementSupportedSharedOptions {}
interface HighlightElementOptions extends HighlightElementHybridOptions {}

/**
* Wrapper around DOMElements to enrich them
Expand Down Expand Up @@ -118,7 +113,9 @@ class HighlightElement {
* Is called when the element has been successfully highlighted
*/
public onHighlighted() {
bringInView(this.highlightDomElement, this.options.scrollIntoViewOptions);
if (this.options.scrollIntoViewOptions !== "no-scroll") {
bringInView(this.highlightDomElement, this.options.scrollIntoViewOptions);
}

// Show the popover once the item has been
// brought in the view, this would allow us to handle
Expand Down
11 changes: 7 additions & 4 deletions src/lib/core/popover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ export default class Popover {
this.renderFooter();

this.setPosition();
bringInView(
this.popover.popoverWrapper,
this.options.scrollIntoViewOptions
);

if (this.options.scrollIntoViewOptions !== "no-scroll") {
bringInView(
this.popover.popoverWrapper,
this.options.scrollIntoViewOptions
);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ document.addEventListener("DOMContentLoaded", function () {
description:
"Have a look at the usage examples and see how you can use it.",
},
scrollIntoViewOptions: "no-scroll",
},
{
element: "#boarding-demo-head",
Expand All @@ -121,6 +122,7 @@ document.addEventListener("DOMContentLoaded", function () {
description:
"This was just a sneak peak, have a look at the API section and examples to learn more!",
},
scrollIntoViewOptions: "no-scroll",
},
];

Expand Down

0 comments on commit e78e2dc

Please sign in to comment.