Skip to content

Commit f2e9b87

Browse files
committed
fix(swap): apply swap delay in swap function instead of handleAjaxResponse
1 parent 27fc37c commit f2e9b87

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/htmx.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,30 @@ var htmx = (function() {
17961796
return oobElts.length > 0
17971797
}
17981798

1799+
/**
1800+
* Apply swapping class and then execute the swap with optional delay
1801+
* @param {string|Element} target
1802+
* @param {string} content
1803+
* @param {HtmxSwapSpecification} swapSpec
1804+
* @param {SwapOptions} [swapOptions]
1805+
*/
1806+
function swap(target, content, swapSpec, swapOptions) {
1807+
if (!swapOptions) {
1808+
swapOptions = {}
1809+
}
1810+
1811+
target = resolveTarget(target)
1812+
target.classList.add(htmx.config.swappingClass)
1813+
const localSwap = function() {
1814+
runSwap(target, content, swapSpec, swapOptions)
1815+
}
1816+
if (swapSpec?.swapDelay && swapSpec.swapDelay > 0) {
1817+
getWindow().setTimeout(localSwap, swapSpec.swapDelay)
1818+
} else {
1819+
localSwap()
1820+
}
1821+
}
1822+
17991823
/**
18001824
* Implements complete swapping pipeline, including: focus and selection preservation,
18011825
* title updates, scroll, OOB swapping, normal swapping and settling
@@ -1804,7 +1828,7 @@ var htmx = (function() {
18041828
* @param {HtmxSwapSpecification} swapSpec
18051829
* @param {SwapOptions} [swapOptions]
18061830
*/
1807-
function swap(target, content, swapSpec, swapOptions) {
1831+
function runSwap(target, content, swapSpec, swapOptions) {
18081832
if (!swapOptions) {
18091833
swapOptions = {}
18101834
}
@@ -4695,8 +4719,6 @@ var htmx = (function() {
46954719
swapSpec.ignoreTitle = ignoreTitle
46964720
}
46974721

4698-
target.classList.add(htmx.config.swappingClass)
4699-
47004722
// optional transition API promise callbacks
47014723
let settleResolve = null
47024724
let settleReject = null
@@ -4783,12 +4805,7 @@ var htmx = (function() {
47834805
})
47844806
}
47854807
}
4786-
4787-
if (swapSpec.swapDelay > 0) {
4788-
getWindow().setTimeout(doSwap, swapSpec.swapDelay)
4789-
} else {
4790-
doSwap()
4791-
}
4808+
doSwap()
47924809
}
47934810
if (isError) {
47944811
triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({ error: 'Response Status Error Code ' + xhr.status + ' from ' + responseInfo.pathInfo.requestPath }, responseInfo))

0 commit comments

Comments
 (0)