Skip to content

(PDS-584) Add mechanism for force updating Overlay #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/react-components/source/react/library/overlay/Overlay.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef, useMemo, useState } from 'react';
import React, { forwardRef, useEffect, useMemo, useState } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { usePopper } from 'react-popper';
Expand All @@ -19,10 +19,10 @@ const propTypes = {
]),
/** Optional (default bottom). Where to position the overlay in relation to the target element. */
position: PropTypes.oneOf(['top', 'right', 'bottom', 'left']),
/** Optional (default false). Whether or not the overlay should be rendered. */
show: PropTypes.bool,
/** React ref for the DOM element that should be used to position the overlay. */
target: reactRef.isRequired,
/** 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. */
show: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
/** React ref for the DOM element that should be used to position the overlay (if not provided the overlay will not be rendered). */
target: reactRef,
};

const defaultProps = {
Expand All @@ -31,6 +31,7 @@ const defaultProps = {
offset: null,
position: 'bottom',
show: false,
target: null,
};

const innerAlignment = (position, { popper }) => {
Expand Down Expand Up @@ -102,11 +103,18 @@ const Overlay = forwardRef((props, fRef) => {
align,
position,
]);
const { styles, attributes } = usePopper(targetNode, overlayNode, {
const { styles, attributes, update } = usePopper(targetNode, overlayNode, {
placement: position,
modifiers: [withOffset(offset || offsetFn)],
});

const updateOverlay = () => {
if (show && update) {
update();
}
};
useEffect(updateOverlay, [show]);

if (!show) {
return null;
}
Expand Down