2.0.0 • Published 3 years ago

react-physics-dragger v2.0.0

Weekly downloads
1,211
License
MIT
Repository
github
Last release
3 years ago

React Physics Dragger

A simple, no-frills horiztonal dragger/slider with physics

example gif

NPM npm bundle size

▶ Example site

This is a React component which basically adds a wrapper element with horizontal dragging capabilities. It uses a little bit of physics to apply friction and boundary bouncing (similar to Apple's interfaces, and the Flickity carousel).

  • Works with both touch and mouse
  • 0 dependencies
  • Super small

Example usage

yarn add react-physics-dragger
# or
npm i react-physics-dragger

Internally it uses ResizeObserver so you might need to add a polyfill if you support IE11. If so, instructions below.

import Dragger from 'react-physics-dragger'
import ResizeObserver from 'resize-observer-polyfill' // this one works pretty great

const App = () => {
  return (
    <Dragger
      ResizeObserverPolyfill={ResizeObserver} // remember; only if you need it, else it uses window.ResizeObserver
    >
        <div>1</div>
        <div>2</div>
        <div>3</div>
    </Dragger>
  )
}

Props

PropertyTypeDefault ValueDescription
frictionnumber0.95Optional. Lower values feel grippier, higher values feel slippier.
draggerReffunctionOptional. Use to access the components internal methods (see below section "Ref Methods") and Example1.
onFramecallback functionOptional. This function is fired on every movement, resize, and mount. It provides one param object includes the x position, progress (from 0 to 1), and dragger dimensions. See Example2.
onDowncallback functionOptional. Fired on mouse or touch press.
onUpcallback functionOptional. Fired on mouse or touch release.
onStaticClickfunctionOptional. Fired when an item within the dragger is clicked (or tapped). Useful for avoiding events where dragging can be considered a click.
setCursorStylesbooleantrueOptional. If enabled, styles are added/removed to the <html> element for grabbing cursor styles.
classNamestringOptional. Add your own class name to the outer element
styleCSSPropertiesOptional. Add your own styles to the outer element
innerStyleCSSPropertiesOptional. Add your own styles to the inner element
disabledbooleanfalseOptional. Enable/disable the component.
ResizeObserverPolyfillOptional. If you need the polyfill pass it in as a prop.

Ref Methods

PropertyTypeDescription
setPositionfunctionChanges the position of the slider. draggerRef.current.setPosition(x, false) where x is the pixel value. Accepts a second argument (boolean, false by default) which determines whether to move to setPosition value instantly.

Acknowledgements

Inspired by Dave DeSandro's work on Practical UI Physics; https://www.youtube.com/watch?v=90oMnMFozEE and https://codepen.io/desandro/pen/QbPKEq

This library was packaged with https://github.com/transitive-bullshit/create-react-library

License

MIT © nickmcmillan

1.4.1

3 years ago

2.0.0

3 years ago

1.4.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago