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

feat(theming): add system opacity values to the theme #1797

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [ ] :ok_hand: design updates will be Garden Designer approved (add the designer as a reviewer)
- [ ] :globe_with_meridians: demo is up-to-date (`npm start`)
- [ ] :arrow_left: renders as expected with reversed (RTL) direction
- [ ] :black_circle: renders as expected in dark mode
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

- [ ] :metal: renders as expected with Bedrock CSS (`?bedrock`)
- [ ] :guardsman: includes new unit tests. Maintain existing coverage (always >= 96%)
- [ ] :wheelchair: tested for WCAG 2.1 AA accessibility compliance
Expand Down
4 changes: 3 additions & 1 deletion packages/theming/demo/stories/GetColorStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable
offset,
shade,
theme,
transparency: transparency ? transparency / 100 : undefined,
transparency,
variable
});

Expand Down Expand Up @@ -71,6 +71,7 @@ const Color = ({ dark, hue, light, offset, shade, theme, transparency, variable
interface IArgs extends Omit<IColorProps, 'theme'> {
theme: {
colors: Omit<IGardenTheme['colors'], 'base'>;
opacity: IGardenTheme['opacity'];
palette: IGardenTheme['palette'];
};
}
Expand All @@ -89,6 +90,7 @@ export const GetColorStory: StoryFn<IArgs> = ({
const theme = {
...parentTheme,
colors: { ..._theme.colors, base: parentTheme.colors.base },
opacity: _theme.opacity,
palette: _theme.palette
};

Expand Down
2 changes: 1 addition & 1 deletion packages/theming/demo/utilities.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import README from '../README.md';
light: { control: { type: 'object' } },
offset: { control: { type: 'number' } },
shade: { control: { type: 'number' } },
transparency: { control: { type: 'range', min: 1 } },
transparency: { control: { type: 'range', min: 100, max: 1200, step: 100 } },
variable: { control: { type: 'text' } }
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ exports[`DEFAULT_THEME matches snapshot 1`] = `
"xxl": "32px",
"xxxl": "44px",
},
"opacity": {
"100": 0.08,
"1000": 0.8,
"1100": 0.88,
"1200": 0.96,
"200": 0.16,
"300": 0.24,
"400": 0.32,
"500": 0.4,
"600": 0.48,
"700": 0.56,
"800": 0.64,
"900": 0.72,
},
"palette": {
"azure": {
"100": "#eff7fe",
Expand Down
16 changes: 16 additions & 0 deletions packages/theming/src/elements/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ const lineHeights = {
xxxl: `${BASE * 11}px`
};

const opacity = {
100: 0.08,
200: 0.16,
300: 0.24,
400: 0.32,
500: 0.4,
600: 0.48,
700: 0.56,
800: 0.64,
900: 0.72,
1000: 0.8,
1100: 0.88,
1200: 0.96
};

const palette = { ...PALETTE };

/* Exclude product palette from the theme */
Expand Down Expand Up @@ -237,6 +252,7 @@ const DEFAULT_THEME: IGardenTheme = {
fontWeights,
iconSizes,
lineHeights,
opacity,
palette,
rtl: false,
shadowWidths,
Expand Down
1 change: 1 addition & 0 deletions packages/theming/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export interface IGardenTheme {
xxl: string;
xxxl: string;
};
opacity: Record<number, number>;
palette: Record<string, Hue>;
shadowWidths: {
xs: string;
Expand Down
9 changes: 9 additions & 0 deletions packages/theming/src/utils/getColor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ describe('getColor', () => {
expect(color).toBe(expected);
});

it('applies transparency via theme `opacity` as expected', () => {
const hue = 'blue';
const transparency = 1000;
const color = getColor({ theme: DEFAULT_THEME, hue, transparency });
const expected = rgba(PALETTE[hue][700], DEFAULT_THEME.opacity[1000]);

expect(color).toBe(expected);
});

it('applies mode transparency as expected', () => {
const hue = 'blue';
const transparency = 0.5;
Expand Down
14 changes: 11 additions & 3 deletions packages/theming/src/utils/getColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const toHex = (
const toColor = (
colors: Omit<IGardenTheme['colors'], 'base' | 'variables'>,
palette: IGardenTheme['palette'],
opacity: IGardenTheme['opacity'],
scheme: 'dark' | 'light',
hue: string,
shade?: number | string,
Expand Down Expand Up @@ -109,7 +110,9 @@ const toColor = (
}

if (retVal && transparency) {
retVal = rgba(retVal, transparency);
const alpha = transparency > 1 ? opacity[transparency] : transparency;

retVal = rgba(retVal, alpha);
}

return retVal;
Expand Down Expand Up @@ -147,7 +150,7 @@ const toProperty = (object: object, path: string) => {
* - `'chromeHue'` = `theme.colors.chromeHue`
* @param {number} [options.shade] A hue shade
* @param {number} [options.offset] A positive or negative value to adjust the shade
* @param {number} [options.transparency] An alpha-channel value between 0 and 1
* @param {number} [options.transparency] A `theme.opacity` key or an alpha-channel value between 0 and 1
*/
export const getColor = memoize(
({ dark, hue, light, offset, shade, theme, transparency, variable }: ColorParameters) => {
Expand Down Expand Up @@ -184,7 +187,12 @@ export const getColor = memoize(
}

if (_hue) {
retVal = toColor(colors, palette, scheme, _hue, _shade, _offset, _transparency);
const opacity =
theme.opacity && Object.keys(theme.opacity).length > 0
? theme.opacity
: DEFAULT_THEME.opacity;

retVal = toColor(colors, palette, opacity, scheme, _hue, _shade, _offset, _transparency);
}

if (retVal === undefined) {
Expand Down