🚲 fixi.js - it ain't much...
fixi.js is an experimental, minimalist implementation
of generalized hypermedia controls
fixi.js is the flagship library of the fixi project, a family of minimalist companion libraries.
The fixi api consists of six HTML attributes, nine events & two properties
Here is an example:
<button fx-action="/content" <!-- URL to issue request to -->
fx-method="get" <!-- HTTP Method to use -->
fx-trigger="click" <!-- Event that triggers the request -->
fx-target="#output" <!-- Element to swap -->
fx-swap="innerHTML"> <!-- How to swap -->
Get Content
</button>
<output id="output"></output>
When this fixi-powered button is clicked it will issue an HTTP GET request to the
/content relative URL and swap the HTML content of
the response inside the output tag below it.
Minimalism
Philosophically, fixi is scheme to htmx's
common lisp: it is designed to be as
lean as possible while still
being useful for real world projects.
As such, it doesn't have many of the features found in htmx, including:
fixi takes advantage of some modern JavaScript features not used by htmx:
A hard constraint on the project is that the unminified, uncompressed size must be less than that of
the minified & compressed version of the (excellent) preact library (currently 4.6Kb).
The current uncompressed size is 3473 bytes, the gzipped size is 1473 bytes and the brotli'd size is 1278 bytes,
as determined by:
ls -l fixi.js | awk '{print "raw:", $5}'; gzip -k fixi.js; ls -l fixi.js.gz | awk '{print "gzipped:", $5}'; rm fixi.js.gz; brotli fixi.js; ls -l fixi.js.br | awk '{print "brotlid:", $5}'; rm fixi.js.br
Another goal is that users should be able to debug
fixi easily, since it is small enough to use unminified.
The code style is dense, but the statements are structured for debugging.
Like a fixed-gear bike, fixi has very few moving parts:
- No dependencies (including test and development)
- No JavaScript API (beyond the events)
- No minified
fixi.min.js file
- No
package.json
- No build step
This repo consists of four files:
fixi.js, the code for the library
test.html, the test suite for the library
- This
README.md, which is the documentation
npm.sh, which generates npm releases of the library
test.html is a stand-alone HTML file that implements its own visual testing infrastructure, mocking for
fetch(), etc. and that can be opened using the file: protocol for easy testing.
Installing
fixi is designed to be easily vendored, that is, copied, into your project:
curl https://raw.githubusercontent.com/bigskysoftware/fixi/refs/tags/0.9.4/fixi.js >> fixi-0.9.4.js
The SHA256 of v0.9.4 is
rPuE58bCyMrZ37o4bByX4epgBA1bAeTylp7TxOXVh90=
generated by the following command line script:
cat fixi.js | openssl sha256 -binary | openssl base64
Alternatively can download the source from here:
https://github.com/bigskysoftware/fixi/archive/refs/tags/0.9.4.zip
You can also use the JSDelivr CDN for local development or testing:
<script src="https://cdn.jsdelivr.net/gh/bigskysoftware/fixi@0.9.4/fixi.js"
crossorigin="anonymous"
integrity="sha256-Qalmv6iqaAnrglOx2AWSoLfJFmXLGtjAIdBgsLc3bgg="></script>
Finally, fixi is available on NPM as the fixi-js package.
Support
You can get support for fixi via:
API
Attributes
| attribute |
description |
example |
fx-action |
The URL to which an HTTP request will be issued, required |
fx-action='/demo' |
fx-method |
The HTTP Method that will be used for the request (case-insensitive), defaults to GET |
fx-method='DELETE' |
fx-target |
A CSS selector specifying where to place the response HTML in the DOM, defaults to the current element |
fx-target='#a-div' |
fx-swap |
A string specifying how the content should be swapped into the DOM, can be one of innerHTML, outerHTML, beforebegin, afterbegin, beforeend, afterend, none, or any valid property on the element (e.g. className or value), defaults to outerHTML |
fx-swap='innerHTML' |
fx-trigger |
The event that will trigger a request, defaults to submit for form elements, change for input-like elements & click for all other elements |
fx-trigger='click' |
fx-ignore |
Any element with this attribute on it or on an ancestor will not be processed for fx-* attributes |
|
Modus Operandi
fixi works in a straight-forward manner & I encourage you to look at the source as you read through this. The
three components of fixi are:
- Processing elements in the DOM (or added to the DOM)
- Issuing HTTP requests in response to events
- Swapping new HTML content into the DOM
Processing
The main entry point is found at the bottom of fixi.js: on the DOMContentLoaded event fixi does two things:
- It has a MutationObserver begin to watch for newly added content with fixi-powered elements
- It processes any existing fixi-powered elements
fixi-powered elements are elements with the fx-action attribute on them.
When fixi finds one it will establish an event listener on that element that will dispatch an AJAX request via
fetch() to the URL specified by fx-action.
fixi will ignore any elements that have the fx-ignore attribute on them or on an ancestor.
The event that will trigger the request is determined by the fx-trigger attribute.
If that attribute is not present, the trigger defaults to:
submit for form elements
change for input:not([type=button]), select & textarea elements
click for everything else.
Requests
When a request is triggered, the HTTP method of the request
will be determined by the fx-method attribute. If this attribute is not present, it will default to GET. This
attribute is case-insensitive.
fixi sends the request header FX-Request, with the value
true. You can add or remove headers using the evt.detail.cfg.headers object, see the fx:config event
below.
If an element is within a form element or has a form attribute, the values of that form will be included with the
request. Otherwise, if the element has a name, its name & value will be sent with the request. You can add or
remove values using the evt.detail.cfg.body FormData object in the fx:config event.
GET & DELETE requests will include values via query parameters, other request types will submit them as a form
encoded body.
Before a request is sent, the aforementioned fx:config event is triggered, which can be used to configure
aspects of the request. If preventDefault() is invoked in this event, the request will not be sent.
The evt.detail.cfg.drop property will be truthy
if there is an existing outstanding request associated with the element, otherwise the value will be
falsy. More specifically, the value is equal
to the number of outstanding requests associated with the element. If the value is truthy after the last
fx:config handler has ran to completion, the request will be dropped (i.e. not issued). This implies,
if you do not customize the behavior, an element will drop all
new requests whilst there is an outstanding request associated with it.
In the fx:config event you can also set the evt.detail.cfg.confirm property to a no-argument function.
This function can return a Promise and can be used to asynchronously confirm that the request should be issued:
function showAsynConfirmDialog() {
//... a Promise-based confirmation dialog...
}
document.addEventListener("fx:config", (evt) => {
evt.detail.cfg.confirm = showAsynConfirmDialog;
})
Note that confirmation will only occur if the fx:config event is not canceled and the request is not
dropped.
After the configuration step and the confirmation, if any, the fx:before event will be triggered,
and then a fetch() will be issued. The evt.detail.cfg object from the events above is passed to the fetch()
function as the second RequestInit argument.
When fixi receives a response it triggers the fx:after event.
In this event there are two more properties available on evt.detail.cfg:
response the fetch Response object
text the text of the response