Skip to content

Commit c141531

Browse files
authored
feat: add refactored getColor utility (#1763)
1 parent 074840e commit c141531

File tree

14 files changed

+752
-33
lines changed

14 files changed

+752
-33
lines changed

.github/CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ on your system. After you clone this repo, run `npm install` to install
4848
dependencies needed for development. After installation, the following
4949
commands are available:
5050

51-
- `npm start` to launch Storybook with live reload.
51+
- `npm start` to launch Storybook with live reload. Use `PACKAGE=dirname npm start`
52+
(where `dirname` is a package directory name) to limit Storybook launch to the
53+
given Garden package.
5254
- `npm test` to run Jest testing.
5355
- `npm run lint` to enforce consistent JavaScript, CSS, and
5456
markdown code conventions across all component packages. Note this is

.storybook/preview.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import React from 'react';
99
import styled, { createGlobalStyle } from 'styled-components';
1010
import { create } from '@storybook/theming/create';
11-
import { ThemeProvider, DEFAULT_THEME, getColorV8 } from '../packages/theming/src';
11+
import { ThemeProvider, DEFAULT_THEME, getColor } from '../packages/theming/src';
1212

1313
const DARK_THEME = { ...DEFAULT_THEME, colors: { ...DEFAULT_THEME.colors, base: 'dark' } };
14-
const DARK = getColorV8('foreground', 600 /* default shade */, DARK_THEME);
15-
const LIGHT = getColorV8('background', 600 /* default shade */, DEFAULT_THEME);
14+
const DARK = getColor({ theme: DARK_THEME, variable: 'background.default' });
15+
const LIGHT = getColor({ theme: DEFAULT_THEME, variable: 'background.default' });
1616

1717
export const parameters = {
1818
actions: { argTypesRegex: '^on[A-Z].*' },
@@ -36,7 +36,7 @@ export const parameters = {
3636

3737
const GlobalPreviewStyling = createGlobalStyle`
3838
body {
39-
background-color: ${p => getColorV8('background', 600 /* default shade */, p.theme)};
39+
background-color: ${p => getColor({ theme: p.theme, variable: 'background.default' })};
4040
/* stylelint-disable-next-line declaration-no-important */
4141
padding: 0 !important;
4242
font-family: ${p => p.theme.fonts.system};
@@ -65,8 +65,6 @@ const withThemeProvider = (story, context) => {
6565
: context.parameters.backgrounds.default === 'dark'
6666
) {
6767
colors.base = 'dark';
68-
colors.background = getColorV8('neutralHue', 900, DEFAULT_THEME);
69-
colors.foreground = getColorV8('neutralHue', 200, DEFAULT_THEME);
7068
}
7169

7270
const theme = { ...DEFAULT_THEME, colors, rtl };

docs/migration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ consider additional positioning prop support on a case-by-case basis.
182182
scheme to custom components that are not part of the Garden framework. It is
183183
recommended to utilize this stopgap measure until such components can be updated
184184
to leverage the full capabilities of v9 `getColor`.
185+
- Utility function `getColor` has been refactored with a signature that supports
186+
v9 light/dark modes. Replace usage with `getColorV8` until custom components can
187+
be upgraded to utilize the new `getColor` function.
185188
- Utility function `getDocument` has been removed. Use `useDocument` instead.
186189
- Utility function `isRtl` has been removed. Use `props.theme.rtl` instead.
187190
- The following exports have changed:

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/theming/demo/stories/GetColorStory.tsx

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,118 @@
66
*/
77

88
import React from 'react';
9-
import styled from 'styled-components';
10-
import { Story } from '@storybook/react';
11-
import { DEFAULT_THEME, getColorV8 } from '@zendeskgarden/react-theming';
9+
import { StoryFn } from '@storybook/react';
10+
import styled, { DefaultTheme, useTheme } from 'styled-components';
11+
import { IGardenTheme, getColor } from '@zendeskgarden/react-theming';
12+
import { Tag } from '@zendeskgarden/react-tags';
1213

13-
interface IArgs {
14-
hue: string;
15-
shade: number;
16-
transparency?: number;
17-
}
14+
const toBackground = (theme: DefaultTheme, backgroundColor: string) => {
15+
const color = getColor({ hue: 'neutralHue', shade: 300, theme });
16+
const size = 26;
17+
const dimensions = `${size}px ${size}px`;
18+
const positionX1 = theme.rtl ? '100%' : '0';
19+
const positionX2 = theme.rtl ? `calc(100% - ${size / 2}px)` : `${size / 2}px`;
20+
const position1 = `${positionX1} 0`;
21+
const position2 = `${positionX2} ${size / 2}px`;
22+
const position3 = `${positionX2} 0`;
23+
const position4 = `${positionX1} ${size / -2}px`;
1824

19-
const StyledDiv = styled.div<IArgs>`
20-
background-color: ${props =>
21-
getColorV8(
22-
props.hue,
23-
props.shade,
24-
DEFAULT_THEME,
25-
props.transparency ? props.transparency / 100 : undefined
26-
)};
25+
return `
26+
linear-gradient(${backgroundColor}, ${backgroundColor}),
27+
linear-gradient(45deg, ${color} 25%, transparent 25%) ${position1} / ${dimensions} repeat,
28+
linear-gradient(45deg, transparent 75%, ${color} 75%) ${position2} / ${dimensions} repeat,
29+
linear-gradient(135deg, ${color} 25%, transparent 25%) ${position3} / ${dimensions} repeat,
30+
linear-gradient(135deg, transparent 75%, ${color} 75%) ${position4} / ${dimensions} repeat
31+
`;
32+
};
33+
34+
const StyledDiv = styled.div<{ background: string }>`
35+
display: flex;
36+
align-items: center;
37+
justify-content: center;
38+
background: ${p => p.background};
2739
height: 208px;
2840
`;
2941

30-
export const GetColorStory: Story<IArgs> = args => <StyledDiv {...args} />;
42+
interface IColorProps {
43+
dark?: object;
44+
hue?: string;
45+
light?: object;
46+
offset?: number;
47+
shade?: number;
48+
theme: IGardenTheme;
49+
transparency?: number;
50+
variable?: string;
51+
}
52+
53+
const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable }: IColorProps) => {
54+
let background;
55+
let tag;
56+
57+
try {
58+
const backgroundColor = getColor({
59+
dark,
60+
hue,
61+
light,
62+
offset,
63+
shade,
64+
theme,
65+
transparency: transparency ? transparency / 100 : undefined,
66+
variable
67+
});
68+
69+
background = toBackground(theme, backgroundColor);
70+
tag = (
71+
<Tag hue={getColor({ theme, variable: 'background.default' })} size="large">
72+
{backgroundColor}
73+
</Tag>
74+
);
75+
} catch (error) {
76+
background = 'transparent';
77+
tag = (
78+
<Tag hue="red" size="large">
79+
{error instanceof Error ? error.message : String(error)}
80+
</Tag>
81+
);
82+
}
83+
84+
return <StyledDiv background={background}>{tag}</StyledDiv>;
85+
};
86+
87+
interface IArgs extends Omit<IColorProps, 'theme'> {
88+
theme: {
89+
colors: Omit<IGardenTheme['colors'], 'base'>;
90+
palette: IGardenTheme['palette'];
91+
};
92+
}
93+
94+
export const GetColorStory: StoryFn<IArgs> = ({
95+
dark,
96+
hue,
97+
light,
98+
offset,
99+
shade,
100+
theme: _theme,
101+
transparency,
102+
variable
103+
}) => {
104+
const parentTheme = useTheme();
105+
const theme = {
106+
...parentTheme,
107+
colors: { ..._theme.colors, base: parentTheme.colors.base },
108+
palette: _theme.palette
109+
};
110+
111+
return (
112+
<Color
113+
dark={dark}
114+
hue={hue}
115+
light={light}
116+
offset={offset}
117+
shade={shade}
118+
theme={theme}
119+
transparency={transparency}
120+
variable={variable}
121+
/>
122+
);
123+
};

packages/theming/demo/utilities.stories.mdx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@ import README from '../README.md';
4848
name="getColor()"
4949
args={{
5050
hue: 'primaryHue',
51-
shade: 600
51+
theme: {
52+
colors: Object.fromEntries(
53+
Object.entries(DEFAULT_THEME.colors).filter(([key]) => key !== 'base')
54+
),
55+
palette: DEFAULT_THEME.palette
56+
}
5257
}}
5358
argTypes={{
54-
transparency: { control: { type: 'range', min: 1 } }
59+
dark: { control: { type: 'object' } },
60+
light: { control: { type: 'object' } },
61+
offset: { control: { type: 'number' } },
62+
shade: { control: { type: 'number' } },
63+
transparency: { control: { type: 'range', min: 1 } },
64+
variable: { control: { type: 'text' } }
5565
}}
5666
>
5767
{args => <GetColorStory {...args} />}

packages/theming/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"@floating-ui/react-dom": "^2.0.0",
2525
"@zendeskgarden/container-focusvisible": "^2.0.0",
2626
"@zendeskgarden/container-utilities": "^2.0.0",
27+
"chroma-js": "^2.4.2",
28+
"lodash.get": "^4.4.2",
2729
"lodash.memoize": "^4.1.2",
2830
"polished": "^4.0.0",
2931
"prop-types": "^15.5.7"
@@ -34,6 +36,8 @@
3436
"styled-components": "^4.2.0 || ^5.3.1"
3537
},
3638
"devDependencies": {
39+
"@types/chroma-js": "2.4.4",
40+
"@types/lodash.get": "4.4.9",
3741
"@types/lodash.memoize": "4.1.9"
3842
},
3943
"keywords": [

packages/theming/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export { default as PALETTE } from './elements/palette';
1111
export { default as PALETTE_V8 } from './elements/palette/v8';
1212
export { default as retrieveComponentStyles } from './utils/retrieveComponentStyles';
1313
export { getArrowPosition } from './utils/getArrowPosition';
14-
export { getColorV8 as getColor, getColorV8 } from './utils/getColorV8';
14+
export { getColor } from './utils/getColor';
15+
export { getColorV8 } from './utils/getColorV8';
1516
export { getFloatingPlacements } from './utils/getFloatingPlacements';
1617
export { getFocusBoxShadow } from './utils/getFocusBoxShadow';
1718
export { default as getLineHeight } from './utils/getLineHeight';

packages/theming/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const PLACEMENT = [
4545

4646
export type Placement = (typeof PLACEMENT)[number];
4747

48-
type Hue = Record<number | string, string> | string;
48+
export type Hue = Record<number | string, string> | string;
4949

5050
export interface IGardenTheme {
5151
rtl: boolean;

packages/theming/src/utils/focusStyles.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import React from 'react';
99
import { render } from 'garden-test-utils';
1010
import styled, { ThemeProps, DefaultTheme, CSSObject } from 'styled-components';
1111
import { focusStyles } from './focusStyles';
12-
import { Hue } from './getColorV8';
12+
import { Hue } from '../types';
1313
import DEFAULT_THEME from '../elements/theme';
1414
import PALETTE_V8 from '../elements/palette/v8';
1515

0 commit comments

Comments
 (0)