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.
Based on the repository content, I'll explain how to use the getColor utility in Garden's React components. Here are the different approaches with examples:
Basic Usage with Theme Variables
jsx
import styled from 'styled-components';
import { getColor } from '@zendeskgarden/react-theming';
const StyledComponent = styled.div`
// Using a theme variable is the safest approach since it handles both light/dark modes
color: ${props => getColor('primaryHue', props.theme)};
background-color: ${props => getColor('background.default', props.theme)};
`;
Using Hue with Optional Parameters
jsx
import styled from 'styled-components';
import { getColor } from '@zendeskgarden/react-theming';
const StyledComponent = styled.div`
// Using hue with shade and transparency
color: ${props => getColor({
theme: props.theme,
hue: 'blue',
shade: 600, // Controls darkness/lightness of the hue
transparency: 0.5 // Adds transparency
})};
`;
import styled from 'styled-components';
import { getColor } from '@zendeskgarden/react-theming';
const StyledComponent = styled.div`
// Using variable with offset for subtle variations
background-color: ${props => getColor({
theme: props.theme,
variable: 'background.default',
offset: 1 // Lightens or darkens relative to theme mode
})};
`;
Best Practices:
Always pass the theme prop from styled-components rather than using DEFAULT_THEME
Prefer using theme variables over direct hue values when possible
Use the light/dark props when you need different colors based on theme mode
Use offset for subtle variations rather than hard-coding different shades
Consider transparency when working with overlays or hover states
Example Component Implementation:
jsx
import React from 'react';
import styled from 'styled-components';
import { ThemeProvider } from '@zendeskgarden/react-theming';
import { getColor } from '@zendeskgarden/react-theming';
const StyledButton = styled.button`
// Primary color with different shades for light/dark
background-color: ${props => getColor({
theme: props.theme,
light: { hue: 'blue', shade: 600 },
dark: { hue: 'blue', shade: 300 }
})};
// Using variable with offset for hover state
&:hover {
background-color: ${props => getColor({
theme: props.theme,
variable: 'primaryHue',
offset: 1
})};
}
// Text color using theme variable
color: ${props => getColor('text.primary', props.theme)};
`;
const Button = (props) => (
<StyledButton {...props}>
{props.children}
</StyledButton>
);
// Usage
const App = () => (
<ThemeProvider>
<Button>Click Me</Button>
</ThemeProvider>
);
Based on the repository content, this covers the main ways to use getColor. Would you like me to elaborate on any particular aspect or provide additional examples for specific use cases?