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

react-leaflet-geoman-v2

v1.0.1

Published

React wrapper for the leaflet-geoman plugin

Downloads

7,086

Readme

react-leaflet-geoman

npm version

Basic React wrapper for the Leaflet Geoman Plugin

Installation

Add Peer Dependencies:

  // npm
  npm i react react-dom react-leaflet @geoman-io/leaflet-geoman-free

  // yarn
  yarn add react react-dom react-leaflet @geoman-io/leaflet-geoman-free

Add React Leaflet Geoman

  // npm
  npm i react-leaflet-geoman-v2

  // yarn
  yarn add react-leaflet-geoman-v2

Usage

Since this package modifies the DOM directly, it can be imported either as hook or a component. At a minimum, it must be initiated inside of a MapContainer component. You can either draw shapes directly to the map container or wrap it in a FeatureGroup component. See the Example code for a more detailed usage example.

V2 Breaking Changes

This component wrapper no longer imports the Leaflet Geoman CSS file. You must import it yourself, this is for compatibility reasons for different frameworks.

// as a React component
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css'
import { GeomanControls } from 'react-leaflet-geoman-v2'

export default function Drawing() {
  const handleChange = () => {
    console.log('Event fired!')
  }
  return (
    <FeatureGroup>
      <GeomanControls
        options={{
          position: 'topleft',
          drawText: false,
        }}
        globalOptions={{
          continueDrawing: true,
          editable: false,
        }}
        onCreate={handleChange}
        onChange={(e) => console.log('onChange', e)}
      />
    </FeatureGroup>
  )
}

// as a hook
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css'
import { useGeomanControls } from 'react-leaflet-geoman-v2'

export default function Drawing() {
  useGeomanControls({
    options: { drawText: true, drawCircle: true },
    onCreate: (e) => console.log('onCreate', e),
    eventDebugFn: console.log,
  })
  return null
}

Options

Respects all of the options, global options, and event handlers from Leaflet Geoman.

// Additional Props
interface GeomanProps extends GeomanHandlers {
  options?: PM.ToolbarOptions // See options link above
  globalOptions?: PM.GlobalOptions // See global options link above
  pathOptions?: L.PathOptions // See Leaflet PathOptions link above
  eventDebugFn?: EventDebugFn // optional function that can be used to debug events, such as `console.log`
  lang?: PM.SupportLocales // See lang options link above
  onMount?: () => void // callback that runs after it mounts to the DOM
  onUnmount?: () => void // callback that runs after it unmounts
}

Event Handlers

Hooks into all of the Leaflet Geoman events. Some events names have been altered slightly to allow more customization. For example, onMapCut allows you to hook into the pm:cut event linked to the map container when a cut happens and onLayerCut allows you to hook a different callback into the pm:cut event linked to each of the layers.

interface GeomanHandlers {
  // Global (also applied to the map container)
  onGlobalDrawModeToggled?: PM.GlobalDrawModeToggledEventHandler
  onGlobalEditModeToggled?: PM.GlobalEditModeToggledEventHandler
  onGlobalDragModeToggled?: PM.GlobalDragModeToggledEventHandler
  onGlobalRemovalModeToggled?: PM.GlobalRemovalModeToggledEventHandler
  onGlobalCutModeToggled?: PM.GlobalCutModeToggledEventHandler
  onGlobalRotateModeToggled?: PM.GlobalRotateModeToggledEventHandler
  onButtonClick?: PM.ButtonClickEventHandler
  onActionClick?: PM.ActionClickEventHandler
  onKeyEvent?: PM.KeyboardKeyEventHandler
  onLangChange?: PM.LangChangeEventHandler

  // Map
  onCreate?: PM.CreateEventHandler
  onDrawStart?: PM.DrawStartEventHandler
  onDrawEnd?: PM.DrawEndEventHandler
  onMapRemove?: PM.RemoveEventHandler
  onMapCut?: PM.CutEventHandler
  onMapRotateEnable?: PM.RotateEnableEventHandler
  onMapRotateDisable?: PM.RotateDisableEventHandler
  onMapRotateStart?: PM.RotateStartEventHandler
  onMapRotate?: PM.RotateEventHandler
  onMapRotateEnd?: PM.RotateEndEventHandler

  // Layer
  onSnapDrag?: PM.SnapEventHandler
  onSnap?: PM.SnapEventHandler
  onUnsnap?: PM.SnapEventHandler
  onCenterPlaced?: PM.CenterPlacedEventHandler
  onEdit?: PM.EditEventHandler
  onUpdate?: PM.UpdateEventHandler
  onEnable?: PM.EnableEventHandler
  onDisable?: PM.DisableEventHandler
  onVertexAdded?: PM.VertexAddedEventHandler
  onVertexRemoved?: PM.VertexRemovedEventHandler
  onVertexClick?: PM.VertexClickEventHandler
  onMarkerDragStart?: PM.MarkerDragStartEventHandler
  onMarkerDrag?: PM.MarkerDragEventHandler
  onMarkerDragEnd?: PM.MarkerDragEndEventHandler
  onLayerReset?: PM.LayerResetEventHandler
  onIntersect?: PM.IntersectEventHandler
  onChange?: PM.ChangeEventHandler
  onTextChange?: PM.TextChangeEventHandler
  onDragStart?: PM.DragStartEventHandler
  onDrag?: PM.DragEventHandler
  onDragEnd?: PM.DragEndEventHandler
  onDragEnable?: PM.DragEnableEventHandler
  onDragDisable?: PM.DragDisableEventHandler
  onLayerRemove?: PM.RemoveEventHandler
  onLayerCut?: PM.CutEventHandler
  onLayerRotateEnable?: PM.RotateEnableEventHandler
  onLayerRotateDisable?: PM.RotateDisableEventHandler
  onLayerRotateStart?: PM.RotateStartEventHandler
  onLayerRotate?: PM.RotateEventHandler
  onLayerRotateEnd?: PM.RotateEndEventHandler
}

Layer Events are automatically applied when...

  • pm:create/onCreate is fired
  • pm:cut/onMapCut is fired
  • If you add new layers to the MapContainer or FeatureGroup via a ref

Though generally not necessarily, you can import and apply layerEvents yourself with:

import { layerEvents } from 'react-leaflet-geoman-v2'

export default function Drawing({ geojson }) {
  React.useEffect(() => {
    L.geoJSON(geojson).eachLayer((layer) => {
      layerEvents(
        layer,
        {
          onDragStart: (e) => console.log('onDragStart', e),
          onDrag: (e) => console.log('onDrag', e),
          onDragEnd: (e) => console.log('onDragEnd', e),
        },
        'on'
      )
    })
  }, [geojson])
}

TypeScript Note

This library is only typed as well as the base plugin, if you see an issue, please consider contributing better types to Leaflet Geoman!

Contributing

  • yarn start to start the Vite dev server with HMR enabled.
  • With VS Code you can open a debugger in Chrome for IDE debugging