MCPcopy
hub / github.com/chartjs/Chart.js / createResizeObserver

Function createResizeObserver

src/platform/platform.dom.js:191–229  ·  view source on GitHub ↗
(chart, type, listener)

Source from the content-addressed store, hash-verified

189}
190
191function createResizeObserver(chart, type, listener) {
192 const canvas = chart.canvas;
193 const container = canvas && _getParentNode(canvas);
194 if (!container) {
195 return;
196 }
197 const resize = throttled((width, height) => {
198 const w = container.clientWidth;
199 listener(width, height);
200 if (w < container.clientWidth) {
201 // If the container size shrank during chart resize, let's assume
202 // scrollbar appeared. So we resize again with the scrollbar visible -
203 // effectively making chart smaller and the scrollbar hidden again.
204 // Because we are inside `throttled`, and currently `ticking`, scroll
205 // events are ignored during this whole 2 resize process.
206 // If we assumed wrong and something else happened, we are resizing
207 // twice in a frame (potential performance issue)
208 listener();
209 }
210 }, window);
211
212 // @ts-ignore until https://github.com/microsoft/TypeScript/issues/37861 implemented
213 const observer = new ResizeObserver(entries => {
214 const entry = entries[0];
215 const width = entry.contentRect.width;
216 const height = entry.contentRect.height;
217 // When its container's display is set to 'none' the callback will be called with a
218 // size of (0, 0), which will cause the chart to lose its original height, so skip
219 // resizing in such case.
220 if (width === 0 && height === 0) {
221 return;
222 }
223 resize(width, height);
224 });
225 observer.observe(container);
226 listenDevicePixelRatioChanges(chart, resize);
227
228 return observer;
229}
230
231function releaseObserver(chart, type, observer) {
232 if (observer) {

Callers

nothing calls this directly

Calls 3

_getParentNodeFunction · 0.85
throttledFunction · 0.85

Tested by

no test coverage detected