provider-storage
Read a JSON value out of localStorage / sessionStorage into provision,
declaratively — and keep it in sync across tabs. No app JS required to display
cached client state.
Features
- Declarative read Point
key-nameat a storage key and read the result - Local or session
store-namepickslocalStorage(default) /sessionStorage - Live across tabs A write from another tab re-reads and fires
provider-storage-changed - Reactive to attributes Changing
key-name/store-namere-reads immediately - Tiny, read-only Never writes
Installation
This package is available in the
<script src="https://unpkg.com/@excom/kit-utils@0.3.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/neutron@0.2.0/dist/index.umd.min.js"></script>
<script src="https://unpkg.com/@excom/provider-storage@0.1.4/dist/index.umd.min.js"></script>npm install @excom/provider-storage<!-- import path to `node_modules` will depend on your build setup -->
<script type="module" src="/node_modules/@excom/provider-storage"></script>
<link rel="stylesheet" href="/node_modules/@excom/provider-storage">import "@excom/provider-storage";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Usage
<provider-storage key-name="user-preferences"></provider-storage>
<provider-storage key-name="checkout-draft" store-name="session"></provider-storage><provider-storage key-name="user-preferences"></provider-storage>
<provider-storage key-name="checkout-draft" store-name="session"></provider-storage>Read-only. It reads on every key-name / store-name set/change and never
writes. Removing key-name clears provision and both states.
Live across tabs, not within one. Browsers fire storage only in other
tabs, so:
- A write from another tab (or a
clear()there) re-reads and firesprovider-storage-changed— no app code needed.sessionStorageis per-tab, so this only applies tostore-name="local". - A write by your own app code (
localStorage.setItem(...)) in the same tab won't appear until you re-trigger a read — re-setkey-name(e.g. to""and back) after writing.
API Reference
Attributes
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
Provision
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
Events
Type
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements
Child or descendant elements are recognized by and relevant to its functionality.Styles
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
provider-storage
Attributes (4)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
key-name
null if absent). Removing it clears provision and both states.
keyName
string
<storage key>nullstore-name
local survives the tab / browser closing and syncs across tabs; session is per-tab and gone when the tab closes. Changing it re-reads key-name from the new store.
storeName
string
local, session"local"is-error
JSON.parse threw on the last read (the stored value isn't valid JSON). provision is null.
isError
boolean
falseis-success
provision is null, not an error).
isSuccess
boolean
falseProvision (1)
Thisprovision property lives on the DOM node.
Read / watch it with Quark’s prop("provision"), or listen
for the neutron-provision event from app JS.
provision
JSON.parse(store.getItem(keyName)), or null if the key is absent or the read failed. Not reflected as an attribute.
provision
object
Events (1)
e.preventDefault() is not
synchronously called on the event.
Type
provider-storage-changed
storage event from another tab for this element's store-name + key-name (or a clear() of that store) has been re-read into provision — so provision, is-success / is-error are already updated when it fires. The read also publishes neutron-provision, as usual.
Type
ProviderStorageChangedEvent
command event (HTML Command API): from a
<button command commandfor>, an <event-handler
command-name>, or a CommandEvent.
Recognized Elements (0)
Child or descendant elements are recognized by provider-storage and relevant to its functionality.Styles (0)
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
@custom-selector synonyms so custom elements can share
semantic styles. Element aliases match the host tag / .tag-* class; state
aliases match attributes (nested under the element alias).
Release notes (1)
0.1.1
- Ship only dist (and declared extras) in the npm tarball; drop build logs, tests and sources
Release notes
e.preventDefault() is not
synchronously called on the event.
all: revert-layer.
Valence.css ships this as .unstyled and .unstyled-all for the entire tree.
Examples
Seed & re-read
Since this element never writes, the demo below seeds a value from a button
(re-setting key-name afterward to force the same-tab re-read) so you can see
it work without opening devtools. Open this page in a second tab and click
there too — this tab updates on its own.