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

[Uptime] fix double scroll on legend height change #92155

Merged
merged 4 commits into from
Feb 23, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const FIXED_AXIS_HEIGHT = 32;
// number of items to display in canvas, since canvas can only have limited size
export const CANVAS_MAX_ITEMS = 150;

export const CHART_LEGEND_PADDING = 62;
export const CHART_LEGEND_PADDING = 33;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import React from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { IWaterfallContext } from '../context/waterfall_chart';
import { WaterfallChartLegendContainer } from './styles';
import { WaterfallChartProps } from './waterfall_chart';

interface LegendProps {
Expand All @@ -18,12 +17,10 @@ interface LegendProps {

export const Legend: React.FC<LegendProps> = ({ items, render }) => {
return (
<WaterfallChartLegendContainer>
<EuiFlexGroup gutterSize="none">
{items.map((item, index) => {
return <EuiFlexItem key={index}>{render(item, index)}</EuiFlexItem>;
})}
</EuiFlexGroup>
</WaterfallChartLegendContainer>
<EuiFlexGroup gutterSize="none">
{items.map((item, index) => {
return <EuiFlexItem key={index}>{render(item, index)}</EuiFlexItem>;
})}
</EuiFlexGroup>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { renderLegendItem } from '../../step_detail/waterfall/waterfall_chart_wr
import { render } from '../../../../../lib/helper/rtl_helpers';

import 'jest-canvas-mock';
import { waitFor } from '@testing-library/dom';

describe('waterfall', () => {
it('sets the correct height in case of full height', () => {
Expand Down Expand Up @@ -38,6 +39,8 @@ describe('waterfall', () => {

const chartWrapper = getByTestId('waterfallOuterContainer');

expect(chartWrapper).toHaveStyleRule('height', 'calc(100vh - 62px)');
waitFor(() => {
expect(chartWrapper).toHaveStyleRule('height', 'calc(100vh - 62px)');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React, { useEffect, useRef, useState } from 'react';
import { EuiFlexGroup } from '@elastic/eui';
import { TickFormatter, DomainRange, BarStyleAccessor } from '@elastic/charts';
import useWindowSize from 'react-use/lib/useWindowSize';
import { useWaterfallContext } from '../context/waterfall_chart';
import {
WaterfallChartOuterContainer,
Expand All @@ -18,6 +19,7 @@ import {
RelativeContainer,
WaterfallChartFilterContainer,
WaterfallChartAxisOnlyContainer,
WaterfallChartLegendContainer,
} from './styles';
import { CHART_LEGEND_PADDING, MAIN_GROW_SIZE, SIDEBAR_GROW_SIZE } from './constants';
import { Sidebar } from './sidebar';
Expand Down Expand Up @@ -67,19 +69,23 @@ export const WaterfallChart = ({
fetchedNetworkRequests,
} = useWaterfallContext();

const { width } = useWindowSize();

const chartWrapperDivRef = useRef<HTMLDivElement | null>(null);
const legendDivRef = useRef<HTMLDivElement | null>(null);

const [height, setHeight] = useState<string>(maxHeight);

const shouldRenderSidebar = !!(sidebarItems && renderSidebarItem);
const shouldRenderLegend = !!(legendItems && legendItems.length > 0 && renderLegendItem);

useEffect(() => {
if (fullHeight && chartWrapperDivRef.current) {
if (fullHeight && chartWrapperDivRef.current && legendDivRef.current) {
const chartOffset = chartWrapperDivRef.current.getBoundingClientRect().top;
setHeight(`calc(100vh - ${chartOffset + CHART_LEGEND_PADDING}px)`);
const legendOffset = legendDivRef.current.getBoundingClientRect().height;
setHeight(`calc(100vh - ${chartOffset + CHART_LEGEND_PADDING + legendOffset}px)`);
}
}, [chartWrapperDivRef, fullHeight]);
}, [chartWrapperDivRef, fullHeight, legendDivRef, width]);

const chartsToDisplay = useBarCharts({ data });

Expand Down Expand Up @@ -135,7 +141,11 @@ export const WaterfallChart = ({
</WaterfallChartAxisOnlyContainer>
</EuiFlexGroup>
</WaterfallChartOuterContainer>
{shouldRenderLegend && <Legend items={legendItems!} render={renderLegendItem!} />}
{shouldRenderLegend && (
<WaterfallChartLegendContainer ref={legendDivRef}>
<Legend items={legendItems!} render={renderLegendItem!} />
</WaterfallChartLegendContainer>
)}
{renderFlyout && renderFlyout()}
</RelativeContainer>
);
Expand Down