Skip to content
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

fix Popover controlled mode during first render #1276

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/poor-bees-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

Fixed an issue where using Popover components in controlled mode (through `visible` prop) was appending it to the wrong root element.
11 changes: 11 additions & 0 deletions packages/itwinui-react/src/core/utils/components/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type PopoverProps = {
export const Popover = React.forwardRef((props: PopoverProps, ref) => {
const [mounted, setMounted] = React.useState(false);
const themeInfo = React.useContext(ThemeContext);
const isDomAvailable = useIsDomAvailable();

const tippyRef = React.useRef<Element>(null);
const refs = useMergedRefs(tippyRef, ref);
Expand Down Expand Up @@ -74,6 +75,10 @@ export const Popover = React.forwardRef((props: PopoverProps, ref) => {
zIndex: 99999,
...props,
className: cx('iui-popover', props.className),
// add additional check for isDomAvailable when using in controlled mode,
// because rootRef is not available in first render
visible:
props.visible !== undefined ? props.visible && isDomAvailable : undefined,
plugins: [
lazyLoad,
removeTabIndex,
Expand Down Expand Up @@ -156,3 +161,9 @@ export const hideOnEscOrTab = {
};

export default Popover;

const useIsDomAvailable = () => {
gretanausedaite marked this conversation as resolved.
Show resolved Hide resolved
const [isDomAvailable, setIsDomAvailable] = React.useState(false);
React.useEffect(() => void setIsDomAvailable(true), []);
return isDomAvailable;
};