Skip to content

Commit

Permalink
improve hx-preserve documentation (#2949)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelWest22 authored Oct 7, 2024
1 parent 8a60c69 commit 1242977
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions www/content/attributes/hx-preserve.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ Elements with `hx-preserve` set are preserved by `id` when htmx updates any ance
You *must* set an unchanging `id` on elements for `hx-preserve` to work.
The response requires an element with the same `id`, but its type and other attributes are ignored.

Note that some elements cannot unfortunately be preserved properly, such as `<input type="text">` (focus and caret position are lost), iframes or certain types of videos. To tackle some of these cases we recommend the [morphdom extension](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/morphdom-swap/README.md), which does a more elaborate DOM
reconciliation.

## OOB Swap Usage

You can include `hx-preserve` in the inner response of a [hx-swap-oob](@/attributes/hx-swap-oob.md) and it will preserve the element unchanged during the out of band partial replacement as well. However, you cannot place `hx-preserve` on the same element as the `hx-swap-oob` is placed. For example, here is an oob response that replaces notify but leaves the retain div unchanged.

```html
<div id="notify" hx-swap-oob="true">
<p>The below content will not be changed</p>
<div id="retain" hx-preserve>Use the on-page contents</div>
</div>
```

## Notes

* `hx-preserve` is not inherited
* `hx-preserve` can cause elements to be relocated to a new location when swapping in a partial response
* You can use `hx-preserve="true"` or use it as a boolean attribute with just `hx-preserve`
* Some elements cannot unfortunately be preserved properly, such as `<input type="text">` (focus and caret position are lost), iframes or certain types of videos. To tackle some of these cases we recommend the [morphdom extension](https://github.com/bigskysoftware/htmx-extensions/blob/main/src/morphdom-swap/README.md), which does a more elaborate DOM
reconciliation
* When using [History Support](@/docs.md#history) for actions like the back button `hx-preserve` elements will also have their state preserved
* Avoid using [hx-swap](@/attributes/hx-swap.md) set to `none` with requests that could contain a `hx-preserve` element to avoid losing it
* `hx-preserve` can cause elements to be removed from their current location and relocated to a new location when swapping in a partial/oob response
```html
<div id="new_location">
Just relocated the video here
<div id="video" hx-preserve></div>
</div>
```
* Can be used on the inside content of a [hx-swap-oob](@/attributes/hx-swap-oob.md) element
```html
<div id="notify" hx-swap-oob="true">
Notification updated but keep the same retain
<div id="retain" hx-preserve></div>
</div>
```

0 comments on commit 1242977

Please sign in to comment.