Skip to content

Commit 54954ce

Browse files
[compat2021] Make the feature graph responsive to narrow layouts (#2495)
This PR also: 1. Sets a max-width of 700px for the page (based on Una's old PR) 2. Centers the page. Fixes #2494
1 parent 2f7e640 commit 54954ce

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

webapp/components/compat-2021.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ class Compat2021 extends PolymerElement {
160160
return html`
161161
<style>
162162
:host {
163+
display: block;
164+
max-width: 700px;
165+
/* Override wpt.fyi's automatically injected common.css */
166+
margin: 0 auto !important;
163167
font-family: system-ui, sans-serif;
164168
line-height: 1.5;
165169
}
@@ -551,6 +555,8 @@ class Compat2021FeatureChart extends PolymerElement {
551555
with the JavaScript defined height. */
552556
height: 350px;
553557
margin: 0 auto;
558+
display: flex;
559+
justify-content: center;
554560
}
555561
556562
paper-dialog {
@@ -622,6 +628,16 @@ class Compat2021FeatureChart extends PolymerElement {
622628
return 'compat-2021-feature-chart';
623629
}
624630

631+
ready() {
632+
super.ready();
633+
634+
// Google Charts is not responsive, even if one sets a percentage-width, so
635+
// we add a resize observer to redraw the chart if the size changes.
636+
window.addEventListener('resize', () => {
637+
this.updateChart(this.feature, this.stable);
638+
});
639+
}
640+
625641
async updateChart(feature, stable) {
626642
// Our observer may be called before the feature is set, so debounce that.
627643
if (!feature) {
@@ -677,7 +693,7 @@ class Compat2021FeatureChart extends PolymerElement {
677693
},
678694
});
679695

680-
chart.draw(dataTable, this.getChartOptions(feature));
696+
chart.draw(dataTable, this.getChartOptions(div, feature));
681697
}
682698

683699
getChromeChangelogUrl(fromVersion, toVersion) {
@@ -704,13 +720,10 @@ class Compat2021FeatureChart extends PolymerElement {
704720
window.open(url);
705721
}
706722

707-
getChartOptions(feature) {
723+
getChartOptions(containerDiv, feature) {
708724
const options = {
709-
width: 800,
710725
height: 350,
711-
chartArea: {
712-
height: '80%',
713-
},
726+
fontSize: 14,
714727
tooltip: {
715728
trigger: 'both',
716729
},
@@ -750,6 +763,27 @@ class Compat2021FeatureChart extends PolymerElement {
750763
};
751764
}
752765

766+
// We draw the chart in two ways, depending on the viewport width. In
767+
// 'full' mode the legend is on the right and we limit the chart size to
768+
// 700px wide. In 'mobile' mode the legend is on the top and we use all the
769+
// space we can get for the chart.
770+
if (containerDiv.clientWidth >= 700) {
771+
options.width = 700;
772+
options.chartArea = {
773+
height: '80%'
774+
};
775+
} else {
776+
options.width = '100%';
777+
options.legend = {
778+
position: 'top',
779+
alignment: 'center',
780+
};
781+
options.chartArea = {
782+
left: 75,
783+
width: '80%',
784+
};
785+
}
786+
753787
return options;
754788
}
755789
}

0 commit comments

Comments
 (0)