barbajs / barba

Create badass, fluid and smooth transitions between your website’s pages
https://barba.js.org/
MIT License
11.65k stars 473 forks source link

About @barba/router #433

Closed dangelion closed 5 years ago

dangelion commented 5 years ago

Hi, we're going to use Barba 2 on a project and we're trying the router. We had some difficult in understanding what it's useful for. I get that it's useful for targeting transitions on some specific situation but what we need is something we can use to define URLs.

Example: we have 2 pages (index.php with namespace home and about.php with namespace about) and we set the router like so:

const routes = [
  {
    path: '/',
    name: 'home' // => relative to data-barba-namespace="home" into index.php
  }, 
  {
    path: '/about',
    name: 'about' // => relative to data-barba-namespace="about" into about.php
  },
];

barba.use(barbaRouter, {
  routes,
});

Then we add a link <a href="about">About</a> in index.php that we expected it navigates to URL /about showing the content of about.php, but we gets a 404 error.

Maybe we're misunderstanding the usage of Barba Router?

Sorry for the trivial question. Thanks

thierrymichel commented 5 years ago

Hi @dangelion,

V2 works with "transition resolution". Based on your transition"rules" (namespace, custom(), from, to), barba.js will pick and use the right transition.

@barba/router adds a new "rule": route. It allows you to let barba.js choose the right transition based on … href/URL. For example, if you want a specific transition when you navigate to some archive page (and do not want to use namespace) you can define an archive route:

const routes = [{
  path: '/archive/:category/:year',
  name: 'archive',
}];

And associate it to your transition :

const archiveTransition = {
  to: {
    route: 'archive',
  },
  ...

Navigating to /archive/sport/2019 or archive/music/2018 will use your archiveTransition. As you can have dynamic segments or use some regex, it's an additional way of managing transitions for your different pages…

In your case, you should add .php to the path property. But as your are using namespace, you probably do not need @barba/router

Hope it helps, Thierry

asphub commented 5 years ago

Hi @thierrymichel ,

I am also facing issue with custom routing. I am using barba.js umd version, Is that cause any issue? Following is the code that I have tried.

Code is working fine for page navigation & Transition except the Custom Routes aren't working properly.

main.js


var routes = [{
path: '/index.html',
name: 'home'
}, {
path: '/about.html',
name: 'about'
}];
// tell Barba to use the router with your custom routes
barba.use(barbaRouter, {
    routes
});
barba.init({
    routes: routes,
    transitions: [{
        name: 'custom-transition',
        from: {

            // define a custom rule based on the trigger class
            custom: ({ trigger }) => {
                return trigger.classList && trigger.classList.contains('use-custom-transition');
            },

            // define rule based on multiple route names
            route: [
                'index',
                'about'
            ]
        },
        to: {

            // define rule based on multiple namespaces
            namespace: [
                'home',
                'about'
            ]
        },
        leave: function (data) {
            var done = this.async();
            TweenMax.to(data.current.container, 1, {
                css: {
                    display: 'none',
                    opacity: 0
                },
                onComplete: done
            });
        },
        enter: function (data) {
            var done = this.async();
            TweenMax.from(data.next.container, 1, {
                css: {
                    display: 'block',
                    opacity: 1
                },
                onComplete: done
            });
        }
    }]
});

> `index.html`
---
```html
<div data-barba="wrapper">
  <div data-barba="container" data-barba-namespace="home">
    <!-- put here the content you wish to change between your pages -->
    <h1>Home Page</h1>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- load some animation library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.2/TweenMax.min.js"></script>

<!-- load barba.js (this will use the latest UMD version of the library) -->
<script src="https://unpkg.com/@barba/core"></script>
<script src="https://unpkg.com/@barba/router"></script>

about.html


<div data-barba="wrapper">
<div data-barba="container" data-barba-namespace="about">
<!-- put here the content you wish to change between your pages -->
<h1>About Page</h1>
</div>
</div>
thierrymichel commented 5 years ago

Hi @asphub,

Thanks for using barba.js and testing the v2! Some things to consider:

asphub commented 5 years ago

Thanks @thierrymichel for Quick Reply, Waiting for the new update with this feature / bug fix

NB: I tried with /about.html too, but it's not working