1.1.0 • Published 1 year ago

dobato v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

dobato

A simple client-side routing library that helps you create smooth page transitions without having to reload.

Features

  • Easy to use.
  • Uses caching and prefetching to speed up page transition.
  • Offers transition hooks.

    Prefetch is inspired by quicklink

Install

npm install dobato
import Dobato from 'dobato';

or

<script src="https://unpkg.com/dobato"></script>

Basic usage

<body>
  <nav>
    <a href="/" dobato-link>Home</a>
    <a href="/about/" dobato-link>About</a>
  </nav>
  <div dobato>
    // will replace.
  </div>
</body>
class Home {
  constructor() {}
  enter() {}
  leave() {}
  destroy() {}
 }

class About {
  constructor() {}
  enter() {}
  leave() {}
  destroy() {}
 }

class App {
  constructor() {
    this.routes = {
      '/': Home,
      '/about/': About
    };

    this.page = new this.routes[location.pathname]();
    this.page.enter();

    this.dobato = new Dobato();

    this.dobato.on('enterStart', ({ path, from }) => {
      this.page = new this.routes[path]();
    });
    this.dobato.on('enterEnd', ({ path, from }) => {
      this.page.enter();
    });
    this.dobato.on('leaveStart', ({ path, to }) => {
      this.page.leave();
    });
    this.dobato.on('leaveEnd', ({ path, to }) => {
      this.page.destroy();
    });
  }
}

new App();

Options

OptionTypeDefaultDescription
containerString'[dobato]'The container that have content replaced on page transition.
linkString'a[dobato-link]'The anchor links will trigger page transition.
historyStateObjectThe state object will be associated with a browser history entry.
prefetchBooleantrueWhether to prefetch links that are in the viewport.

Method

MethodArgumentsDescription
on(eventName: string, callback: ({ path: string, from?: string, to?: string }) => void)eventName: The name of transition event.callback: A callback function that will be called when the transition event occurs.Add a transition hook.
off(eventName: string, callback: ({ path: string, from?: string, to?: string }) => void)eventName: The name of transition event.callback: A callback function that will be called when the transition event occurs.Remove a transition hook.

Transition hooks

NameDescriptionCallback arguments
leaveStartStart leave transition. d-leave-start class will be added to body.{ path, to }
leaveEndEnd leave transition. d-leave-end class will be added to body.{ path, to }
enterStartStart enter transition. d-enter-start class will be added to body.{ path, from }
enterEndEnd enter transition. d-enter-end class will be added to body.{ path, from }

License

ISC License

1.0.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.1.2

1 year ago

0.2.0

1 year ago

0.1.3

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago