@barba/core
Barba core manages your page transitions with ease.
Syntax
barba.init(<options>)
Option | Type | Default | Description |
---|---|---|---|
transitions |
Array | [] | Array of <Transition> |
views |
Array | [] | Array of <View> |
debug |
Boolean | false | Set logLevel to ‘debug’ |
logLevel |
string | ‘off’ | Log level |
schema |
Object | schemaAttribute |
Data attributes |
cacheIgnore |
Boolean | string | string[] | false | Cache strategy |
prefetchIgnore |
Boolean | string | string[] | true | Prefetch strategy |
prevent |
Function | (optional) | Custom prevent test |
requestError |
Function | (optional) | Custom request error callback |
<transition>
object
name
An optional name for your transition.
Example:
1 | import barba from '@barba/core'; |
Hooks
All hooks are methods and receive the same data
object.
Order | Name | Description |
---|---|---|
1 | beforeAppear |
Before appear transition |
2 | appear |
Current page appear transition |
3 | afterAppear |
Before appear transition |
Order | Name | Description |
---|---|---|
1 | before |
Before everything |
2 | beforeLeave |
Before leave transition |
3 | leave |
Current page leave transition |
4 | afterLeave |
After leave transition |
5 | beforeEnter |
Before enter transition and after adding next container |
6 | enter |
Next page enter transition |
7 | afterEnter |
After enter transition |
8 | after |
After everything |
“Hooks can be run either synchronously or asynchronously using the common
this.async()
style (see run-async) or returning a promise.If you use
sync: true
, as leave and enter will be concurrent, order will differ: all before*, then enter/leave, then all after*.Note that you can define global hooks using
barba.hooks
and apply it to all your transitions.
Example:
1 | import barba from '@barba/core'; |
data
argument
Data argument is an object passed to all transition hooks, view hooks subset and custom rules.
Property | Type | Description |
---|---|---|
data.current |
Object | Current page related |
data.next |
Object | Next page related |
data.trigger |
HTMLElement | Link that triggered the transition |
string ‘popstate’ | Browser back/forward | |
string ‘barba’ | Programmatic navigation |
current/next
properties
Properties attached to data.current
and data.next
objects.
Name | Type | Description |
---|---|---|
container |
HTMLElement | Barba container |
namespace |
string | Barba namespace |
url |
Object | URL data the page |
html |
string | HTML of the page |
route |
Object | Route object |
Depending on rules, sync mode or cache availability, some properties can be
undefined
.
url
properties
Properties attached to data.current.url
and data.next.url
objects.
Property | Type | Default | Description |
---|---|---|---|
hash |
string | URL hash | |
href |
string | location.href |
Complete URL |
path |
string | URL path (without origin, hash and query) | |
query |
Object | {} | URL query (key: value) |
route
properties
Properties attached to data.current.route
and data.next.route
objects.
route
is available with@barba/router
Property | Type | Default | Description |
---|---|---|---|
name |
String | - | Route name |
params |
Object | {} | Route segment params |
Example:
1 | import barba from '@barba/core' |
Rules
Rules define the transition resolution, concretely “selecting the right transition to use”.
You can combine multiple rules on each transition.
Priority | Name | Type | Argument | Apply when… |
---|---|---|---|---|
1 | custom |
Function | data object | Return true |
2 | route |
String|String[] | - | current.route.name match |
3 | namespace |
String|String[] | - | current.namespace match |
Any rules can be used within
from
and/orto
properties.
Priority | Usage | Apply when… |
---|---|---|
x.1 | from AND to |
Rule(s) match for current AND next data |
x.2 | to |
Rule(s) match for next data |
x.3 | from |
Rule(s) match for current data |
Notice that you can use
from
andto
properties independently.
Example:
1 | import barba from '@barba/core'; |
In this example, based on the priority order, Barba will use the custom-transition
:
- if the link you clicked 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
Priority
The transition resolution follows this order:
- Rule(s) priority
from
and/orto
priority- Declaration order
The common/default behaviour is to start leave as soon as possible.
enter will be called when leave ends AND next page is “available” (fetched or cached).
If you use someto
logic, Barba can not predict and select the right transition until the next page is available.
This also applies when usingsync: true
.
But this does not apply ifto
is used withroute
property because “next route” is known when the click occurs…Bear with this!
Sync mode
A mode that indicate whether leave and enter hooks should “play together”.
This involves waiting until the next page is available (fetched or cached).
By default the sync mode is set to false
.
Example:
1 | import barba from '@barba/core' |
<view>
object
Views allow you to have some logic related to the content of a namespace.
You can see it as “lifecycle” into Barba. It is a good place to init or destroy things…
They use a subset of transition hooks and receive the same data
object.
Available hooks are:
beforeAppear
afterAppear
beforeLeave
afterLeave
beforeEnter
afterEnter
Example:
1 | import barba from '@barba/core' |
debug
Useful console.info
about transition used, for debugging purpose only.
It sets logLevel to debug
. Default is off
.
Example:
1 | import barba from '@barba/core' |
logLevel
1 | 0. off |
schema
Allows you to override data attributes. See default schemaAttribute
.
Example:
1 | import barba from '@barba/core' |
cacheIgnore
Allows Barba to cache your pages.
Saving pages in the cache result in less bandwidth usage and less server-side load: no XMLHttpRequest
are made for pages that have already been visited.
If disabled, Barba will retrieve each page from the server on every request: this could be useful if your page contains scripts that need to be evaluated on each page call.
You can also define “route” pattern(s) (see @barba/router).
Value | Description |
---|---|
false (default) |
Cache all |
true |
Ignore all |
`string | string[]` |
Cache lifetime is restricted to Barba instance and will be cleared when leaving the site.
Example:
1 | import barba from '@barba/core' |
prefetchIgnore
Allows Barba to prefetch your pages on mouseover
or touchstart
events.
Since there is a 100-300ms delay during the user hover and the click, Barba is using this time to start prefetching the next page. Most of the time this dead time is enough to get the next page ready!
If follows the same logic as the above cacheIgnore
option…
To prefetch all eligible links that enter the viewport, use the @barba/prefetch module.
Example:
1 | import barba from '@barba/core' |
prevent
Allows you to add a custom “prevent” test.
If your function returns true
, Barba will not be enabled.
Argument | Property | Description |
---|---|---|
Object | el |
Clicked element |
event |
Triggered event | |
href |
Next page href |
Example:
1 | import barba from '@barba/core' |
Note that you can prevent a link of using Barba with the
data-barba-prevent
attribute:
data-barba-prevent
ordata-barba-prevent="self"
prevents the current linkdata-barba-prevent="all"
prevents all children links of a container (div
,p
, etc.)
requestError
Allows you to catch request errors.
If this function returns false
, wrong links will not be “force” triggered.
Argument | Type | Description |
---|---|---|
trigger |
HTMLElement|string | The clicked/hovered HTMLElement, string ‘popstate’ or string ‘barba’ (see data.trigger ) |
action |
string | The user action on the link: ‘enter’ when hovering, ‘click’ when clicking, or ‘prefetch’ with @barba/prefetch |
url |
string | Requested URL |
response |
Object | Fetch error with message or response with status , statusText , … |
Example:
1 | import barba from '@barba/core' |
Note that if you use
barba.go()
directive without returningfalse
, you will be redirected to the requested URL because Barba usesbarba.force()
to reach the page.
Partial output
Barba sends a custom HTTP Header named x-barba
in the XMLHttpRequest
.
If you are using a server side language like PHP, you can detect this custom HTTP Header and output just the container instead of the entire page: this could result in less bandwidth usage and less server-side load.
Note that doing so, you have to manually handle the upate of the page
title
.