An open API service indexing awesome lists of open source software.

https://github.com/daviddarnes/storage-form

A Web Component that allows you to submit data to local storage
https://github.com/daviddarnes/storage-form

component components customelement customelements webcomponent webcomponents

Last synced: 12 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# `storage-form`

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

**[Demo](https://daviddarnes.github.io/storage-form/demo.html)** | **[Further reading](https://darn.es/storage-form-web-component/)**

## Examples

General usage example:

```html



Example


Submit

```

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

```html




Dark mode



Light mode



Default mode






High contrast

```

Example of hooking into `storage` event:

```html



Update the
output
by checking the box



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

```

## 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](https://www.npmjs.com/package/@daviddarnes/storage-form): `npm install @daviddarnes/storage-form`
1. [Download the source manually from GitHub](https://github.com/daviddarnes/storage-form/releases) into your project.
1. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

### Usage

Make sure you include the `` in your project (choose one of these):

```html
<!-- Host yourself -->
<script type="module" src="storage-form.js">
```

```html

```

```html

```

## Credit

With thanks to the following people:

- [Zach Leatherman](https://zachleat.com) for inspiring this [Web Component repo template](https://github.com/daviddarnes/component-template)
- [Nathan Knowler](https://knowler.dev) for proposing a solution to the `updateForm()` method