Skip to content

Commit

Permalink
fix(swap): apply swap delay in swap function instead of handleAjaxRes…
Browse files Browse the repository at this point in the history
…ponse
  • Loading branch information
boonedox committed Dec 27, 2024
1 parent 27fc37c commit f2e9b87
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -1796,6 +1796,30 @@ var htmx = (function() {
return oobElts.length > 0
}

/**
* Apply swapping class and then execute the swap with optional delay
* @param {string|Element} target
* @param {string} content
* @param {HtmxSwapSpecification} swapSpec
* @param {SwapOptions} [swapOptions]
*/
function swap(target, content, swapSpec, swapOptions) {
if (!swapOptions) {
swapOptions = {}
}

target = resolveTarget(target)
target.classList.add(htmx.config.swappingClass)
const localSwap = function() {
runSwap(target, content, swapSpec, swapOptions)
}
if (swapSpec?.swapDelay && swapSpec.swapDelay > 0) {
getWindow().setTimeout(localSwap, swapSpec.swapDelay)
} else {
localSwap()
}
}

/**
* Implements complete swapping pipeline, including: focus and selection preservation,
* title updates, scroll, OOB swapping, normal swapping and settling
Expand All @@ -1804,7 +1828,7 @@ var htmx = (function() {
* @param {HtmxSwapSpecification} swapSpec
* @param {SwapOptions} [swapOptions]
*/
function swap(target, content, swapSpec, swapOptions) {
function runSwap(target, content, swapSpec, swapOptions) {
if (!swapOptions) {
swapOptions = {}
}
Expand Down Expand Up @@ -4695,8 +4719,6 @@ var htmx = (function() {
swapSpec.ignoreTitle = ignoreTitle
}

target.classList.add(htmx.config.swappingClass)

// optional transition API promise callbacks
let settleResolve = null
let settleReject = null
Expand Down Expand Up @@ -4783,12 +4805,7 @@ var htmx = (function() {
})
}
}

if (swapSpec.swapDelay > 0) {
getWindow().setTimeout(doSwap, swapSpec.swapDelay)
} else {
doSwap()
}
doSwap()
}
if (isError) {
triggerErrorEvent(elt, 'htmx:responseError', mergeObjects({ error: 'Response Status Error Code ' + xhr.status + ' from ' + responseInfo.pathInfo.requestPath }, responseInfo))
Expand Down

0 comments on commit f2e9b87

Please sign in to comment.