Newskit Logo
About

Get CSS from theme utils

A group of functions used to retrieve token values from theme or component props. The following functions getColorCssFromTheme, getSizingCssFromTheme, getBorderCssFromTheme, getShadowFromTheme, getCssMotionFromTheme 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 getCssColorFromTheme 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

cssPropertystring | FromThemeCallback
It is a valid CSS property or callback function which will take CSS property as argument
defaultTokenMQ<string>
The getter function will use this as a default token name to retrieve its value from the corresponding theme section.
propsProps
The getter functions are curried and the function returned takes the props of the component (including the theme). If the function is used inside a styled component template literal, or a CSS block, Emotion.js will invoke the curried function, passing the component props.

Syntax

(string, string) -> props -> CSSObject
getColorCssFromTheme(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

getMediaQueryFromTheme(minWidth, maxWidth)(props)

Parameters

minWidthBreakpointKeys
The min-width for the media query. Can be left falsy if only a max-width is to be used.
maxWidthBreakpointKeys
The max-width for the media query. Can be left falsy if only a min-width is to be used.
propsProps
Is the props of the component including the theme. If the function is used inside a styled component template literal, or a css block, Emotion.js will call the function first with the component props passed in.

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

getDeviceQueryFromTheme(Array<Devices>, CSSObject | SerializedStyles)()

Parameters

targetDevicesArray<Devices>
An array of the devices to be targeted.
cssRulesCSSObject | SerializedStyles
A list of CSS rules that can be passed as eitherCSSObject 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

getStylePresetFromTheme(defaultToken, customProp, options)(props)

Parameters

defaultTokenstring
Is the name of the style preset token to be retrieved. If provided, it is used as an object key on the style presets section of the theme to get the token value.
customPropstring
Is the name of the component prop in which a style preset token name can be provided by the end-user of the component. If provided, the style preset to be retrieved will come from the component prop.
optionsGetStylePresetFromThemeOptions
This prop consists of loading, selected, omitStates and omitStyles options. These are component specific behaviours that can be controlled via either state or passed down from parent as prop.
propsProps
The getter functions are curried and the function returned takes the props of the component (including the theme). If the function is used inside a styled component template literal, or a CSS block, Emotion.js will invoke the curried function, passing the component props.

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

<Paragraph stylePreset="linkInline" />

In this example the Paragraph component will use linkInline style preset set in the default theme.

<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.

<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.

<Paragraph stylePreset="linkInline" omitStates={["disabled"]} />

In this example the component is using the linkInline style preset, without using disabled state style presets.

<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

getTypographyPresetFromTheme(defaultToken, customProp, options)(props)

Parameters

defaultTokenThemeToken
Is the name of the typography preset to be retrieved. If provided, it is used as an object key on the typography preset theme section to get the token value.
customPropExclude<keyof Props, 'theme'>
Is the name of the component prop in which a typography preset name can be provided by the end-user of the component. If provided, the typography preset to be retrieved will come from the component prop.
options{withCrop: boolean}
When true, will apply the crop options retrieved from the preset.
propsProps
Is the props of the component including the theme. If the function is used inside a styled component template literal, or a css block, Emotion.js will call the function first with the component props passed in.

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:

<Paragraph />

However, it will include styles from editorialSubheadline050 if the Paragraph is used with the font prop and the value passed in is editorialSubheadline050:

<Paragraph typographyPreset="editorialSubheadline050" />