Auto Guideline Plugin
Scale your UI automatically based on the screen size. Can replace react-native-responsive-screen.
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.
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.
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:
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. |
UnistylesRegistry
Once you have your plugin, you can register it with UnistylesRegistry
:
Unistyles offers the option to register plugins at runtime, allowing you to enable them only for specific components.
Similarly, you can disable plugins at runtime:
You can also check which plugins are enabled at runtime:
Let’s build an example plugin that will add fontFamily
based on the fontWeight
.
We can use the onParsedStyle
function to check if the fontWeight
property is present in the style object.
If so we will add a fontFamily
:
Looking for other inspirations?
Auto Guideline Plugin
Scale your UI automatically based on the screen size. Can replace react-native-responsive-screen.
High Contrast Plugin
Automatically adjust the contrast of your UI based on the theme.