1- import { useEffect , useRef , useState } from 'react'
1+ import { useRef , useState } from 'react'
22import { cn } from '@/lib/utils'
33import {
44 Popover ,
@@ -26,18 +26,17 @@ export function LongText({
2626 const ref = useRef < HTMLDivElement > ( null )
2727 const [ isOverflown , setIsOverflown ] = useState ( false )
2828
29- useEffect ( ( ) => {
30- if ( checkOverflow ( ref . current ) ) {
31- setIsOverflown ( true )
32- return
29+ // Use ref callback to check overflow when element is mounted
30+ const refCallback = ( node : HTMLDivElement | null ) => {
31+ ref . current = node
32+ if ( node && checkOverflow ( node ) ) {
33+ queueMicrotask ( ( ) => setIsOverflown ( true ) )
3334 }
34-
35- setIsOverflown ( false )
36- } , [ ] )
35+ }
3736
3837 if ( ! isOverflown )
3938 return (
40- < div ref = { ref } className = { cn ( 'truncate' , className ) } >
39+ < div ref = { refCallback } className = { cn ( 'truncate' , className ) } >
4140 { children }
4241 </ div >
4342 )
@@ -48,7 +47,7 @@ export function LongText({
4847 < TooltipProvider delayDuration = { 0 } >
4948 < Tooltip >
5049 < TooltipTrigger asChild >
51- < div ref = { ref } className = { cn ( 'truncate' , className ) } >
50+ < div ref = { refCallback } className = { cn ( 'truncate' , className ) } >
5251 { children }
5352 </ div >
5453 </ TooltipTrigger >
@@ -61,7 +60,7 @@ export function LongText({
6160 < div className = 'sm:hidden' >
6261 < Popover >
6362 < PopoverTrigger asChild >
64- < div ref = { ref } className = { cn ( 'truncate' , className ) } >
63+ < div ref = { refCallback } className = { cn ( 'truncate' , className ) } >
6564 { children }
6665 </ div >
6766 </ PopoverTrigger >
0 commit comments