Giter Club home page Giter Club logo

storage-form's Introduction

storage-form

A Web Component that allows you to submit data to local storage.

Demo | Further reading

Examples

General usage example:

<script type="module" src="storage-form.js"></script>

<storage-form>
  <form>
    <label>
      Example
      <input type="text" name="example" value="value" />
    </label>
    <button>Submit</button>
  </form>
</storage-form>

Example of submitting with change event instead of a submit button. Use case for light/dark mode:

<script type="module" src="storage-form.js"></script>

<storage-form>
  <form>
    <label>
      <input type="radio" name="theme" value="dark" />
      <span>Dark mode</span>
    </label>
    <label>
      <input type="radio" name="theme" value="light" />
      <span>Light mode</span>
    </label>
    <label>
      <input type="radio" name="theme" value="" checked />
      <span>Default mode</span>
    </label>
    <br />
    <label>
      <input type="hidden" name="contrast" value />
      <input type="checkbox" name="contrast" value="true" />
      <span>High contrast</span>
    </label>
  </form>
</storage-form>

Example of hooking into storage event:

<script type="module" src="storage-form.js"></script>

<storage-form>
  <form>
    <label>
      Update the
      <code>output</code>
      by checking the box
      <input type="hidden" name="output" value />
      <input type="checkbox" name="output" value="updated" />
    </label>
  </form>
</storage-form>

<output></output>
<script>
  const updatePage = () => {
    document.querySelector("output").innerHTML = JSON.stringify(
      window.localStorage
    );
  };
  window.addEventListener("storage", updatePage);
  updatePage();
</script>

Features

This Web Component allows you to:

  • Use regular form elements to manipulate data in local storage
  • Invoke a storage event on the page to hook into elsewhere on the page
  • Maintain the forms state, using local storage, when the page is refreshed or reloaded
  • Optionally submit the form on the forms change event by omitting the forms submit button/input element

Installation

You have a few options (choose one of these):

  1. Install via npm: npm install @daviddarnes/storage-form
  2. Download the source manually from GitHub into your project.
  3. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

Usage

Make sure you include the <script> in your project (choose one of these):

<!-- Host yourself -->
<script type="module" src="storage-form.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://www.unpkg.com/@daviddarnes/[email protected]/storage-form.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://esm.sh/@daviddarnes/[email protected]"
></script>

Credit

With thanks to the following people:

storage-form's People

Contributors

daviddarnes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

storage-form's Issues

this.forms returns undefined

get forms() {
    return this.querySelectorAll("form");
  }

Will return undefined when the Web Component is defined before DOM is parsed.
e.g. when the WC is defined in a <script> tag in the HEAD

Does not work if script loads before markup

Describe the bug
If the script is loaded before the DOM is loaded, for each new storage-form tag the forms method will return nothing (since those DOM elements aren't in the tree yet), and the event handler will not be added. It works fine if you load the script after the DOM is loaded, but that's not clear in the documentation, and later additions to the page won't work as expected.

To Reproduce
Steps to reproduce the behaviour:

  1. Add the script link in the <head/> (I know this isn't best practice)
  2. Add the theme-switcher example into the <body/>
  3. Load the page
  4. See that the form does not persist

Expected behaviour
The form should persist

Desktop (please complete the following information):

  • OS: MacOS Sonoma
  • Browser Safari
  • Version 17.2.1

Additional context
I'm using the unpkg link in dev on a static webpage, loaded via file:/// URLs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.