Skip to content

Commit

Permalink
fix(data-viz): ensure color palette updates on theme change (#2007)
Browse files Browse the repository at this point in the history
* fix(data-viz): ensure color palette updates on theme change

* fix(data-viz): update alert colors

Co-authored-by: Alessandra Davila <[email protected]>
Co-authored-by: jeanservaas <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2020
1 parent 50d95c6 commit c7806e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
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,
},
];

1 comment on commit c7806e9

@vercel
Copy link

@vercel vercel bot commented on c7806e9 Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.