Skip to content

Commit

Permalink
Popover: Allow legitimate 0 positions to update popover position (#51320
Browse files Browse the repository at this point in the history
)

* Popover: Allow legitmate 0 positions to update popover position

* Add changelog entry

* Refactor to utility function

* Add unit tests

* Switch to undefined fallback for null and NaN cases, update snapshots

---------

Co-authored-by: Marco Ciampini <[email protected]>
  • Loading branch information
andrewserong and ciampo authored Jun 12, 2023
1 parent 625b2de commit b873f43
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`URLPopover matches the snapshot in its default state 1`] = `
<span>
<div
class="components-popover block-editor-url-popover is-positioned"
style="position: absolute; top: 0px; left: 0px; transform: translateZ(0);"
style="position: absolute; top: 0px; left: 0px; transform: none;"
tabindex="-1"
>
<div
Expand Down Expand Up @@ -53,7 +53,7 @@ exports[`URLPopover matches the snapshot when the settings are toggled open 1`]
<span>
<div
class="components-popover block-editor-url-popover is-positioned"
style="position: absolute; top: 0px; left: 0px; transform: translateZ(0);"
style="position: absolute; top: 0px; left: 0px; transform: none;"
tabindex="-1"
>
<div
Expand Down Expand Up @@ -108,7 +108,7 @@ exports[`URLPopover matches the snapshot when there are no settings 1`] = `
<span>
<div
class="components-popover block-editor-url-popover is-positioned"
style="position: absolute; top: 0px; left: 0px; transform: translateZ(0);"
style="position: absolute; top: 0px; left: 0px; transform: none;"
tabindex="-1"
>
<div
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- `Popover`: Allow legitimate 0 positions to update popover position ([#51320](https://github.com/WordPress/gutenberg/pull/51320)).

## 25.1.0 (2023-06-07)

### Enhancements
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import Button from '../button';
import ScrollLock from '../scroll-lock';
import { Slot, Fill, useSlot } from '../slot-fill';
import {
computePopoverPosition,
getFrameOffset,
getFrameScale,
positionToPlacement,
Expand Down Expand Up @@ -499,8 +500,8 @@ const UnforwardedPopover = (
// to use `translateX` and `translateY` because those values would
// be overridden by the return value of the
// `placementToMotionAnimationProps` function in `AnimatedWrapper`
x: Math.round( x ?? 0 ) || undefined,
y: Math.round( y ?? 0 ) || undefined,
x: computePopoverPosition( x ),
y: computePopoverPosition( y ),
}
}
>
Expand Down
23 changes: 22 additions & 1 deletion packages/components/src/popover/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import { positionToPlacement, placementToMotionAnimationProps } from '../utils';
import {
computePopoverPosition,
positionToPlacement,
placementToMotionAnimationProps,
} from '../utils';
import Popover from '..';
import type { PopoverProps } from '../types';

Expand Down Expand Up @@ -248,4 +252,21 @@ describe( 'Popover', () => {
);
} );
} );

describe( 'computePopoverPosition', () => {
it.each( [
[ 14, 14 ], // valid integers shouldn't be changes
[ 14.02, 14 ], // floating numbers are parsed to integers
[ 0, 0 ], // zero remains zero
[ null, undefined ],
[ NaN, undefined ],
] )(
'converts `%s` to `%s`',
( inputCoordinate, expectedCoordinated ) => {
expect( computePopoverPosition( inputCoordinate ) ).toEqual(
expectedCoordinated
);
}
);
} );
} );
12 changes: 12 additions & 0 deletions packages/components/src/popover/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,15 @@ export const getReferenceElement = ( {
// Convert any `undefined` value to `null`.
return referenceElement ?? null;
};

/**
* Computes the final coordinate that needs to be applied to the floating
* element when applying transform inline styles, defaulting to `undefined`
* if the provided value is `null` or `NaN`.
*
* @param c input coordinate (usually as returned from floating-ui)
* @return The coordinate's value to be used for inline styles. An `undefined`
* return value means "no style set" for this coordinate.
*/
export const computePopoverPosition = ( c: number | null ) =>
c === null || Number.isNaN( c ) ? undefined : Math.round( c );

0 comments on commit b873f43

Please sign in to comment.