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 arrow padding to be configured for a step. #3051

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
12 changes: 10 additions & 2 deletions shepherd.js/src/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export interface StepOptions {
advanceOn?: StepOptionsAdvanceOn;

/**
* Whether to display the arrow for the tooltip or not
* Whether to display the arrow for the tooltip or not, or options for the arrow.
*/
arrow?: boolean;
arrow?: boolean | StepOptionsArrow;

/**
* A function that returns a promise.
Expand Down Expand Up @@ -221,6 +221,14 @@ export type PopperPlacement =
| 'left-start'
| 'left-end';

export interface StepOptionsArrow {
/*
* The padding from the edge for the arrow.
* Not used if this is not a -start or -end placement.
*/
padding?: number
}

export interface StepOptionsAttachTo {
element?:
| HTMLElement
Expand Down
7 changes: 6 additions & 1 deletion shepherd.js/src/utils/floating-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,13 @@ export function getFloatingUIOptions(
);

if (arrowEl) {
const arrowOptions =
typeof step.options.arrow === "object"
? step.options.arrow
: { padding: 4 };

Comment on lines +214 to +218
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const arrowOptions =
typeof step.options.arrow === "object"
? step.options.arrow
: { padding: 4 };

options.middleware.push(
arrow({ element: arrowEl, padding: hasEdgeAlignment ? 4 : 0 })
arrow({ element: arrowEl, padding: hasEdgeAlignment ? arrowOptions.padding : 0 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
arrow({ element: arrowEl, padding: hasEdgeAlignment ? arrowOptions.padding : 0 })
arrow({ element: arrowEl, padding: hasEdgeAlignment ? step.options.arrow?.padding ?? 4 : 0 })

What would you think about putting this inline here? I think it is a little less verbose.

);
}

Expand Down
38 changes: 38 additions & 0 deletions test/unit/components/shepherd-element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,44 @@ describe('components/ShepherdElement', () => {
container.querySelectorAll('.shepherd-element .shepherd-arrow').length
).toBe(0);
});

it('arrow: object with padding shows arrow', async () => {
const testElement = document.createElement('div');
const tour = new Tour();
const step = new Step(tour, {
arrow: { padding: 10 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests! Could we perhaps add an assertion checking that padding of 10px is applied to the arrow element?

attachTo: { element: testElement, on: 'top' }
});

const { container } = render(ShepherdElement, {
props: {
step
}
});

expect(
container.querySelectorAll('.shepherd-element .shepherd-arrow').length
).toBe(1);
});

it('arrow: empty object shows arrow', async () => {
const testElement = document.createElement('div');
const tour = new Tour();
const step = new Step(tour, {
arrow: {},
attachTo: { element: testElement, on: 'top' }
});

const { container } = render(ShepherdElement, {
props: {
step
}
});

expect(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one, perhaps we could also assert padding, and I think it would be 0px here?

container.querySelectorAll('.shepherd-element .shepherd-arrow').length
).toBe(1);
});
});

describe('handleKeyDown', () => {
Expand Down