Skip to content

fix: preserve details open state across htmx swaps#918

Draft
inoovador wants to merge 1 commit intoNixOS:mainfrom
inoovador:fix/preserve-details-open-state
Draft

fix: preserve details open state across htmx swaps#918
inoovador wants to merge 1 commit intoNixOS:mainfrom
inoovador:fix/preserve-details-open-state

Conversation

@inoovador
Copy link
Copy Markdown

Summary

  • Fixes Don't collapse ignored items sections on changing collections #913: <details> sections (e.g. "Ignored packages", "Ignored maintainers") no longer collapse when ignoring or restoring items
  • Adds a small inline script after htmx.min.js in base.html that saves the open state of <details> elements (by ID) before an htmx swap and restores it after the swap completes
  • Uses htmx:beforeSwap and htmx:afterSwap events as suggested in htmx#2195

How it works

  1. On htmx:beforeSwap, the script collects the IDs of all <details open> elements inside the swap target
  2. On htmx:afterSwap, it finds those elements by ID in the new content and sets the open attribute back

Only <details> elements with an id attribute are tracked, which currently covers the ignored packages and ignored maintainers sections — exactly the ones affected by this issue.

Test plan

  • Open a suggestion with multiple ignored packages
  • Expand the "Ignored packages" section
  • Click "Restore" on one package — verify the section stays open
  • Repeat for "Ignored maintainers" section
  • Verify the "Ignore" action on active items also preserves open state of other <details> sections
  • Verify non-JS fallback still works (form submits normally, page reloads)

🤖 Generated with Claude Code

Copy link
Copy Markdown
Collaborator

@fricklerhandwerk fricklerhandwerk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs tests :)

// Preserve the open/closed state of <details> elements across htmx swaps.
// Without this, expanded "Ignored packages" or "Ignored maintainers"
// sections collapse every time an item is ignored or restored.
// See: https://github.com/NixOS/nix-security-tracker/issues/913
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to the author of that snippet and give proper credit

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.

Approach inspired by @kizmonavt (bigskysoftware/htmx#2195).

Fixes NixOS#913
@inoovador inoovador force-pushed the fix/preserve-details-open-state branch from 9727024 to cf13c52 Compare March 29, 2026 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't collapse ignored items sections on changing collections

2 participants