Giter Site home page Giter Site logo

single-page's Introduction

single-page

Write single-page apps with a single callback to handle pushState events

Like the classic @subtack module, but now with ESM + CJS versions.

featuring

  • 0 dependencies
  • ESM & CJS versions via package.json exports field

install

npm i -S @nichoth/single-page

test

npm test

example

ESM

import { singlePage } from '@nichoth/single-page'

CJS

const singlePage = require('@nichoth/single-page').default

Given some html with elements #foo, #bar, and #baz:

<html>
  <style>
  </style>
  <body>
    <div id="foo">
      foo foO fOo fOO Foo FoO FOo FOO
      <div><a href="/bar">bar</a></div>
    </div>
    
    <div id="bar">
      bar baR bAr bAR Bar BaR BAr BAR
      <div><a href="/baz">baz</a></div>
    </div>
    
    <div id="baz">
      baz baZ bAz bAZ Baz BaZ BAz BAZ
      <div><a href="/foo">foo</a></div>
    </div>
    
    <script src="bundle.js"></script>
  </body>
</html>

Now turn each of the divs into pages with their own routes. Note that this module doesn't update the link callbacks for you. You'll need to handle that for yourself.

var divs = {
    foo: document.querySelector('#foo'),
    bar: document.querySelector('#bar'),
    baz: document.querySelector('#baz')
};

const singlePage = require('@nichoth/single-page').default
var showPage = singlePage(function (href) {
    Object.keys(divs).forEach(function (key) {
        hide(divs[key]);
    });
    
    var div = divs[href.replace(/^\//, '')];
    if (div) show(div)
    else show(divs.foo)
    
    function hide (e) { e.style.display = 'none' }
    function show (e) { e.style.display = 'block' }
});

var links = document.querySelectorAll('a[href]');
for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('click', function (ev) {
        ev.preventDefault();
        showPage(this.getAttribute('href'));
    });
}

You'll need to have a server that will serve up the same static content for each of the pushState routes. Something like this will work:

var http = require('http');
var ecstatic = require('ecstatic')(__dirname);

var server = http.createServer(function (req, res) {
    if (/^\/[^\/.]+$/.test(req.url)) {
        req.url = '/';
    }
    ecstatic(req, res);
});
server.listen(5000);

Now when you go to http://localhost:5000 and click around, you'll see /foo, /bar and /baz in the address bar when you click links, even though you're not reloading the page.

methods

var singlePage = require('@nichoth/single-page').default

var showPage = singlePage(cb, opts)

Fire cb(href, page) at the start and whenever the page navigation changes so you can update the page contents accordingly.

If opts.saveScroll is !== false, page.scrollX and page.scrollY are saved for every unique href so that you can jump back to the same scroll position that the user left off at.

showPage(href)

Navigate to href, firing the callback passed to singlePage.

showPage.push(href)

Update the location href in the address bar without firing any callbacks.

license

MIT

single-page's People

Contributors

juliangruber avatar nichoth avatar

Watchers

 avatar

single-page's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.