'React Popper. How to move element relatively parent?
I'm trying to move Popper that is inside parent component. How i can do it?
I tried to use offset property in modifiers object. e.g.:
offset: {
offset: 20,
}
But offset only move Popper in one direction. If placement={'top' || 'bottom'} => it moves right and left, if placement={'right' || 'left'} => it moves top and bottom.
const Layer = ({
children,
align,
inner = true,
parent,
portal,
}) => (
<Manager>
<Popper
placement={align}
modifiers={{
inner: {
enabled: inner,
},
}}
referenceElement={parent}
>
{({ ref, style, placement }) => (
<div
ref={ref}
data-placement={placement}
style={style}
>
{console.log(style)}
{children}
</div>
)}
</Popper>
</Manager>
)
Solution 1:[1]
Just dealt with this: Place MaterialUI Tooltip "on top" of anchor element? (React)
PopperProps={{
popperOptions: {
modifiers: {
flip: { enabled: false },
offset: {
enabled: true,
offset: '20px 20px'
}
}
}
}}
Solution 2:[2]
I solved it like that. Added topOffset and leftOffset props to popper child and used spread syntax. Don't think it is cleanest solution.
const Layer = ({
children,
align,
inner = true,
parent,
portal,
topOffset,
leftOffset,
}) => (
<Manager>
<Popper
placement={align}
modifiers={{
inner: {
enabled: inner,
},
}}
referenceElement={parent}
>
{({ ref, style, placement }) => (
<div
ref={ref}
data-placement={placement}
style={{
...style,
top: style.top + topOffset,
left: style.left + leftOffset,
}}
>
{console.log(style)}
{children}
</div>
)}
</Popper>
</Manager>
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | doublejosh |
| Solution 2 | Mario Petrovic |
