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

feat(Popper): made inline logic internal to Popper #8669

Merged
merged 4 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
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
40 changes: 20 additions & 20 deletions packages/react-core/src/helpers/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export interface PopperProps {
position?: 'right' | 'left' | 'center';
/** Instead of direction and position can set the placement of the popper */
placement?: Placement;
/** The container to append the popper to. Defaults to 'document.body' */
appendTo?: HTMLElement | (() => HTMLElement);
/** The container to append the popper to. Defaults to 'inline'. */
appendTo?: HTMLElement | (() => HTMLElement) | 'inline';
/** z-index of the popper element */
zIndex?: number;
/** True to make the popper visible */
Expand Down Expand Up @@ -148,7 +148,7 @@ export const Popper: React.FunctionComponent<PopperProps> = ({
direction = 'down',
position = 'left',
placement,
appendTo = () => document.body,
appendTo = 'inline',
zIndex = 9999,
isVisible = true,
positionModifiers,
Expand Down Expand Up @@ -347,13 +347,24 @@ export const Popper: React.FunctionComponent<PopperProps> = ({
...attributes.popper
};

const menuWithPopper = React.cloneElement(popper, options);
const getMenuWithPopper = () => {
const localPopper = React.cloneElement(popper, options);
return popperRef ? (
localPopper
) : (
<div style={{ display: 'contents' }} ref={node => setPopperElement(node?.firstElementChild as HTMLElement)}>
{localPopper}
</div>
);
};

const getTarget: () => HTMLElement = () => {
if (typeof appendTo === 'function') {
return appendTo();
const getPopper = () => {
if (appendTo === 'inline') {
return getMenuWithPopper();
} else {
const target = typeof appendTo === 'function' ? appendTo() : appendTo;
return ReactDOM.createPortal(getMenuWithPopper(), target);
}
return appendTo;
};

return (
Expand All @@ -363,18 +374,7 @@ export const Popper: React.FunctionComponent<PopperProps> = ({
{trigger}
</div>
)}
{ready &&
isVisible &&
ReactDOM.createPortal(
popperRef ? (
menuWithPopper
) : (
<div style={{ display: 'contents' }} ref={node => setPopperElement(node?.firstElementChild as HTMLElement)}>
{menuWithPopper}
</div>
),
getTarget()
)}
{ready && isVisible && getPopper()}
</>
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-integration/demo-app-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"serve:demo-app": "node scripts/serve"
},
"dependencies": {
"@patternfly/react-core": "^5.0.0-alpha.6",
"@patternfly/react-core": "^5.0.0-alpha.9",
"react": "^18",
"react-dom": "^18",
"react-router": "^5.3.3",
Expand Down