JSS Plugins
September 18, 2021 ยท View on GitHub
Plugins API allows modifying style sheets at different stages. A plugin can, for example, add new style properties, modify values or add new rules.
Order does matter
Plugins application happens in the same order as they are registered. In case you use any of the following plugins please bear in mind that they should be registered in this order:
- jss-plugin-rule-value-function - enables functions for dynamic styles.
- jss-plugin-rule-value-observable - enables TC39 Observables.
- jss-plugin-template - enables string templates.
- jss-plugin-cache - caches the rules by reference for performance.
- jss-plugin-global - enables global styles.
- jss-plugin-extend - enables extending rules at compile time.
- jss-plugin-nested - enables nesting selectors and pseudo selectors.
- jss-plugin-compose - enables composing classes.
- jss-plugin-camel-case - enables camel case syntax for properties.
- jss-plugin-default-unit - adds default units to numeric values.
- jss-plugin-expand - enables better syntax for complex properties and values.
- jss-plugin-vendor-prefixer - adds vendor prefixes in the browser runtime (not for SSR).
- jss-plugin-props-sort - ensures alphabetical props order.
- jss-plugin-isolate - enables rules isolation through automatic properties reset.
jss-preset-default a preset that allows you to set up a predefined list of most useful plugins with one function call.
Authoring plugins
jss.use(plugin)
You need to register a plugin only once per JSS instance. There are some hooks available. The same plugin may implement multiple hooks.
-
Hook
onCreateRule(name, decl, options).Invocation happens when a rule is about to be created. If this object returns an object, it is supposed to be a rule instance. If the returned value is empty, JSS will fall back to a regular rule.
import jss from 'jss' jss.use({ onCreateRule: (name, decl, options) => null }) -
Hook
onProcessRule(rule, sheet).Invocation happens when rule instance is available with the rule as an argument.
import jss from 'jss' jss.use({ onProcessRule: (rule, sheet) => { // Do something here. } }) -
Hook
onProcessStyle(style, rule, sheet).Invocation happens after creation and processing of the rule instance. It should be used to make
styleobject transformations. For performance reasons, you are allowed to mutate thestyleobject itself, though NOT the nested objects. It is limited to the first level because thestyleobject is shallow-cloned in the core, but the nested objects have to be cloned by plugins if they need to mutate it. Usejss.cloneStyle()utility to clone style object. The returned object from the hook will replacerule.style.import jss from 'jss' jss.use({ onProcessStyle: (style, rule, sheet) => style }) -
Hook
onProcessSheet(sheet).This hook is invoked on every created
StyleSheetafter all rules are processed, with thesheetas an argument.import jss from 'jss' jss.use({ onProcessSheet: (sheet) => { // Do something here. } }) -
Hook
onChangeValue(value, prop, rule).Invocation happens after
rule.prop(prop, value)is called as a setter (with a value). Methodsheet.update()usesrule.prop()internally. The returned value will be set on the style object and the CSSOM CSSRule object if the sheet is linked. If multiple plugins implement this hook, return value from the first one will be passed to the second one and so on, like a chain ofmapfunctions.import jss from 'jss' jss.use({ onChangeValue: (value, prop, rule) => value }) -
Hook
onUpdate(data, rule, sheet).This hook is invoked on every created rule when
sheet.update(data)is called, with the passed data as an argument. It allows you to transform style object after every update of dynamic values or dynamic style objects.import jss from 'jss' jss.use({ onUpdate: (data, rule, sheet) => { // Do something here. } })