Skip to content

Instantly share code, notes, and snippets.

@Bukunmikuti
Last active July 8, 2021 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bukunmikuti/cbf1e75ec19dacd29f06420e695e31ad to your computer and use it in GitHub Desktop.
Save Bukunmikuti/cbf1e75ec19dacd29f06420e695e31ad to your computer and use it in GitHub Desktop.
View Router Demo

This is a basic illustration of View Router. You can play with the code and try out more ways to use View Router.

several views with viewrouter.config.js

For applications with several views, it is better to instantiate ViewRouter () in a separate 'viewrouter.config.js' file and export.

// viewrouter.config.js
import ViewRouter from "https://cdn.skypack.dev/@bukunmikuti/view-router"
let v = new ViewRouter ({...});
export default v
//index.js
import v from './viewrouter.config.js' 
v.routeTo(/*id*/)

The mounted() and render() can call other methods for your application logic.

Manipulate elements in the view with mounted() and put your view on-screen logic in the render() method.

Transitions

You can define custom transitions for your views. Simply use the usual @keyframes animation in your css and reference in view <template> tag.

/* index.css */
.customTransition {
animation: myCustom 0.5s ease-in-out forwards; 
}

@keyframes {
from {opacity: 0}
to {opacity: 1} 
}
<!-- index.html -->
<template id='about' class='v-router customTransition'>
</template>
<!DOCTYPE html>
<html>
<!-- Required head tag -->
<body>
<!-- Login -->
<template id="login" class="v-router fadeInLeft">
<h1>Log into your account!</h1>
<input placeholder="username" />
<input placeholder="password" />
<button>Sign in</button>
</template>
<!-- Sign up -->
<template id="signup" class="v-router fadeInRight">
<h1>Create your account!</h1>
<input placeholder="email" />
<input placeholder="password" />
<button>Sign up</button>
</template>
<script type="module" src="index.js"></script>
</body>
</html>
import ViewRouter from "https://cdn.skypack.dev/@bukunmikuti/view-router"
let v = new ViewRouter({
views: [
{
id: 'signup',
path: '/register',
mounted() {
console.log('signup now accessible')
},
render() {
console.log('Rendered sign up! 🎉');
}
},
{
id: 'login',
path: '/login',
mounted() {
console.log('Log in now accessible')
},
render() {
console.log('Rendered Log in 🎉')
}
}
],
navigation: 'hash',
transition: true,
})
//show log in
v.routeTo('login')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment