List with details and code examples written in javascript, react, and styled-component, all the ways to use the getColor utility. Include best practices. Because the theme is dynamic and its color base light or 'dark` can change, discourage use of DEFAULT_THEME in getColor. Show examples encouraging variables, including cases where the value from the variable has a different offset in light and dark mode. Double check your work. Ask questions if unsure.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, I understand. I will provide a detailed explanation of how to use the getColor utility from @zendeskgarden/react-theming, including best practices, and avoiding the use of DEFAULT_THEME. I'll also demonstrate how to handle different offsets in light and dark modes and encourage the use of theme variables.
Here's the breakdown:
Understanding getColor
The getColor utility is designed to retrieve color values from a theme object, considering different scenarios like light/dark mode, color hues, shades, and offsets. It's crucial for creating themable components that adapt to user preferences or brand guidelines.
Best Practices
Avoid DEFAULT_THEME: Directly using DEFAULT_THEME is discouraged because it hardcodes the theme and doesn't allow for dynamic theme changes (like switching between light and dark mode). Instead, always rely on the theme provided by ThemeProvider via the useTheme hook in your components.
Prefer Theme Variables: Utilize theme variables for color lookups. This approach makes your components more maintainable and allows for easy adjustments to the overall theme without modifying individual components. Variables also support different color values and offsets in light and dark themes.
Handle Light/Dark Mode:getColor intelligently adjusts based on the theme.colors.base value (light or dark). Design your theme variables accordingly, with different offsets based on light and dark modes.
Error Handling:getColor may throw an error if a color can't be resolved. It is a good practice to handle these errors.
Transparency: The transparency prop can be used to retrieve translucent colors.
Code Examples
Here's how to use getColor in various scenarios:
jsx
import React from 'react';
import styled, { useTheme } from 'styled-components';
import { getColor, getCheckeredBackground, IGardenTheme } from '@zendeskgarden/react-theming';
import { Tag } from '@zendeskgarden/react-tags';
const StyledDiv = styled.div.attrs<{ background: string }>(p => ({
style: { background: p.background }
}))<{ background: string }>`
display: flex;
align-items: center;
justify-content: center;
height: 208px;
`;
interface ColorProps {
variable: string;
theme: IGardenTheme;
offset?: number;
transparency?: number;
}
const Color = ({ variable, theme, offset, transparency }: ColorProps) => {
let background;
let tag;
try {
const backgroundColor = getColor({
theme,
variable,
offset,
transparency
});
background = getCheckeredBackground({
theme,
size: 26,
overlay: backgroundColor
});
tag = (
<Tag hue={getColor({ theme, variable: 'background.default' })} size="large">
{backgroundColor}
</Tag>
);
}
catch (error) {
background = 'transparent';
tag = (
<Tag hue="red" size="large">
{error instanceof Error ? error.message : String(error)}
</Tag>
);
}
return <StyledDiv background={background}>{tag}</StyledDiv>;
};
const MyComponent = () => {
const theme = useTheme();
return (
<>
{/* Example 1: Using a simple background color variable */}
<Color variable="primaryHue" theme={theme} />
{/* Example 2: Using a variable with offset */}
<Color variable="primaryHue" theme={theme} offset={100}/>
{/* Example 3: Using a variable with different offsets in light and dark mode */}
<Color variable="myCustomColor" theme={theme} />
{/* Example 4: Using transparency */}
<Color variable="primaryHue" theme={theme} transparency={0.5} />
</>
);
};
export default MyComponent;
Explanation
Import necessary hooks and functions: We import useTheme from styled-components, getColor and getCheckeredBackground from @zendeskgarden/react-theming.
Styled Component: The StyledDiv component is a simple styled div that uses a dynamic background prop.
Color Component:
It receives the variable, theme, offset, and transparency as props.
It retrieves the background color using getColor with the provided props.
It generates a checkered background to visualize the color, useful for translucent colors
Handles errors using a try/catch block that will render an error tag in case getColor throws an error.
MyComponent Component:
Uses the useTheme() hook to access the current theme.
Demonstrates usage of the Color component with different variables.
Example 3 demonstrates how to handle different color variations depending on the theme base.
Theme Variable Example
Here's how you might define your theme variables to support different offsets in light and dark mode:
typescript
// Example theme augmentation
const myTheme = {
colors: {
myCustomColor: {
light: {
0: 'hsl(200, 80%, 80%)', // base color
100: 'hsl(200, 80%, 70%)', // offset color
},
dark: {
0: 'hsl(200, 20%, 20%)', // base color
100: 'hsl(200, 20%, 30%)', // offset color
},
},
primaryHue: 'hsl(200, 100%, 50%)'
}
}
javascript
// Applying the custom theme using ThemeProvider
import {ThemeProvider} from "@zendeskgarden/react-theming"
import MyComponent from './MyComponent'
function App() {
const theme = {
...DEFAULT_THEME,
...[Redacted Person],
}
return (
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
);
}
export default App;
In this example:
myCustomColor is a variable that has different values for light and dark themes.
The base color is set using the 0 offset.
An offset is used to access a different value for that variable in light and dark mode using the offset prop.
primaryHue is set for any theme base.
Important Considerations:
Theme Structure: Ensure that your theme structure is consistent and that you define variables to use them effectively in getColor.
Accessibility: Always check color contrast ratios to ensure your components are accessible.
Let me know if you have more questions or would like to explore any specific scenarios further.