Open volfpeter opened 3 months ago
Good idea imo! If you'd like to work on a PR and suggest an implementation, feel free to do so!
I'm not familiar with the internals of HTMX yet, so it would take time for me to learn the necessary things (e.g. what to override in the event details) and implement (and test) this feature (I guess the pattern would be the same as for the target override). I would be happy to contribute this feature, but I can not promise for sure as my time is quite limited for the coming months. I'll leave a comment here if I can start work on this.
Made some small change 😄 tested on my project.
var swapPrefix = "hx-swap-";
// It's the same as getRespCodeTarget but the return value is the swap style string
function getSwapStyle(elt, respCodeNumber, prefix) {
if (!elt || !respCodeNumber) return null;
var respCode = respCodeNumber.toString();
var attrPossibilities = [
respCode,
respCode.substr(0, 2) + "*",
respCode.substr(0, 2) + "x",
respCode.substr(0, 1) + "*",
respCode.substr(0, 1) + "x",
respCode.substr(0, 1) + "**",
respCode.substr(0, 1) + "xx",
"*",
"x",
"***",
"xxx",
];
if (startsWith(respCode, "4") || startsWith(respCode, "5")) {
attrPossibilities.push("error");
}
for (var i = 0; i < attrPossibilities.length; i++) {
var attr = prefix + attrPossibilities[i];
var attrValue = api.getClosestAttributeValue(elt, attr);
if (attrValue) {
return attrValue;
}
// Uncomment this for more specific swap styles
// if (
// attrValue == "innerHTML" ||
// attrValue == "outerHTML" ||
// attrValue == "beforebegin" ||
// attrValue == "afterbegin" ||
// attrValue == "beforeend" ||
// attrValue == "afterend" ||
// attrValue == "delete" ||
// attrValue == "none" ||
// attrValue == string
// ) {
// return attrValue;
// }
}
return null;
}
// Get the swap style inside the onEvent callback
var swapMethod = getSwapStyle(
evt.detail.requestConfig.elt,
evt.detail.xhr.status,
swapPrefix
);
// Add the found swap style to the event detail
if (swapMethod) {
evt.detail.swapStyle = swapMethod;
}
// If the swap style is not found, the default swap style from config will be used
Hi guys,
First of all, thanks for the great work on HTMX and its extensions. I just started a new project with it, and I must say I'm surprised how convenient it is to work with.
I ran into one issue though with the
response-targets
extension: it would be really handy if it was possible to override thehx-swap
attribute similarly tohx-target
, because the two are tightly related.In my specific case, I'd need to replace a
delete
swap rule withinnerHTML
. I know I could tweak my components and API to be able to use the same swap rule, but that would really degrade the code (especially the API). My current workaround is to include a custom header in the HX request that triggers a custom "reswap" logic (HX-Reswap
response header) on the backend, effectively implementinghx-swap-<error-code>
manually on the backend, but solving this problem with anhx-swap-<error-code>
attribute in the HTML would be way cleaner and more elegant in my opinion.