@wordpress/components
Version:
UI components for WordPress.
56 lines (55 loc) • 2.83 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let __use_on_value_update_ts = require("./use-on-value-update.cjs");
// packages/components/src/utils/hooks/use-animated-offset-rect.ts
/**
* A utility used to animate something in a container component based on the "offset
* rect" (position relative to the container and size) of a subelement. For example,
* this is useful to render an indicator for the selected option of a component, and
* to animate it when the selected option changes.
*
* Takes in a container element and the up-to-date "offset rect" of the target
* subelement, obtained with `useTrackElementOffsetRect`. Then it does the following:
*
* - Adds CSS variables with rect information to the container, so that the indicator
* can be rendered and animated with them. These are kept up-to-date, enabling CSS
* transitions on change.
* - Sets an attribute (`data-subelement-animated` by default) when the tracked
* element changes, so that the target (e.g. the indicator) can be animated to its
* new size and position.
* - Removes the attribute when the animation is done.
*
* The need for the attribute is due to the fact that the rect might update in
* situations other than when the tracked element changes, e.g. the tracked element
* might be resized. In such cases, there is no need to animate the indicator, and
* the change in size or position of the indicator needs to be reflected immediately.
*/
function useAnimatedOffsetRect(container, rect, { prefix = "subelement", dataAttribute = `${prefix}-animated`, transitionEndFilter = () => true, roundRect = false } = {}) {
const setProperties = (0, _wordpress_compose.useEvent)(() => {
Object.keys(rect).forEach((property) => property !== "element" && container?.style.setProperty(`--${prefix}-${property}`, String(roundRect ? Math.floor(rect[property]) : rect[property])));
});
(0, _wordpress_element.useLayoutEffect)(() => {
setProperties();
}, [rect, setProperties]);
(0, __use_on_value_update_ts.useOnValueUpdate)(rect.element, ({ previousValue }) => {
if (rect.element && previousValue) container?.setAttribute(`data-${dataAttribute}`, "");
});
(0, _wordpress_element.useLayoutEffect)(() => {
function onTransitionEnd(event) {
if (transitionEndFilter(event)) container?.removeAttribute(`data-${dataAttribute}`);
}
container?.addEventListener("transitionend", onTransitionEnd);
return () => container?.removeEventListener("transitionend", onTransitionEnd);
}, [
dataAttribute,
container,
transitionEndFilter
]);
}
exports.useAnimatedOffsetRect = useAnimatedOffsetRect;
//# sourceMappingURL=use-animated-offset-rect.cjs.map