Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/centrefordigitalhumanities/html-swap


https://github.com/centrefordigitalhumanities/html-swap

Last synced: 10 days ago
JSON representation

Awesome Lists containing this project

README

        

`html-swap` is roughly what you would expect it to be; it is a way to dynamically swap in some new html.
This is for you if:

- you have used `htmx`, but decided that its main feature is the `oob-swap` and that it has too many bells and whistles for just that
- you are (rightfully) very excited about using webassembly

Also, this does not require you to understand how to use `npm` or `yarn` or whatever is cool at the moment of reading.
Just download the `pkg` zipfile, extract, and reference it as described below.

## 🚴 Usage

### 🛠️ Build with `wasm-pack build`

```
wasm-pack build --target no-modules
```

Or download the `pkg.zip` from the releases.

### Include in your base HTML

```html

const { swap_ctl } = wasm_bindgen;
async function run() {
await wasm_bindgen();
}
run();

```

Then in your HTML mark elements that serve as a swap controller by changing the `onclick` behavior to `swap_ctl(this); return false;`.
The supported tags are `a` tags which will issue a get request and `input` elements with a type of `submit` which will perform the relevant form action.

```html


```

```html
Link
```

This will prevent default behavior of showing the response and instead will update the page with the response.
A response is comprised of a series of updates that apply to slots on the page.

For instance, consider the following `index.html`

```html
Link

Default content

```

And the following `example.html`

```html

Updated Content

```

By clicking the anchor tag you will replace the inner html of the initial slot with the content in the slot from the response.
You can perform multiple updates by providing multiple elements in the same response:

`index.html`

```html
Link

Default content

Default content

```

`example.html`

```html

Update 1

Update 2

```

### Notes

- This only works with `input` and `a` tags
- For the `input` tag method it must be part of a form and of type submit
- yes, you have to append `return false` to every invocation (or wrap it in a custom function)