Skip to content

fix: make scrollTarget:window and showTarget:window scroll the page - #4159

Merged
1cg merged 1 commit into
bigskysoftware:four-devfrom
Enesuygurs:fix/scroll-show-target-window
Nov 1, 2026
Merged

1cg merged 1 commit into
bigskysoftware:four-devfrom
Enesuygurs:fix/scroll-show-target-window

Conversation

@Enesuygurs

Copy link
Copy Markdown

Description

hx-swap="scroll:top scrollTarget:window" and hx-swap="show:top showTarget:window" don't scroll the page. __findExt resolves the window extended selector to the window object, which has no scrollTop and no scrollIntoView. So in the scroll case __handleScroll sets a property that has no effect, and in the show case the scrollIntoView?.() call is silently skipped.

htmx 4 suggests this form as the replacement for the old show:window:top / scroll:window:top syntax, and the upgrade checker rewrites show:window:top to show:top showTarget:window. As a result, upgraded pages lose their scroll-to-top behaviour.

This change maps window to document.scrollingElement for both scrollTarget and showTarget. It also updates the hx-swap reference: its "Scroll the window" example still used the removed scroll:window:top form, which now parses as scroll: "window:top" and does nothing.

<div hx-get="/items" hx-swap="innerHTML scroll:top scrollTarget:window">Load</div>
<div style="height:5000px"></div>

Scroll down and click: before this change the page stays where it is, after it the page scrolls back to the top.

Corresponding issue: related to #3544

Testing

I added two tests to test/tests/attributes/hx-swap.js:

  • scroll:top scrollTarget:window scrolls the page back to the top. Without the fix it fails with expected 1016 to equal +0.
  • show:bottom showTarget:window scrolls the page down. Without the fix it fails with expected +0 to be above +0.

Both pass with the fix. I ran the full suite in Chrome with web-test-runner --config test/web-test-runner.config.mjs and CHROME_PATH set (--concurrency 1): 1777 passed, 0 failed, 3 skipped. I couldn't use npm run test because the Playwright browser build it expects isn't installed on this machine.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@1cg
1cg merged commit 67827f3 into bigskysoftware:four-dev Nov 1, 2026
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.

2 participants