Skip to content

Commit 4184d1f

Browse files
authored
fix(swap): apply swap delay in swap function instead of handleAjaxResponse (#2845)
* fix(swap): apply swap delay in swap function instead of handleAjaxResponse * add swap delay test
1 parent 6d238f3 commit 4184d1f

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/htmx.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,30 @@ var htmx = (function() {
18501850
return oobElts.length > 0
18511851
}
18521852

1853+
/**
1854+
* Apply swapping class and then execute the swap with optional delay
1855+
* @param {string|Element} target
1856+
* @param {string} content
1857+
* @param {HtmxSwapSpecification} swapSpec
1858+
* @param {SwapOptions} [swapOptions]
1859+
*/
1860+
function swap(target, content, swapSpec, swapOptions) {
1861+
if (!swapOptions) {
1862+
swapOptions = {}
1863+
}
1864+
1865+
target = resolveTarget(target)
1866+
target.classList.add(htmx.config.swappingClass)
1867+
const localSwap = function() {
1868+
runSwap(target, content, swapSpec, swapOptions)
1869+
}
1870+
if (swapSpec?.swapDelay && swapSpec.swapDelay > 0) {
1871+
getWindow().setTimeout(localSwap, swapSpec.swapDelay)
1872+
} else {
1873+
localSwap()
1874+
}
1875+
}
1876+
18531877
/**
18541878
* Implements complete swapping pipeline, including: focus and selection preservation,
18551879
* title updates, scroll, OOB swapping, normal swapping and settling
@@ -1858,7 +1882,7 @@ var htmx = (function() {
18581882
* @param {HtmxSwapSpecification} swapSpec
18591883
* @param {SwapOptions} [swapOptions]
18601884
*/
1861-
function swap(target, content, swapSpec, swapOptions) {
1885+
function runSwap(target, content, swapSpec, swapOptions) {
18621886
if (!swapOptions) {
18631887
swapOptions = {}
18641888
}
@@ -4790,8 +4814,6 @@ var htmx = (function() {
47904814
swapSpec.ignoreTitle = ignoreTitle
47914815
}
47924816

4793-
target.classList.add(htmx.config.swappingClass)
4794-
47954817
// optional transition API promise callbacks
47964818
let settleResolve = null
47974819
let settleReject = null
@@ -4878,12 +4900,7 @@ var htmx = (function() {
48784900
})
48794901
}
48804902
}
4881-
4882-
if (swapSpec.swapDelay > 0) {
4883-
getWindow().setTimeout(doSwap, swapSpec.swapDelay)
4884-
} else {
4885-
doSwap()
4886-
}
4903+
doSwap()
48874904
}
48884905
if (isError) {
48894906
triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({ error: 'Response Status Error Code ' + xhr.status + ' from ' + responseInfo.pathInfo.requestPath }, responseInfo))

test/core/api.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ describe('Core htmx API test', function() {
483483
output.innerHTML.should.be.equal('<div>Swapped!</div>')
484484
})
485485

486+
it('swap works with a swap delay', function(done) {
487+
var div = make("<div hx-get='/test'></div>")
488+
div.innerText.should.equal('')
489+
htmx.swap(div, 'jsswapped', { swapDelay: 10 })
490+
div.innerText.should.equal('')
491+
setTimeout(function() {
492+
div.innerText.should.equal('jsswapped')
493+
done()
494+
}, 30)
495+
})
496+
486497
it('swaps content properly (with select)', function() {
487498
var output = make('<output id="output"/>')
488499
htmx.swap('#output', '<div><p id="select-me">Swapped!</p></div>', { swapStyle: 'innerHTML' }, { select: '#select-me' })

0 commit comments

Comments
 (0)