Skip to content

Commit

Permalink
fix(vis-type: bar): jump to full width after rendering narrow initial…
Browse files Browse the repository at this point in the history
…ly using debounced resize observer (#576)

Co-authored-by: Usama Ansari <[email protected]>
  • Loading branch information
thinkh and dv-usama-ansari authored Oct 18, 2024
1 parent f67d209 commit 39a0c20
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/vis/vishooks/hooks/useChart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react-compiler/react-compiler */
import * as React from 'react';
import { useSetState } from '@mantine/hooks';
import { useDebouncedCallback, useSetState } from '@mantine/hooks';
import type { ECElementEvent, ECharts, ComposeOption } from 'echarts/core';
import { use, init } from 'echarts/core';
import { BarChart, LineChart } from 'echarts/charts';
Expand Down Expand Up @@ -85,6 +85,11 @@ export function useChart({
instance: null as ECharts | null,
});

const debouncedResizeObserver = useDebouncedCallback((entries: ResizeObserverEntry[]) => {
const newDimensions = entries[0]?.contentRect;
setState({ width: newDimensions?.width, height: newDimensions?.height });
}, 250);

const mouseEventsRef = React.useRef(mouseEvents);
mouseEventsRef.current = mouseEvents;

Expand Down Expand Up @@ -132,10 +137,7 @@ export function useChart({

const { ref, setRef } = useSetRef<HTMLElement>({
register: (element) => {
const observer = new ResizeObserver((entries) => {
const newDimensions = entries[0]?.contentRect;
setState({ width: newDimensions?.width, height: newDimensions?.height });
});
const observer = new ResizeObserver(debouncedResizeObserver);
// create the instance
const instance = init(element);
// Save the mouse events
Expand Down

0 comments on commit 39a0c20

Please sign in to comment.