Utilities
Utilities in Jux provide a way to create custom CSS properties that can be used in your styled components. These utilities allow you to define reusable, complex style transformations that can significantly simplify your styling code and enhance its readability.
Jux comes with a set of built-in utilities that you can use out of the box. However, you can also define your own custom utilities to suit your specific needs.
What are Utilities?
Utilities are custom functions that transform a given value into one or more CSS properties. They are defined in your jux.config
file and can be used in your styled and css function calls.
Each utility can be configured using the following properties:
acceptedValues
: The values that the utility accepts. This is used for type definition generation and intellisense. This can be a string representing token type or an array of strings representing specific tokens.transform
: A function that transforms the given value into one or more CSS properties.
Using Utilities
Once defined, you can use your custom utilities in your styled components or css function calls:
The transform
function
The transform function in a utility is the core of its functionality. It receives two parameters:
value
: The resolved value of the utility.args
: An object containing additional information and utilities.
value parameter
The value parameter contains the resolved value of the utility. If a token is used, this will be the CSS variable representation of that token. For example:
-
Direct value:
-
Single token value:
-
Multiple token values:
-
Combination of tokens and direct values:
-
For composite tokens that are not scoped to themes (core tokens), the value will be an object:
-
For composite tokens that are scoped to themes, the value will be an array of objects, one for each theme:
Additional helpers
The second parameter to the transform
function is an object that provides access to the token system and raw input. It contains:
tokensManager
: A class representing the available tokens in the system. This can be used to interact with or query the token system.tokens
: An array ofTokenParser
objects representing the tokens in question. This is populated when a token value is provided. For themed tokens, this array will contain multiple TokenParser objects, one for each theme defined in the system.rawValue
: The original, unresolved value provided to the utility.
Here’s an example of a utility that uses this to access the token system:
Built-in Utilities
TODO