Plugins
If you find that Unistyles lacks certain features, you can easily extend its functionality with plugins.
Plugins are functions that accept a style object and return a new style object.
They are resolved in the order in which they were passed to the addConfig
function in UnistylesRegistry.
Create a plugin
To create a plugin, you need to import the UnistylesPlugin
type:
Then, create a function that conforms to this type:
Your plugin must have a unique name; otherwise, it will be rejected from the registry.
onParsedStyle
The onParsedStyle
function is called for every style object in the stylesheet passed to the useStyles
hook.
These objects are processed after Unistyles has finished parsing them, so they do not include:
- variants
- breakpoints
- theme
- media queries
For example:
You will get, for instance, the following calls:
Your function will be called twice: once for the container
and once for text
styles.
Argument | Type | Description |
---|---|---|
key | string | Name of the style eg. container or text |
styles | StyleObject | Style object for the corresponding key |
runtime | UnistylesRuntime | Runtime with all required information about your configuration eg. breakpoint, themes etc. |
Register plugins with UnistylesRegistry
Once you have your plugin, you need to register it with UnistylesRegistry
:
Enable plugin at runtime
Unistyles offers the option to register plugins at runtime, allowing you to enable them only for specific components.
Disable plugin at runtime
Similarly, you can disable plugins at runtime:
Get enabled plugins
You can also check which plugins are enabled at runtime: