11import React from 'react' ;
22
3- import { Text } from '@gravity-ui/uikit' ;
3+ import once from 'lodash/once' ;
4+
5+ import { Text , type TextProps } from '@gravity-ui/uikit' ;
46
57import { block } from '../../utils' ;
68
79import './LongValue.scss' ;
810
911const b = block ( 'long-value' ) ;
1012
11- export interface LongValueProps {
13+ export interface LongValueProps extends TextProps {
1214 value ?: string | number | boolean ;
13- className ?: string ;
1415}
1516
16- export const LongValue : React . FC < LongValueProps > = ( { value, className} ) => {
17- const prevValue = React . useRef < typeof value | null > ( null ) ;
17+ export const LongValue : React . FC < LongValueProps > = ( { value, className, ...restProps } ) => {
1818 const ref = React . useRef < HTMLDivElement > ( null ) ;
1919 const [ open , setOpen ] = React . useState ( false ) ;
2020 const [ long , setLong ] = React . useState ( false ) ;
@@ -23,10 +23,10 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
2323
2424 React . useEffect ( ( ) => {
2525 if ( ref . current ) {
26- if ( value !== prevValue . current ) {
27- const { offsetWidth, scrollWidth} = ref . current ;
26+ const { offsetWidth, scrollWidth} = ref . current ;
2827
29- if ( offsetWidth < scrollWidth ) {
28+ const setFlags = once ( ( offsetW : number , scrollW : number ) => {
29+ if ( offsetW < scrollW ) {
3030 if ( ! long ) {
3131 setLong ( true ) ;
3232 }
@@ -39,15 +39,37 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
3939 setOpen ( false ) ;
4040 }
4141 }
42+ } ) ;
43+
44+ // element has been rendered, but is not displayed on the page
45+ if ( offsetWidth === 0 && scrollWidth === 0 ) {
46+ const observer = new IntersectionObserver ( ( entries ) => {
47+ const entry = entries . find ( ( e ) => e . target === ref . current ) ;
4248
43- prevValue . current = value ;
49+ if ( entry && entry . isIntersecting ) {
50+ const target = entry . target as HTMLDivElement ;
51+
52+ setFlags ( target . offsetWidth , target . scrollWidth ) ;
53+ }
54+ } ) ;
55+
56+ observer . observe ( ref . current ) ;
57+
58+ return ( ) => {
59+ observer . disconnect ( ) ;
60+ } ;
61+ } else {
62+ setFlags ( offsetWidth , scrollWidth ) ;
4463 }
4564 }
46- } ) ;
65+
66+ return ;
67+ } , [ value ] ) ;
4768
4869 return (
4970 < Text
5071 as = "div"
72+ { ...restProps }
5173 ref = { ref }
5274 className = { b ( { open, long} , className ) }
5375 // @ts -ignore
0 commit comments