44 ReactElement ,
55 ReactNode ,
66 createContext ,
7+ useCallback ,
78 useContext ,
9+ useEffect ,
810 useRef ,
911 useState ,
1012} from 'react' ;
@@ -87,10 +89,26 @@ interface IContent {
8789}
8890const Content = ( { children } : IContent ) => {
8991 const { isOpen, triggerRef } = usePopoverContext ( ) ;
92+ const [ position , setPosition ] = useState ( { x : 0 , y : 0 } ) ;
9093 const hasButtonRef = triggerRef && triggerRef . current ;
9194 const canOpen = isOpen && hasButtonRef ;
9295 //TODO 타입 정리필요, 상수 정리
93- const { top, height } = triggerRef ! . current ! . getBoundingClientRect ( ) ;
96+ const updatePosition = useCallback ( ( ) => {
97+ if ( triggerRef && triggerRef . current ) {
98+ const trigger = triggerRef . current ! ;
99+ const { top, height } = trigger . getBoundingClientRect ( ) ;
100+ setPosition ( { x : top , y : height } ) ;
101+ }
102+ } , [ triggerRef ] ) ;
103+
104+ useEffect ( ( ) => {
105+ if ( isOpen ) {
106+ window . addEventListener ( 'scroll' , updatePosition ) ;
107+ }
108+ return ( ) => {
109+ window . removeEventListener ( 'scroll' , updatePosition ) ;
110+ } ;
111+ } , [ isOpen , updatePosition ] ) ;
94112
95113 return (
96114 < >
@@ -99,7 +117,7 @@ const Content = ({ children }: IContent) => {
99117 < div
100118 className = "fixed z-[999] cursor-default"
101119 style = { {
102- top : top + height + 24 ,
120+ top : position . x + position . y + 24 ,
103121 right : 32 ,
104122 } } >
105123 { children }
0 commit comments