Skip to content

Commit 900a4c0

Browse files
committed
(PDS-584) Add mechanism for force updating Overlay
Updated `show` prop to allow a string so that the Overaly can be updated if the value of `show` changes. This is useful if the UI shifts and the target is not in the same position as it was when the overlay was created.
1 parent 0af36b8 commit 900a4c0

File tree

1 file changed

+14
-6
lines changed
  • packages/react-components/source/react/library/overlay

1 file changed

+14
-6
lines changed

packages/react-components/source/react/library/overlay/Overlay.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, useMemo, useState } from 'react';
1+
import React, { forwardRef, useEffect, useMemo, useState } from 'react';
22
import ReactDOM from 'react-dom';
33
import PropTypes from 'prop-types';
44
import { usePopper } from 'react-popper';
@@ -19,10 +19,10 @@ const propTypes = {
1919
]),
2020
/** Optional (default bottom). Where to position the overlay in relation to the target element. */
2121
position: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
22-
/** Optional (default false). Whether or not the overlay should be rendered. */
23-
show: PropTypes.bool,
24-
/** React ref for the DOM element that should be used to position the overlay. */
25-
target: reactRef.isRequired,
22+
/** Optional (default false). Whether or not the overlay should be rendered. String value can be used instead of boolean to force the overlay to recalculate its position when the value changes. */
23+
show: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
24+
/** React ref for the DOM element that should be used to position the overlay (if not provided the overlay will not be rendered). */
25+
target: reactRef,
2626
};
2727

2828
const defaultProps = {
@@ -31,6 +31,7 @@ const defaultProps = {
3131
offset: null,
3232
position: 'bottom',
3333
show: false,
34+
target: null,
3435
};
3536

3637
const innerAlignment = (position, { popper }) => {
@@ -102,11 +103,18 @@ const Overlay = forwardRef((props, fRef) => {
102103
align,
103104
position,
104105
]);
105-
const { styles, attributes } = usePopper(targetNode, overlayNode, {
106+
const { styles, attributes, update } = usePopper(targetNode, overlayNode, {
106107
placement: position,
107108
modifiers: [withOffset(offset || offsetFn)],
108109
});
109110

111+
const updateOverlay = () => {
112+
if (show && update) {
113+
update();
114+
}
115+
};
116+
useEffect(updateOverlay, [show]);
117+
110118
if (!show) {
111119
return null;
112120
}

0 commit comments

Comments
 (0)