Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add observer for long value component #221

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/lib/kit/components/LongValue/LongValue.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import React from 'react';

import {Text} from '@gravity-ui/uikit';
import once from 'lodash/once';

import {Text, type TextProps} from '@gravity-ui/uikit';

import {block} from '../../utils';

import './LongValue.scss';

const b = block('long-value');

export interface LongValueProps {
export interface LongValueProps extends TextProps {
value?: string | number | boolean;
className?: string;
}

export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
const prevValue = React.useRef<typeof value | null>(null);
export const LongValue: React.FC<LongValueProps> = ({value, className, ...restProps}) => {
const ref = React.useRef<HTMLDivElement>(null);
const [open, setOpen] = React.useState(false);
const [long, setLong] = React.useState(false);
Expand All @@ -23,10 +23,10 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {

React.useEffect(() => {
if (ref.current) {
if (value !== prevValue.current) {
const {offsetWidth, scrollWidth} = ref.current;
const {offsetWidth, scrollWidth} = ref.current;

if (offsetWidth < scrollWidth) {
const setFlags = once((offsetW: number, scrollW: number) => {
if (offsetW < scrollW) {
if (!long) {
setLong(true);
}
Expand All @@ -39,15 +39,37 @@ export const LongValue: React.FC<LongValueProps> = ({value, className}) => {
setOpen(false);
}
}
});

// element has been rendered, but is not displayed on the page
if (offsetWidth === 0 && scrollWidth === 0) {
const observer = new IntersectionObserver((entries) => {
const entry = entries.find((e) => e.target === ref.current);

prevValue.current = value;
if (entry && entry.isIntersecting) {
const target = entry.target as HTMLDivElement;

setFlags(target.offsetWidth, target.scrollWidth);
}
});

observer.observe(ref.current);

return () => {
observer.disconnect();
};
} else {
setFlags(offsetWidth, scrollWidth);
}
}
});

return;
}, [value]);

return (
<Text
as="div"
{...restProps}
ref={ref}
className={b({open, long}, className)}
// @ts-ignore
Expand Down
14 changes: 6 additions & 8 deletions src/stories/components/InputPreview/InputPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,12 @@ export const InputPreview: React.FC<InputPreviewProps> = ({
search={searchInput}
/>
</div>
{togglerInput === 'view' ? (
<div className={b('input-view')}>
<DynamicView
value={form.values.input}
spec={transformIncorrect(form.values.options)}
/>
</div>
) : null}
<div className={b('input-view', {hidden: togglerInput !== 'view'})}>
<DynamicView
value={form.values.input}
spec={transformIncorrect(form.values.options)}
/>
</div>
{togglerInput === 'json' ? (
<div className={b('monaco')}>{renderMonaco(form.values.input)}</div>
) : null}
Expand Down
Loading