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

@ryanflorence/persist-form

v1.1.0

Published

Form persistence for browser applications

Downloads

815

Readme

@ryanflorence/persist-form

Brings the browser's default HTML form value persistence to client side rendering with both a Custom Element or a React Hook.

Installation

npm install @ryanflorence/persist-form

Usage

Custom Element

First register the custom element:

import "@ryanflorence/persist-form/html";

Then render it inside a form.

<form>
  <rf-persist-form key="some-form" />
</form>

If no key is given, window.location.pathname is used.

<html>
  <body>
    <button>Toggle form</button>

    <div id="target"></div>

    <template id="my-form">
      <form method="post" class="login-form">
        <!-- rendering this persists the form across attach/detach -->
        <rf-persist-form key="login" />

        <input name="name" />
        <input name="email" type="email" />
        <button type="submit">Submit</button>
      </form>
    </template>

    <script type="module">
      // import the custom element
      import "https://esm.sh/@ryanflorence/persist-form/html.js";

      // some silly stuff to attach/detach a form from the dom to see it restore
      document.querySelector("button").addEventListener("click", () => {
        let target = document.querySelector("#target");
        let form = document.querySelector(".login-form");
        if (form) {
          target.removeChild(form);
        } else {
          target.appendChild(
            document.querySelector("#my-form").content.cloneNode(true),
          );
        }
      });
    </script>
  </body>
</html>

React Router Integration

With React 19 you can use a ref:

import { usePersistedForm } from "@ryanflorence/persist-form/react";

function MyForm() {
  let { persistFormRef } = usePersistedForm();
  return (
    <form ref={persistFormRef} method="post">
      <input name="name" />
      <input name="email" type="email" />
      <button type="submit">Submit</button>
    </form>
  );
}

Or use the hooks for more control (and if you're on React 18):

import { usePersistedForm } from "@ryanflorence/persist-form/react";

function MyForm() {
  const { persist, restore, clear } = usePersistedForm();
  return (
    <form
      // restore when the form mounts
      ref={node => node && restore(node)}
      // persist on change of form elements
      onChange={e => persist(e.currentTarget)}
      // clear after submission
      onSubmit={e => {
        e.preventDefault();
        clear();
      }}
    >
      {/* form fields */}
    </form>
  );
}

Custom Key

By default the location.key is used, but you can provide a custom key.

// not attached to locations
usePersistedForm("login-form");

// use the URL instead of location.key
usePersistedForm(location.pathname);

Core Package

Integrate with any setup:

import { persist, restore, clear } from "@ryanflorence/persist-form";

// Save form state
let form = document.querySelector("form");
persist("my-form", form);

// Restore form state
restore("my-form", form);

// Clear saved state
clear("my-form");

// Example with event handlers
form.addEventListener("change", () => {
  persist("my-form", form);
});

document.addEventListener("DOMContentLoaded", () => {
  restore("my-form", form);
});

Notes

  • Uses sessionStorage, so form data persists only for the current browser session
  • For React Router integration, form data is keyed by location.key, so the same form can be persisted with different data across the history stack
  • Handles all form input types including text fields, checkboxes, radio buttons, etc. (Files aren't supported due to browser limitations)

License

MIT