From 31a59b60b855858d03a96a4e1777e4e9e5936e0b Mon Sep 17 00:00:00 2001 From: mkelvers Date: Fri, 5 Jun 2026 16:32:31 +0200 Subject: [PATCH] feat: dedupe after htmx swap on swap target --- static/dedupe.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/static/dedupe.ts b/static/dedupe.ts index 320f2b9..9f0f230 100644 --- a/static/dedupe.ts +++ b/static/dedupe.ts @@ -32,6 +32,32 @@ const dedupe = (): void => { }); }; +const dedupeSwapTarget = (target: EventTarget | null): void => { + if (!(target instanceof HTMLElement)) { + return; + } + + if (target.matches("[data-id]")) { + const parent = target.parentElement; + if (parent) { + dedupeWithin(parent); + } + return; + } + + const containers = new Set(); + const elements = target.querySelectorAll("[data-id]"); + elements.forEach((item) => { + if (item.parentElement) { + containers.add(item.parentElement); + } + }); + + containers.forEach((container) => { + dedupeWithin(container); + }); +}; + if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", dedupe); } else { @@ -39,3 +65,8 @@ if (document.readyState === "loading") { } window.addEventListener("load", dedupe); + +document.addEventListener("htmx:afterSwap", (event: Event): void => { + const customEvent = event as CustomEvent<{ target?: EventTarget | null }>; + dedupeSwapTarget(customEvent.detail?.target ?? event.target); +});