DEV Community

Chris Whong for Mapbox

Posted on

Adding a Hover Tooltip to a Polygon Layer in Mapbox GL JS

Hover tooltips are a great way to enhance the UX of your web maps. When the user moves their mouse across a region, district, delivery zone, or any other polygon, the map can talk back — instantly showing what the user is pointing at.

Screen capture of hover tooltips on a polygon layer in a web map

At Mapbox, we just published an updated code example demonstrating this pattern, and it highlights one of the most common UX needs: a lightweight tooltip that follows the cursor and displays the properties of the polygon underneath. If you're designing thematic maps, dashboards, or interactive exploratory tools, this is a pattern worth adding to your toolkit.


Why this pattern matters

A hover-follow tooltip gives users:

  • Instant context without clicking
  • A smooth, responsive feel as users move the mouse across boundaries
  • A clean, focused UI (no permanent labels cluttering the map)
  • A natural interaction pattern for data-rich polygons

This approach uses a single popup that moves dynamically with the cursor, updating its content with the feature you're hovering.


Key Concepts (with snippet examples)

1. Create a single reusable popup

You don’t want a new popup on every movement — just one that updates.

const popup = new mapboxgl.Popup({
  closeButton: false,
  closeOnClick: false,
  offset: [0, -20]  // keeps tooltip above the cursor
});
Enter fullscreen mode Exit fullscreen mode
  1. Update the popup position + content on mousemove The new example uses map.addInteraction to target a specific polygon layer and update the tooltip as the cursor moves across it.
map.addInteraction('hover-tooltip', {
  type: 'mousemove',
  target: { layerId: 'polygon-fills' },
  handler: (e) => {
    const label = e.feature.properties.name;

    popup
      .setLngLat(e.lngLat)
      .setHTML(`<strong>${label}</strong>`)
      .addTo(map);
  }
});
Enter fullscreen mode Exit fullscreen mode

This keeps the tooltip synced to the cursor while pulling feature properties on demand.

  1. Hide the tooltip on mouseleave A tiny interaction detail that keeps your UI clean:
map.on('mouseleave', 'polygon-fills', () => {
  popup.remove();
});
Enter fullscreen mode Exit fullscreen mode

See the full working example

These snippets illustrate the pattern, but the full example includes a real dataset, layer styling, and the complete interaction flow.

👉 Check out the latest Mapbox GL JS hover tooltip example:

https://docs.mapbox.com/mapbox-gl-js/example/hover-tooltip/

The example shows exactly how this pattern works in a realistic scenario — a polygon dataset that updates the tooltip in real time as you move the mouse pointer across the map.

Explore More UX Patterns

If you're building interactive maps—dashboards, analytics tools, administrative boundaries, or selection-based interfaces—you’ll find more great patterns across the Mapbox GL JS example gallery.

👉 Browse all Mapbox GL JS examples

https://docs.mapbox.com/mapbox-gl-js/examples/

These examples are excellent starting points for designing modern, intuitive map interactions to make your web maps sing!

Top comments (5)

Collapse
 
brettgato profile image
Brett Gato

Hi Chris, I checked your examples. It's good. Could you please tell me about developing for ArcGIS using Mapbox?

Collapse
 
chriswhong profile image
Chris Whong Mapbox

I have a GIS background but I am not sure what you mean by "developing for ArcGIS". Can you say more about what you would like to build?

Collapse
 
brettgato profile image
Brett Gato

Thanks for your reply! What I mean is building applications or maps that use ArcGIS data together with Mapbox tools. For example, I’m interested in integrating Mapbox visualizations into ArcGIS projects, or creating custom interactive maps that combine the strengths of both platforms. Does that make sense? I’d be happy to provide more details about the specific use cases I have in mind. To make our discussion easier and more interactive, would you be open to continuing this conversation on Telegram?

Thread Thread
 
chriswhong profile image
Chris Whong Mapbox

You can consume data directly from an ArcGIS featureservice and display it on a Mapbox GL JS map. You just need to get the featureservice URL into the right format:

stackoverflow.com/questions/437985...

In any case, you want to export the data in GeoJSON to make it compatible with Mapbox tools.

Thread Thread
 
brettgato profile image
Brett Gato

Hi Chris, thanks again for your guidance on ArcGIS and Mapbox—it’s been really helpful.
I also wanted to share a small opportunity I’m working on. My friend and I are growing a business, and we’re looking for someone with your expertise to collaborate with us.
The commitment would be flexible—just a few hours per week depending on your availability.
After a few months of initial work, this role could provide a stable income of $2K–$5K per month or potentially more. I believe your skills and experience would make a huge difference, and I’d love to discuss how we could work together.
Would you be open to chat to explore this further?