Skip to content

Commit 9727024

Browse files
inoovadorclaude
andcommitted
fix: preserve <details> open state across htmx swaps
When ignoring or restoring items (packages/maintainers), htmx replaces the suggestion element, causing all <details> sections to revert to their default collapsed state. This is disruptive when working through multiple ignored items in a row. Add a small script that listens to htmx:beforeSwap to record which <details> elements (by ID) are currently open, then restores the open attribute on matching elements after htmx:afterSwap completes. Fixes #913 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 4db1eda commit 9727024

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/shared/templates/base.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,25 @@ <h1>
122122
</p>
123123
</footer>
124124
<script src="/static/htmx.min.js"></script>
125+
<script>
126+
// Preserve the open/closed state of <details> elements across htmx swaps.
127+
// Without this, expanded "Ignored packages" or "Ignored maintainers"
128+
// sections collapse every time an item is ignored or restored.
129+
// See: https://github.com/NixOS/nix-security-tracker/issues/913
130+
(function () {
131+
var openDetails = [];
132+
document.addEventListener("htmx:beforeSwap", function (event) {
133+
var target = event.detail.target;
134+
openDetails = Array.from(target.querySelectorAll("details[open][id]"))
135+
.map(function (el) { return el.id; });
136+
});
137+
document.addEventListener("htmx:afterSwap", function () {
138+
openDetails.forEach(function (id) {
139+
var el = document.getElementById(id);
140+
if (el) el.setAttribute("open", "");
141+
});
142+
});
143+
})();
144+
</script>
125145
</body>
126146
</html>

0 commit comments

Comments
 (0)