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

Allow disabling default middlewares using options.disableDefaultMiddlewares #3010

Open
wants to merge 1 commit into
base: main
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
7 changes: 7 additions & 0 deletions shepherd.js/src/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ export interface StepOptions {
*/
floatingUIOptions?: object;

/**
* Shepherd adds default middlewares (flip, shift)
* which get merged with step options defined in `floatingUIOptions.middleware`.
* You can disable this behavior by passing true
*/
disableDefaultMiddlewares?: boolean;

/**
* Should the element be scrolled to when this step is shown?
*/
Expand Down
21 changes: 12 additions & 9 deletions shepherd.js/src/utils/floating-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
shift,
type ComputePositionConfig,
type MiddlewareData,
type Placement
type Placement,
autoPlacement
} from '@floating-ui/dom';
import type { Step, StepOptions, StepOptionsAttachTo } from '../step.ts';
import { isHTMLElement } from './type-check.ts';
Expand Down Expand Up @@ -181,14 +182,16 @@ export function getFloatingUIOptions(
const shouldCenter = shouldCenterStep(attachToOptions);

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

if (arrowEl) {
const hasEdgeAlignment =
Expand Down
81 changes: 81 additions & 0 deletions test/unit/tour.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,85 @@ describe('Tour | Top-Level Class', function () {
expect(modalContainer.contains(modalElement)).toBe(true);
});
});

describe('disableDefaultMiddlewares', () => {
it('removes default middlewares', () => {
instance = new Shepherd.Tour();

const step = instance.addStep({
id: 'test',
title: 'This is a test step for our tour',
disableDefaultMiddlewares: true
});

instance.start();

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

it('adds middlewares from defaultStepOptions', () => {
instance = new Shepherd.Tour({ defaultStepOptions });

const step = instance.addStep({
id: 'test',
title: 'This is a test step for our tour',
disableDefaultMiddlewares: true
});

instance.start();

const floatingUIOptions = setupTooltip(step);

const middlewareNames = floatingUIOptions.middleware.map(
({ name }) => name
);
const defaultMiddlewareNames =
defaultStepOptions.floatingUIOptions.middleware.map(({ name }) => name);

expect(
middlewareNames.every((middleware) =>
defaultMiddlewareNames.includes(middleware)
)
).toBe(true);
});

it('adds step middlewares', () => {
instance = new Shepherd.Tour();

const step1 = instance.addStep({
id: 'test',
title: 'This is a test step for our tour',
disableDefaultMiddlewares: true,
floatingUIOptions: {
middleware: [{ name: 'foo', options: 'bar', fn: (args) => args }]
}
});

const step2 = instance.addStep({
id: 'test',
title: 'This is a test step for our tour',
disableDefaultMiddlewares: true,
floatingUIOptions: {
middleware: [
{ name: 'foo', options: 'bar', fn: (args) => args },
{ name: 'bar', options: 'baz', fn: (args) => args }
]
}
});

instance.start();

const step1FloatingUIOptions = setupTooltip(step1);
expect(step1FloatingUIOptions.middleware.length).toBe(1);
expect(step1FloatingUIOptions.middleware[0].name).toBe('foo');

instance.next();

const step2FloatingUIOptions = setupTooltip(step2);
expect(step2FloatingUIOptions.middleware.length).toBe(2);
expect(step2FloatingUIOptions.middleware[0].name).toBe('foo');
expect(step2FloatingUIOptions.middleware[1].name).toBe('bar');
});
});
});