Skip to content

fix(google-maps): prevent zoom/pan reset when overlay toggles#685

Merged
harlan-zw merged 2 commits intomainfrom
fix/google-maps-zoom-pan-reset
Mar 30, 2026
Merged

fix(google-maps): prevent zoom/pan reset when overlay toggles#685
harlan-zw merged 2 commits intomainfrom
fix/google-maps-zoom-pan-reset

Conversation

@harlan-zw
Copy link
Copy Markdown
Collaborator

🔗 Linked issue

N/A (user report)

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

The watch(options) watcher in ScriptGoogleMaps called map.setOptions() with the full options object (including initial zoom and center values) whenever the computed re-evaluated. Since defu returns a new object reference each time, any parent re-render (e.g. from toggling an overlay's open state) would trigger this watcher and reset user pan/zoom interactions back to the initial prop values.

The fix excludes center and zoom from the generic setOptions call and adds a dedicated setZoom watcher that only fires when the zoom value actually changes. Center already had its own dedicated watcher.

Includes regression tests that reproduce both the old (broken) and new (fixed) behavior.

The options watcher called setOptions with initial zoom and center values
on every re-evaluation, resetting user interactions when parent components
re-rendered (e.g. overlay open/close toggling state). Split zoom and center
into dedicated watchers that only fire on actual prop value changes.
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Mar 30, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
scripts-playground Ready Ready Preview, Comment Mar 30, 2026 2:04pm

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 30, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@nuxt/scripts@685

commit: 05d0512

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 30, 2026

📝 Walkthrough

Walkthrough

This pull request modifies the Google Maps component to separately handle zoom updates from other map options. The component's options watcher is updated to exclude center and zoom when calling setOptions, with a new dedicated watcher for options.value.zoom that calls setZoom directly. Supporting test infrastructure includes a new createMockMap() helper function and regression tests validating the fix prevents zoom and center from being reset by the options update.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main bug fix: preventing zoom/pan reset when an overlay toggles, which is directly addressed by the changes to ScriptGoogleMaps.
Description check ✅ Passed The description clearly explains the bug, the root cause (defu returning new object references), and how the fix works by separating zoom/center handling into dedicated watchers.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/google-maps-zoom-pan-reset

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
test/unit/google-maps-regressions.test.ts (1)

320-400: Add one regression for the new dedicated zoom watcher path.

This suite verifies setOptions no longer carries zoom/center, but it doesn’t assert the new setZoom watcher behavior itself (zoom change vs unrelated re-render). Adding that would lock in the full fix contract.

✅ Suggested additional test
 describe('map setOptions should not reset zoom or center', () => {
+  it('fixed behavior: zoom watcher updates zoom only when zoom value changes', () => {
+    const map = createMockMap()
+    let prevZoom: number | undefined
+
+    function applyZoomWatcher(zoom: number | undefined) {
+      if (zoom !== prevZoom && zoom != null)
+        map.setZoom(zoom)
+      prevZoom = zoom
+    }
+
+    applyZoomWatcher(12) // initial controlled value
+    applyZoomWatcher(12) // unrelated re-render, no zoom change
+    applyZoomWatcher(13) // actual prop change
+
+    expect(map.setZoom).toHaveBeenCalledTimes(2)
+    expect(map.setZoom).toHaveBeenNthCalledWith(1, 12)
+    expect(map.setZoom).toHaveBeenNthCalledWith(2, 13)
+  })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/unit/google-maps-regressions.test.ts` around lines 320 - 400, Add a new
test that verifies the dedicated zoom watcher calls map.setZoom only when the
zoom value actually changes and not on unrelated re-renders: create a mock map
via createMockMap(), call applyOptionsFixed(map, options) with the same zoom
repeatedly and assert map.setZoom was not called, then call
applyOptionsFixed(map, { ...options, zoom: newZoom }) and assert map.setZoom was
called once with newZoom (and similarly ensure setOptions never received
center/zoom). Reference applyOptionsFixed, createMockMap, map.setZoom and
map.setOptions to locate where to add the assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/unit/google-maps-regressions.test.ts`:
- Around line 320-400: Add a new test that verifies the dedicated zoom watcher
calls map.setZoom only when the zoom value actually changes and not on unrelated
re-renders: create a mock map via createMockMap(), call applyOptionsFixed(map,
options) with the same zoom repeatedly and assert map.setZoom was not called,
then call applyOptionsFixed(map, { ...options, zoom: newZoom }) and assert
map.setZoom was called once with newZoom (and similarly ensure setOptions never
received center/zoom). Reference applyOptionsFixed, createMockMap, map.setZoom
and map.setOptions to locate where to add the assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3fad9c7-0577-4b79-8cdc-0cbc29717c08

📥 Commits

Reviewing files that changed from the base of the PR and between f07c2fe and 14e7484.

📒 Files selected for processing (3)
  • packages/script/src/runtime/components/GoogleMaps/ScriptGoogleMaps.vue
  • test/unit/__mocks__/google-maps-api.ts
  • test/unit/google-maps-regressions.test.ts

@harlan-zw harlan-zw merged commit 0e4ae9e into main Mar 30, 2026
17 checks passed
@harlan-zw harlan-zw deleted the fix/google-maps-zoom-pan-reset branch March 30, 2026 14:05
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