UNPKG

@chayns-components/core

Version:

A set of beautiful React components for developing your own applications with chayns.

69 lines 1.59 kB
import React, { useEffect, useRef } from 'react'; import { useMasonryContext } from '../Masonry.context'; import { StyledMasonryItem } from './MasonryItem.styles'; const MasonryItem = ({ children }) => /*#__PURE__*/React.createElement(React.Fragment, null, children); export const InternalMasonryItem = ({ children, itemKey, x, y, width }) => { const ref = useRef(null); const { registerItem } = useMasonryContext(); useEffect(() => { const element = ref.current; if (!element) { return undefined; } let frameId; const updateHeight = () => { if (frameId) { window.cancelAnimationFrame(frameId); } frameId = window.requestAnimationFrame(() => { registerItem(itemKey, element.getBoundingClientRect().height); }); }; updateHeight(); const observer = new ResizeObserver(updateHeight); observer.observe(element); return () => { if (frameId) { window.cancelAnimationFrame(frameId); } observer.disconnect(); }; }, [itemKey, registerItem, width]); return /*#__PURE__*/React.createElement(StyledMasonryItem, { ref: ref, initial: { opacity: 0, scale: 0.96 }, animate: { opacity: 1, scale: 1, x, y, width }, exit: { opacity: 0, scale: 0.96 }, transition: { type: 'spring', stiffness: 320, damping: 34, mass: 0.85 } }, children); }; MasonryItem.displayName = 'Masonry.Item'; export default MasonryItem; //# sourceMappingURL=MasonryItem.js.map