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 misplaced Popover when using Floating UI (#566) #576

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
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
30 changes: 27 additions & 3 deletions src/components/Popover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should reword:

This prop is filtered and should only …

to:

This prop should only …

The code does not filter anything. It merely shows warning and error when in dev mode. I find this sufficient, but the docs should match the code.

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`

⚠️ `inset` is a shorthand for `top right bottom left`, not for `inset-*` properties.

As opposed to `top right bottom left` and the `inset` shorthand, `inset-*` properties are writing-direction aware.


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

Expand Down Expand Up @@ -267,9 +289,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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In dev mode the allowed styles are already handled by propType validation. I dont think we need this explicit check at all.

If we do, the errors in console caused by bject.keys(placementStyle) when placementStyle === null need to be fixed,

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)),
);
};
Loading