npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-history-api

v1.1.4

Published

Browser History API with Mobx

Downloads

117

Readme

mobx-history-api

NPM downloads license
Browser History API with Mobx 3 and more.

Installation

npm

npm i mobx-history-api

yarn

yarn add mobx-history-api

Using

To start working with Mobx History API just get an instance of History.

import History from 'mobx-history-api'

const history = new History()

Fields

url string

This is an observable field, returns current relative URL includes pathname, search and hash.

history.url // current url

url does not include locale and every time starts from /

path string

This is an observable field, returns current pathname of URL.

history.path // current pathname

path does not include locale

hash string

This is an observable field, returns current hash of URL.

history.hash

href string

This is an observable field, returns current path + search of URL.

history.href

href does not include locale

locales string

This is an observable and writable field.
Use this field when you wanna have locale prefix in the url.

history.locales = 'en|fr|ru'

Or you can provide locales via constructor.

const history = new History('ru|en|de')

locale string

This is an observable and writable field, returns current locale of URL.

history.locales // returns empty string by default
history.locales = 'en|ru'

history.locale // returns empty string by default

history.push('/test')

history.url // '/test'
location.pathname // '/test'

history.locale = 'ru'

history.url // '/test'
location.pathname // '/ru/test'

history.locales = ''

history.url // '/ru/test'
location.pathname // '/ru/test'
location.locale // ''

localUrl string

This is just current url with the locale

history.localUrl // '/'

history.locale = 'ru'

history.localUrl // '/ru'

movement string string undefined

This is an observable field, returns undefined if you just load the page. When you moved through history the field changes to the status of the moving.

// history.movement === undefined

history.push('/test')

// history.movement === 'forward'

history.back()

// history.movement === 'back'

state object

This is an observable field, returns the state of history api.
state equals window.history.state when number of steps more than 1.

history.state // {key: '...', steps: [{...}]}

history.push('/test')

history.state // {key: '...', steps: [{...}, {...}]}

Methods

search

This is an observable method.
That means you can use the method inside reaction of mobx, when result of the function changed the reaction be called.

The method returns the value of the provided key in the URL query.
search(key: string): string

history.search('key') // returns 'value' for url equals '?key=value'

push

You can push to history any URL and you be moved forward in the history.
push(url: string, position: number | string = 0, scrollFirst = false): this

history.push('/test')

By default any time when you push a new URL the page scrolls up to position 0. If you wanna custom scroll the page after the pushing you can provide the second argument as a position of scroll.

history.push('/test', 200)

history.push('/', '#root')

If you do not want to scroll, provide -1 as a position of scrolling.
If you wanna scroll first with scroll-behavior equals smooth, provide true to the third argument.

history.push('/test', 0, true)

back

You can move back like you click on back button in your browser.
back(reg?: RegExp | BackChk, def = '/', scrollFirst = false): this

history.back()

With the method, you can push to history a position which was before. Just provide an argument to the method. The argument should be regex to test previous states.

history.back(/.*/) // push any previous url

You can handle all previous steps by function

history.back(url => url !== '/test')
// push any previous url except for '/test'

The second argument is used when nothing found in history.
The third argument works the same as the third of push.

replace

You can replace url on current history position with replace.
replace(url: string, position: number | string = 0, scrollFirst = false): this

history.push('/test1')
history.push('/test2')

history.replace('/test3')

history.back()
this.url // `/test1`

history.forward()
this.url // `/test3`

go

You can move to any position of history with method go.
go(delta: number): this

history.go(-1) // the same back()

forward

You can move forward like you click on forward button in your browser.
forward(): this

history.forward()

is

This is observable method. This method just returns result of regex testing which provided to the method.
is(reg: string): boolean

history.is('^/$') // true if this is home page

The regexp tests url field of history. That means is don't include locale.

get

This method is the same as method is, but returns result of matching.
get(reg: string, index = 0, defaultValue = ''): string

history.get('^/user/([0-9]+)$')
// returns current url if it matches the regex, otherwise empty string

The second argument is used for get information inside round brackets.

history.get('^/user/([0-9]+)$', 1)
// returns current user if url matches the regex, otherwise empty string

destructor

If you finished working with history and wanna get rid of it, just run destructor method.
destructor()

history.destructor()

Utils

decode

Just decodes URL to string

import {decode} from 'mobx-history-api'

decode(location.href)

parseUrl

Returns an object with path, search and hash fields of relative URL

import {parseUrl} from 'mobx-history-api'

parseUrl(location.pathname + location.search + location.hash)

setSearch

Sets search value to relative URL

import {setSearch} from 'mobx-history-api'

setSearch('/test', 'key', 'value') // "/test?key=value"

Use 2 arguments to remove a key.

import {setSearch} from 'mobx-history-api'

setSearch('/test?key=value', 'key') // "/test"

scroll

You can scroll the current page with scroll function.
scroll(position: number | string, callback?: function): this

import {scroll} from 'mobx-history-api'

scroll(100)

If you wanna scroll the page to a defined element you can provide CSS selector to find the element and scroll to view it.

import scroll from 'mobx-history-api/scroll'

scroll('#root')

When you use scroll-behavior equals smooth you can get callback when the scrolling finished.

scroll(0, () => console.log('scrolling is finished'))

Example

import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import History from 'mobx-history-api'
import {observer} from 'mobx-react'

const history = new History()

@observer
class Url extends Component {
  render () {
    return history.url
  }
}

ReactDOM.render(<Url />, document.getElementById('root'))

// render current URL. Let it be '/'

history.push('/test')

// now the root element contains '/test' 

links

Issues

If you find a bug, please file an issue on GitHub
issues


stars watchers