> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/mapbox-gl-js/llms.txt)

# Add a hover tooltip that follows the mouse pointer

This example shows how to use a [`Popup`](https://docs.mapbox.com/mapbox-gl-js/api/markers/#popup) with custom event listeners to create a tooltip that follows the mouse pointer when hovering over a polygon feature.

It uses [`Map.addInteraction`](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#addinteraction) to detect `mousemove` events over the `state-fills` layer, updating the popup's position and content based on the feature being hovered. When the mouse leaves the feature, a [`Map.on('mouseleave')`](https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:mouseleave) event listener removes the popup from the map.

To add a highlight effect to complement the tooltip, see the [Create a hover effect](https://docs.mapbox.com/mapbox-gl-js/example/hover-styles/) example

> Example code:

**JavaScript**

```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add a hover tooltip that follows the mouse pointer</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.28.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.28.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
    const map = new mapboxgl.Map({
        // TO MAKE THE MAP APPEAR YOU MUST
        // ADD YOUR ACCESS TOKEN FROM
        // https://account.mapbox.com
        accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
        container: 'map',
        // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
        style: 'mapbox://styles/mapbox/standard',
        center: [-100.486052, 37.830348],
        zoom: 2
    });

    // Create a popup, but don't add it to the map yet
    const popup = new mapboxgl.Popup({
        closeButton: false,
        closeOnClick: false,
        offset: [0, -20]
    });

    map.on('style.load', () => {
        map.addSource('states', {
            'type': 'geojson',
            'data': 'https://docs.mapbox.com/mapbox-gl-js/assets/us_states.geojson'
        });

        map.addLayer({
            'id': 'state-fills',
            'type': 'fill',
            'source': 'states',
            'layout': {},
            'paint': {
                'fill-color': '#627BC1',
                'fill-opacity': 0.5
            }
        });

        map.addLayer({
            'id': 'state-borders',
            'type': 'line',
            'source': 'states',
            'layout': {},
            'paint': {
                'line-color': '#627BC1',
                'line-width': 2
            }
        });

        // use addInteraction for quick access to the feature under the mouse
        map.addInteraction('state-fills-mousemove', {
            type: 'mousemove',
            target: {
                layerId: 'state-fills'
            },
            handler: (e) => {
                // Position the popup at the cursor location and show it
                popup
                    .setLngLat(e.lngLat)
                    .setHTML(
                        `<strong>${e.feature.properties.STATE_NAME}</strong>`
                    )
                    .addTo(map);
            }
        });

        // Use a standard Mapbox GL JS event listener for mouseleave to hide the popup
        map.on('mouseleave', 'state-fills', () => {
            popup.remove();
        });
    });
</script>

</body>
</html>
```

**React**

```jsx
import React, { useEffect, useRef } from 'react';
import * as mapboxgl from 'mapbox-gl/esm';

import 'mapbox-gl/dist/mapbox-gl.css';

const MapboxExample = () => {
  const mapContainerRef = useRef();
  const mapRef = useRef();

  useEffect(() => {
    mapRef.current = new mapboxgl.Map({
      // TO MAKE THE MAP APPEAR YOU MUST
      // ADD YOUR ACCESS TOKEN FROM
      // https://account.mapbox.com
      accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
      container: mapContainerRef.current,
      // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
      style: 'mapbox://styles/mapbox/standard',
      center: [-100.486052, 37.830348],
      zoom: 2
    });

    // Create a popup, but don't add it to the map yet
    const popup = new mapboxgl.Popup({
      closeButton: false,
      closeOnClick: false,
      offset: [0, -20]
    });

    mapRef.current.on('style.load', () => {
      mapRef.current.addSource('states', {
        type: 'geojson',
        data: 'https://docs.mapbox.com/mapbox-gl-js/assets/us_states.geojson'
      });

      // The feature-state dependent fill-opacity expression will render the hover effect
      // when a feature's hover state is set to true.
      mapRef.current.addLayer({
        id: 'state-fills',
        type: 'fill',
        source: 'states',
        layout: {},
        paint: {
          'fill-color': '#627BC1',
          'fill-opacity': 0.5
        }
      });

      mapRef.current.addLayer({
        id: 'state-borders',
        type: 'line',
        source: 'states',
        layout: {},
        paint: {
          'line-color': '#627BC1',
          'line-width': 2
        }
      });

      // use addInteraction for quick access to the feature under the mouse
      mapRef.current.addInteraction('state-fills-mousemove', {
        type: 'mousemove',
        target: {
          layerId: 'state-fills'
        },
        handler: (e) => {
          // Position the popup at the cursor location and show it
          popup
            .setLngLat(e.lngLat)
            .setHTML(`<strong>${e.feature.properties.STATE_NAME}</strong>`)
            .addTo(mapRef.current);
        }
      });

      // Use a standard Mapbox GL JS event listener for mouseleave to hide the popup
      mapRef.current.on('mouseleave', 'state-fills', () => {
        popup.remove();
      });
    });
  }, []);

  return <div id="map" ref={mapContainerRef} style={{ height: '100%' }} />;
};

export default MapboxExample;
```