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

@ttoss/google-maps

v2.1.0

Published

<strong>@ttoss/google-maps</strong> is an opinionated way to use Google Maps in your React application.

Downloads

171

Readme

@ttoss/google-maps

@ttoss/google-maps is an opinionated way to use Google Maps in your React application.

Installing

Install @ttoss/google-maps in your project:

$ pnpm add @ttoss/google-maps

If you use TypeScript, install the types for Google Maps:

$ pnpm add -D @types/google.maps

Then, add the following to a declaration file (generally typings.d.ts):

/// <reference types="google.maps" />

Getting Started

Configuring GoogleMapsProvider

At the root of your application, configure GoogleMapsProvider with your Google Maps API key. This way, the whole application can access the google variable.

import { GoogleMapsProvider } from '@ttoss/google-maps';

const App = ({ children }) => {
  return (
    <OtherProviders>
      <GoogleMapsProvider apiKey={process.env.GOOGLE_MAPS_API_KEY}>
        {children}
      </GoogleMapsProvider>
    </OtherProviders>
  );
};

Rendering the Map

At the component level, render Google Maps using useMap hook:

import { Box, Text } from '@ttoss/ui';
import { useMap } from '@ttoss/google-maps';

const MyComponent = () => {
  const { ref, map } = useMap();

  React.useEffect(() => {
    if (map) {
      map.setOptions({
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });
    }
  }, [map]);

  return (
    <Box>
      <Text>My Map</Text>
      <Box ref={ref} sx={{ height, width }} />
    </Box>
  );
};

If everything is set up correctly, you should see a map centered at the specified coordinates.

Retrieve google.maps object

If you need to access the google.maps object, you can use the useGoogleMaps hook:

import { useGoogleMaps } from '@ttoss/google-maps';

const MyComponent = () => {
  const { google } = useGoogleMaps();

  return <Text>{google.maps.version}</Text>;
};

With this, you can perform any operation that the google.maps object allows, such as creating markers, drawing polygons, etc.

Advanced Usage

Using with Next.js (custom Script component)

If you use Next.js, you can use the GoogleMapsProvider passing Next.js Script component as a prop:

import { GoogleMapsProvider } from '@ttoss/google-maps';
import Script from 'next/script';

const App = ({ children }) => {
  return (
    <OtherProviders>
      <GoogleMapsProvider
        apiKey={process.env.GOOGLE_MAPS_API_KEY}
        Script={Script}
      >
        {children}
      </GoogleMapsProvider>
    </OtherProviders>
  );
};

Reusing map object

If you need to access the map object in multiple components, you can use MapProvider to share it:

import { MapProvider, useMap } from '@ttoss/google-maps';

const ChildComponent = () => {
  // Access the map object created by the parent component
  const { map } = useMap();

  React.useEffect(() => {
    if (map) {
      map.setOptions({
        center: { lat: -34.397, lng: 150.644 },
        zoom: 8,
      });
    }
  }, [map]);

  return null;
};

const ParentComponent = () => {
  const { ref, map } = useMap();

  return (
    <MapProvider map={map} ref={ref}>
      <Box>
        <Text>My Map</Text>
        <Box ref={ref} sx={{ height, width }} />
        <ChildComponent />
      </Box>
    </MapProvider>
  );
};

API

GoogleMapsProvider

Props

  • apiKey: string - Google Maps API key.
  • libraries: string[] - Libraries to load.
  • language: string - Language.
  • Script: React.ComponentType - Custom Script component to use.
  • onError: (error: Error) => void - Callback to handle script loading errors.

MapProvider

Props

  • map: google.maps.Map | null - Google Maps object.
  • children: React.ReactNode - Children to render.
  • ref: React.RefObject - Reference to the map container.

useMap

useMap is a hook that returns a reference to the map container and the Google Maps object. It creates a new map object if it doesn't exist or returns the existing map if MapProvider wraps the component tree.

Returns

  • ref: React.RefObject<HTMLDivElement> - Reference to the map container.
  • map: google.maps.Map | null - Google Maps object.

useGoogleMaps

Returns

  • google: typeof google - google.maps object.
  • status: 'idle' | 'error' | 'loading' | 'ready' - Status of the script loading.
  • isReady: boolean - Whether the script is ready. The same as status === 'ready'.