Skip to content

Commit

Permalink
Merge pull request #46 from brown-ccv/vega-darkmode-option
Browse files Browse the repository at this point in the history
fix: make dark mode opt out-able for vega mixin
  • Loading branch information
fernandogelin authored Nov 11, 2020
2 parents 7cb4417 + e67d438 commit bcb58f6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
64 changes: 38 additions & 26 deletions src/mixins/vega-base-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const vegaBaseMixin = {
return {};
},
},
enableDarkmode: {
type: Boolean,
required: false,
default: true,
},
},
computed: {
actionsWidth() {
Expand All @@ -45,6 +50,36 @@ const vegaBaseMixin = {
spec() {
return _.merge({}, this.baseSpec, this.specOverride);
},
darkmodeConfig() {
// check for dark mode, if so, make labels/axes and such white
if (this.enableDarkmode) {
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
const contrastColor = 'white';
return {
axis: {
domainColor: contrastColor,
gridColor: contrastColor,
labelColor: contrastColor,
tickColor: contrastColor,
titleColor: contrastColor,
},
legend: {
labelColor: contrastColor,
titleColor: contrastColor,
},
title: {
color: contrastColor,
subtitleColor: contrastColor,
},
};
}
}

return {};
},
},
watch: {
spec() {
Expand All @@ -53,45 +88,22 @@ const vegaBaseMixin = {
includeActions() {
this.updatePlot();
},
darkmodeConfig() {
this.updatePlot();
},
},
data() {
return {
view: null,
fullId: null,
resizeObserver: null,
parentElement: null,
darkmodeConfig: {},
};
},
mounted() {
// add random number to id to ensure uniqueness - important for storybook
this.fullId = this.id + Math.floor(Math.random() * Math.floor(1000));

// check for dark mode, if so, make labels/axes and such white
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
const contrastColor = 'white';
this.darkmodeConfig = {
axis: {
domainColor: contrastColor,
gridColor: contrastColor,
labelColor: contrastColor,
tickColor: contrastColor,
titleColor: contrastColor,
},
legend: {
labelColor: contrastColor,
titleColor: contrastColor,
},
title: {
color: contrastColor,
subtitleColor: contrastColor,
},
};
}

this.$nextTick(() => {
const el = document.querySelector('#' + this.fullId);
this.parentElement = el.parentElement;
Expand Down
4 changes: 3 additions & 1 deletion src/stories/heatmap.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const Template = (args, { argTypes }) => ({
:variable-label="variableLabel"
:include-actions="includeActions"
:spec-override="specOverride"
:enable-darkmode="enableDarkmode"
/>
</template>
</DChartContainer>
Expand Down Expand Up @@ -90,7 +91,8 @@ export const Template = (args, { argTypes }) => ({
{ month: 3, station: 'B', temp: 2, depth: 3.2},
{ month: 3, station: 'C', temp: 2.3, depth: 3.9}
],
specOverride: {axes: [{orient: 'top'}]}
specOverride: {axes: [{orient: 'top'}]},
enableDarkmode: true,
}}>
{Template.bind({})}
</Story>
Expand Down
2 changes: 2 additions & 0 deletions src/stories/multilinechart.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const Template = (args, { argTypes }) => ({
:variable-label="variableLabel"
:include-actions="includeActions"
:spec-override="specOverride"
:enable-darkmode="enableDarkmode"
/>
</template>
</DChartContainer>
Expand Down Expand Up @@ -93,6 +94,7 @@ export const Template = (args, { argTypes }) => ({
{ month: 3, station: 'C', temp: 2.3, depth: 3.9}
],
specOverride: {axes: [{orient: 'top'}]},
enableDarkmode: true,
}}>
{Template.bind({})}
</Story>
Expand Down

0 comments on commit bcb58f6

Please sign in to comment.