(H)ypertext e(x)tension for the browser.
Hx
enables <a>
and <form>
elements to optionally fetch hypertext and update the DOM without page refreshes.
Add hx-js
to an html
document.
<script
type="module"
src="https://raw.githubusercontent.com/wolfpup-software/hx-js/main/hx/dist/hx.js">
</script>
Anchor and form elements with an hx-projection
attribute will fetch html fragments
and update the dom.
<!-- anchors -->
<a href="/document/fragment" target="ul" hx-projection="start"> click me! </a>
<!-- forms -->
<form
action="/post/something"
method="post"
target="li:last-child"
hx-projection="replace">
<input type="submit" />
</form>
Elements without the hx-projection
attribute behave like normal <a>
and <form>
elements.
Hx
queries an element using the target
attribute.
A target
value can be:
_target
-> thetarget
property of an hx event._currentTarget
-> thecurrentTarget
property for hx event_document
-> the document- any valid CSS selector.
The hx-projection
property defines how a fragment
is projected onto a document
relative to the target
element.
An hx-projection
properties can have the following values:
none
-> nothing will happenbefore
-> insert a fragment before the target elementafter
-> insert a fragment after the target elementremove
-> remove the target elementreplace
-> place the target element with a fragmentstart
-> insert a fragment before the target element descendantsend
-> append a fragment after the target element descendantsremove_children
-> remove the target element descendantsreplace_children
-> replace the target element descendants with a fragment
The hx-status
attribute is used to reflect the state of a hypertext request onto the original <a>
or <form>
element.
The following values will be applied:
- requested
- response-error
- responded
- projection-error
- projected
<a
href="/document/fragment"
target="ul"
hx-projection="start"
hx-status="requested">
click me!
</a>
The hx-status-code
attribute is used to signal request state to the original <a>
or <form>
element
<a
href="/document/fragment"
target="ul"
hx-projection="start"
hx-status="responded"
hx-status-code="200">
click me!
</a>
The hx-composed
attribute adds the composed
property to an event allowing hx events to propagate through a shadow root.
<a href="/document/fragment" target="ul" hx-projection="start" hx-composed>
click me!
</a>
Hx
is designed to work with the DOM.
It's modular and easily extensible.
In lieu of "expressive" apis, Hx
staggers small hypertext jumps into a series of DOM events:
- hx-request events
- hx-response events
- hx-projection events
The hx-request
module dispatches hx-request events from <a>
and <form>
elements with an hx-projection
attribute.
The hx-response
module dispatches hx-response events after recieving valid hx-request events.
The hx-project
module dispatches an hx-project event after placing a fragment into a document or shadow dom.
Every step is opt-in. Pups can listen to events to drop invalid requests and react to projections.
Deprecation by RFC. The browser should already be doing this.
Maybe not the same events, maybe not the same attributes, but little hypertext jumps should already be browser spec.
Until then, hx
intends to be a polyfil (at 1.5kb minified, zipped).
Hx-js
is released under the BSD 3-Clause License.