@@ -4,15 +4,8 @@ import { VStack } from '@devup-ui/react'
44import { useRef , useState } from 'react'
55import { useEffect } from 'react'
66
7- export default function Tooltip ( {
8- children,
9- isOpen,
10- } : {
11- children : React . ReactNode
12- isOpen : boolean
13- } ) {
7+ export default function Tooltip ( { children } : { children : React . ReactNode } ) {
148 const [ viewportWidth , setViewportWidth ] = useState ( 0 )
15- const [ tooltipRect , setTooltipRect ] = useState < DOMRect | null > ( null )
169 const ref = useRef < HTMLDivElement > ( null )
1710 useEffect ( ( ) => {
1811 if ( typeof window === 'undefined' ) return
@@ -30,22 +23,36 @@ export default function Tooltip({
3023 }
3124 } , [ ] )
3225
33- useEffect ( ( ) => {
34- if ( ref . current ) {
35- setTooltipRect ( ref . current . getBoundingClientRect ( ) )
36- }
37- } , [ isOpen ] )
38-
3926 return (
4027 < VStack
41- ref = { ref }
28+ ref = { ( el ) => {
29+ if ( ! el ) return
30+
31+ ref . current = el
32+
33+ const mo = new ResizeObserver ( ( entries ) => {
34+ entries . forEach ( ( entry ) => {
35+ const target = entry . target as HTMLDivElement
36+ const x = target . offsetLeft
37+ const width = target . offsetWidth
38+ if ( x + width > viewportWidth ) {
39+ target . style . right = '16px'
40+ }
41+ } )
42+ } )
43+
44+ mo . observe ( el )
45+
46+ return ( ) => mo . disconnect ( )
47+ } }
4248 _groupHover = { {
43- display : isOpen ? 'flex' : 'none ',
49+ display : 'flex' ,
4450 } }
4551 bg = "rgba(0, 0, 0, 0.75)"
4652 borderRadius = "4px"
4753 display = "none"
4854 justifyContent = "center"
55+ maxW = "calc(100vw - 32px)"
4956 onMouseEnter = { ( e ) => {
5057 e . stopPropagation ( )
5158 } }
@@ -55,12 +62,6 @@ export default function Tooltip({
5562 pos = "absolute"
5663 px = "10px"
5764 py = "8px"
58- style = { {
59- right :
60- tooltipRect && tooltipRect . x + tooltipRect . width > viewportWidth
61- ? '16px'
62- : 'unset' ,
63- } }
6465 transform = "translateY(10px)"
6566 transition = "all 0.3s ease-in-out"
6667 >
0 commit comments