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
- Name8.42.0•View source•View on npm
- Installnpm install @zendeskgarden/react-theming
- Depsnpm install react react-dom styled-components
- Importimport { arrowStyles, getColor, getLineHeight, mediaQuery, menuStyles, retrieveComponentStyles, withTheme } 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.animationModifier] | string | – | Indicates a CSS class or attribute selector which, when applied to the base element, animates the arrow's appearance |
getColor
Get a color for the given hue, shade, and theme. The algorithm returns a valid color even if the particular shade doesn’t exist in the current theme.
Parameter | Type | Default | Description |
---|---|---|---|
hue | Object | string | Required | Provides a theme object palette hue or color key, or any valid CSS color notation |
shade | number | 600 | Selects a shade for the given hue |
theme | DefaultTheme | DEFAULT_THEME | Provides values used to resolve the desired color |
transparency | number | – | Sets an alpha channel between 0 and 1 |
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'between' is the specified query |
theme | DefaultTheme | DEFAULT_THEME | 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 (e.g. via Popper) 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 | DEFAULT_THEME | 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 | – | Specifies the z-index for the absolutely positioned menu wrapper |
[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 theme object |
withTheme
Use this component factory to pass the context theme object to any React
component. The React useContext
hook
may be used as a favorable alternative.
Parameter | Type | Default | Description |
---|---|---|---|
Component | Object | Required | Gets wrapped by a higher-order component with an added theme prop |