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

@bazooka_se/react-map-interaction

v1.3.4

Published

'Add map like zooming and dragging to any element'

Downloads

1

Readme

This is a fork of https://github.com/transcriptic/react-map-interaction

This is a temporary fork that adds a couple of new options.

react-map-interaction

Add map like zooming and panning to any React element. This works on both touch devices (pinch to zoom, drag to pan) as well as with a mouse or trackpad (wheel scroll to zoom, mouse drag to pan).

example zooming map

Install

npm install --save @bazooka_se/react-map-interaction

Examples

We use storybook in development, and you can pull this repo and run storybook with npm run storybook. See files with *.stories.* for examples.

Basic

import { MapInteractionCSS } from 'react-map-interaction';

// This component uses CSS to scale your content.
// Just pass in content as children and it will take care of the rest.
const ThingMap = () => {
  return (
    <MapInteractionCSS>
      <img src="path/to/thing.png" />
    </MapInteractionCSS>
  );
}

Usage without CSS

import { MapInteraction } from 'react-map-interaction';

// Use MapInteraction if you want to determine how to use the resulting translation.
const NotUsingCSS = () => {
  return (
    <MapInteraction>
      {
        ({ translation, scale }) => { /* Use the passed values to scale content on your own. */ }
      }
    </MapInteraction>
  );
}

Controlled

import { MapInteractionCSS } from 'react-map-interaction';

// If you want to have control over the scale and translation,
// then use the `scale`, `translation`, and `onChange` props.
class Controlled extends Component {
  constructor(props) {
    super(props);
    this.state = {
      scale: 1,
      translation: { x: 0, y: 0 }
    };
  }

  render() {
    const { scale, translation } = this.state;
    return (
      <MapInteractionCSS
        scale={scale}
        translation={translation}
        onChange={({ scale, translation }) => this.setState({ scale, translation })}
      >
        <img src="path/to/thing.png" />
      </MapInteractionCSS>
    );
  }
}

Click and drag handlers on child elements

This component lets you decide how to respond to click/drag events on the children that you render inside of the map. To know if an element was clicked or dragged, you can attach onClick or onTouchEnd events and then check the e.defaultPrevneted attribute. MapInteraction will set defaultPrevented to true if the touchend/mouseup event happened after a drag, and false if it was a click. See index.stories.js for an example.

Prop Types for MapInteractionCSS (all optional)

MapInteraction doesn't require any props. It will control its own internal state, and pass values to its children. If you need to control the scale and translation then you can pass those values as props and listen to the onChange event to receive updates.

{
  // The scale applied to the dimensions of the contents. A scale of 1 means the
  // contents appear at actual size, greater than 1 is zoomed, and between 0 and 1 is shrunken.
  scale: PropTypes.number,
  defaultScale: PropTypes.number,
  // Stops user from being able to zoom, but will still adhere to props.scale
  disableZoom: PropTypes.bool,


  // The distance in pixels to translate the contents by.
  translation: PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }),
  defaultTranslation: PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }),

  // Stops user from being able to pan. Note that translation can still be
  // changed via zooming, in order to keep the focal point beneath the cursor. This prop does not change the behavior of the `translation` prop.
  disablePan: PropTypes.bool,

  // Apply a limit to the translation in any direction in pixel values. The default is unbounded.
  translationBounds: PropTypes.shape({
    xMin: PropTypes.number, xMax: PropTypes.number, yMin: PropTypes.number, yMax: PropTypes.number
  }),

  // Called with an object { scale, translation }
  onChange: PropTypes.func,

  // The min and max of the scale of the zoom. Must be > 0.
  minScale: PropTypes.number,
  maxScale: PropTypes.number,

  // When 'showControls' is 'true', plus/minus buttons are rendered
  // that let the user control the zoom factor
  showControls: PropTypes.bool,

  // Content to render in each of the control buttons (only when 'showControls' is 'true')
  plusBtnContents: PropTypes.node,
  minusBtnContents: PropTypes.node,

  // Class applied to the controls wrapper (only when 'showControls' is 'true')
  controlsClass: PropTypes.string,

  // Class applied to the plus/minus buttons (only when 'showControls' is 'true')
  btnClass: PropTypes.string,

  // Classes applied to each button separately (only when 'showControls' is 'true')
  plusBtnClass: PropTypes.string,
  minusBtnClass: PropTypes.string,

  // Disable zooming with scroll wheel. This allows a user to scroll the page without getting "caught" in the component
  disableMouseWheelZoom: PropTypes.bool,

  // Disable panning with one finger. This allows a user to scroll the page without getting "caught" in the component
  disableSingleFingerPan: PropTypes.bool
};

Prop Types for MapInteraction (all optional)

{
  // Function called with an object { translation, scale }
  // translation: { x: number, y: number }, The current origin of the content
  // scale:       number, The current multiplier mapping original coordinates to current coordinates
  children: PropTypes.func,

  // The rest of the prop types are the same as MapInteractionCSS
  ...MapInteractionCSS.propTypes,
}

Development

Please feel free to file issues or put up a PR. There are currently no automated tests, but there is an example application in the example directory. When you build this library it will inject the dist into the example/node_modules so it can be imported by that application. Re-run npm run start when you make changes to the lib (create-react-app doesn't watch node modules).