Skip to content

Commit 7bb710c

Browse files
committed
Adds modalOverlayOpeningElement option for specifying a separate element to draw overlay around
Added re-usable `parseElementOrSelector` function for new arg and `attachTo.element`
1 parent 61454c6 commit 7bb710c

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

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
@@ -82,6 +82,8 @@ export class Step extends Evented {
8282
* @param {string} options.highlightClass An extra class to apply to the `attachTo` element when it is
8383
* highlighted (that is, when its step is active). You can then target that selector in your CSS.
8484
* @param {string} options.id The string to use as the `id` for the step.
85+
* @param {HTMLElement|string} options.modalOverlayOpeningElement An element selector string or a DOM element that the modal overlay
86+
* opening should target. Defaults to using the same element as `attachTo` if unspecified.
8587
* @param {number} options.modalOverlayOpeningPadding An amount of padding to add around the modal overlay opening
8688
* @param {number} options.modalOverlayOpeningRadius An amount of border radius to add around the modal overlay opening
8789
* @param {object} options.popperOptions Extra options to pass to Popper

src/js/utils/general.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,34 @@ export function parseAttachTo(step) {
2626
const options = step.options.attachTo || {};
2727
const returnOpts = Object.assign({}, options);
2828

29-
if (isString(options.element)) {
30-
// Can't override the element in user opts reference because we can't
31-
// guarantee that the element will exist in the future.
29+
returnOpts.element = parseElementOrSelector(options.element);
30+
if (!returnOpts.element) {
31+
console.error(
32+
`The element for this Shepherd step was not found ${options.element}`
33+
);
34+
}
35+
36+
return returnOpts;
37+
}
38+
39+
/**
40+
* Takes in an HTMLElement or string or nullish value, returns HTMLElement or null if not found
41+
* @param {HTMLElement|string|void} element DOM element or string selector (or any falsy value)
42+
* @returns {HTMLElement|null}
43+
*/
44+
export function parseElementOrSelector(element) {
45+
if (!element) {
46+
return null;
47+
}
48+
if (isString(element)) {
3249
try {
33-
returnOpts.element = document.querySelector(options.element);
50+
return document.querySelector(element);
3451
} catch (e) {
3552
// TODO
36-
}
37-
if (!returnOpts.element) {
38-
console.error(
39-
`The element for this Shepherd step was not found ${options.element}`
40-
);
53+
return null;
4154
}
4255
}
43-
44-
return returnOpts;
56+
return element;
4557
}
4658

4759
/**

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)