Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Anchor menus inside size containers to their button (#4370)

jaysbeekaya580ab3d91d0
diff --git a/config/importmap.rb b/config/importmap.rb
index b31c191ea7..8392c63d2e 100644
--- a/config/importmap.rb
+++ b/config/importmap.rb
@@ -50,8 +50,8 @@ pin "internmap" # @2.0.3
pin "robust-predicates" # @3.0.2
pin "@floating-ui/dom", to: "@floating-ui--dom.js" # @1.7.0
pin "@floating-ui/core", to: "@floating-ui--core.js" # @1.7.0
-pin "@floating-ui/utils", to: "@floating-ui--utils.js" # @0.2.9
-pin "@floating-ui/utils/dom", to: "@floating-ui--utils--dom.js" # @0.2.9
+pin "@floating-ui/utils", to: "@floating-ui--utils.js" # @0.2.12
+pin "@floating-ui/utils/dom", to: "@floating-ui--utils--dom.js" # @0.2.12
pin "d3-sankey" # @0.12.3
pin "d3-array-src", to: "d3-array.js"
pin "d3-shape-src", to: "d3-shape.js"
diff --git a/test/system/bill_menu_position_test.rb b/test/system/bill_menu_position_test.rb
new file mode 100644
index 0000000000..3fc2950c0d
--- /dev/null
+++ b/test/system/bill_menu_position_test.rb
@@ -0,0 +1,58 @@
+require "application_system_test_case"
+
+# A bill's "..." menu opens against its button. The All bills list sits in a
+# size container, and floating-ui 0.2.9 took any `container-type` ancestor for
+# the containing block of a `position: fixed` menu, so it subtracted the
+# container's offset and the menu opened a container's width away, mid-page.
+class BillMenuPositionTest < ApplicationSystemTestCase
+ setup do
+ sign_in @user = users(:family_admin)
+ @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
+
+ due = Date.current + 10
+ @user.family.recurring_transactions.create!(
+ name: "City Water", account: accounts(:depository), amount: 80, currency: "USD",
+ expected_day_of_month: due.day, last_occurrence_date: due - 1.month,
+ next_expected_date: due, status: "active"
+ )
+ end
+
+ test "on a desktop, the All bills menu opens against its button, right edges aligned" do
+ button, menu = open_first_menu
+
+ # Not near the button: on it. The defect put the right edge 445px left of
+ # the button's, so a loose tolerance would still prove the fix.
+ assert_in_delta button["right"], menu["right"], 1, "menu is not aligned to its button"
+ assert_adjacent button, menu
+ end
+
+ test "on a phone, the All bills menu opens directly against its button" do
+ page.current_window.resize_to(375, 812)
+ button, menu = open_first_menu
+
+ # The full-width branch sets left and width itself, so only the top comes
+ # from floating-ui; the defect moved the top 270px above the button.
+ assert_adjacent button, menu
+ ensure
+ reset_viewport
+ end
+
+ private
+ def open_first_menu
+ visit bills_url(view: "all")
+ # A row's menu, not the page header's, which sits outside the container.
+ button = find(%q(.\@container [data-DS--menu-target="button"]), match: :first)
+ button.click
+ menu = find("##{button["aria-controls"]}", visible: true)
+
+ rect = ->(node) { node.evaluate_script("(r => ({ left: r.left, top: r.top, right: r.right, bottom: r.bottom }))(this.getBoundingClientRect())") }
+ [ rect.(button), rect.(menu) ]
+ end
+
+ # flip() may put the menu above the button when there is no room below,
+ # so accept either side, but only a gap the size of the offset.
+ def assert_adjacent(button, menu)
+ gap = menu["top"] >= button["bottom"] ? menu["top"] - button["bottom"] : button["top"] - menu["bottom"]
+ assert gap.between?(0, 20), "menu is #{gap}px from its button, expected it beside it (button #{button}, menu #{menu})"
+ end
+end
diff --git a/vendor/javascript/@floating-ui--utils--dom.js b/vendor/javascript/@floating-ui--utils--dom.js
index 82db906e0a..57494bef93 100644
--- a/vendor/javascript/@floating-ui--utils--dom.js
+++ b/vendor/javascript/@floating-ui--utils--dom.js
@@ -1,4 +1,167 @@
-// @floating-ui/utils/[email protected] downloaded from https://ga.jspm.io/npm:@floating-ui/[email protected]/dist/floating-ui.utils.dom.mjs
+// @floating-ui/utils/[email protected] downloaded from https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.12.tgz (dist/floating-ui.utils.dom.mjs)
-function hasWindow(){return typeof window!=="undefined"}function getNodeName(e){return isNode(e)?(e.nodeName||"").toLowerCase():"#document"}function getWindow(e){var t;return(e==null||(t=e.ownerDocument)==null?void 0:t.defaultView)||window}function getDocumentElement(e){var t;return(t=(isNode(e)?e.ownerDocument:e.document)||window.document)==null?void 0:t.documentElement}function isNode(e){return!!hasWindow()&&(e instanceof Node||e instanceof getWindow(e).Node)}function isElement(e){return!!hasWindow()&&(e instanceof Element||e instanceof getWindow(e).Element)}function isHTMLElement(e){return!!hasWindow()&&(e instanceof HTMLElement||e instanceof getWindow(e).HTMLElement)}function isShadowRoot(e){return!(!hasWindow()||typeof ShadowRoot==="undefined")&&(e instanceof ShadowRoot||e instanceof getWindow(e).ShadowRoot)}function isOverflowElement(e){const{overflow:t,overflowX:n,overflowY:o,display:r}=getComputedStyle(e);return/auto|scroll|overlay|hidden|clip/.test(t+o+n)&&!["inline","contents"].includes(r)}function isTableElement(e){return["table","td","th"].includes(getNodeName(e))}function isTopLayer(e){return[":popover-open",":modal"].some((t=>{try{return e.matches(t)}catch(e){return false}}))}function isContainingBlock(e){const t=isWebKit();const n=isElement(e)?getComputedStyle(e):e;return["transform","translate","scale","rotate","perspective"].some((e=>!!n[e]&&n[e]!=="none"))||!!n.containerType&&n.containerType!=="normal"||!t&&!!n.backdropFilter&&n.backdropFilter!=="none"||!t&&!!n.filter&&n.filter!=="none"||["transform","translate","scale","rotate","perspective","filter"].some((e=>(n.willChange||"").includes(e)))||["paint","layout","strict","content"].some((e=>(n.contain||"").includes(e)))}function getContainingBlock(e){let t=getParentNode(e);while(isHTMLElement(t)&&!isLastTraversableNode(t)){if(isContainingBlock(t))return t;if(isTopLayer(t))return null;t=getParentNode(t)}return null}function isWebKit(){return!(typeof CSS==="undefined"||!CSS.supports)&&CSS.supports("-webkit-backdrop-filter","none")}function isLastTraversableNode(e){return["html","body","#document"].includes(getNodeName(e))}function getComputedStyle(e){return getWindow(e).getComputedStyle(e)}function getNodeScroll(e){return isElement(e)?{scrollLeft:e.scrollLeft,scrollTop:e.scrollTop}:{scrollLeft:e.scrollX,scrollTop:e.scrollY}}function getParentNode(e){if(getNodeName(e)==="html")return e;const t=e.assignedSlot||e.parentNode||isShadowRoot(e)&&e.host||getDocumentElement(e);return isShadowRoot(t)?t.host:t}function getNearestOverflowAncestor(e){const t=getParentNode(e);return isLastTraversableNode(t)?e.ownerDocument?e.ownerDocument.body:e.body:isHTMLElement(t)&&isOverflowElement(t)?t:getNearestOverflowAncestor(t)}function getOverflowAncestors(e,t,n){var o;t===void 0&&(t=[]);n===void 0&&(n=true);const r=getNearestOverflowAncestor(e);const i=r===((o=e.ownerDocument)==null?void 0:o.body);const l=getWindow(r);if(i){const e=getFrameElement(l);return t.concat(l,l.visualViewport||[],isOverflowElement(r)?r:[],e&&n?getOverflowAncestors(e):[])}return t.concat(r,getOverflowAncestors(r,[],n))}function getFrameElement(e){return e.parent&&Object.getPrototypeOf(e.parent)?e.frameElement:null}export{getComputedStyle,getContainingBlock,getDocumentElement,getFrameElement,getNearestOverflowAncestor,getNodeName,getNodeScroll,getOverflowAncestors,getParentNode,getWindow,isContainingBlock,isElement,isHTMLElement,isLastTraversableNode,isNode,isOverflowElement,isShadowRoot,isTableElement,isTopLayer,isWebKit};
+function hasWindow() {
+ return typeof window !== 'undefined';
+}
+function getNodeName(node) {
+ if (isNode(node)) {
+ return (node.nodeName || '').toLowerCase();
+ }
+ // Mocked nodes in testing environments may not be instances of Node. By
+ // returning `#document` an infinite loop won't occur.
+ // https://github.com/floating-ui/floating-ui/issues/2317
+ return '#document';
+}
+function getWindow(node) {
+ var _node$ownerDocument;
+ return (node == null || (_node$ownerDocument = node.ownerDocument) == null ? void 0 : _node$ownerDocument.defaultView) || window;
+}
+function getDocumentElement(node) {
+ var _ref;
+ return (_ref = (isNode(node) ? node.ownerDocument : node.document) || window.document) == null ? void 0 : _ref.documentElement;
+}
+function isNode(value) {
+ if (!hasWindow()) {
+ return false;
+ }
+ return value instanceof Node || value instanceof getWindow(value).Node;
+}
+function isElement(value) {
+ if (!hasWindow()) {
+ return false;
+ }
+ return value instanceof Element || value instanceof getWindow(value).Element;
+}
+function isHTMLElement(value) {
+ if (!hasWindow()) {
+ return false;
+ }
+ return value instanceof HTMLElement || value instanceof getWindow(value).HTMLElement;
+}
+function isShadowRoot(value) {
+ if (!hasWindow() || typeof ShadowRoot === 'undefined') {
+ return false;
+ }
+ return value instanceof ShadowRoot || value instanceof getWindow(value).ShadowRoot;
+}
+function isOverflowElement(element) {
+ const {
+ overflow,
+ overflowX,
+ overflowY,
+ display
+ } = getComputedStyle(element);
+ return /auto|scroll|overlay|hidden|clip/.test(overflow + overflowY + overflowX) && display !== 'inline' && display !== 'contents';
+}
+function isTableElement(element) {
+ return /^(table|td|th)$/.test(getNodeName(element));
+}
+function isTopLayer(element) {
+ try {
+ if (element.matches(':popover-open')) {
+ return true;
+ }
+ } catch (_e) {
+ // no-op
+ }
+ try {
+ return element.matches(':modal');
+ } catch (_e) {
+ return false;
+ }
+}
+const willChangeRe = /transform|translate|scale|rotate|perspective|filter/;
+const containRe = /paint|layout|strict|content/;
+const isNotNone = value => !!value && value !== 'none';
+let isWebKitValue;
+function isContainingBlock(elementOrCss) {
+ const css = isElement(elementOrCss) ? getComputedStyle(elementOrCss) : elementOrCss;
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block
+ // https://drafts.csswg.org/css-transforms-2/#individual-transforms
+ return isNotNone(css.transform) || isNotNone(css.translate) || isNotNone(css.scale) || isNotNone(css.rotate) || isNotNone(css.perspective) || !isWebKit() && (isNotNone(css.backdropFilter) || isNotNone(css.filter)) || willChangeRe.test(css.willChange || '') || containRe.test(css.contain || '');
+}
+function getContainingBlock(element) {
+ let currentNode = getParentNode(element);
+ while (isHTMLElement(currentNode) && !isLastTraversableNode(currentNode)) {
+ if (isContainingBlock(currentNode)) {
+ return currentNode;
+ } else if (isTopLayer(currentNode)) {
+ return null;
+ }
+ currentNode = getParentNode(currentNode);
+ }
+ return null;
+}
+function isWebKit() {
+ if (isWebKitValue == null) {
+ isWebKitValue = typeof CSS !== 'undefined' && CSS.supports && CSS.supports('-webkit-backdrop-filter', 'none');
+ }
+ return isWebKitValue;
+}
+function isLastTraversableNode(node) {
+ return /^(html|body|#document)$/.test(getNodeName(node));
+}
+function getComputedStyle(element) {
+ return getWindow(element).getComputedStyle(element);
+}
+function getNodeScroll(element) {
+ if (isElement(element)) {
+ return {
+ scrollLeft: element.scrollLeft,
+ scrollTop: element.scrollTop
+ };
+ }
+ return {
+ scrollLeft: element.scrollX,
+ scrollTop: element.scrollY
+ };
+}
+function getParentNode(node) {
+ if (getNodeName(node) === 'html') {
+ return node;
+ }
+ const result =
+ // Step into the shadow DOM of the parent of a slotted node.
+ node.assignedSlot ||
+ // DOM Element detected.
+ node.parentNode ||
+ // ShadowRoot detected.
+ isShadowRoot(node) && node.host ||
+ // Fallback.
+ getDocumentElement(node);
+ return isShadowRoot(result) ? result.host : result;
+}
+function getNearestOverflowAncestor(node) {
+ const parentNode = getParentNode(node);
+ if (isLastTraversableNode(parentNode)) {
+ return (node.ownerDocument || node).body;
+ }
+ if (isHTMLElement(parentNode) && isOverflowElement(parentNode)) {
+ return parentNode;
+ }
+ return getNearestOverflowAncestor(parentNode);
+}
+function getOverflowAncestors(node, list, traverseIframes) {
+ var _node$ownerDocument2;
+ if (list === void 0) {
+ list = [];
+ }
+ if (traverseIframes === void 0) {
+ traverseIframes = true;
+ }
+ const scrollableAncestor = getNearestOverflowAncestor(node);
+ const isBody = scrollableAncestor === ((_node$ownerDocument2 = node.ownerDocument) == null ? void 0 : _node$ownerDocument2.body);
+ const win = getWindow(scrollableAncestor);
+ if (isBody) {
+ const frameElement = getFrameElement(win);
+ return list.concat(win, win.visualViewport || [], isOverflowElement(scrollableAncestor) ? scrollableAncestor : [], frameElement && traverseIframes ? getOverflowAncestors(frameElement) : []);
+ } else {
+ return list.concat(scrollableAncestor, getOverflowAncestors(scrollableAncestor, [], traverseIframes));
+ }
+}
+function getFrameElement(win) {
+ return win.parent && Object.getPrototypeOf(win.parent) ? win.frameElement : null;
+}
+
+export { getComputedStyle, getContainingBlock, getDocumentElement, getFrameElement, getNearestOverflowAncestor, getNodeName, getNodeScroll, getOverflowAncestors, getParentNode, getWindow, isContainingBlock, isElement, isHTMLElement, isLastTraversableNode, isNode, isOverflowElement, isShadowRoot, isTableElement, isTopLayer, isWebKit };
diff --git a/vendor/javascript/@floating-ui--utils.js b/vendor/javascript/@floating-ui--utils.js
index b457af7b94..4eaa0bc1a0 100644
--- a/vendor/javascript/@floating-ui--utils.js
+++ b/vendor/javascript/@floating-ui--utils.js
@@ -1,2 +1,138 @@
-const t=["top","right","bottom","left"];const e=["start","end"];const n=t.reduce(((t,n)=>t.concat(n,n+"-"+e[0],n+"-"+e[1])),[]);const i=Math.min;const o=Math.max;const g=Math.round;const c=Math.floor;const createCoords=t=>({x:t,y:t});const s={left:"right",right:"left",bottom:"top",top:"bottom"};const r={start:"end",end:"start"};function clamp(t,e,n){return o(t,i(e,n))}function evaluate(t,e){return typeof t==="function"?t(e):t}function getSide(t){return t.split("-")[0]}function getAlignment(t){return t.split("-")[1]}function getOppositeAxis(t){return t==="x"?"y":"x"}function getAxisLength(t){return t==="y"?"height":"width"}function getSideAxis(t){return["top","bottom"].includes(getSide(t))?"y":"x"}function getAlignmentAxis(t){return getOppositeAxis(getSideAxis(t))}function getAlignmentSides(t,e,n){n===void 0&&(n=false);const i=getAlignment(t);const o=getAlignmentAxis(t);const g=getAxisLength(o);let c=o==="x"?i===(n?"end":"start")?"right":"left":i==="start"?"bottom":"top";e.reference[g]>e.floating[g]&&(c=getOppositePlacement(c));return[c,getOppositePlacement(c)]}function getExpandedPlacements(t){const e=getOppositePlacement(t);return[getOppositeAlignmentPlacement(t),e,getOppositeAlignmentPlacement(e)]}function getOppositeAlignmentPlacement(t){return t.replace(/start|end/g,(t=>r[t]))}function getSideList(t,e,n){const i=["left","right"];const o=["right","left"];const g=["top","bottom"];const c=["bottom","top"];switch(t){case"top":case"bottom":return n?e?o:i:e?i:o;case"left":case"right":return e?g:c;default:return[]}}function getOppositeAxisPlacements(t,e,n,i){const o=getAlignment(t);let g=getSideList(getSide(t),n==="start",i);if(o){g=g.map((t=>t+"-"+o));e&&(g=g.concat(g.map(getOppositeAlignmentPlacement)))}return g}function getOppositePlacement(t){return t.replace(/left|right|bottom|top/g,(t=>s[t]))}function expandPaddingObject(t){return{top:0,right:0,bottom:0,left:0,...t}}function getPaddingObject(t){return typeof t!=="number"?expandPaddingObject(t):{top:t,right:t,bottom:t,left:t}}function rectToClientRect(t){const{x:e,y:n,width:i,height:o}=t;return{width:i,height:o,top:n,left:e,right:e+i,bottom:n+o,x:e,y:n}}export{e as alignments,clamp,createCoords,evaluate,expandPaddingObject,c as floor,getAlignment,getAlignmentAxis,getAlignmentSides,getAxisLength,getExpandedPlacements,getOppositeAlignmentPlacement,getOppositeAxis,getOppositeAxisPlacements,getOppositePlacement,getPaddingObject,getSide,getSideAxis,o as max,i as min,n as placements,rectToClientRect,g as round,t as sides};
+// @floating-ui/[email protected] downloaded from https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.12.tgz (dist/floating-ui.utils.mjs)
+/**
+ * Custom positioning reference element.
+ * @see https://floating-ui.com/docs/virtual-elements
+ */
+
+const sides = ['top', 'right', 'bottom', 'left'];
+const alignments = ['start', 'end'];
+const placements = /*#__PURE__*/sides.reduce((acc, side) => acc.concat(side, side + "-" + alignments[0], side + "-" + alignments[1]), []);
+const min = Math.min;
+const max = Math.max;
+const round = Math.round;
+const floor = Math.floor;
+const createCoords = v => ({
+ x: v,
+ y: v
+});
+const oppositeSideMap = {
+ left: 'right',
+ right: 'left',
+ bottom: 'top',
+ top: 'bottom'
+};
+function clamp(start, value, end) {
+ return max(start, min(value, end));
+}
+function evaluate(value, param) {
+ return typeof value === 'function' ? value(param) : value;
+}
+function getSide(placement) {
+ return placement.split('-')[0];
+}
+function getAlignment(placement) {
+ return placement.split('-')[1];
+}
+function getOppositeAxis(axis) {
+ return axis === 'x' ? 'y' : 'x';
+}
+function getAxisLength(axis) {
+ return axis === 'y' ? 'height' : 'width';
+}
+function getSideAxis(placement) {
+ const firstChar = placement[0];
+ return firstChar === 't' || firstChar === 'b' ? 'y' : 'x';
+}
+function getAlignmentAxis(placement) {
+ return getOppositeAxis(getSideAxis(placement));
+}
+function getAlignmentSides(placement, rects, rtl) {
+ if (rtl === void 0) {
+ rtl = false;
+ }
+ const alignment = getAlignment(placement);
+ const alignmentAxis = getAlignmentAxis(placement);
+ const length = getAxisLength(alignmentAxis);
+ let mainAlignmentSide = alignmentAxis === 'x' ? alignment === (rtl ? 'end' : 'start') ? 'right' : 'left' : alignment === 'start' ? 'bottom' : 'top';
+ if (rects.reference[length] > rects.floating[length]) {
+ mainAlignmentSide = getOppositePlacement(mainAlignmentSide);
+ }
+ return [mainAlignmentSide, getOppositePlacement(mainAlignmentSide)];
+}
+function getExpandedPlacements(placement) {
+ const oppositePlacement = getOppositePlacement(placement);
+ return [getOppositeAlignmentPlacement(placement), oppositePlacement, getOppositeAlignmentPlacement(oppositePlacement)];
+}
+function getOppositeAlignmentPlacement(placement) {
+ return placement.includes('start') ? placement.replace('start', 'end') : placement.replace('end', 'start');
+}
+const lrPlacement = ['left', 'right'];
+const rlPlacement = ['right', 'left'];
+const tbPlacement = ['top', 'bottom'];
+const btPlacement = ['bottom', 'top'];
+function getSideList(side, isStart, rtl) {
+ switch (side) {
+ case 'top':
+ case 'bottom':
+ if (rtl) return isStart ? rlPlacement : lrPlacement;
+ return isStart ? lrPlacement : rlPlacement;
+ case 'left':
+ case 'right':
+ return isStart ? tbPlacement : btPlacement;
+ default:
+ return [];
+ }
+}
+function getOppositeAxisPlacements(placement, flipAlignment, direction, rtl) {
+ const alignment = getAlignment(placement);
+ let list = getSideList(getSide(placement), direction === 'start', rtl);
+ if (alignment) {
+ list = list.map(side => side + "-" + alignment);
+ if (flipAlignment) {
+ list = list.concat(list.map(getOppositeAlignmentPlacement));
+ }
+ }
+ return list;
+}
+function getOppositePlacement(placement) {
+ const side = getSide(placement);
+ return oppositeSideMap[side] + placement.slice(side.length);
+}
+function expandPaddingObject(padding) {
+ var _padding$top, _padding$right, _padding$bottom, _padding$left;
+ return {
+ top: (_padding$top = padding.top) != null ? _padding$top : 0,
+ right: (_padding$right = padding.right) != null ? _padding$right : 0,
+ bottom: (_padding$bottom = padding.bottom) != null ? _padding$bottom : 0,
+ left: (_padding$left = padding.left) != null ? _padding$left : 0
+ };
+}
+function getPaddingObject(padding) {
+ return typeof padding !== 'number' ? expandPaddingObject(padding) : {
+ top: padding,
+ right: padding,
+ bottom: padding,
+ left: padding
+ };
+}
+function rectToClientRect(rect) {
+ const {
+ x,
+ y,
+ width,
+ height
+ } = rect;
+ return {
+ width,
+ height,
+ top: y,
+ left: x,
+ right: x + width,
+ bottom: y + height,
+ x,
+ y
+ };
+}
+
+export { alignments, clamp, createCoords, evaluate, expandPaddingObject, floor, getAlignment, getAlignmentAxis, getAlignmentSides, getAxisLength, getExpandedPlacements, getOppositeAlignmentPlacement, getOppositeAxis, getOppositeAxisPlacements, getOppositePlacement, getPaddingObject, getSide, getSideAxis, max, min, placements, rectToClientRect, round, sides };