Plugins API allows to modify sheets and rules at different stages. A plugin can for e.g. add new style properties, modify values or even add new rules.
A number of plugins do exist already. We are happy to add more.
The order in which plugins are registered matters since they will be applied sequentially.
In case you use any of the following plugins please bear in mind that they should be registered in this order:
- jss-template - JSS plugin enables string templates
- jss-cache - JSS plugin that caches the rules and delivers insane performance.
- jss-global - JSS plugin, allows global styles.
- jss-extend - JSS plugin, allows to extend rules at compile time.
- jss-nested - JSS plugin, allows nested selectors and pseudo selectors.
- jss-compose - JSS plugin, allows classes composition.
- jss-camel-case - JSS plugin, allows to write styles in camel case.
- jss-default-unit - JSS plugin, adds default units to numeric values.
- jss-expand - JSS plugin, gives you a better syntax than CSS itself.
- jss-vendor-prefixer - JSS plugin, adds vendor prefixes at runtime, in the browser (not for SSR)
- jss-props-sort - JSS plugin, ensures alphabetical props order.
- jss-isolate - JSS plugin, gives you rules isolation through automatic properties reset.
To make your life easier, we made jss-default-preset which is a ready to use and ordered preset of plugins.
jss.use(plugin)
You need to register a plugin
only once per JSS instance. There is a number of hooks available. Multiple hooks may be implemented by the same plugin.
-
Hook
onCreateRule(name, decl, options)
.This hook is invoked when a rule is about to be created. If this object returns an object, it is supposed to be a rule instance. If an empty value is returned, JSS will fall back to a regular rule.
jss.use({ onCreateRule: (name, decl, options) => { // Do something here. return newRule } })
-
Hook
onProcessRule(rule, sheet)
.This hook is invoked on every created rule with the rule as an argument.
jss.use({ onProcessRule: (rule, sheet) => { // Do something here. } })
-
Hook
onProcessStyle(style, rule, sheet)
.This hook is invoked on every created rule with
style
as a primary argument. It is designed forstyle
object transformations and rule manipulations. For performance reasons, you are allowed to mutate thestyle
object itself, though NOT the nested objects. It is limited to the first level because thestyle
object 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 for style cloning. The returned object from the hook will replacerule.style
.jss.use({ onProcessStyle: (style, rule, sheet) => { // Do something here. return style } })
-
Hook
onProcessSheet(sheet)
.This hook is invoked on every created
StyleSheet
after all rules are processed, with thesheet
as an argument.jss.use({ onProcessSheet: sheet => { // Do something here. } })
-
Hook
onChangeValue(value, prop, rule)
.This hook is invoked when
rule.prop(prop, value)
is called as a setter (with a value). Methodsheet.update()
usesrule.prop()
internally. If this hook is implemented by a plugin, the returned value will be set on the style object and on 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 ofmap
functions.jss.use({ onChangeValue: (value, prop, rule) => { // Do something here. return newValue } })
-
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.jss.use({ onUpdate: (data, rule, sheet) => { // Do something here. } })