Skip to content

Added .viewport modifier to x-on directive to add listeners to window.visualViewport instead - #4350

Closed
signorekai wants to merge 2 commits into
alpinejs:mainfrom
signorekai:main
Closed

signorekai wants to merge 2 commits into
alpinejs:mainfrom
signorekai:main

Conversation

@signorekai

Copy link
Copy Markdown

Problem

On iOS Safari (v17.3), resize events on window does not fire immediately to viewport resizes (caused by browser chrome hiding address bars). However, it does fire almost immediately when binded to window.visualViewport.

Solution

Added .viewport modifier to x-on directive to add listeners to window.visualViewport

@SimoTod

SimoTod commented Aug 23, 2024

Copy link
Copy Markdown
Collaborator

Does the resize plugin (x-resize) have the same issue?

@signorekai

signorekai commented Aug 23, 2024 •

Copy link
Copy Markdown
Author

Yes it does.

Screenshot 2024-08-23 at 4 56 38 PM
(from iOS inspector)

As the user scrolls and the browser starts to hide the address bar, the resize event for window.visualViewport gets called almost immediately while the x-resize only gets called at the end of the scroll (after easing).

@ekwoka

ekwoka commented Aug 24, 2024 •

Copy link
Copy Markdown
Contributor

Interesting.

Could this be an issue where the listener should be passive or you risk causing issues?

@calebporzio

Copy link
Copy Markdown
Collaborator

PR Review: #4350 — Added .viewport modifier to x-on directive

Type: Feature
Verdict: Close

What's happening (plain English)

On iOS Safari (v17.3+), when the browser chrome hides/shows (e.g., the address bar slides away as you scroll), the window resize event fires late — only after the animation completes. But window.visualViewport's resize event fires immediately as the viewport changes size.

This PR adds a .visual modifier to x-on so that:

<div @resize.visual="foo = window.visualViewport.width">

attaches the listener to window.visualViewport instead of the element.

The implementation is a single line in on.js:

if (modifiers.includes('visual')) listenerTarget = window.visualViewport

Other approaches considered

  1. Add a .visual-viewport modifier to the x-resize plugin — This is the better home. x-resize already handles resize observation and has a .document modifier pattern. Adding viewport support there keeps core small and puts the feature where users would naturally look for it.

  2. Users can already do x-on:resize.window — For many cases, the slight delay on iOS is acceptable. The PR solves a real but narrow iOS-specific timing issue.

  3. A custom Alpine plugin — This is roughly 5 lines of code as a userland plugin. Given the narrow use case, that's probably the right level of abstraction.

Changes Made

No changes made — recommending close.

Test Results

  • tests/cypress/integration/directives/x-on.spec.js: 42/42 passing
  • The .viewport modifier test correctly fails without the fix and passes with it
  • No CI checks configured on this branch

Code Review

Issues found:

  1. Naming inconsistency — PR title says .viewport, test is named .viewport modifier, but the actual modifier in code is .visual. Pick one name and be consistent.

  2. Missing key modifier exclusion (on.js:131) — 'visual' is not in the exclusion list inside isListeningForASpecificKeyThatHasntBeenPressed. The existing modifiers window and document are both excluded there. Without this, using .visual on a keydown event would break key filtering.

  3. No null guard — window.visualViewport can be null in non-visual contexts (workers, some test environments). The code would throw when trying to call addEventListener on null.

  4. No docs — Feature needs documentation alongside .window and .document in the modifier docs.

  5. Test name vs modifier mismatch — Test says .viewport modifier but tests .visual. The viewport destructured from the test callback is Cypress's cy.viewport() helper, not related to this feature — adds to the confusion.

Security

No security concerns identified.

Verdict

Recommend closing. Here's why:

  1. This belongs in x-resize, not core. window.visualViewport only emits resize and scroll events. A generic listener-target modifier in core x-on implies it works for any event — misleading. The x-resize plugin already handles resize listening and is the natural home for viewport-aware resize detection.

  2. Zero community demand. No reactions on the PR or linked issues. The bar for new core API surface should be high.

  3. Narrow use case. This solves an iOS Safari timing issue for viewport resize. It's the kind of thing a 5-line userland plugin handles perfectly.

  4. Implementation has bugs. Missing key modifier exclusion, no null guard, naming inconsistency between title/test/code.

If the iOS visualViewport timing issue is important enough to address, I'd suggest the contributor open a PR against the x-resize plugin instead, adding something like x-resize.viewport that uses visualViewport.addEventListener('resize', ...) under the hood.


Reviewed by Claude

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.

4 participants