Skip to content

Commit 300f5f9

Browse files
authored
fix(theming): return hex from generated getColor values (#1999)
1 parent a424bc7 commit 300f5f9

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

packages/theming/demo/stories/GetColorStory.tsx

+73-19
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,40 @@
88
import React from 'react';
99
import { StoryFn } from '@storybook/react';
1010
import styled, { useTheme } from 'styled-components';
11-
import { IGardenTheme, getCheckeredBackground, getColor } from '@zendeskgarden/react-theming';
11+
import { opacify } from 'color2k';
12+
import {
13+
ColorParameters,
14+
IGardenTheme,
15+
getCheckeredBackground,
16+
getColor
17+
} from '@zendeskgarden/react-theming';
18+
import { LG, SM } from '@zendeskgarden/react-typography';
19+
import { Grid } from '@zendeskgarden/react-grid';
1220
import { Tag } from '@zendeskgarden/react-tags';
1321

14-
const StyledDiv = styled.div.attrs<{ background: string }>(p => ({
15-
style: { background: p.background }
16-
}))<{ background: string }>`
22+
const StyledDiv = styled.div.attrs<{ $background: string }>(p => ({
23+
style: { background: p.$background }
24+
}))`
1725
display: flex;
1826
align-items: center;
1927
justify-content: center;
28+
min-width: 32px;
2029
height: 208px;
2130
`;
2231

23-
interface IColorProps {
24-
dark?: object;
25-
hue?: string;
26-
light?: object;
27-
offset?: number;
28-
shade?: number;
29-
theme: IGardenTheme;
30-
transparency?: number;
31-
variable?: string;
32-
}
33-
34-
const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable }: IColorProps) => {
32+
const Color = ({
33+
dark,
34+
hue,
35+
light,
36+
offset,
37+
shade,
38+
theme,
39+
transparency,
40+
variable
41+
}: ColorParameters) => {
3542
let background;
3643
let tag;
44+
let generatedHue;
3745

3846
try {
3947
const backgroundColor = getColor({
@@ -57,19 +65,65 @@ const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable
5765
{backgroundColor}
5866
</Tag>
5967
);
68+
69+
if (!variable) {
70+
const hues = [...Object.keys(theme.colors), ...Object.keys(theme.palette)].filter(
71+
_hue => _hue !== 'base' && _hue !== 'variables'
72+
);
73+
const selectedHue = (theme.colors.base === 'dark' ? dark?.hue : light?.hue) || hue || '';
74+
75+
if (!hues.includes(selectedHue)) {
76+
generatedHue = [...Array(12).keys()].reduce<Record<number, string>>((retVal, index) => {
77+
const _shade = (index + 1) * 100;
78+
79+
retVal[_shade] = getColor({ theme, hue: opacify(backgroundColor, 1), shade: _shade });
80+
81+
return retVal;
82+
}, {});
83+
84+
/* eslint-disable-next-line no-console */
85+
console.log(generatedHue);
86+
}
87+
}
6088
} catch (error) {
6189
background = 'transparent';
6290
tag = (
63-
<Tag hue="red" size="large">
91+
<Tag hue="dangerHue" size="large">
6492
{error instanceof Error ? error.message : String(error)}
6593
</Tag>
6694
);
6795
}
6896

69-
return <StyledDiv background={background}>{tag}</StyledDiv>;
97+
return (
98+
<>
99+
<StyledDiv $background={background}>{tag}</StyledDiv>
100+
{!!generatedHue && (
101+
<>
102+
<LG style={{ margin: '20px 0 12px' }}>Generated hue</LG>
103+
<Grid gutters={false}>
104+
<Grid.Row wrap="nowrap">
105+
{Object.entries(generatedHue).map(([_shade, backgroundColor], index) => (
106+
<Grid.Col key={index} textAlign="center">
107+
<StyledDiv $background={backgroundColor}>
108+
<Tag
109+
hue={getColor({ theme, variable: 'background.default' })}
110+
style={{ position: 'absolute', transform: 'rotate(-90deg)' }}
111+
>
112+
{backgroundColor}
113+
</Tag>
114+
</StyledDiv>
115+
<SM>{_shade}</SM>
116+
</Grid.Col>
117+
))}
118+
</Grid.Row>
119+
</Grid>
120+
</>
121+
)}
122+
</>
123+
);
70124
};
71125

72-
interface IArgs extends Omit<IColorProps, 'theme'> {
126+
interface IArgs extends Omit<ColorParameters, 'theme'> {
73127
theme: {
74128
colors: Omit<IGardenTheme['colors'], 'base'>;
75129
opacity: IGardenTheme['opacity'];

packages/theming/src/utils/getColor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* found at http://www.apache.org/licenses/LICENSE-2.0.
66
*/
77

8-
import { getScale, parseToRgba } from 'color2k';
8+
import { getScale, parseToRgba, toHex as _toHex } from 'color2k';
99
import { darken, getContrast, lighten, rgba } from 'polished';
1010
import get from 'lodash.get';
1111
import memoize from 'lodash.memoize';
@@ -151,7 +151,7 @@ const generateColorScale = memoize((color: string) => {
151151
const contrastRatios = [];
152152

153153
for (let i = 0; i <= scaleSize; i++) {
154-
const _color = scale(i);
154+
const _color = _toHex(scale(i));
155155
colors.push(_color);
156156
contrastRatios.push(getContrast('#FFF', _color));
157157
}

0 commit comments

Comments
 (0)