Transitions
Barba allows you to define a global transition, that run everywhere on your site, and/or specific transitions using rules that run when navigating from one page to another.
They use all base hooks
.
Summary
Syntax
1 | barba.init({ |
transition
name
is optional, but recommended.
Rules
Rules define the “transition resolution”, concretely “selecting the right transition to use”.
This allows you to create specific transitions, based on namespace, route or custom logic.
They are made of keyword(s) and condition(s).
Keyword(s)
Rules keyword(s) define the logic to follow, if your transition should play when leaving the current.container
, using from
, and/or when entering the next.container
, using to
:
1 | barba.init({ |
They must contain a set of condition(s), and run within a priority order:
Priority | Keyword | Rule apply to… | Rule apply when… |
---|---|---|---|
1 | from AND to |
current AND next containers |
Condition(s) of from AND to are fulfilled |
2 | to |
next container |
Condition(s) of to is/are fulfilled |
3 | from |
current container |
Condition(s) of from is/are fulfilled |
Note that you can use
from
andto
properties independently.
Condition(s)
Rules condition(s) define the logic to fulfilled inside keyword(s) in order to play the transition.
They are defined within keyword(s):
1 | barba.init({ |
Condition(s) can be applied to a namespace, route or through a custom function.
Barba will then select the right transition to use and play it only if all conditions are fulfilled.
Priority | Condition | Type | Argument | Fulfilled when… |
---|---|---|---|---|
1 | custom |
Function | data argument | true is returned |
2 | route |
String | String[] | none | current.route.name matched |
3 | namespace |
String | String[] | none | current.namespace matched |
Any conditions can be used within
from
and/orto
keywords.
Priority
The “transition resolution” follows this steps:
- Condition(s) priority
- Keyword(s) priority
- Declaration order
The common/default behaviour is to start leave as soon as possible.
Then enter will be called when leave ends AND next page is “available”: fetched or cached.If you use some
to
logic, Barba can not predict and select the right transition until the next page is available. This also applies when using the sync mode, but this does not apply ifto
is used withroute
property because “next route” is known when the click occurs.Bear with this!
Multiple rules
You can combine multiple rules on each transition.
Here is an example of what you could do with multiple rules:
1 | barba.init({ |
Based on the priority order, Barba will play the custom-transition
:
- if the link you clicked on contains a
.use-custom-transition
CSS class - if you come from the
index
orproduct
route - if you are navigating to the
home
oritem
namespace AND from theindex
orproduct
route
Self
Barba have an internal mechanism called “self transition”: it allows you to call a transition when navigating into the same page. Just define a transition with the “magic” name self
, then links that point to your current page, like page.html#hash
, will call this specific transition.
1 | transitions: [{ |
Like a regular transition, this one can use all base hooks.
Sync mode
A mode that indicates whether leave and enter hooks should “play together”.
The sync mode is disabled by default, but you can easily enable it using the sync
option:
1 | barba.init({ |
This involves waiting until the next page is available: fetched or cached.
In another words, regarding the legacy example, if you have an opacity transition from 1 -> 0
for leave
and 0 -> 1
for enter
:
sync: false
will play theleave
transition first, making your page content goes transparent, then play theenter
transition, making the next page content goes opaque (two step transition)
This will immediately run the
before
,beforeLeave
andleave
hooks on user click, then it will wait for the next page to be “ready” (prefetched) in order to run theenter
transition with the other hooks.
sync: true
will make the current page goes transparent while the next page becomes opaque at the same time (crossfade transition)
This will do nothing on user click, even if doing something inside
before
orbeforeLeave
hooks, this mode will always wait for the next page to be “ready” (prefetched) in order to run bothleave
/enter
transitions.