Okay, let's break down how to use the getColor utility in the @zendeskgarden/react-theming package, along with best practices, and examples in JavaScript, React, and styled-components.
Purpose of getColor
The getColor utility is a powerful function that allows you to extract color values from a theme object within the Zendesk Garden Design System. It handles various color formats, including theme variables, palette hues, and adjustments for dark/light modes, transparency and shade variations. It's memoized for performance, so it won't recalculate the same color repeatedly.
Ways to Use getColor
Here are the primary ways you can use getColor, with detailed explanations:
1. Basic Usage with Theme Hues:
Concept: Retrieving a color based on a named hue from the theme.
Example (React):
jsx
import React from 'react';
import styled, { useTheme } from 'styled-components';
import { getColor, IGardenTheme } from '@zendeskgarden/react-theming';
const MyComponent = () => {
const theme = useTheme() as IGardenTheme;
const backgroundColor = getColor({ theme, hue: 'primaryHue', shade: 600 });
const textColor = getColor({ theme, hue: 'neutralHue', shade: 800 });
return (
<div style={{ backgroundColor, color: textColor, padding: '10px' }}>
This is my component!
</div>
);
};
code
- **Explanation:**
code
- We use `useTheme` hook to access the theme object.
- `getColor` is called with a `theme` object, the `hue` (e.g. 'primaryHue', 'neutralHue') and the `shade` (e.g. `600`, `800`).
- The function returns the resolved color value, which can be used as a CSS property.
- **Best Practice:**
- Prefer using semantic `hue` names (like `primaryHue`, `neutralHue`) instead of hardcoding color values. This allows you to change your theme colors in one place.
2. Using Theme Variables:
Concept: Retrieving colors based on theme variables (defined in theme.colors.variables). These are helpful to represent named color references used across your components.
- Example (styled-components):
- We are using the `getColor` function inside a styled-component.
- The `variable` parameter specifies the path to the variable in the theme (e.g., 'background.default').
- The `theme` object is passed via props.
- **Best Practice:**
- Use variables for colors that represent common UI elements (e.g., background, foreground, etc).
- You can compose variables, referencing palette hues or other variables:
```ts
// in your theme
colors: {
variables: {
light: {
'myComponent.background': 'primaryHue.100',
'myComponent.text': 'neutralHue.800'
}
dark: {
'myComponent.background': 'primaryHue.800',
'myComponent.text': 'neutralHue.100'
}
}
}
```
3. Adjusting Shades with shade and offset:
Concept: Modifying a hue's base shade using the shade and offset parameters.
- Example (JavaScript):
dark and light properties define an object that allows you to pass different values for hue, shade, offset, and transparency that are specific to each color scheme.
getColor automatically picks the dark or light color values based on the base property inside the theme (e.g. theme.colors.base === 'dark' or 'light').
Best Practice:
Use dark and light to provide different color values based on the theme's base color scheme.
Keep light and dark mode values consistent and logical for readability and accessibility.
5. Transparency:
Concept: Applying transparency to colors.
Example (styled-components):
jsx
import styled, { useTheme } from 'styled-components';
import { getColor, IGardenTheme } from '@zendeskgarden/react-theming';
const StyledDiv = styled.div`
background-color: ${props =>
getColor({ theme: props.theme as IGardenTheme, hue: 'primaryHue', shade: 600, transparency: 0.5 })};
padding: 10px;
`;
const MyComponent = () => {
const theme = useTheme() as IGardenTheme;
return <StyledDiv theme={theme}>
I have transparency!
</StyledDiv>
}
code
- **Explanation:**
code
- The `transparency` value can be a number between 0 and 1 (representing an alpha value), or a key within the theme's `opacity` object (e.g., `transparency: 'semiOpaque'`).
- **Best Practice:**
- Use transparency to create depth, subtle overlays, or disabled states.
- For better readability, avoid using transparency on text.
getColor Function Signature
typescript
getColor({
theme, // IGardenTheme
variable?, // string
dark?, // { hue?: string, shade?: number, offset?: number, transparency?: number }
light?, // { hue?: string, shade?: number, offset?: number, transparency?: number }
hue?, // string
shade?, // number
offset?, // number
transparency? // number | string
}: ColorParameters): string;
Where:
theme: The theme object
variable: A string representing a theme variable path.
dark: Specific color values for dark mode.
light: Specific color values for light mode.
hue: A named hue (string) from the theme palette or color keys (e.g. primaryHue).
shade: The base shade for the hue.
offset: A value to adjust the shade.
transparency: An alpha value (0-1) or a key in theme.opacity.
Best Practices Summary
Use Semantic Names: Prefer named hues and variables rather than hardcoding color hex values.
Theme Variables: Organize your color palette using variables for reuse and maintainability.
Dark and Light Mode: Provide distinct values for dark and light modes using the dark and light properties.
Transparency: Use transparency for layering, subtle effects, or disabled states, always considering readability.
Consistency: Maintain consistency in your usage of hues, shades and transparency throughout your application.
Let me know if you'd like more examples or have further questions about the getColor utility!