Skip to content

Commit f91f654

Browse files
committed
update doco
1 parent c1bea43 commit f91f654

2 files changed

Lines changed: 63 additions & 32 deletions

File tree

src/htmx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,8 +1908,8 @@ var htmx = (function() {
19081908
// @ts-ignore
19091909
end: activeElt ? activeElt.selectionEnd : null
19101910
}
1911-
if (swapSpec.settleDelay !== undefined) {
1912-
oobSettleInfo = undefined // for oobSwaps with settle modifier make and perform own settleInfo
1911+
if (swapSpec.settleDelay !== undefined || swapSpec.swapDelay != undefined) {
1912+
oobSettleInfo = undefined // for oobSwaps with swap or settle modifier make and perform own settleInfo
19131913
}
19141914
const settleInfo = oobSettleInfo || makeSettleInfo(target)
19151915

www/content/attributes/hx-swap-oob.md

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = """\
77
+++
88

99
The `hx-swap-oob` attribute allows you to specify that some content in a response should be
10-
swapped into the DOM somewhere other than the target, that is "Out of Band". This allows you to piggyback updates to other element updates on a response.
10+
swapped into the DOM somewhere other than the target, that is "Out of Band". This allows you to update multiple elements on a page with a single request.
1111

1212
Consider the following response HTML:
1313

@@ -23,44 +23,71 @@ Consider the following response HTML:
2323

2424
The first div will be swapped into the target the usual manner. The second div, however, will be swapped in as a replacement for the element with the id `alerts`, and will not end up in the target.
2525

26-
The value of the `hx-swap-oob` can be:
26+
### Syntax Options
2727

28-
* `true`
29-
* any valid basic [`hx-swap`](@/attributes/hx-swap.md) strategy (innerHTML, outerHTML, beforebegin, etc)
30-
* any valid basic [`hx-swap`](@/attributes/hx-swap.md) strategy, followed by a colon, followed by a CSS selector
31-
* any valid complex [`hx-swap`](@/attributes/hx-swap.md) value including modifiers like `target:` to set the target of the swap
28+
The value of the `hx-swap-oob` attribute can be:
29+
30+
* `true` - Uses the default `outerHTML` swap strategy with ID-based targeting
31+
* Any valid basic [`hx-swap`](@/attributes/hx-swap.md) strategy (innerHTML, outerHTML, beforebegin, etc)
32+
* Any valid basic [`hx-swap`](@/attributes/hx-swap.md) strategy, followed by a colon, followed by a target CSS selector
33+
* Any valid complex [`hx-swap`](@/attributes/hx-swap.md) value including modifiers like `target:` to set the target of the swap
34+
35+
### Behavior Details
3236

3337
If the value is `true` or `outerHTML` (which are equivalent) the element will be swapped inline.
3438

35-
If a swap strategy is given, that swap strategy will be used and the encapsulating tag pair will be stripped for all strategies other than `outerHTML`. You can now use `strip:true` modifier to enable tag stripping for `outerHTML` or `strip:false` to disable it for the other strategies.
39+
If a different swap strategy than `true`/`outerHTML` is supplied the encapsulating tag pair will be stripped so only the inner contents of the element will be used. You can now use `strip:true` modifier to enable tag stripping for `outerHTML` or `strip:false` to disable it for the other strategies when required.
3640

3741
If a selector is given, all elements matched by that selector will be swapped. If not, the element on the page with an ID matching that of the oob element will be swapped instead.
3842

39-
If you include any [`hx-swap`](@/attributes/hx-swap.md) modifers (e.g. `innerHTML swap:1s` to delay the swap) then you need to also use `target:<Selector>` if you want to target something other than the oob elements ID instead of using the basic colon followed by a selector. So `innerHTML:#foo` with a delay becomes `innerHTML swap:1s target:#foo`.
43+
### Modifers
44+
45+
The following modifers from [`hx-swap`](@/attributes/hx-swap.md) can now be included after the swap strategy sperated by spaces:
4046

41-
You can include `strip:true` as a modifer to allow you to override the `outerHTML` swap strategy to remove the encapsulating tag pair and allow you to swap in just the inner nodes of the oob element instead
47+
* `transition:` - Wrap the oob swap in its own [View Transition](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API)
48+
* `swap:`/`settle:` - Delay the swap or settle time for the oob swap and make its settle independant from the main swap
49+
* `scroll:`/`show:`/`focus-scroll:` - Control the scrolling behaviour of the oob swap
50+
* `strip:` - Override the stripping or not of the oob elements encapsulating tag pair
51+
* `target:` - Set a custom CSS selector to use as the target
4252

43-
You can include `strip:false` as a modifer to allow you to override an inner swap strategy like `innerHTML` or `beforeend` to keep the encapsulating tag pair and swap in the whole oob element instead of just its inner contents.
53+
Before modifers were supported the swap strategy value could not contain spaces and the target selector was placed after a colon like `innerHTML:#status`. Now when the swap value contains a space it is parsed as swap modifers and you have to be explicit and use the `target:<selector>` modifier like `innerHTML swap:1s target:#status`.
4454

4555
### Using alternate swap strategies
4656

47-
As mentioned previously when using swap strategies other than `true` or `outerHTML` the encapsulating tags are stripped by default, as such you need to excapsulate the returned data with the correct tags for the context.
57+
Here are some examples of the various swap strategies:
58+
59+
```html
60+
<!-- Basic usage with ID targeting -->
61+
<div id="notifications" hx-swap-oob="true">New notification!</div>
62+
63+
<!-- Using innerHTML with explicit selector -->
64+
<div hx-swap-oob="innerHTML:#status">Processing complete</div>
65+
66+
<!-- With timing modifier -->
67+
<div hx-swap-oob="innerHTML swap:1s target:#status">Processing complete</div>
68+
69+
<!-- With scrolling -->
70+
<div hx-swap-oob="beforeend scroll:bottom target:#log">New log entry</div>
71+
72+
<!-- With transition effect -->
73+
<div id="animated-element" hx-swap-oob="outerHTML transition:true">Updated content</div>
74+
```
75+
76+
### Proper Element Encapsulation
77+
78+
As mentioned previously when using swap strategies other than `true` or `outerHTML` the encapsulating tags are stripped by default, however you still need to excapsulate the returned data with the correct tags for the content so it can be parsed as valid html.
4879

4980
When trying to insert a `<tr>` in a table that uses `<tbody>`:
5081
```html
51-
<tbody hx-swap-oob="beforeend target:#table tbody">
52-
<tr>
53-
...
54-
</tr>
82+
<tbody hx-swap-oob="beforeend:#table tbody">
83+
<tr>...</tr>
5584
</tbody>
5685
```
5786

5887
A "plain" table:
5988
```html
6089
<table hx-swap-oob="beforeend target:#table2">
61-
<tr>
62-
...
63-
</tr>
90+
<tr>...</tr>
6491
</table>
6592
```
6693

@@ -78,31 +105,34 @@ A `<p>` can be encapsulated in `<div>` or `<span>`:
78105
</span>
79106
```
80107

81-
You can also now use `template` tag as this should work for nearly any tag type:
108+
You can also now use `template` tag as a universal tag that can encapsulate all tag types:
82109
```html
83110
<template hx-swap-oob="beforeend target:#table tbody">
84-
<tr>
85-
...
86-
</tr>
111+
<tr>...</tr>
87112
</template>
88113
```
89114

115+
### Overriding Element Encapsulation
116+
90117
Another new option is the `strip:true` swap modifier that allows you to replace an element with multiple nodes:
91118
```html
92119
<div id="foo" hx-swap-oob="outerHTML strip:true">
93-
<div id="foo2">
94-
Replace original
95-
</div>
96-
<div>
97-
And add something more
98-
</div>
120+
<div id="foo2">Replace original</div>
121+
<div>And add something more</div>
122+
</div>
123+
```
124+
125+
You can also use `strip:false` to allow you to place the oob element itself in various locations
126+
```html
127+
<!-- insert after #username -->
128+
<div hx-swap-oob="afterend strip:false target:#username">
129+
User Name is already taken!
99130
</div>
100131
```
101132

102133
### Troublesome Tables and lists
103134

104-
Note that you can use a `template` tag to encapsulate types of elements that, by the HTML spec, can't stand on their own in the
105-
DOM (`<tr>`, `<td>`, `<th>`, `<thead>`, `<tbody>`, `<tfoot>`, `<colgroup>`, `<caption>`, `<col>` & `<li>`).
135+
Note that you can use a `template` tag to encapsulate types of elements that, by the HTML spec, can not be placed adjacent to other normal tags in the DOM (`<tr>`, `<td>`, `<th>`, `<thead>`, `<tbody>`, `<tfoot>`, `<colgroup>`, `<caption>`, `<col>` & `<li>`).
106136

107137
Here is an example with an out-of-band swap of a table row being encapsulated in this way:
108138

@@ -118,6 +148,7 @@ Here is an example with an out-of-band swap of a table row being encapsulated in
118148
```
119149

120150
Note that these template tags will be removed from the final content of the page.
151+
When the main content node tag is one of the restricted ones you may also need to wrap the oob nodes.
121152

122153
### Slippery SVGs
123154

0 commit comments

Comments
 (0)