Skip to content
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

Fix centering steps with no attachTo.on #2183

Merged
merged 1 commit into from
Dec 12, 2022
Merged
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
52 changes: 34 additions & 18 deletions src/js/utils/floating-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export function setupTooltip(step) {

let target = attachToOptions.element;
const floatingUIOptions = getFloatingUIOptions(attachToOptions, step);
const shouldCenter = shouldCenterStep(attachToOptions);

if (shouldCenterStep(attachToOptions)) {
if (shouldCenter) {
target = document.body;
const content = step.shepherdElementComponent.getElement();
content.classList.add('shepherd-centered');
Expand All @@ -45,7 +46,7 @@ export function setupTooltip(step) {
return;
}

setPosition(target, step, floatingUIOptions);
setPosition(target, step, floatingUIOptions, shouldCenter);
});

step.target = attachToOptions.element;
Expand Down Expand Up @@ -87,10 +88,10 @@ export function destroyTooltip(step) {
*
* @return {Promise<*>}
*/
function setPosition(target, step, floatingUIOptions) {
function setPosition(target, step, floatingUIOptions, shouldCenter) {
return (
computePosition(target, step.el, floatingUIOptions)
.then(floatingUIposition(step))
.then(floatingUIposition(step, shouldCenter))
// Wait before forcing focus.
.then(
(step) =>
Expand All @@ -110,19 +111,29 @@ function setPosition(target, step, floatingUIOptions) {
/**
*
* @param step
* @param shouldCenter
* @return {function({x: *, y: *, placement: *, middlewareData: *}): Promise<unknown>}
*/
function floatingUIposition(step) {
function floatingUIposition(step, shouldCenter) {
return ({ x, y, placement, middlewareData }) => {
if (!step.el) {
return step;
}

Object.assign(step.el.style, {
position: 'absolute',
left: `${x}px`,
top: `${y}px`
});
if (shouldCenter) {
Object.assign(step.el.style, {
position: 'fixed',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)'
});
} else {
Object.assign(step.el.style, {
position: 'absolute',
left: `${x}px`,
top: `${y}px`
});
}

step.el.dataset.popperPlacement = placement;

Expand Down Expand Up @@ -167,22 +178,27 @@ function placeArrow(el, middlewareData) {
export function getFloatingUIOptions(attachToOptions, step) {
const options = {
strategy: 'absolute',
middleware: [
middleware: []
};

const arrowEl = addArrow(step);

const shouldCenter = shouldCenterStep(attachToOptions);

if (!shouldCenter) {
options.middleware.push(
flip(),
// Replicate PopperJS default behavior.
shift({
limiter: limitShift(),
crossAxis: true
})
]
};
);

const arrowEl = addArrow(step);
if (arrowEl) {
options.middleware.push(arrow({ element: arrowEl }));
}
if (arrowEl) {
options.middleware.push(arrow({ element: arrowEl }));
}

if (!shouldCenterStep(attachToOptions)) {
options.placement = attachToOptions.on;
}

Expand Down
7 changes: 3 additions & 4 deletions test/unit/tour.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ describe('Tour | Top-Level Class', function() {
instance.start();

const floatingUIOptions = setupTooltip(step);
expect(floatingUIOptions.middleware.length).toBe(3);
expect(floatingUIOptions.middleware.length).toBe(1);
});

it('adds a step modifer to default modifiers', function() {
Expand All @@ -458,7 +458,7 @@ describe('Tour | Top-Level Class', function() {
instance.start();

const floatingUIOptions = setupTooltip(step);
expect(floatingUIOptions.middleware.length).toBe(4);
expect(floatingUIOptions.middleware.length).toBe(2);
});

it('correctly changes modifiers when going from centered to attached', function() {
Expand Down Expand Up @@ -488,10 +488,9 @@ describe('Tour | Top-Level Class', function() {

const centeredOptions = setupTooltip(centeredStep);
const centeredMiddlewareNames = centeredOptions.middleware.map(({name}) => name);
expect(centeredOptions.middleware.length).toBe(4);
expect(centeredOptions.middleware.length).toBe(2);
expect(centeredMiddlewareNames.includes('offset')).toBe(true);
expect(centeredMiddlewareNames.includes('foo')).toBe(true);
expect(centeredMiddlewareNames.includes('shift')).toBe(true);
expect(centeredMiddlewareNames.includes('arrow')).toBe(false);

instance.next();
Expand Down