Skip to content

Commit

Permalink
fixup! Fix misplaced Popover when using Floating UI (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
atmelmicro committed Dec 10, 2024
1 parent 05094c7 commit a9a60fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
27 changes: 22 additions & 5 deletions src/components/Popover/Popover.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createPortal } from 'react-dom';
import { withGlobalProps } from '../../provider';
import { classNames } from '../../utils/classNames';
import { transferProps } from '../../utils/transferProps';
import cleanPlacementStyle from './_helpers/cleanPlacementStyle';
import getRootSideClassName from './_helpers/getRootSideClassName';
import getRootAlignmentClassName from './_helpers/getRootAlignmentClassName';
import styles from './Popover.module.scss';
Expand All @@ -12,6 +13,7 @@ export const Popover = React.forwardRef((props, ref) => {
const {
placement,
children,
placementStyle,
portalId,
position,
x,
Expand All @@ -29,11 +31,7 @@ export const Popover = React.forwardRef((props, ref) => {
getRootAlignmentClassName(placement, styles),
)}
ref={ref}
style={{
left: x,
position,
top: y,
}}
style={cleanPlacementStyle(placementStyle)}
>
{children}
<span className={styles.arrow} />
Expand All @@ -49,6 +47,7 @@ export const Popover = React.forwardRef((props, ref) => {

Popover.defaultProps = {
placement: 'bottom',
placementStyle: null,
portalId: null,
position: null,
x: null,
Expand Down Expand Up @@ -78,6 +77,24 @@ Popover.propTypes = {
'left-start',
'left-end',
]),
/**
* Used for positioning the popover with a library like Floating UI. It is filtered,
* then passed as to the popover as the `style` prop.
*/
placementStyle: PropTypes.shape({
bottom: PropTypes.string,
inset: PropTypes.string,
'inset-block-end': PropTypes.string,
'inset-block-start': PropTypes.string,
'inset-inline-end': PropTypes.string,
'inset-inline-start': PropTypes.string,
left: PropTypes.string,
position: PropTypes.string,
right: PropTypes.string,
top: PropTypes.string,
'transform-origin': PropTypes.string,
translate: PropTypes.string,
}),
/**
* If set, popover is rendered in the React Portal with that ID.
*/
Expand Down
25 changes: 22 additions & 3 deletions src/components/Popover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ automatically, including smart position updates to ensure Popover visibility,
we recommend to involve an external library designed specifically for this
purpose.

To position the popover, you need to provide the `placementStyle` prop with the
style you want to apply to the popover. This prop is filtered and should only
be used to position the popover. The allowed props are

- `position`
- `inset`
- `inset-inline-start`
- `inset-inline-end`
- `inset-block-start`
- `inset-block-end`
- `top`
- `right`
- `bottom`
- `left`
- `translate`
- `transform-origin`

ℹ️ The following example is using external library [Floating UI]. To use
Floating UI, install it first:

Expand Down Expand Up @@ -267,9 +284,11 @@ React.createElement(() => {
<Popover
id="my-advanced-popover"
placement={finalPlacement}
position={strategy}
x={x}
y={y}
placementStyle={{
position: strategy,
top: y,
left: x,
}}
ref={floating}
>
Auto-flipping Popover
Expand Down
29 changes: 29 additions & 0 deletions src/components/Popover/_helpers/cleanPlacementStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default (placementStyle) => {
const validProps = [
'position',
'inset',
'inset-inline-start',
'inset-inline-end',
'inset-block-start',
'inset-block-end',
'top',
'right',
'bottom',
'left',
'translate',
'transform-origin',
];

if (process.env.NODE_ENV !== 'production') {
const invalidProps = Object.keys(placementStyle).filter((prop) => !validProps.includes(prop));

if (invalidProps.length > 0) {
// eslint-disable-next-line no-console
console.error(`Invalid prop(s) supplied to the "placementStyle" prop: "${invalidProps.join('", "')}"`);
}
}

return Object.fromEntries(
Object.entries(placementStyle).filter(([prop]) => validProps.includes(prop)),
);
};

0 comments on commit a9a60fa

Please sign in to comment.