@wordpress/components
Version:
UI components for WordPress.
119 lines (118 loc) • 3.95 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_element = require("@wordpress/element");
let _wordpress_compose = require("@wordpress/compose");
// packages/components/src/resizable-box/resize-tooltip/utils.ts
const noop = () => {};
const POSITIONS = {
bottom: "bottom",
corner: "corner"
};
/**
* Custom hook that manages resize listener events. It also provides a label
* based on current resize width x height values.
*
* @param props
* @param props.axis Only shows the label corresponding to the axis.
* @param props.fadeTimeout Duration (ms) before deactivating the resize label.
* @param props.onResize Callback when a resize occurs. Provides { width, height } callback.
* @param props.position Adjusts label value.
* @param props.showPx Whether to add `PX` to the label.
*
* @return Properties for hook.
*/
function useResizeLabel({ axis, fadeTimeout = 180, onResize = noop, position = POSITIONS.bottom, showPx = false }) {
const [resizeListener, sizes] = (0, _wordpress_compose.useResizeObserver)();
const isAxisControlled = !!axis;
const [moveX, setMoveX] = (0, _wordpress_element.useState)(false);
const [moveY, setMoveY] = (0, _wordpress_element.useState)(false);
const { width, height } = sizes;
const heightRef = (0, _wordpress_element.useRef)(height);
const widthRef = (0, _wordpress_element.useRef)(width);
const moveTimeoutRef = (0, _wordpress_element.useRef)(void 0);
(0, _wordpress_element.useEffect)(() => {
return () => window.clearTimeout(moveTimeoutRef.current);
}, []);
const debounceUnsetMoveXY = (0, _wordpress_element.useCallback)(() => {
const unsetMoveXY = () => {
if (isAxisControlled) return;
setMoveX(false);
setMoveY(false);
};
if (moveTimeoutRef.current) window.clearTimeout(moveTimeoutRef.current);
moveTimeoutRef.current = window.setTimeout(unsetMoveXY, fadeTimeout);
}, [fadeTimeout, isAxisControlled]);
(0, _wordpress_element.useEffect)(() => {
if (!(width !== null || height !== null)) return;
const didWidthChange = width !== widthRef.current;
const didHeightChange = height !== heightRef.current;
if (!didWidthChange && !didHeightChange) return;
if (width && !widthRef.current && height && !heightRef.current) {
widthRef.current = width;
heightRef.current = height;
return;
}
if (didWidthChange) {
setMoveX(true);
widthRef.current = width;
}
if (didHeightChange) {
setMoveY(true);
heightRef.current = height;
}
onResize({
width,
height
});
debounceUnsetMoveXY();
}, [
width,
height,
onResize,
debounceUnsetMoveXY
]);
return {
label: getSizeLabel({
axis,
height,
moveX,
moveY,
position,
showPx,
width
}),
resizeListener
};
}
/**
* Gets the resize label based on width and height values (as well as recent changes).
*
* @param props
* @param props.axis Only shows the label corresponding to the axis.
* @param props.height Height value.
* @param props.moveX Recent width (x axis) changes.
* @param props.moveY Recent width (y axis) changes.
* @param props.position Adjusts label value.
* @param props.showPx Whether to add `PX` to the label.
* @param props.width Width value.
*
* @return The rendered label.
*/
function getSizeLabel({ axis, height, moveX = false, moveY = false, position = POSITIONS.bottom, showPx = false, width }) {
if (!moveX && !moveY) return;
if (position === POSITIONS.corner) return `${width} x ${height}`;
const labelUnit = showPx ? " px" : "";
if (axis) {
if (axis === "x" && moveX) return `${width}${labelUnit}`;
if (axis === "y" && moveY) return `${height}${labelUnit}`;
}
if (moveX && moveY) return `${width} x ${height}`;
if (moveX) return `${width}${labelUnit}`;
if (moveY) return `${height}${labelUnit}`;
}
exports.POSITIONS = POSITIONS;
exports.useResizeLabel = useResizeLabel;
//# sourceMappingURL=utils.cjs.map