htmx icon indicating copy to clipboard operation
htmx copied to clipboard

hx-swap="scroll:none" not working on a progressively enhanced form

Open samueljcook opened this issue 3 years ago • 3 comments

Prior discussion here: https://github.com/bigskysoftware/htmx/pull/1459#issuecomment-1791420162

Goal: Prevent scroll-to-top behavior after hx-boosted form submissions.

Code that should achieve this per the linked discussion above:

<body hx-swap="innerHTML" hx-boost="true">
  <form method="POST" action="/whatever" hx-swap="scroll:none">

Result: Still scrolls to top.

I believe this is a bug in HTMX and will do some digging in / debugging to find out.

Thanks much

samueljcook avatar Nov 02 '23 19:11 samueljcook

Ok, I see what the issue is.

Syntax that works:

<form hx-swap="show:none>

Syntax that doesn't work:

<form hx-swap="scroll:none>

Under the hood, show calls element.scrollIntoView, and scroll calls element.scrollTop.

Boosted elements get show: top by default. So you don't want to override scroll, you want to override show.

I think this just needs a documentation PR to (1) explain what show and scroll actually mean in terms of the real DOM APIs, and (2) explain that if you want to override it for a boosted element, you need to use show, not scroll.

If you agree happy to pull a PR.

samueljcook avatar Nov 02 '23 22:11 samueljcook

You are right. I added a note in the hx-swap documentation on how to disable the default behavior on a per-element basis in #1459.

xhaggi avatar Nov 03 '23 12:11 xhaggi

Addressed in #1459

samueljcook avatar Nov 03 '23 19:11 samueljcook