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

svelte-hash

v1.0.1

Published

Easy URL hash management for Svelte.

Downloads

45

Readme

Svelte Hash

Easy URL hash management for Svelte.

svelte-hash provides a simple way to manage URL hash/fragment in Svelte.

[!NOTE] This library is client-side only, meaning SvelteKit (SSR) is not supported.

Installation

# Bun
bun i -D svelte-hash

# NPM
npm i -D svelte-hash

# Yarn
yarn add -D svelte-hash

# PNPM
pnpm add -D svelte-hash

Usage

[!TIP] It's really suggested to have a look to these interactive examples to test the library and understand the functionality (there you can see hash/fragment updates in the URL).

First of all, create and export a new hash store instance:

// store.ts
import { createHashStore } from 'svelte-hash'

interface Hash {
	foo: string
	bar: string
}

export const hash = createHashStore<Hash>()

[!NOTE]

  • Explicitly defining the type of the hash is optional, but extremely suggested as it provides type checking and autocompletion
  • Currently only string values are supported: this might change in the future with v2
  • Only one hash store can be initialized in the same project: multiple hash stores initialization will throw an error

Then, you can use the hash store in your components:

<!-- App.svelte -->
<script>
	import { hash } from './store.ts'

	// Prevent pushing to browser history every change with a "proxy" variable
	// Every hash key could also not exist, so we need to provide a default value
	let valueProxy = hash.foo || ''
</script>

<!-- The form will update the hash value only when submitted -->
<!-- result (on submit) will be: <URL>#foo={valueProxy} -->
<form on:submit|preventDefault={() => hash.foo = valueProxy}>
	<input type="text" bind:value={valueProxy} />
	<button type="submit">Submit</button>
</form>
<p>

To delete a hash key from the URL, you can set it to a falsy value:

import { hash } from './store.ts'

hash.foo = null
// or
hash.foo = undefined
// or
hash.foo = ''

Behind the scenes

The reason I created this library was to accomplish a simple URL hash/fragment management for my Svelte music player, Musicale (you should really check it out!).

I originally created the implementation directly in that project, but then I decided to extract it and make it a standalone library for everyone to use.

Why do I call URL fragment "hash"?

On the browser, to access the URL fragment you use window.location.hash, so I decided to take that for the library name.

Contributing

We :heart: contributions!
Feel free to open an issue or a pull request but follow Contributing Guidelines.

If you don't know where to start, check out the help wanted issues!

License

GPL-3.0 License here.