Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(data-viz): ensure color palette updates on theme change #2007

Merged
merged 5 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/components/ColorPalette/ColorPalette.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import { ContentSwitcher, Switch, Dropdown } from 'carbon-components-react';
import cx from 'classnames';
import {
Expand All @@ -16,7 +16,8 @@ import {
fiveColorDark,
monoColors,
divergingColors,
alert,
alertLight,
alertDark,
} from '../../data/data-visualization/palettes';
import ColorPaletteColor from './ColorPaletteColor';
import PalettesContainer from './PalettesContainer';
Expand Down Expand Up @@ -46,6 +47,7 @@ const ColorPalette = ({ type, isMono, isDiverging }) => {
const threeColor = dark ? threeColorDark : threeColorLight;
const fourColor = dark ? fourColorDark : fourColorLight;
const fiveColor = dark ? fiveColorDark : fiveColorLight;
const alertColor = dark ? alertDark : alertLight;

// SET RENDERED COLORS
const [colorGroup, setColorGroup] = useState(oneColor); // used to render type === "grouped" colors
Expand All @@ -56,7 +58,7 @@ const ColorPalette = ({ type, isMono, isDiverging }) => {
} else if (type === 'categorical') {
colors = categorical;
} else if (type === 'alert') {
colors = alert;
colors = alertColor;
}

// DROPDOWN STUFF
Expand Down Expand Up @@ -84,11 +86,13 @@ const ColorPalette = ({ type, isMono, isDiverging }) => {
];

const onDropdownChange = (e) => {
const id = e.selectedItem ? e.selectedItem.id : e;

// update selected option
setGroupNumber(e.selectedItem.id);
setGroupNumber(id);

// update colors rendered
switch (e.selectedItem.id) {
switch (id) {
case 1:
setColorGroup(oneColor);
break;
Expand All @@ -108,6 +112,11 @@ const ColorPalette = ({ type, isMono, isDiverging }) => {
}
};

// Rerender color group values when theme is toggled
useEffect(() => {
onDropdownChange(groupNumber);
}, [dark]);

// SWITCHER STUFF
const activateFirstSwitcher = () => {
if (type === 'sequential') {
Expand Down Expand Up @@ -159,7 +168,6 @@ const ColorPalette = ({ type, isMono, isDiverging }) => {
label="Color group selection"
id="color-group-dropdown"
size="xl"
light
items={dropdownItems}
onChange={onDropdownChange}
selectedItem={dropdownItems[groupNumber - 1]}
Expand Down
29 changes: 26 additions & 3 deletions src/data/data-visualization/palettes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ export const divergingColors = [
},
];

export const alert = [
export const alertLight = [
{
name: 'Red 60',
hex: 'da1e28',
Expand All @@ -1180,13 +1180,36 @@ export const alert = [
light: false,
},
{
name: 'Green 50',
hex: '24a148',
name: 'Yellow 30',
hex: 'f1c21b',
light: false,
},
{
name: 'Green 60',
hex: '198038',
light: true,
},
];

export const alertDark = [
{
name: 'Red 50',
hex: 'fa4d56',
light: false,
},
{
name: 'Orange 40',
hex: 'ff832b',
light: false,
},
{
name: 'Yellow 30',
hex: 'f1c21b',
light: false,
},
{
name: 'Green 50',
hex: '24a148',
light: false,
},
];