You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/content/attributes/hx-swap-oob.md
+61-30Lines changed: 61 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ description = """\
7
7
+++
8
8
9
9
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.
11
11
12
12
Consider the following response HTML:
13
13
@@ -23,44 +23,71 @@ Consider the following response HTML:
23
23
24
24
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.
25
25
26
-
The value of the `hx-swap-oob` can be:
26
+
### Syntax Options
27
27
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
32
36
33
37
If the value is `true` or `outerHTML` (which are equivalent) the element will be swapped inline.
34
38
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.
36
40
37
41
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.
38
42
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:
40
46
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
42
52
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`.
44
54
45
55
### Using alternate swap strategies
46
56
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:
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.
48
79
49
80
When trying to insert a `<tr>` in a table that uses `<tbody>`:
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>`).
106
136
107
137
Here is an example with an out-of-band swap of a table row being encapsulated in this way:
108
138
@@ -118,6 +148,7 @@ Here is an example with an out-of-band swap of a table row being encapsulated in
118
148
```
119
149
120
150
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.
0 commit comments