npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hyperobjekt/mapbox

v2.0.6

Published

## `<Mapbox />`

Downloads

49

Readme

Components

<Mapbox />

Props

  • defaultViewport: object containing zoom, latitude, and longitude properties that define the default / reset viewport
  • children: any children (e.g. legend)
  • onHover: handler function for hovering over a feature
  • onClick: handler function for clicking on a feature
  • onLoad: handler function for when the map has loaded
  • maxBounds: form of a Mapbox LngLatBoundsLike array of arrays, [[lng,lat], [lng,lat]].
  • ContainerProps: an object containing props to pass to the container div
  • sources: an array of source objects following mapboxgl source format
  • layers: an array of layer styles (with optional additional parameters for beforeId and interactive)

Any additional props are passed on to the ReactMapGL Interactive Map

Example

A map centered on New York City

const CustomMap = (props) => (
  <Mapbox
    defaultViewport={{
      zoom: 11,
      latitude: 40.74,
      longitude: -73.96,
    }}
    {...props}
  />
);

Setting Map Bounds

Use the maxBounds prop in the form of a Mapbox LngLatBoundsLike array of arrays, [[lng,lat], [lng,lat]].

const BoundMap = (props) => (
  <Mapbox
    defaultViewport={{
      zoom: 11,
      latitude: 40.74,
      longitude: -73.96,
    }}
    maxBounds={[
      [-107.6, 33.8],
      [-65, 49.9],
    ]}
    {...props}
  />
);

Getting the ReactMapGL ref

The component forwards the ref to ReactMapGL ref, so you can get the mapboxgl map for advanced usage.

const AdvancedMap = (props) => {
  const mapRef = useRef(null);

  useEffect(() => {
    if (mapRef.current) {
      // gives you access to StaticMap methods from ReactMapGL
      // https://visgl.github.io/react-map-gl/docs/api-reference/static-map#methods
      const map = mapRef.current.getMap()
      // do something with the map
    }

  }, [mapRef.current])

  return (
    <Mapbox
      ref={mapRef}
      {...props}
    >
  )
}

Hooks

A collection of hooks are provided to allow access to map viewport information and manipulation.

useMapViewport()

Provides map viewport value and setter

const [viewport, setViewport] = useMapViewport();

Viewport value looks like:

{
  width: 400,
  height: 400,
  latitude: 37.7577,
  longitude: -122.4376,
  zoom: 8
}

useMapSize()

Provides pixel dimensions of the map viewport

const [width, height] = useMapSize();

useFlyToState()

Provides a function that will fly the map to a state when given a fips code.

const flyToState = useFlyToState();
flyToState("06");

useFlyToFeature()

Provides a function that will fly the map to a GeoJSON feature

const flyToFeature = useFlyToFeature();
flyToFeature(feature);

useFlyToLatLon()

Provides a function that will fly the map to a provided latitude, longitude, and zoom;

const flyToLatLon = useFlyToLatLon();
flyToLatLon(37, -122, 8);

useFlyToReset()

Provides a function that flies the viewport back to the default viewport (set on the <MapBase /> component with defaultViewport prop).

const flyToReset = useFlyToReset();
flyToReset();

useMapStore()

This is the store where all the other hooks pull from. You can access parts of the other hooks from this one. Helpful if you want to be able to access a setter but avoid re-renders, for example, with useMapViewport().

The following can be retrieved from the store:

const setViewport = useMapStore((state) => state.setViewport);
  • loaded: true or false, depending on if the map is loaded or not
  • setLoaded: setter for loaded state
  • resetViewport: the default viewport object
  • setResetViewport: setter for reset viewport
  • viewport: the current viewport object
  • setViewport: setter for current viewport
  • hoveredFeature: the currently hovered feature (if any)
  • setHoveredFeature: setter for hovered feature