Repository navigation
Popover: update @floating-ui to latest version, remove custom fix for iframe positioning and scaling
#46845
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
Popover: update @floating-ui to latest version, remove custom fix for iframe positioning and scaling
#46845
Changes from all commits
b092e33
8a914dd
982d6b0
3dfc78c
b5d6805
54cd253
0af6e87
3d62c20
d5e4e29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,11 @@ | |
| */ | ||
| import type { ForwardedRef, SyntheticEvent, RefCallback } from 'react'; | ||
| import classnames from 'classnames'; | ||
| import type { Middleware, MiddlewareArguments } from '@floating-ui/react-dom'; | ||
| import { | ||
| useFloating, | ||
| flip as flipMiddleware, | ||
| shift as shiftMiddleware, | ||
| limitShift, | ||
| autoUpdate, | ||
| arrow, | ||
| offset as offsetMiddleware, | ||
|
|
@@ -50,8 +50,6 @@ import ScrollLock from '../scroll-lock'; | |
| import { Slot, Fill, useSlot } from '../slot-fill'; | ||
| import { | ||
| computePopoverPosition, | ||
| getFrameOffset, | ||
| getFrameScale, | ||
| positionToPlacement, | ||
| placementToMotionAnimationProps, | ||
| getReferenceOwnerDocument, | ||
|
|
@@ -64,7 +62,6 @@ import type { | |
| PopoverAnchorRefReference, | ||
| PopoverAnchorRefTopBottom, | ||
| } from './types'; | ||
| import { limitShift as customLimitShift } from './limit-shift'; | ||
| import { overlayMiddlewares } from './overlay-middlewares'; | ||
|
|
||
| /** | ||
|
|
@@ -262,69 +259,34 @@ const UnforwardedPopover = ( | |
| ? positionToPlacement( position ) | ||
| : placementProp; | ||
|
|
||
| /** | ||
| * Offsets the position of the popover when the anchor is inside an iframe. | ||
| * | ||
| * Store the offset in a ref, due to constraints with floating-ui: | ||
| * https://floating-ui.com/docs/react-dom#variables-inside-middleware-functions. | ||
| */ | ||
| const frameOffsetRef = useRef( getFrameOffset( referenceOwnerDocument ) ); | ||
|
|
||
| const middleware = [ | ||
| ...( placementProp === 'overlay' ? overlayMiddlewares() : [] ), | ||
| // Custom middleware which adjusts the popover's position by taking into | ||
| // account the offset of the anchor's iframe (if any) compared to the page. | ||
| { | ||
| name: 'frameOffset', | ||
| fn( { x, y }: MiddlewareArguments ) { | ||
| if ( ! frameOffsetRef.current ) { | ||
| return { | ||
| x, | ||
| y, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| x: x + frameOffsetRef.current.x, | ||
| y: y + frameOffsetRef.current.y, | ||
| data: { | ||
| // This will be used in the customLimitShift() function. | ||
| amount: frameOffsetRef.current, | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| offsetMiddleware( offsetProp ), | ||
| computedFlipProp ? flipMiddleware() : undefined, | ||
| computedResizeProp | ||
| ? size( { | ||
| apply( sizeProps ) { | ||
| const { firstElementChild } = | ||
| refs.floating.current ?? {}; | ||
|
|
||
| // Only HTMLElement instances have the `style` property. | ||
| if ( ! ( firstElementChild instanceof HTMLElement ) ) | ||
| return; | ||
|
|
||
| // Reduce the height of the popover to the available space. | ||
| Object.assign( firstElementChild.style, { | ||
| maxHeight: `${ sizeProps.availableHeight }px`, | ||
| overflow: 'auto', | ||
| } ); | ||
| }, | ||
| } ) | ||
| : undefined, | ||
| shift | ||
| ? shiftMiddleware( { | ||
| crossAxis: true, | ||
| limiter: customLimitShift(), | ||
| padding: 1, // Necessary to avoid flickering at the edge of the viewport. | ||
| } ) | ||
| : undefined, | ||
| computedFlipProp && flipMiddleware(), | ||
| computedResizeProp && | ||
| size( { | ||
| apply( sizeProps ) { | ||
| const { firstElementChild } = refs.floating.current ?? {}; | ||
|
|
||
| // Only HTMLElement instances have the `style` property. | ||
| if ( ! ( firstElementChild instanceof HTMLElement ) ) | ||
| return; | ||
|
|
||
| // Reduce the height of the popover to the available space. | ||
| Object.assign( firstElementChild.style, { | ||
| maxHeight: `${ sizeProps.availableHeight }px`, | ||
| overflow: 'auto', | ||
| } ); | ||
| }, | ||
| } ), | ||
| shift && | ||
| shiftMiddleware( { | ||
| crossAxis: true, | ||
| limiter: limitShift(), | ||
| padding: 1, // Necessary to avoid flickering at the edge of the viewport. | ||
| } ), | ||
| arrow( { element: arrowRef } ), | ||
| ].filter( | ||
| ( m: Middleware | undefined ): m is Middleware => m !== undefined | ||
| ); | ||
| ]; | ||
| const slotName = useContext( slotNameContext ) || __unstableSlotName; | ||
| const slot = useSlot( slotName ); | ||
|
|
||
|
|
@@ -353,10 +315,6 @@ const UnforwardedPopover = ( | |
| // Positioning coordinates | ||
| x, | ||
| y, | ||
| // Callback refs (not regular refs). This allows the position to be updated. | ||
| // when either elements change. | ||
| reference: referenceCallbackRef, | ||
| floating, | ||
| // Object with "regular" refs to both "reference" and "floating" | ||
| refs, | ||
| // Type of CSS position property to use (absolute or fixed) | ||
|
|
@@ -372,6 +330,7 @@ const UnforwardedPopover = ( | |
| middleware, | ||
| whileElementsMounted: ( referenceParam, floatingParam, updateParam ) => | ||
| autoUpdate( referenceParam, floatingParam, updateParam, { | ||
| layoutShift: false, | ||
| animationFrame: true, | ||
| } ), | ||
| } ); | ||
|
|
@@ -406,17 +365,16 @@ const UnforwardedPopover = ( | |
| fallbackReferenceElement, | ||
| fallbackDocument: document, | ||
| } ); | ||
| const scale = getFrameScale( resultingReferenceOwnerDoc ); | ||
|
|
||
| const resultingReferenceElement = getReferenceElement( { | ||
| anchor, | ||
| anchorRef, | ||
| anchorRect, | ||
| getAnchorRect, | ||
| fallbackReferenceElement, | ||
| scale, | ||
| } ); | ||
|
|
||
| referenceCallbackRef( resultingReferenceElement ); | ||
| refs.setReference( resultingReferenceElement ); | ||
|
|
||
| setReferenceOwnerDocument( resultingReferenceOwnerDoc ); | ||
| }, [ | ||
|
|
@@ -429,23 +387,17 @@ const UnforwardedPopover = ( | |
| anchorRect, | ||
| getAnchorRect, | ||
| fallbackReferenceElement, | ||
| referenceCallbackRef, | ||
| refs, | ||
| ] ); | ||
|
|
||
| // If the reference element is in a different ownerDocument (e.g. iFrame), | ||
| // we need to manually update the floating's position as the reference's owner | ||
| // document scrolls. Also update the frame offset if the view resizes. | ||
| // document scrolls. | ||
| useLayoutEffect( () => { | ||
| if ( | ||
| // Reference and root documents are the same. | ||
| referenceOwnerDocument === document || | ||
| // Reference and floating are in the same document. | ||
| referenceOwnerDocument === refs.floating.current?.ownerDocument || | ||
| // The reference's document has no view (i.e. window) | ||
| // or frame element (ie. it's not an iframe). | ||
| ! referenceOwnerDocument?.defaultView?.frameElement | ||
| ! referenceOwnerDocument || | ||
| ! referenceOwnerDocument.defaultView | ||
| ) { | ||
| frameOffsetRef.current = undefined; | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -456,23 +408,17 @@ const UnforwardedPopover = ( | |
| ? getScrollContainer( frameElement ) | ||
| : null; | ||
|
|
||
| const updateFrameOffset = () => { | ||
| frameOffsetRef.current = getFrameOffset( referenceOwnerDocument ); | ||
| update(); | ||
| }; | ||
| defaultView.addEventListener( 'resize', updateFrameOffset ); | ||
| scrollContainer?.addEventListener( 'scroll', updateFrameOffset ); | ||
|
|
||
| updateFrameOffset(); | ||
| defaultView.addEventListener( 'resize', update ); | ||
| scrollContainer?.addEventListener( 'scroll', update ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason why we need to attach our own listeners to the iframe's scroll container is described in floating-ui/floating-ui#2518. Once that's fixed, Floating UI will attach them automatically for us, will watch for scroll position changes for us, and our workaround code can be removed. |
||
|
|
||
| return () => { | ||
| defaultView.removeEventListener( 'resize', updateFrameOffset ); | ||
| scrollContainer?.removeEventListener( 'scroll', updateFrameOffset ); | ||
| defaultView.removeEventListener( 'resize', update ); | ||
| scrollContainer?.removeEventListener( 'scroll', update ); | ||
| }; | ||
| }, [ referenceOwnerDocument, update, refs.floating ] ); | ||
| }, [ referenceOwnerDocument, update ] ); | ||
|
|
||
| const mergedFloatingRef = useMergeRefs( [ | ||
| floating, | ||
| refs.setFloating, | ||
| dialogRef, | ||
| forwardedRef, | ||
| ] ); | ||
|
|
@@ -543,18 +489,12 @@ const UnforwardedPopover = ( | |
| left: | ||
| typeof arrowData?.x !== 'undefined' && | ||
| Number.isFinite( arrowData.x ) | ||
| ? `${ | ||
| arrowData.x + | ||
| ( frameOffsetRef.current?.x ?? 0 ) | ||
| }px` | ||
| ? `${ arrowData.x }px` | ||
| : '', | ||
| top: | ||
| typeof arrowData?.y !== 'undefined' && | ||
| Number.isFinite( arrowData.y ) | ||
| ? `${ | ||
| arrowData.y + | ||
| ( frameOffsetRef.current?.y ?? 0 ) | ||
| }px` | ||
| ? `${ arrowData.y }px` | ||
| : '', | ||
| } } | ||
| > | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.