Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 38c7d2d

Browse files
danielkaxisTigge
authored andcommitted
feat(popover): render pop-over at component DOM-level
Adds a prop `inline` to pop-over component, that when true, will render the pop-over at the component DOM-level and not in the root layer which is by default.
1 parent 1033070 commit 38c7d2d

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed
-23.1 KB
Binary file not shown.

packages/core/src/Menu/BaseMenu.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ export const BaseMenu = memo<BaseMenuProps>(
544544
disabled = false,
545545
components,
546546
onBlur,
547+
inline,
547548
...props
548549
}) => {
549550
const [menuVisible, openMenu, hideMenu] = useBoolean(false)
@@ -761,6 +762,7 @@ export const BaseMenu = memo<BaseMenuProps>(
761762
horizontalAlignment={align}
762763
anchorEl={anchorRef.current}
763764
onScroll={hideAndBlurMenu}
765+
inline={inline}
764766
>
765767
<MenuList onEscape={hideMenu} onClick={hideAndBlurMenu}>
766768
{components.map((component, index) => {

packages/core/src/PopOver/index.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
HTMLAttributes,
77
FC,
88
ReactNode,
9+
useMemo,
910
} from 'react'
1011
import ReactDOM from 'react-dom'
1112
import styled from 'styled-components'
@@ -40,6 +41,13 @@ export interface PopOverProps extends BaseProps {
4041
* The element to anchor the pop-over to.
4142
*/
4243
readonly anchorEl: HTMLElement | null
44+
/**
45+
* Renders the pop-over at the same DOM-level as the
46+
* anchorEl, not at the root layer which is by default.
47+
*
48+
* Default: `false`
49+
*/
50+
readonly inline?: boolean
4351
/**
4452
* The horizontal position of the pop-over relative to the anchor.
4553
*
@@ -89,6 +97,7 @@ export interface PopOverProps extends BaseProps {
8997

9098
export const PopOver: FC<PopOverProps> = ({
9199
anchorEl,
100+
inline = false,
92101
horizontalPosition = 'left',
93102
verticalPosition = 'bottom',
94103
horizontalAlignment = 'left',
@@ -146,15 +155,24 @@ export const PopOver: FC<PopOverProps> = ({
146155
position()
147156
}, [position, popOverContainer])
148157

158+
const popOverElement = useMemo(
159+
() => (
160+
<PopOverContainer ref={setPopOverContainer} {...props}>
161+
{children}
162+
</PopOverContainer>
163+
),
164+
[children, props]
165+
)
166+
149167
// When the layer element is not available, there is no layer yet.
150168
if (layerRoot === null) {
151169
return null
152170
}
153171

154-
return ReactDOM.createPortal(
155-
<PopOverContainer ref={setPopOverContainer} {...props}>
156-
{children}
157-
</PopOverContainer>,
158-
layerRoot
159-
)
172+
// Render the pop-over at the same DOM-level as anchorEl
173+
if (inline && anchorEl !== null) {
174+
return popOverElement
175+
}
176+
177+
return ReactDOM.createPortal(popOverElement, layerRoot)
160178
}

packages/core/src/Select/BaseSelect.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ export function BaseSelect<V extends string = string>({
433433
align = 'left',
434434
error = '',
435435
noOptionsLabel,
436+
inline,
436437
...props
437438
}: BaseSelectProps<V>): JSX.Element {
438439
const { compact: compactFromTheme, selectMarker: selectMarkerFromTheme } =
@@ -631,6 +632,7 @@ export function BaseSelect<V extends string = string>({
631632
itemRefs={itemRefs}
632633
listRef={listRef}
633634
noOptionsLabel={noOptionsLabel}
635+
inline={inline}
634636
/>
635637
</SelectContainer>
636638
)

0 commit comments

Comments
 (0)