Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't work since the existing model allows arrow to be a boolean. (Which I kept to avoid a breaking change). We need to handle the case where step.options.arrow is a boolean which is why I wrote this way I did.

);
}

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 },
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(
container.querySelectorAll('.shepherd-element .shepherd-arrow').length
).toBe(1);
});
});

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