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

@watch-state/history-api

v2.0.0-alpha.1

Published

Browser History API with watch-state

Downloads

25

Readme

  @watch-state/history-api

 

NPM minzipped size downloads changelog license

Browser History API with watch-state.

stars watchers

Install

npm

npm i @watch-state/history-api

yarn

yarn add @watch-state/history-api

Usage

You can change History API by historyPush and historyReplace from this library or use some default History API methods: history.back(), history.forward(), history.go(delta)

historyPush

This is an action to add History API step. Use the action instead of history.pushState.

import { historyPush } from '@watch-state/history-api'

historyPush('/new-url')

This action returns a promise because of History API changes non-immediately with historyPush. It first of all scrolls to page up by default.

historyPush('/new-url').then(() => {
  console.log('done')
})

Use scroll-behavior equals smooth to have a smooth scroll effect.

You can scroll to another position by the second argument.

historyPush('/new-url', 100)

You can use a selector to scroll at an element.

historyPush('/new-url', '#header')

historyReplace

This is an action to replace History API step. Use the action instead of history.replaceState.

import { historyReplace } from '@watch-state/history-api'

historyReplace('/new-url')

It works the same as historyPush so it returns a promise and has 2 arguments.


You can react on History API changes by next store elements:


locationHref

Returns observable location.href

import { Watch } from 'watch-state'
import { locationHref, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(locationHref.value)
})

historyPush('/test')

locationURL

Returns observable location.pathname + location.search + location.hash

import { Watch } from 'watch-state'
import { locationURL, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(locationURL.value)
})

historyPush('/test')

locationPath

Returns observable location.pathname

import { Watch } from 'watch-state'
import { locationPath, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(locationPath.value)
})

historyPush('/test')

locationSearch

Returns observable location.search

import { Watch } from 'watch-state'
import { locationSearch, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(locationSearch.value)
})

historyPush('?test=1')

locationHash

Returns observable location.hash

import { Watch } from 'watch-state'
import { locationHash, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(locationHash.value)
})

historyPush('#hash')

historyMovement

This is a Cache returns one of the next string: back | forward | same.

If you go back by browser button or history.back() or history.go(delta) with a negative delta then historyMovement value equals back.

If you go forward by browser button or history.forward() or history.go(delta) with a positive delta or historyPush then historyMovement value equals forward.

If you use historyReplace, historyMovement value equals same

import { Watch } from 'watch-state'
import { historyMovement, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(historyMovement.value)
})

historyPush('/test')
history.back()
history.forward()

historyState

Returns observable history.state

import { Watch } from 'watch-state'
import { historyState, historyPush } from '@watch-state/history-api'

new Watch(() => {
  console.log(historyState.value)
})

historyPush('/test')

Issues

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

issues