Skip to content

Functions

The Magnesium functions help you to easily manage theme styles from user-provided theme's tokens map.

emit-variable($theme, $token, $fallback, $prefix)

Emits CSS variable declaration from a user-provided theme's.

Parameters

ParameterDescriptionDefault
$themeThe reference theme's tokens map.undefined
$tokenThe theme token key.undefined
$fallbackAllow to display the CSS variable fallback.false
$prefixToken's prefix name to prepend for each token's custom property name.null

Usage

scss
@use "@magnesium/theme";

$theme: (
    "text-color": darkcyan
);

.foo {
    color: theme.emit-variable($theme, "text-color", false, "button");
}

Returns

scss
.foo {
    color: var(--mg-button-text-color);
}

// ...or with fallback option at `true`...

.foo {
    color: var(--mg-button-text-color, darkcyan);
}

create-theme-vars($theme, $prefix)

Transforms a user-provided theme's tokens map values into a var() custom properties.

Parameters

OptionDescriptionDefault
$themeThe user-provided theme tokens map.undefined
$prefixComponent's name to prepend for each token's custom property name. Set to false for disabled.undefined

Usage

scss
@use "@magnesium/theme";

$tokens: (
    "text-color": darkcyanz
);

$theme: theme.create-theme-vars($tokens, "button");

Returns

scss
$theme: (
    "text-color": var(--mg-button-text-color, darkcyan)
);

validation($reference, $tokens)

Validates a user-provided theme's token and throws an error if tokens are invalid. Return error if token key doesn't exist or the map.

Parameters

ParameterDescriptionDefault
$referenceThe reference theme's tokens map.undefined
$tokensThe user-provided theme's tokens map.undefined

Usage

scss
@use "@magnesium/theme";

$reference: (
    "text-color": darkcyan
);

$tokens: (
    "text-color": darkorange
);

$theme: theme.validation($reference, $tokens);
// Will return map is `true` or error message if `false`.

Released under the MIT License.