@wordpress/components
Version:
UI components for WordPress.
191 lines (190 loc) • 5.53 kB
JavaScript
import { cubicBezier } from "framer-motion";
// packages/components/src/popover/utils.ts
const POSITION_TO_PLACEMENT = {
bottom: "bottom",
top: "top",
"middle left": "left",
"middle right": "right",
"bottom left": "bottom-end",
"bottom center": "bottom",
"bottom right": "bottom-start",
"top left": "top-end",
"top center": "top",
"top right": "top-start",
"middle left left": "left",
"middle left right": "left",
"middle left bottom": "left-end",
"middle left top": "left-start",
"middle right left": "right",
"middle right right": "right",
"middle right bottom": "right-end",
"middle right top": "right-start",
"bottom left left": "bottom-end",
"bottom left right": "bottom-end",
"bottom left bottom": "bottom-end",
"bottom left top": "bottom-end",
"bottom center left": "bottom",
"bottom center right": "bottom",
"bottom center bottom": "bottom",
"bottom center top": "bottom",
"bottom right left": "bottom-start",
"bottom right right": "bottom-start",
"bottom right bottom": "bottom-start",
"bottom right top": "bottom-start",
"top left left": "top-end",
"top left right": "top-end",
"top left bottom": "top-end",
"top left top": "top-end",
"top center left": "top",
"top center right": "top",
"top center bottom": "top",
"top center top": "top",
"top right left": "top-start",
"top right right": "top-start",
"top right bottom": "top-start",
"top right top": "top-start",
middle: "bottom",
"middle center": "bottom",
"middle center bottom": "bottom",
"middle center left": "bottom",
"middle center right": "bottom",
"middle center top": "bottom"
};
/**
* Converts the `Popover`'s legacy "position" prop to the new "placement" prop
* (used by `floating-ui`).
*
* @param position The legacy position
* @return The corresponding placement
*/
const positionToPlacement = (position) => POSITION_TO_PLACEMENT[position] ?? "bottom";
/**
* @typedef AnimationOrigin
* @type {Object}
* @property {number} originX A number between 0 and 1 (in CSS logical properties jargon, 0 is "start", 0.5 is "center", and 1 is "end")
* @property {number} originY A number between 0 and 1 (0 is top, 0.5 is center, and 1 is bottom)
*/
const PLACEMENT_TO_ANIMATION_ORIGIN = {
top: {
originX: .5,
originY: 1
},
"top-start": {
originX: 0,
originY: 1
},
"top-end": {
originX: 1,
originY: 1
},
right: {
originX: 0,
originY: .5
},
"right-start": {
originX: 0,
originY: 0
},
"right-end": {
originX: 0,
originY: 1
},
bottom: {
originX: .5,
originY: 0
},
"bottom-start": {
originX: 0,
originY: 0
},
"bottom-end": {
originX: 1,
originY: 0
},
left: {
originX: 1,
originY: .5
},
"left-start": {
originX: 1,
originY: 0
},
"left-end": {
originX: 1,
originY: 1
},
overlay: {
originX: .5,
originY: .5
}
};
/**
* Given the floating-ui `placement`, compute the framer-motion props for the
* popover's entry animation.
*
* @param placement A placement string from floating ui
* @return The object containing the motion props
*/
const placementToMotionAnimationProps = (placement) => {
const translateProp = placement.startsWith("top") || placement.startsWith("bottom") ? "translateY" : "translateX";
const translateDirection = placement.startsWith("top") || placement.startsWith("left") ? 1 : -1;
return {
style: PLACEMENT_TO_ANIMATION_ORIGIN[placement],
initial: {
opacity: 0,
[translateProp]: `${4 * translateDirection}px`
},
animate: {
opacity: 1,
[translateProp]: 0
},
transition: {
opacity: {
duration: .1,
ease: "linear"
},
[translateProp]: {
duration: .2,
ease: cubicBezier(.25, 0, 0, 1)
}
}
};
};
function isTopBottom(anchorRef) {
return !!anchorRef?.top;
}
function isRef(anchorRef) {
return !!anchorRef?.current;
}
const getReferenceElement = ({ anchor, anchorRef, anchorRect, getAnchorRect, fallbackReferenceElement }) => {
let referenceElement = null;
if (anchor) referenceElement = anchor;
else if (isTopBottom(anchorRef)) referenceElement = { getBoundingClientRect() {
const topRect = anchorRef.top.getBoundingClientRect();
const bottomRect = anchorRef.bottom.getBoundingClientRect();
return new window.DOMRect(topRect.x, topRect.y, topRect.width, bottomRect.bottom - topRect.top);
} };
else if (isRef(anchorRef)) referenceElement = anchorRef.current;
else if (anchorRef) referenceElement = anchorRef;
else if (anchorRect) referenceElement = { getBoundingClientRect() {
return anchorRect;
} };
else if (getAnchorRect) referenceElement = { getBoundingClientRect() {
const rect = getAnchorRect(fallbackReferenceElement);
return new window.DOMRect(rect.x ?? rect.left, rect.y ?? rect.top, rect.width ?? rect.right - rect.left, rect.height ?? rect.bottom - rect.top);
} };
else if (fallbackReferenceElement) referenceElement = fallbackReferenceElement.parentElement;
return referenceElement ?? null;
};
/**
* Computes the final coordinate that needs to be applied to the floating
* element when applying transform inline styles, defaulting to `undefined`
* if the provided value is `null` or `NaN`.
*
* @param c input coordinate (usually as returned from floating-ui)
* @return The coordinate's value to be used for inline styles. An `undefined`
* return value means "no style set" for this coordinate.
*/
const computePopoverPosition = (c) => c === null || Number.isNaN(c) ? void 0 : Math.round(c);
export { computePopoverPosition, getReferenceElement, placementToMotionAnimationProps, positionToPlacement };
//# sourceMappingURL=utils.mjs.map