Skip to content

feat/add button to reset map to initial viewport#52

Open
henriwoodcock wants to merge 1 commit intoAnmolSaini16:mainfrom
henriwoodcock:feature/reset-to-initial-viewport
Open

feat/add button to reset map to initial viewport#52
henriwoodcock wants to merge 1 commit intoAnmolSaini16:mainfrom
henriwoodcock:feature/reset-to-initial-viewport

Conversation

@henriwoodcock
Copy link
Copy Markdown

@henriwoodcock henriwoodcock commented Mar 7, 2026

Summary by CodeRabbit

  • New Features

    • Added a reset control to MapControls that allows users to return the map to its initial viewport with a single click.
  • Documentation

    • Updated API reference to document the new showReset prop for MapControls component.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 7, 2026

@henriwoodcock is attempting to deploy a commit to the mapcn oss program Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 7, 2026

📝 Walkthrough

Walkthrough

The changes add a new optional showReset boolean prop to the MapControls component that enables a reset button control. When enabled, users can return the map to its initial viewport. Updates span the component implementation, example usage, and API documentation.

Changes

Cohort / File(s) Summary
Documentation & Examples
src/app/docs/_components/examples/map-controls-example.tsx, src/app/docs/api-reference/page.tsx
Updated example usage and API reference documentation to include the new showReset prop with default value false and description of reset button functionality.
Map Component Implementation
src/registry/map.tsx
Added showReset?: boolean to MapControlsProps, implemented initial viewport capture on map reference, created handleReset function for viewport animation, imported RotateCcw icon, and conditionally rendered reset button control in the UI.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A reset button hops into view,
With RotateCcw spinning true,
Fly back home, the map now knows,
To where the journey first arose! 🗺️✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding a button/control to reset the map to its initial viewport, which aligns perfectly with the file changes across the codebase.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

@henriwoodcock
Copy link
Copy Markdown
Author

@AnmolSaini16 please review

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/registry/map.tsx`:
- Around line 784-788: The effect currently captures the initial viewport as
soon as `map` exists; change the destructuring from `useMap()` in `MapControls`
to also pull `isLoaded` and update the `useEffect` to only set
`initialViewportRef.current = getViewport(map)` when `map` is defined AND
`isLoaded` is true, using the same pattern as `MapRoute` (i.e., check `isLoaded`
before calling `getViewport`), so the initial camera is captured after the
MapLibre `load` event finalizes the camera state.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c36320f2-392c-4302-ab14-c894226a724f

📥 Commits

Reviewing files that changed from the base of the PR and between 94628a5 and 2118f72.

📒 Files selected for processing (3)
  • src/app/docs/_components/examples/map-controls-example.tsx
  • src/app/docs/api-reference/page.tsx
  • src/registry/map.tsx

Comment on lines +784 to +788
useEffect(() => {
if (map && !initialViewportRef.current) {
initialViewportRef.current = getViewport(map);
}
}, [map]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

head -n 900 src/registry/map.tsx | tail -n 130 | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 4641


🏁 Script executed:

sed -n '1,50p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 1825


🏁 Script executed:

sed -n '710,790p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 2993


🏁 Script executed:

sed -n '830,850p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 728


🏁 Script executed:

# Search for the Map component definition and its initialization
rg -n "function Map|const Map" src/registry/map.tsx | head -20

Repository: AnmolSaini16/mapcn

Length of output: 319


🏁 Script executed:

# Find the getViewport function definition
rg -n "function getViewport|const getViewport" src/registry/map.tsx

Repository: AnmolSaini16/mapcn

Length of output: 122


🏁 Script executed:

# Search for MapViewport type definition
rg -n "type MapViewport|interface MapViewport" src/registry/map.tsx

Repository: AnmolSaini16/mapcn

Length of output: 86


🏁 Script executed:

# Check if there's any 'load' or 'idle' event handling in the file
rg -n "\.on\(|load|idle" src/registry/map.tsx | head -30

Repository: AnmolSaini16/mapcn

Length of output: 1298


🏁 Script executed:

# Get more context around the Map component initialization
sed -n '80,250p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 6362


🏁 Script executed:

# Check more context around Map component initialization and load event
sed -n '164,280p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 4341


🏁 Script executed:

# Check how isLoaded is provided to MapControls through context
sed -n '300,360p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 2170


🏁 Script executed:

# Check what MapControls receives from useMap hook
rg -n "useMap\(\)" src/registry/map.tsx

Repository: AnmolSaini16/mapcn

Length of output: 298


🏁 Script executed:

# Check MapLibre documentation reference about defaults and load event
# Also verify that isLoaded is reliably set before camera is resolved
sed -n '164,165p' src/registry/map.tsx | cat -n

Repository: AnmolSaini16/mapcn

Length of output: 82


🏁 Script executed:

# Verify the exact MapLibre version to confirm load event behavior
rg -n "maplibre" package.json

Repository: AnmolSaini16/mapcn

Length of output: 94


🏁 Script executed:

# Verify MapLibre 5.15.0 behavior by checking if there's documentation about the load event
# and when camera properties are finalized
web_search "maplibre-gl 5.15 load event camera initialization"

Repository: AnmolSaini16/mapcn

Length of output: 112


Wait for isLoaded before capturing the initial viewport.

The useEffect at lines 775–779 captures initialViewportRef.current = getViewport(map) as soon as the map instance becomes available, but MapLibre may not have finished resolving the initial camera state. The load event (registered in the Map component at line 88) signals when the camera is finalized.

Instead of moving the capture logic, MapControls already has access to the isLoaded flag via useMap() (see MapRoute at line 1091 for the pattern). Update the destructuring to include isLoaded and only capture the viewport when isLoaded is true.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/registry/map.tsx` around lines 784 - 788, The effect currently captures
the initial viewport as soon as `map` exists; change the destructuring from
`useMap()` in `MapControls` to also pull `isLoaded` and update the `useEffect`
to only set `initialViewportRef.current = getViewport(map)` when `map` is
defined AND `isLoaded` is true, using the same pattern as `MapRoute` (i.e.,
check `isLoaded` before calling `getViewport`), so the initial camera is
captured after the MapLibre `load` event finalizes the camera state.

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.

1 participant