fetchable-element
Composition base that owns the fetch() lifecycle for Neutron elements —
build the request from attributes, a <form>, or a custom override, then
track loading / success / error state automatically.
Features
Shared lifecycle States,
provisionand events come fromloadable-element Attribute-driven requests URL, method, headers, redirect, and credentials all configurable declaratively
Form-aware Point
form-refat a<form>to source action, method, enctype, and field valuesMerged payloads Attributes, form, and custom args deep-merge (lowest → highest priority)
Lifecycle state
is-loading/is-success/is-errormanaged for youProvision
provisionis the parsed response (or error payload) for Quarkprop("provision")— not a reflected attributeCancel-safe Superseded or disconnected requests are aborted via
AbortableElement
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/fetchable-element@0.3.0/dist/index.umd.min.js"></script>npm install @excom/fetchable-elementimport { /* … */ } from "@excom/fetchable-element";Peer dependencies (0)
Packages a consumer must install alongside this one. Workspace deps are bundled.
| Package | Version |
|---|---|
|
Usage
Compose FetchableElement, then call doFetch([url, requestInit]) —
usually built via getFetchArgs(customFetchArgs?) — whenever the
subclass decides a request should run. Concrete consumers include
<provider-fetch> (fetch on attribute change), <super-form> (fetch on
submit), and <web-authn> (WebAuthn ceremonies that still round-trip to
a server).
import { Neutron } from "@excom/neutron";
import { FetchableElement } from "@excom/fetchable-element";
export const RefreshOnClick = Neutron.compose([
FetchableElement,
Neutron({ tag: "refresh-on-click" }),
])
.onEvent("click", ({ getFetchArgs }) => ({
doFetch: [getFetchArgs()],
}));
RefreshOnClick.define();import { Neutron } from "@excom/neutron";
import { FetchableElement } from "@excom/fetchable-element";
export const RefreshOnClick = Neutron.compose([
FetchableElement,
Neutron({ tag: "refresh-on-click" }),
])
.onEvent("click", ({ getFetchArgs }) => ({
doFetch: [getFetchArgs()],
}));
RefreshOnClick.define();<refresh-on-click api-url="/api/status"></refresh-on-click><refresh-on-click api-url="/api/status"></refresh-on-click>Every prop, state field, and event documented below is inherited
verbatim by any element that composes FetchableElement — it flattens
directly into that element's own generated docs, so <provider-fetch>
and friends don't redeclare it.
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).
Attributes (16)
Role —option is configurable, state
is managed by the element (read-only), or hybrid which is both.
api-method
apiMethod
string
"GET"api-url
form-ref or custom doFetch() args) is merged in as query params instead.
apiUrl
string
""fetch-credentials
RequestInit.credentials mode.
fetchCredentials
string
"omit" | "same-origin" | "include""include"fetch-redirect
RequestInit.redirect mode. Unset defers to the browser default (follow).
fetchRedirect
string
"follow" | "error" | "manual"nullform-ref
<form> to source the request from — its action (URL), method, enctype (Content-Type), and field values (as the JSON payload) all take priority over the matching attributes below. Omit to build the request entirely from attributes / custom doFetch() args.
formRef
string
<CSS Selector>nullhas-body
GET / HEAD). Already implied for POST / PUT / PATCH.
hasBody
boolean
falseheader-accept
Accept request header.
headerAccept
string
"application/json"header-cache-control
Cache-Control request header. Unset by default (browser default caching applies).
headerCacheControl
string
nullheader-content-type
Content-Type request header. Dropped entirely when the request has no body.
headerContentType
string
"application/json"did-load
[did-load] rather than [is-success]. Cleared on error.
didLoad
boolean
falseis-error
AbortError). Fires with the error event.
isError
boolean
falseis-error
is-loading and is-success.
isError
boolean
falseis-loading
isLoading
boolean
falseis-loading
isLoading
boolean
falseis-success
is-loading and is-error.
isSuccess
boolean
falseis-success
is-loading and is-error.
isSuccess
boolean
falseProvision (2)
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
{ status, statusText, ok, headers, url, redirected, bodyUsed, type, body }. Failure shape is either that same response shape (server responded with an error status) or { message, stack } (request never completed). Not reflected as an attribute.
provision
FetchResponse
provision
provision
unknown
Events (6)
e.preventDefault() is not
synchronously called on the event.
Type
{tag}-error
event.detail is the error payload (see provision). Not dispatched for aborted requests.
Type
FetchableErrorEvent
{tag}-error
is-error is set. event.detail is the error payload (also stored as provision).
Type
LoadableErrorEvent
{tag}-loading
Type
FetchableLoadingEvent
{tag}-loading
is-loading is set (work started).
Type
LoadableLoadingEvent
{tag}-success
event.detail is the parsed response (see provision).
Type
FetchableSuccessEvent
{tag}-success
is-success is set. event.detail is the new provision.
Type
LoadableSuccessEvent
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 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 (4)
0.3.0
- Add hydration of prerendered pages: an identical GET or HEAD request is answered from the page's hydration island, as often as the prerender made it, so the element announces
loadingthensuccesswithout a network request
0.2.0
- Add
did-loadfromloadable-element: set on the first success, kept while a refresh runs, cleared on an error - Update failed-request logging to one line: an error status (400 or above) logs a warning,
<tag>: request failedwith the response, which the defaultKitLoggerlevel hides; a network or parse failure logs an error; an abort logs nothing
0.1.2
- Fix typo in error message.
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.
There are no demos for this package — see
FetchableElement in
action against a real endpoint.