Skip to content

Commit 5405c1e

Browse files
authored
refactor!: prevent vaadin-chart from expanding parent containers (#10366)
1 parent 7065a37 commit 5405c1e

File tree

5 files changed

+120
-11
lines changed

5 files changed

+120
-11
lines changed

packages/charts/src/vaadin-chart-mixin.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,6 @@ export const ChartMixin = (superClass) =>
795795
// Detect if the chart had already been initialized. This might happen in
796796
// environments where the chart is lazily attached (e.g Grid).
797797
if (this.configuration) {
798-
const { height } = this.$.wrapper.style;
799-
this.$.wrapper.style.height = '';
800-
this.configuration.setSize(null, null, false);
801-
this.$.wrapper.style.height = height;
802798
return;
803799
}
804800

@@ -829,6 +825,16 @@ export const ChartMixin = (superClass) =>
829825
const { height, width } = contentRect;
830826
const { chartHeight, chartWidth } = this.configuration;
831827

828+
this.$.wrapper.style.minHeight = '';
829+
// Use 1px as the threshold to align with Highcharts
830+
if (this.$.wrapper.offsetHeight <= 1) {
831+
this.$.wrapper.style.minHeight = `${chartHeight}px`;
832+
}
833+
this.$.wrapper.style.minWidth = '';
834+
if (this.$.wrapper.offsetWidth <= 1) {
835+
this.$.wrapper.style.minWidth = `${chartWidth}px`;
836+
}
837+
832838
if (height !== chartHeight || width !== chartWidth) {
833839
this.__reflow();
834840
}

packages/charts/src/vaadin-chart.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ class Chart extends ChartMixin(ThemableMixin(ElementMixin(PolylitMixin(LumoInjec
178178
/** @protected */
179179
render() {
180180
return html`
181-
<div id="wrapper" style="height: 100%; width: 100%;">
182-
<div id="chart" style="height: 100%; width: 100%;"></div>
181+
<div id="wrapper" style="height: 100%; width: 100%; position: relative;">
182+
<div id="chart" style="height: 100%; width: 100%; position: absolute;"></div>
183183
</div>
184184
<slot id="slot"></slot>
185185
`;

packages/charts/test/chart-element.test.js

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { aTimeout, fixtureSync, nextFrame, nextRender, oneEvent } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame, nextRender, nextResize, oneEvent } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import './chart-not-animated-styles.js';
55
import '../src/vaadin-chart.js';
@@ -344,6 +344,47 @@ describe('vaadin-chart', () => {
344344
expect(rect.width).to.be.equal(300);
345345
expect(chart.configuration.chartWidth).to.be.equal(300);
346346
});
347+
348+
it('should use intrinsic width when container provides no width', async () => {
349+
chart = fixtureSync(`
350+
<div style="display: inline-flex; height: 300px">
351+
<vaadin-chart></vaadin-chart>
352+
</div>`).querySelector('vaadin-chart');
353+
354+
await nextResize(chart);
355+
356+
expect(chart.offsetHeight).to.be.equal(300);
357+
expect(chart.offsetWidth).to.be.greaterThan(0);
358+
expect(chart.configuration.chartHeight).to.be.equal(chart.offsetHeight);
359+
});
360+
361+
it('should resize from intrinsic width to explicit width', async () => {
362+
chart = fixtureSync(`
363+
<div style="display: inline-flex; height: 300px">
364+
<vaadin-chart></vaadin-chart>
365+
</div>`).querySelector('vaadin-chart');
366+
367+
await nextResize(chart);
368+
369+
chart.style.width = '100px';
370+
await nextResize(chart);
371+
372+
expect(chart.offsetHeight).to.be.equal(300);
373+
expect(chart.offsetWidth).to.be.equal(100);
374+
expect(chart.configuration.chartWidth).to.be.equal(chart.offsetWidth);
375+
});
376+
377+
it('should collapse to zero width when container width is zero', async () => {
378+
chart = fixtureSync(`
379+
<div style="display: inline-flex; height: 300px; width: 0px">
380+
<vaadin-chart></vaadin-chart>
381+
</div>`).querySelector('vaadin-chart');
382+
383+
await nextResize(chart);
384+
385+
expect(chart.offsetHeight).to.be.equal(300);
386+
expect(chart.offsetWidth).to.be.equal(0);
387+
});
347388
});
348389

349390
describe('height', () => {
@@ -367,6 +408,66 @@ describe('vaadin-chart', () => {
367408
expect(rect.height).to.be.equal(300);
368409
expect(chart.configuration.chartHeight).to.be.equal(300);
369410
});
411+
412+
it('should not cause container height expansion in flex layout', async () => {
413+
const chartMinHeight = 300;
414+
const siblingHeight = 5;
415+
416+
// See https://github.com/vaadin/web-components/issues/10316
417+
chart = fixtureSync(`
418+
<div style="display: flex">
419+
<div>
420+
<vaadin-chart style="height: 100%; min-height: ${chartMinHeight}px;"></vaadin-chart>
421+
<div style="height: ${siblingHeight}px"></div>
422+
</div>
423+
</div>`).querySelector('vaadin-chart');
424+
425+
await nextResize(chart);
426+
427+
expect(chart.offsetHeight).to.be.equal(chartMinHeight + siblingHeight);
428+
expect(chart.configuration.chartHeight).to.be.equal(chart.offsetHeight);
429+
});
430+
431+
it('should use intrinsic height when container provides no height', async () => {
432+
chart = fixtureSync(`
433+
<div style="display: flex; width: 300px">
434+
<vaadin-chart></vaadin-chart>
435+
</div>`).querySelector('vaadin-chart');
436+
437+
await nextResize(chart);
438+
439+
expect(chart.offsetWidth).to.be.equal(300);
440+
expect(chart.offsetHeight).to.be.greaterThan(0);
441+
expect(chart.configuration.chartHeight).to.be.equal(chart.offsetHeight);
442+
});
443+
444+
it('should resize from intrinsic height to explicit height', async () => {
445+
chart = fixtureSync(`
446+
<div style="display: flex; width: 300px">
447+
<vaadin-chart></vaadin-chart>
448+
</div>`).querySelector('vaadin-chart');
449+
450+
await nextResize(chart);
451+
452+
chart.style.height = '100px';
453+
await nextResize(chart);
454+
455+
expect(chart.offsetWidth).to.be.equal(300);
456+
expect(chart.offsetHeight).to.be.equal(100);
457+
expect(chart.configuration.chartHeight).to.be.equal(chart.offsetHeight);
458+
});
459+
460+
it('should collapse to zero height when container height is zero', async () => {
461+
chart = fixtureSync(`
462+
<div style="display: flex; width: 300px; height: 0px">
463+
<vaadin-chart></vaadin-chart>
464+
</div>`).querySelector('vaadin-chart');
465+
466+
await nextResize(chart);
467+
468+
expect(chart.offsetWidth).to.be.equal(300);
469+
expect(chart.offsetHeight).to.be.equal(0);
470+
});
370471
});
371472

372473
describe('resize', () => {

packages/charts/test/reattach.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextFrame, nextResize, oneEvent } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import './chart-not-animated-styles.js';
55
import '../src/vaadin-chart.js';
@@ -262,18 +262,19 @@ describe('reattach', () => {
262262

263263
it('should restore default height when moving from different container with defined height', async () => {
264264
await oneEvent(chart, 'chart-load');
265+
await nextResize(chart);
265266

266267
const initialHeight = getComputedStyle(chart).height;
267268

268269
inner.style.height = '700px';
269270
inner.appendChild(chart);
270-
await nextFrame();
271+
await nextResize(chart);
271272

272273
expect(getComputedStyle(chart).height).to.be.equal(inner.style.height);
273274

274275
// Move back to first parent
275276
wrapper.appendChild(chart);
276-
await nextFrame();
277+
await nextResize(chart);
277278

278279
expect(getComputedStyle(chart).height).to.be.equal(initialHeight);
279280
});

packages/charts/test/styling.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2-
import { fixtureSync, oneEvent } from '@vaadin/testing-helpers';
2+
import { fixtureSync, nextResize, oneEvent } from '@vaadin/testing-helpers';
33
import './theme-styles.js';
44
import '../src/vaadin-chart.js';
55

@@ -14,6 +14,7 @@ describe('vaadin-chart styling', () => {
1414
</vaadin-chart>
1515
`);
1616
await oneEvent(chart, 'chart-load');
17+
await nextResize(chart);
1718
chartContainer = chart.$.chart;
1819
});
1920

0 commit comments

Comments
 (0)