1- import React , { useRef , useLayoutEffect , useContext , useCallback } from 'react'
1+ import React , {
2+ useLayoutEffect ,
3+ useContext ,
4+ useCallback ,
5+ useState ,
6+ } from 'react'
27import ReactDOM from 'react-dom'
38import styled from 'styled-components'
49
510import { LayerContext } from '../Practical'
6- import { useOnResizeEffect , useOnScrollEffect , anchorPosition } from './utils'
11+ import {
12+ useOnResizeParentEffect ,
13+ useOnResizeEffect ,
14+ useOnScrollEffect ,
15+ anchorPosition ,
16+ } from './utils'
717
818type BaseElement = HTMLDivElement
919type BaseProps = React . HTMLAttributes < BaseElement >
@@ -84,23 +94,24 @@ export const PopOver: React.FC<PopOverProps> = ({
8494 children,
8595 ...props
8696} ) => {
87- const popOverContainerRef = useRef < HTMLDivElement > ( null )
97+ const [ popOverContainer , setPopOverContainer ] =
98+ useState < HTMLDivElement | null > ( null )
8899
89100 const { el : layerRoot } = useContext ( LayerContext )
90101
91102 const position = useCallback ( ( ) => {
92103 // Position the pop-over element synchronously
93- if ( anchorEl !== null && popOverContainerRef . current !== null ) {
104+ if ( anchorEl !== null && popOverContainer !== null ) {
94105 if ( onPosition === undefined ) {
95- anchorPosition ( anchorEl , popOverContainerRef . current , {
106+ anchorPosition ( anchorEl , popOverContainer , {
96107 horizontalPosition,
97108 verticalPosition,
98109 horizontalAlignment,
99110 verticalAlignment,
100111 } )
101112 return
102113 }
103- onPosition ( anchorEl , popOverContainerRef . current )
114+ onPosition ( anchorEl , popOverContainer )
104115 }
105116 } , [
106117 horizontalPosition ,
@@ -109,6 +120,7 @@ export const PopOver: React.FC<PopOverProps> = ({
109120 verticalAlignment ,
110121 anchorEl ,
111122 onPosition ,
123+ popOverContainer ,
112124 ] )
113125
114126 useOnScrollEffect (
@@ -119,20 +131,24 @@ export const PopOver: React.FC<PopOverProps> = ({
119131 /** */
120132 }
121133 )
122- useOnResizeEffect ( anchorEl , position )
134+
135+ // Used when resizing the parent anchorEl
136+ useOnResizeParentEffect ( anchorEl , position )
137+
138+ useOnResizeEffect ( popOverContainer , position )
123139
124140 // Reposition on initialize
125141 useLayoutEffect ( ( ) => {
126142 position ( )
127- } , [ position ] )
143+ } , [ position , popOverContainer ] )
128144
129145 // When the layer element is not available, there is no layer yet.
130146 if ( layerRoot === null ) {
131147 return null
132148 }
133149
134150 return ReactDOM . createPortal (
135- < PopOverContainer ref = { popOverContainerRef } { ...props } >
151+ < PopOverContainer ref = { setPopOverContainer } { ...props } >
136152 { children }
137153 </ PopOverContainer > ,
138154 layerRoot
0 commit comments