Skip to content

Commit

Permalink
Update floating-ui.js - Fix floatingUI arrow() padding
Browse files Browse the repository at this point in the history
Related to shipshapecode#2302
Arrow function passed through a step's middleware will have a null element, in which case the padding will not be applied. Sum up all such occurrences and pass the final padding to the arrow function with the newly added arrow element.
  • Loading branch information
lilyMrt authored Apr 4, 2023
1 parent 2d9a0c6 commit 058868e
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/js/utils/floating-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ export function getFloatingUIOptions(attachToOptions, step) {
);

if (arrowEl) {
options.middleware.push(arrow({ element: arrowEl }));
const arrowPadding = getPaddingSum(step.options.floatingUIOptions.middleware || []);

options.middleware.push(arrow({ element: arrowEl, padding: arrowPadding}));
}

options.placement = attachToOptions.on;
Expand All @@ -207,3 +209,26 @@ function addArrow(step) {

return false;
}

/**
*
* @param {Object[]} middleware
* @returns {number}
*/
function getPaddingSum(middleware) {
const arrowFunctions = middleware.filter(isUnusedArrowFunction);
let sum = 0;
arrowFunctions.forEach((func)=> {
sum += func.options.padding;
})
return sum;
}

/**
*
* @param {Object} el
* @returns {boolean}
*/
function isUnusedArrowFunction(el) {
return el.name === "arrow" && el.options.element === null;
}

0 comments on commit 058868e

Please sign in to comment.