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
- Host: GitHub
- URL: https://github.com/daviddarnes/storage-form
- Owner: daviddarnes
- License: mit
- Created: 2023-12-11T08:01:58.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-15T17:12:15.000Z (almost 2 years ago)
- Last Synced: 2025-04-12T02:05:42.708Z (12 months ago)
- Topics: component, components, customelement, customelements, webcomponent, webcomponents
- Language: HTML
- Homepage: https://daviddarnes.github.io/storage-form/demo.html
- Size: 17.6 KB
- Stars: 31
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
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