Utilities
Utility functions enable advanced theme usage. They let you customize components in a granular way.
How to use them
Menu and arrow styles
Use menu and arrow styling utilities to build Garden-styled menu components.
Configuration
- Name9.1.0•View source•View on npm
- Installnpm install @zendeskgarden/react-theming
- Depsnpm install react react-dom styled-components
- Importimport { arrowStyles, focusStyles, getCheckeredBackground, getColor, getColorV8, getFocusBoxShadow, getLineHeight, mediaQuery, menuStyles, retrieveComponentStyles, useText } from '@zendeskgarden/react-theming'
API
arrowStyles
CSS for an arrow at the given position and with the given size. The arrow
inherits the base element’s border
, background
, and box-shadow
. For
proper border
and box-shadow
styling, the element to which arrow styles
are applied must be wrapped in a positioned box (relative
, absolute
, or
fixed
) that has a z-index
greater than or equal to zero.
Parameter | Type | Default | Description |
---|---|---|---|
position | 'top' | 'top-left' | 'top-right' | 'right' | 'right-top' | 'right-bottom' | 'bottom' | 'bottom-left' | 'bottom-right' | 'left' | 'left-top' | 'left-bottom' | Required | Positions the arrow against the base element |
[options.size] | string | '6px' | Expresses a CSS dimension as a distance from the base (hypotenuse) to point (right angle) of the arrow |
[options.inset] | string | '0' | Tweaks arrow positioning by adjusting with a positive (in) or negative (out) CSS dimension |
[options.shift] | string | '0' | Shifts arrow positioning along the edge of the base element |
[options.animationModifier] | string | Indicates a CSS class or attribute selector which, when applied to the base element, animates the arrow's appearance |
focusStyles
CSS for Garden standard box-shadow
focus styling. The hue and shade are used
to determine the color of the focus ring.
Parameter | Type | Default | Description |
---|---|---|---|
[options.theme] | IGardenTheme | Required | Provides values used to resolve the desired colors |
[options.color] | Object | { variable: 'border.primaryEmphasis' } | Provides an object with getColor parameters used to determine the focus ring color |
[options.shadowWidth] | string | 'md' | Provides a theme object shadowWidth key for the cumulative width of the |
[options.spacerColor] | Object | { variable: 'background.default' } | Provides an object with getColor parameters used to determine the spacer color |
[options.spacerWidth] | string | 'xs' | Provides a theme object shadowWidth for the white spacer, or |
[options.inset] | boolean | Determines whether the | |
[options.selector] | string | &:focus-visible | Provides a subsitute pseudo-class CSS selector |
[options.styles] | CSSObject | Adds CSS property values to be rendered on focus | |
[options.condition] | boolean | true | Supplies an optional condition that can be used to prevent the focus |
getCheckeredBackground
Generate CSS for a checkered background image.
Parameter | Type | Default | Description |
---|---|---|---|
theme | IGardenTheme | Required | Provides information for pattern color and LTR/RTL layout |
size | number | Required | Provides the pixel size of the checkered pattern |
overlay | string | Specifies color with transparency or | |
positionY | number | 0 | Adjusts vertical position for starting the pattern |
repeat | 'repeat' | 'repeat-x' | 'repeat' | Sets a repeat value for the pattern; either |
getColor
Get a color value from the theme. Variable lookup takes precedence, followed by
dark
and light
object values. If none of these are provided, hue
, shade
,
offset
, and transparency
are used as fallbacks to determine the color. A
particularly effective use of getColor
might look something like the following
example.
import styled from 'styled-components';
import { getColor } from '@zendeskgarden/react-theming';
const options = { variable: 'background.primaryEmphasis' };
const offset100 = { dark: { offset: -100 }, light: { offset: 100 } };
const offset200 = { dark: { offset: -200 }, light: { offset: 200 } };
export const StyledCustomComponent = styled.div`
background-color: ${props => getColor({ theme: props.theme, ...options })};
:hover {
background-color: ${props => getColor({ theme: props.theme, ...options, ...offset100 })};
}
:active {
background-color: ${props => getColor({ theme: props.theme, ...options, ...offset200 })};
}
`;
Parameter | Type | Default | Description |
---|---|---|---|
[options.theme] | IGardenTheme | Required | Provides values used to resolve the desired color |
[options.variable] | string | Specifies a variable key ( | |
[options.hue] | string | Provides a theme object palette hue or color key, or any valid CSS color notation | |
[options.shade] | number | 700 (light) | 500 (dark) | Selects a shade for the given hue |
[options.offset] | number | Adjusts the shade by a positive or negative value. Works best used along with a | |
[options.transparency] | number | Sets transparency using a theme opacity key or an alpha channel between 0 and 1 | |
[options.dark] | Object | Supplies an object with optional | |
[options.light] | Object | Supplies an object with optional |
getFocusBoxShadow
Get a CSS box-shadow
property value for focus state styling. Use this function
when focusStyles is not the right fit.
Parameter | Type | Default | Description |
---|---|---|---|
[options.theme] | IGardenTheme | Provides values used to resolve the desired colors | |
[options.color] | Object | { variable: 'border.primaryEmphasis' } | Provides an object with getColor parameters used to determine the focus ring color |
[options.shadowWidth] | string | 'md' | Provides a theme object shadowWidth key for the cumulative width of the |
[options.spacerColor] | Object | { variable: 'background.default' } | Provides an object with getColor parameters used to determine the spacer color |
[options.spacerWidth] | string | 'xs' | Provides a theme object shadowWidth for the white spacer, or |
[options.inset] | boolean | Determines whether the | |
[options.boxShadow] | string | Provides an existing |
getLineHeight
Derive a unitless line height based on the given pixel-valued height and font size.
Parameter | Type | Default | Description |
---|---|---|---|
height | string | number | Required | Specifies the desired line height in pixels |
fontSize | string | number | Required | Specifies the font size (in pixels) of text contained within the line |
mediaQuery
Get a CSS media query string for the given query specifier, breakpoint name, and theme. Use this function to build responsive UI that works well with Garden’s themed Grid.
Parameter | Type | Default | Description |
---|---|---|---|
query | 'up' | 'down' | 'only' | 'between' | Required | Specifies the query, one of:
|
breakpoint | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | Array | Required | Specifies a theme object breakpoints key, or an array of two keys when |
theme | DefaultTheme | Provides values used to resolve the specified breakpoint |
menuStyles
CSS for a menu at the given position. Apply these styles to an absolutely positioned wrapper (for example via Floating UI) which contains a child menu component.
Parameter | Type | Default | Description |
---|---|---|---|
position | 'top' | 'right' | 'bottom' | 'left' | Required | Specifies the menu position relative to the component that triggers menu expansion |
[options.theme] | DefaultTheme | Provides theming values used to style the menu component | |
[options.hidden] | boolean | Determines whether the menu is hidden | |
[options.margin] | string | Determines the amount of space, as a CSS dimension, between the menu and its trigger | |
[options.zIndex] | number | 0 | Specifies the |
[options.childSelector] | string | '> *' | Indicates a CSS selector which, when applied to the wrapper, identifies the child menu component |
[options.animationModifier] | string | Indicates a CSS class or attribute selector which, when applied to the wrapper, animates the menu's appearance |
retrieveComponentStyles
Retrieve customized CSS for themable component styling overrides. This example demonstrates how to construct a custom styled component that permits customizations via the theme’s components object.
import styled from 'styled-components';
import { retrieveComponentStyles } from '@zendeskgarden/react-theming';
const COMPONENT_ID = 'custom.component';
export const StyledCustomComponent = styled.div.attrs({
/* Render component ID attribute for reference */
'data-custom-id': COMPONENT_ID
})`
display: flex;
align-items: center;
/* CSS provided on the theme will be rendered inline */
${props => retrieveComponentStyles(COMPONENT_ID, props)};
`;
Parameter | Type | Default | Description |
---|---|---|---|
id | string | Required | Specifies the unique ID used to retrieve CSS styles from the theme's components object |
props | Partial<ThemeProps<Partial<DefaultTheme>>> | Required | Provides component props which contain the context |
useText
A custom React hook that provides default text for aria-label
or other
critical attribute strings. If necessary, a development mode console warning
prompts the consumer to provide customized, translated text.
Parameter | Type | Default | Description |
---|---|---|---|
component | string | Required | Specifies the React component to which the |
props | Record<string, any> | Required | Provides component props to check for |
name | string | Required | Determines the name of the component prop to set default text on |
text | string | Required | Specifies default text to apply if the value of |
condition | boolean | Supplies an optional condition that can be used to prevent evaluation |