2.0.1 • Published 5 years ago

react-leaflet-dialog v2.0.1

Weekly downloads
47
License
MIT
Repository
github
Last release
5 years ago

react-leaflet-dialog

version react-leaflet compatibility travis build dependencies peer dependencies issues downloads MIT License

React wrapper of Leaflet.Dialog for react-leaflet.

A dialog modal window that is resizable and positionable on the map.

Tested with Leaflet 1.4.0 and React-Leaflet 1.9.1, React-Leaflet 2.2.0

Demos

VersionDemo
react-leaflet@1.9.1CodePen
react-leaflet@2.xCodePen

Installation

Install via NPM

npm install react-leaflet-dialog --save

Include font-awesome css to your project.

<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

Usage example for react-leaflet v1

import { Map, TileLayer } from 'react-leaflet';
import Dialog from 'react-leaflet-dialog';
		
<Map center={[101.483459, 2.938926]} zoom={12}>
  <TileLayer
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  />

  <Dialog ref={(ref) => { this.dialog = ref; }} id="dialog1">
	<div>Dialog content.</div>
  </Dialog>
</Map>

Usage example for react-leaflet v2

import { Map, TileLayer, withLeaflet } from 'react-leaflet';
import DialogDefault from 'react-leaflet-dialog';

// wrap `Dialog` component with `withLeaflet` HOC
const Dialog = withLeaflet(DialogDefault);
		
<Map center={[101.483459, 2.938926]} zoom={12}>
  <TileLayer
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  />

  <Dialog ref={(ref) => { this.dialog = ref; }} id="dialog1">
	<div>Dialog content.</div>
  </Dialog>
</Map>

Options

Any props passed to Dialog are passed down to Leaflet.Dialog.

Refer Leaflet.Dialog options for a complete list of options supported.

PropertyTypeDefaultDescription
sizeArray 300, 300 An array of the starting width and height values (in pixels).
minSizeArray 100, 100 An array with the minimum resize width and height (in pixels).
maxSizeArray 350, 350 An array with the maximum resize width and height (in pixels).
anchorArray 250, 250 The starting point anchor location (from the upper left) in pixels.
positionString'topleft'The leaflet control div to place the modal inside, as a starting reference point.
initOpenBooleantrueWhether or not to initialize in an open state.

Methods

MethodReturnsExampleDescription
open()thisdialog.open();Opens the dialog window.
close()thisdialog.close();Closes the dialog window.
destroy()thisdialog.destroy();Removes the dialog box from the window.
setLocation( Array anchor location )thisdialog.setLocation( 15, 15 );Move the dialog box to the specified pixel location ( Relative to the 'position' option )
setSize( Array size )thisdialog.setSize( 150, 150 );Resize the dialog window to the specified width and height.
setContent( [Stringnode] content )thisdialog.setContent("Hello! Welcome to your nice new dialog box!");Set new content for the dialog.
lock()thisdialog.lock();Hides all visible dialog window controls.
unlock()thisdialog.unlock();Re-instates all dialog window controls.
freeze()thisdialog.freeze();Hides all visible dialog window movement/sizing controls.
unfreeze()thisdialog.unfreeze();Re-instates all dialog window movement/sizing controls.
hideClose()thisdialog.hideClose();Hides the closing 'x'
showClose()thisdialog.showClose();Shows the closing 'x'
hideResize()thisdialog.hideResize();Hides the resizing symbol
showResize()thisdialog.showResize();Shows the resizing symbol

Example

...

openDialog() {
  this.dialog.open();
}

closeDialog() {
  this.dialog.close();
}

destroyDialog() {
  this.dialog.destroy();
}

updateDialogContent() {
  this.dialog.setContent("<div>This is the new dialog content.</div>");
}

render() {
  return (
	<Map center={[101.483459, 2.938926]} zoom={12}>
	  <TileLayer
		url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
		attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
	  />

	  <Dialog ref={(ref) => { this.dialog = ref; }} id="dialog1" />
	</Map>
  );
}

...

Events

EventDataDescription
onOpenedthisFired when open() is called.
onClosedthisFired when close() is called or when the 'x' is clicked.
onDestroyedthisFired when destroy() is called.
onLockedthisFired when lock() is called.
onUnlockedthisFired when unlock() is called.
onFrozenthisFired when freeze() is called.
onUnfrozenthisFired when unfreeze() is called.
onUpdatedthisFired when contents are set or when added to the map.
onResizeStartthisFired when the resize button is clicked.
onResizingthisFired continuously as the resize button is dragged.
onResizeEndthisFired when the resize button click is released.
onMoveStartthisFired when the move button is clicked.
onMovingthisFired continuously as the move button is dragged.
onMoveEndthisFired when the move button click is released.
onCloseHiddenthisFired when hideClose() is called.
onCloseShownthisFired when the showClose() is called.
onResizeHiddenthisFired when hideResize() is called.
onResizeShownthisFired when the showResize() is called.

Credits

Credits goes to NBTSolutions and all the contributors for the original work.

License

MIT License