@atlaskit/popup
Version:
A popup displays brief content in an overlay.
25 lines (24 loc) • 1.09 kB
JavaScript
import React, { Fragment, useLayoutEffect, useRef } from 'react';
// eslint-disable-next-line @repo/internal/react/require-jsdoc
export var RepositionOnUpdate = function RepositionOnUpdate(_ref) {
var children = _ref.children,
update = _ref.update;
// Ref used here to skip update on first render (when refs haven't been set)
var isFirstRenderRef = useRef(true);
useLayoutEffect(function () {
if (isFirstRenderRef.current) {
isFirstRenderRef.current = false;
return;
}
// callback function from popper that repositions pop-up on content Update.
// Guard against `update` being undefined: on Safari the Popper instance may
// not be fully initialised when this layout effect fires, in which case the
// render-prop `update` is undefined and calling it throws
// `TypeError: e.update is not a function`.
if (typeof update === 'function') {
update();
}
}, [update]);
// wrapping in fragment to make TS happy (known issue with FC returning children)
return /*#__PURE__*/React.createElement(Fragment, null, children);
};