Get CSS from theme utils
A group of functions used to retrieve token values from theme or component props.
The following functions getBorderCssFromTheme
, getColorCssFromTheme
, getMotionCssFromTheme
, getOverlayCssFromTheme
,
getShadowCssFromTheme
, getSizingCssFromTheme
, getSpacingCssFromTheme
, getTransitionPresetFromTheme
share the same annotation, therefore we will generalize them as the getter function
.
Also, since those functions are used in the same way, we will use only getColorCssFromTheme
to build our Syntax and Example sections.
Overview
Tokens are the foundation of every design system. The visual representation of every component is formed by combining multiple tokens into presets and group of presets later stored into component defaults object. The provided functions, specified above, are used to retrieve the token values from the correspodning getter sections.
Parameters
Syntax
1(string, string) -> props -> CSSObject
1getColorCssFromTheme(cssProp, defaultToken?)(props)
Return value
Returns a CSS object if valid attributes are received. Returns an empty Object if invalid attributes or no attributes are provided.
Examples
This is the most basic example. Here Paragraph
component will include the color value for token: blue020
found in the theme.
The function will accept non token values
You can pass a callback instead of a CSS property to generate a custom CSS Object
More utility getters
The following section details additional theme utility getter functions.
getMediaQueryFromTheme()
Contructs a media query from the provided breakpoint keys. Breakpoints are specified in the theme, there are 5: xs, sm, md, lg, xl.
Syntax
1getMediaQueryFromTheme(minWidth, maxWidth)(props)
Parameters
Return value
Returns a media query line, to be used before an opening brace.
Returns an empty screen query (@media screen
) if no min or max width tokens are provided.
Example
In this example the Paragraph
component will use the min-width breakpoint value from sm
.
In this example the Paragraph
component will use the min-width breakpoint value from sm
and the max-width value from md
.
getDeviceQueryFromTheme()
Target a specific device with media query and apply custom styles to it with either a CSSObject
or a SerializedStyles
string.
Syntax
1getDeviceQueryFromTheme(Array<Devices>, CSSObject | SerializedStyles)()
Parameters
CSSObject
orSerializedStyles
;Return value
Returns css rules wrapped inside the specific media queries.
Example
With CSSObject
With SerializedStyles
In this example the Paragraph
component will be hidden from all models of iPad.
getStylePresetFromTheme()
Retrieve a style preset values as a css template from the current theme. For more information on what consists of style preset see doc here.
Syntax
1getStylePresetFromTheme(defaultToken, customProp, options)(props)
Parameters
Return value
Returns a template string with style preset css styles.
Returns an empty string if invalid attributes or no attributes are provided.
Example
In this example the Paragraph
component will use the sizing value
1<Paragraph stylePreset="linkInline" />
In this example the Paragraph
component will use linkInline
style preset set in the default theme.
1<Paragraph stylePreset="linkInline" loading={true} />
In this example the component has a loading state passed down from the parent component.
If so the loading
style presets, if present, will override the base
style presets.
1<Paragraph stylePreset="linkInline" isSelected={true} />
In this example the component has a current state passed down from the parent component.
If so the selected
style presets, if present, will override the base
style presets.
1<Paragraph stylePreset="linkInline" omitStates={["disabled"]} />
In this example the component is using the linkInline
style preset, without using disabled
state style presets.
1<Paragraph stylePreset="linkInline" omitStyles={["backgroundColor"]} />
In this example the component is using the linkInline
style preset, while ignorning all backgroundColor
across all style presets.
getTypographyPresetFromTheme()
Retrieve a typography preset from the current theme. A typography preset is a collection of typography related styles.
Syntax
1getTypographyPresetFromTheme(defaultToken, customProp, options)(props)
Parameters
Return value
Returns a typography preset if valid attributes are received.
Returns an empty string if invalid attributes or no attributes are provided.
Example
In this example the Paragraph
component will include the typography styles from editorialParagraph030
found in the theme if the Paragraph
is used with no props:
1<Paragraph />
However, it will include styles from editorialSubheadline050
if the Paragraph
is used with the font
prop and the value passed in is editorialSubheadline050
:
1<Paragraph typographyPreset="editorialSubheadline050" />
getTransitionPresetFromTheme()
Retrieves a transition preset from the current theme.
Syntax
1getTransitionPresetFromTheme(token, componentClassName)
Parameters
Example
In the example above backgroundColorChange
is the transition preset that will be applied.
When combined with the following stylePreset
the paragraphs background will transition between states with different duration.