All Projects → estrattonbailey → Operator

estrattonbailey / Operator

1.8kb drop-in "PJAX" solution for fluid, smooth transitions between pages.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Operator

Laravel Vuejs.com
Laravel and VueJs Blog, using Laravel nova, GraphQL, NuxtJs, Apollo and ...more
Stars: ✭ 54 (-35.71%)
Mutual labels:  spa
Cozy Calendar
An application to manage your events
Stars: ✭ 68 (-19.05%)
Mutual labels:  spa
Laravel Vue Starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 76 (-9.52%)
Mutual labels:  spa
Vue axios spa
基于vue2+axios+vux+vue-router+vuex构建的单页微信端项目
Stars: ✭ 54 (-35.71%)
Mutual labels:  spa
Mailman
Mailman is a GUI to help you manage your email accounts stored in a MySQL/MariaDB database.
Stars: ✭ 62 (-26.19%)
Mutual labels:  spa
School Spa
VueJS Interface for using school-api
Stars: ✭ 70 (-16.67%)
Mutual labels:  spa
Opencart
Free PWA & SPA for OpenCart
Stars: ✭ 50 (-40.48%)
Mutual labels:  spa
Aspnetcorespa
Asp.Net 5.0 & Angular 11 SPA Fullstack application with plenty of examples. Live demo:
Stars: ✭ 1,211 (+1341.67%)
Mutual labels:  spa
Prerenderer
Server pre-rendering for Single Page Applications using ClojureScript/JavaScript by use of NodeJS.
Stars: ✭ 68 (-19.05%)
Mutual labels:  spa
Captain Fact Frontend
🔎 CaptainFact - Frontend. The one you see on https://captainfact.io
Stars: ✭ 74 (-11.9%)
Mutual labels:  spa
Photo Blog
The Photo Blog Application based on Laravel 5 and Vue.js 2 + Prerender
Stars: ✭ 55 (-34.52%)
Mutual labels:  spa
Prestashop
Free PWA & SPA for PrestaShop
Stars: ✭ 59 (-29.76%)
Mutual labels:  spa
Quench Vue
Simple, client-side hydration of pre-rendered Vue.js apps
Stars: ✭ 71 (-15.48%)
Mutual labels:  spa
Icestark
🐯 Micro Frontends solution for large application(面向大型应用的微前端解决方案)
Stars: ✭ 1,073 (+1177.38%)
Mutual labels:  spa
Vue Horizontal
An ultra simple pure vue horizontal layout for modern responsive web with zero dependencies. (SPA/SSG/SSR)
Stars: ✭ 75 (-10.71%)
Mutual labels:  spa
Serverless Application
🍤 ALIS Media - Serverless Application
Stars: ✭ 52 (-38.1%)
Mutual labels:  spa
Vue Mix
Bootstrap your Vue 2.0 SPA in less than 2 minutes.
Stars: ✭ 69 (-17.86%)
Mutual labels:  spa
Plus Small Screen Client
本库已经合并进入 https://github.com/slimkit/plus 中,仅提供 2.1 及以前版本下载使用!
Stars: ✭ 83 (-1.19%)
Mutual labels:  spa
Nuxt Chat App
Frontend of real-time chat application built using nuxtjs, socket.io. Check the backend at https://github.com/binbytes/chat-app-server.
Stars: ✭ 77 (-8.33%)
Mutual labels:  spa
Laravel Vuetify Spa
Laravel-Vue SPA starter project template with Vuetify frontend.
Stars: ✭ 73 (-13.1%)
Mutual labels:  spa

operator npm

1.8kb drop-in "PJAX" solution for fluid, smooth transitions between pages. Zero stress.

Features

  1. Advanced routing via matchit
  2. Client-side redirects
  3. Async-by-default, easy data loading between routes
  4. Pages are cached after initial visit
  5. Pre-fetch select pages, as needed

Install

npm i operator --save

Usage

Basically zero config by default, just specify a root DOM node to attach to.

import operator from 'operator'

operator('#root')

Defining routes

To define custom handlers for a given route, pass an object with a path property and handler method.

operator('#root', [
  {
    path: '/',
    handler (state) {
      console.log(state)
    }
  }
])

Routes handlers can also return Promises, and they support params, optional params, and wildcards.

operator('#root', [
  {
    path: '/',
    handler (state) {
      console.log(state)
    }
  },
  {
    path: '/products',
    handler (state) {
      return getProducts() // Promise
    }
  },
  {
    path: '/products/:category/:slug?',
    handler ({ params }) {
      const reqs = [ getProductCategory(params.category) ]
      if (params.slug) reqs.push(getProductBySlug(params.slug))
      return Promise.all(reqs)
    }
  }
])

Route Caching

Routes are cached by default, so on subsequent visits, no data will be loaded. To follow links to pages via AJAX, but fetch fresh content on each navigation action, set cache to false:

operator('#root', [
  {
    'path': '/',
    cache: false
  }
])

Ignoring Routes

Sometimes you need to navigate to a page without AJAX, perhaps to load some sort of iframe content. To do so, set ignore to true:

operator('#root', [
  {
    'path': '/',
    ignore: true
  }
])

Lifecycle

Any function passed to the route config will be called on every route change, kind of like middleware.

const app = operator('#root', [
  state => console.log(state)
])

Operator also emits some helpful events.

app.on('navigate', state => {}) // on valid link click
app.on('before', state => {}) // before render
app.on('after', state => {}) // after render
app.on('hash', state => {}) // when the URL contains a hash

History state

Operator does not manage History or page title, for maximum flexibility to the user. Most people should probably just use this snippet:

app.on('after', ({ previousDocument, location }) => {
  document.title = previousDocumnt.title
  window.history.pushState({}, '', location)
})

If you want to ignore things like query strings or hashes, use pathname:

app.on('after', ({ previousDocumnt, pathname }) => {
  document.title = previousDocumnt.title
  window.history.pushState({}, '', pathname)
})

Hash Anchors

When a hash is encountered – whether on a navigate action between pages, or for scroll-anchors on the same page - Operator will emit a hash event. It's up to you to handle scrolling.

For most sites, this should work:

app.on('hash', ({ hash }) => {
  const target = document.getElementById(hash)

  if (target) {
    const scroll = target.getBoundingClientRect().top + window.pageYOffset
    window.scrollTo(0, scroll)
  }
})

Smooth scrolling is also pretty easy:

import sscroll from 'sscroll'

app.on('hash', ({ hash }) => {
  const target = document.getElementById(hash)
  target && sscroll(target, { duration: 500 })
})

API

go(path)

app.go('/about')

load(path)

Use this for prefetching pages.

app.load('/about')

state (getter)

app.state // => { previousDocument, pathname, location, params, hash, search, handler }

Recipes

Redirects

app.on('before', ({ pathname }) => {
  if (/redirect/.test(pathname)) {
    app.go('/') // redirect
  }
})

Transition animation

import wait from 'w2t'

operator('#root', [
  state => {
    return wait(600, [
      const root = document.documentElement.classList
      return new Promise(res => {
        root.add('is-transitioning')
        setTimeout(() => {
          root.remove('is-transitioning')
          res()
        }, 600)
      })
    ])
  }
])

Changelog

v1.8.0

Removed default scroll handling. This should be moved to user-space in the event the user doesn't want the page to reset to the top.

v1.7.0

Added previousDocument (a complete cloned document object) to the state object. Replaces state.title via previousDocument.title.

v1.6.0

  • Implemented hash event, see docs
  • Fix bad mailto and tel regex, thanks @gabrielloeb!

v1.2.0

Slight update to the API, will require brief migration to new syntax for most users.

  • Deprecated Array format for route configs in favor of more flexible Object syntax
  • Add ignore and cache options

License

MIT License © Eric Bailey

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].