UNPKG

@wordpress/components

Version:
113 lines (112 loc) • 3.6 kB
import { useCallback, useEffect, useRef, useState } from "@wordpress/element"; import { useResizeObserver } from "@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] = useResizeObserver(); const isAxisControlled = !!axis; const [moveX, setMoveX] = useState(false); const [moveY, setMoveY] = useState(false); const { width, height } = sizes; const heightRef = useRef(height); const widthRef = useRef(width); const moveTimeoutRef = useRef(void 0); useEffect(() => { return () => window.clearTimeout(moveTimeoutRef.current); }, []); const debounceUnsetMoveXY = 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]); 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}`; } export { POSITIONS, useResizeLabel }; //# sourceMappingURL=utils.mjs.map