Git Product home page Git Product logo

slocation's Introduction

slocation

Reactive Svelte Location Store

Introduction

slocation is Reactive Svelte Location Store . location which is used to access information about current Location is not Reactive . slocation wraps location as Readable Svelte Store which gets updated every time location changes thus making it Reactive.

It also provides extra methods to update URL without reloading the page . With reactive location Object and methods to route without reloading the page , slocation can be viable minimal alternative to Router. It is also a very small library.

Features

  • Small in Size
  • Reactive location Object
  • Listens to popstate and hashchange events
  • Supports both History API and hash
  • Can be viable small alternative to Routers
  • Written in TypeScript

Installation

Install slocation as dependency in your project .

npm install slocation

Usage

slocation is Readable Svelte Store. It must be imported to be used.

import slocation from "slocation";

It wraps location reactively

<div>{JSON.stringify($slocation)}</div>

It's properties are reactive too!

<pre>
{$slocation.href}
{$slocation.origin}
{$slocation.protocol}
{$slocation.host}
{$slocation.hostname}
{$slocation.port}
{$slocation.pathname}
{$slocation.search}
{$slocation.hash}
</pre>

Most Important among these are href , pathname , search and hash because other properties of location only change on page reload. With these important properties , you can create routing in your Svelte Application.

Note that Since slocation wraps location , methods of location are also exposed with slocation . Methods which are common to both location and slocation are effectively same.

Usage as History-API based Router

{#if $slocation.pathname === '/'}
<HomeComponent />
{:else if $slocation.pathname === '/about'}
<AboutComponent />
{:else if $slocation.pathname.startsWith('/browse/') }
<!-- This Component will only appear if route begins with '/browse/' -->
<!-- $slocation.pathname / $slocation.href can further -->
<!-- be parsed to get required params (Out of Scope for this library) -->
<!-- Parse $slocation.search to get queries -->
<!-- Parse $slocation.hash to parse hash/fragment identifier -->
<SpecialComponent />
{:else}
<FallBackComponent />
{/if}

To Navigate without refreshing / reloading the Page , slocation provides these extra methods.



  1. slocation.goto(url?: string, replace?: boolean | undefined) => void

Navigates to specified URL path. replace is optional and defaults to false. If replace is given true, most recent entry on the history stack will be updated with new one.

Example:

slocation.goto("/about"); // This will Navigate to '/about' without refreshing the page
slocation.goto("/help", true); // This will Navigate to '/help' without
// refreshing the page but history State will be replaced instead of adding new one
  1. slocation.pushState(data: any,title: string,url?: string | null | undefined) => void

If history.pushState is used , slocation store will not be updated breaking it's reactivity. So, slocation.pushState must be used which will update slocation. slocation.pushState is just wrapper function to history.pushState which also updates slocation to keep it reactive.

Example:

slocation.pushState({}, "", "/about"); // This will Navigate to '/about'
// without refreshing the page
// Code Below won't update slocation
// history.pushState({}, "", "/about"); // Although , this navigates to '/about',
// slocation store won't be updated
  1. slocation.replaceState(data: any,title: string,url?: string | null | undefined) => void

Same as slocation.pushState but slocation.replaceState is wrapper to history.replaceState . slocation.replaceState must be used instead of history.replaceState .

  1. slocation.reset() => void

Resets slocation with current location object . This force-updates slocation store.



Note that history.back() , history.forward() , history.go() will update slocation automatically , so these can be used. Only use slocation.pushState() and slocation.replaceState() instead of history.pushState() and history.replaceState() respectively.

Usage as Hash based Router

{#if $slocation.hash === ''}
<HomeComponent />
{:else if $slocation.hash === '#/about'}
<AboutComponent />
{:else if $slocation.hash.startsWith('#/browse/') }
<!-- This Component will only appear if hash route begins with '/browse/' -->
<!-- Parse $slocation.hash to parse hash/fragment identifier -->
<SpecialComponent />
{:else}
<FallBackComponent />
{/if}

To Update the Route , you just need to update hash of location object which also updates slocation.

location.hash = "#/about";

This must update the Hash Along with slocation store.

License

MIT

slocation's People

Contributors

varbhat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

slocation's Issues

improve the project implementation description

can yo elaborate it..?

i mean, i like the reactive lang. but you should said something "it helps to you to implement location bla bla bla" event the so technically descrfiption in the README

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.