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-use-persist

v0.7.0

Published

A Svelte action that can persist any form or input value to local storage (or your own store)

Downloads

18

Readme

svelte-use-persist

svelte-use-persist

A Svelte action that saves forms and inputs client side to local storage and restores them on page load.

Demo

Try it out here!

Installation

npm install svelte-use-persist

Features

  • Persist any form or inputs to local storage (or your own stores) and restore them on page load automatically
  • Currently supports and end-to-end tested with:
    • <form> or any element that contains inputs
    • <input type="text">
    • <input type="email">
    • <input type="tel">
    • <input type="date">
    • <input type="number">
    • <input type="checkbox">
    • <input type="radio">
    • <input type="password"> (off by default, only use if you know what you're doing)
    • <textarea>
    • <select> (multiple not supported for now)

Usage

Forms

Create a form and add the use:persist action to it. The action takes an object with a key property. This key is the identifier for the form in local storage.

Note

Make sure all inputs in the form have the name attribute. This is used to identify the input in local storage.

<form
	use:persist={{
		key: 'my-form'
	}}
>
	<label for="title">Title</label>
	<input type="text" name="title" />

	<label for="content">Content</label>
	<textarea name="content" />

	<label for="date">Date</label>
	<input type="date" name="date" />

	<label for="phone">Phone</label>
	<input type="tel" name="phone" />

	<label for="email">Email</label>
	<input type="email" name="email" />

	<button type="submit">Save</button>
</form>

Inputs

Create an input and add the use:persist action to it. The action takes an object with a key property. This key is the identifier for the input in local storage.

  • Make sure your input has a name attribute
<script>
	import { persist } from 'svelte-use-persist';
</script>

<input
	type="text"
	name="my-input"
	use:persist={{
		key: 'my-input'
	}}
/>

Use with your own store

You can use this action with your own stores. Just pass the store as the store property in the action object and make sure you don't specify the key property. If you use your own store, the action will not save anything to local storage. It will only save the form or input value to your store.

<script>
	import { persist } from 'svelte-use-persist';
	import { writable } from 'svelte/store';

	const store = writable({});
</script>

<input
	type="text"
	name="my-input"
	use:persist={{
		store
	}}
/>

FAQ

You should definitely be using the Snapshot feature in Sveltekit. But if you're not using SvelteKit in the first place, this package is a good option.

  • Why did you create this?

I used to run an audit company where people had to fill out very long forms (70+ questions). I thought that It would be a really bad experience if one of our customers had to re-do the form. So svelte-use-persist was born!

Acknowledgements

This work would have not been possible without the following projects and people, so thank you!

License

MIT