Skip to content

Commit 3f53777

Browse files
wbobeirnemonshan
authored andcommitted
Adds modalOverlayOpeningElement option for specifying a separate element to draw overlay around
Added re-usable `parseElementOrSelector` function for new arg and `attachTo.element` Rebases with master
1 parent b296bb8 commit 3f53777

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

src/js/components/shepherd-modal.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { uuid } from '../utils/general.js';
2+
import { uuid, parseElementOrSelector } from '../utils/general.js';
33
import { makeOverlayPath } from '../utils/overlay-path.js';
44
55
export let element, openingProperties;
@@ -128,11 +128,13 @@
128128
*/
129129
function _styleForStep(step) {
130130
const {
131+
modalOverlayOpeningElement,
131132
modalOverlayOpeningPadding,
132133
modalOverlayOpeningRadius
133134
} = step.options;
134135
135-
const scrollParent = _getScrollParent(step.target);
136+
const target = parseElementOrSelector(modalOverlayOpeningElement) || step.target;
137+
const scrollParent = _getScrollParent(target);
136138
137139
// Setup recursive function to call requestAnimationFrame to update the modal opening position
138140
const rafLoop = () => {
@@ -141,7 +143,7 @@
141143
modalOverlayOpeningPadding,
142144
modalOverlayOpeningRadius,
143145
scrollParent,
144-
step.target
146+
target
145147
);
146148
rafId = requestAnimationFrame(rafLoop);
147149
};

src/js/step.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export class Step extends Evented {
8383
* @param {string} options.highlightClass An extra class to apply to the `attachTo` element when it is
8484
* highlighted (that is, when its step is active). You can then target that selector in your CSS.
8585
* @param {string} options.id The string to use as the `id` for the step.
86+
* @param {HTMLElement|string} options.modalOverlayOpeningElement An element selector string or a DOM element that the modal overlay
87+
* opening should target. Defaults to using the same element as `attachTo` if unspecified.
8688
* @param {number} options.modalOverlayOpeningPadding An amount of padding to add around the modal overlay opening
8789
* @param {number} options.modalOverlayOpeningRadius An amount of border radius to add around the modal overlay opening
8890
* @param {object} options.popperOptions Extra options to pass to Popper

src/js/utils/general.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,41 @@ export function parseAttachTo(step) {
3636
// guarantee that the element will exist in the future.
3737
try {
3838
returnOpts.element = document.querySelector(returnOpts.element);
39+
returnOpts.element = parseElementOrSelector(options.element);
3940
} catch (e) {
4041
// TODO
42+
return null;
4143
}
42-
if (!returnOpts.element) {
43-
console.error(
44-
`The element for this Shepherd step was not found ${options.element}`
45-
);
46-
}
44+
}
45+
if (!returnOpts.element) {
46+
console.error(
47+
`The element for this Shepherd step was not found ${options.element}`
48+
);
4749
}
4850

4951
return returnOpts;
5052
}
5153

54+
/**
55+
* Takes in an HTMLElement or string or nullish value, returns HTMLElement or null if not found
56+
* @param {HTMLElement|string|void} element DOM element or string selector (or any falsy value)
57+
* @returns {HTMLElement|null}
58+
*/
59+
export function parseElementOrSelector(element) {
60+
if (!element) {
61+
return null;
62+
}
63+
if (isString(element)) {
64+
try {
65+
return document.querySelector(element);
66+
} catch (e) {
67+
// TODO
68+
return null;
69+
}
70+
}
71+
return element;
72+
}
73+
5274
/**
5375
* Checks if the step should be centered or not. Does not trigger attachTo.element evaluation, making it a pure
5476
* alternative for the deprecated step.isCentered() method.

src/types/step.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ declare namespace Step {
164164
*/
165165
id?: string;
166166

167+
/**
168+
* An element selector string or a DOM element that the modal overlay
169+
* opening should target. Defaults to using the same element as `attachTo`
170+
* if unspecified.
171+
*/
172+
modalOverlayOpeningElement?: HTMLElement | string;
173+
167174
/**
168175
* An amount of padding to add around the modal overlay opening
169176
*/

0 commit comments

Comments
 (0)