2.0.4 • Published 4 years ago

react-sled v2.0.4

Weekly downloads
136
License
MIT
Repository
github
Last release
4 years ago

react-sled Logo

react-sled is a carousel made with react-spring.

NPM

  • Super-smooth spring animations (thanks to react-spring)
  • Lightweight and performant architecture
  • Touch- and Mousedrag (thanks to react-with-gesture)
  • Easy to style
  • Ready for server-side-rendering
  • All props are dynamically changeable
  • (Should be) Compatible with older Browsers from Internet Explorer 11 (Needs testing!)

đź›· Have a look at the example!

New Major release 2.0

Breaking Changes:

  • Removed styled-components
  • Removed custom ow-unit
  • Use react-spring Version 9 and react-with-gesture Version 7

New Features:

  • Full Type-Script support
  • Vertical Sliding
  • Set fixed proportion
  • Show multiple elements at once (showElements)
  • Move by multiple elements (slideBy)

Install

Install all dependencies via Yarn or NPM.

yarn add react-sled react-spring@next react-use-gesture react react-dom

Usage

import React from "react";
import { Sled, Views, Progress, Control } from "react-sled";
import "react-sled/dist/main.css";

const images = ["my-image-1.jpg", "my-image-2.jpg"];

const App = () => {
  return (
    <Sled>
      <Views>
        {images.map((image, index) => (
          <img key={image} src={image} alt={`My Image #${index}`} />
        ))}
      </Views>
      <Progress />
      <div className="controls arrows">
        <Control select="prev" />
        <Control select="next" />
      </div>
      <div className="controls dots">
        {images.map((image, index) => (
          <Control key={image} select={index} />
        ))}
      </div>
    </Sled>
  );
};

export default App;

Sled

Sled is the wrapper-component. It takes no props.

Views

Render all your views into this component. It takes these optional props:

NameTypeDefaultDescription
widthString'100%'Sets the viewpager’s width. Allowed units: all CSS-units
heightStringnullSets the viewpager’s height.
proportionString2:1Provide either a width or height and set the other dimension proportional to it. If you provide a height and a width proportion is disabled.
showElementsNumber1Determines how many Slides/Views fit in the Sled’s viewport.
slideByNumber1Determines how many Slides/Views the Sled’s slides with one movement .
selectNumberundefinedSelect certain view.
styleObjectnullAdd inline styles to the view-wrapper. 
keyboardBooleantrueSet Keyboard controls. 
draggingBooleantrueSet Mouse- and Touch-Dragging. 
dragDistanceNumber or String40Distance the user has to drag the slider to trigger action. A number is calculated in Pixel. A string is converted to a number unless it has the unit %, which means "percent of Sled’s width". 
autoPlayNumberundefinedActivates automatic Sliding-Interval in Milliseconds. 
configNumber{ mass: 1, tension: 210, friction: 20, clamp: true }react-spring animation-settings. 
pauseBooleanfalseautoPlay (if activated) gets paused. 
pauseOnMouseOverBooleantrueautoPlay (if activated) gets paused, as long as the user hovers over the sled. 
stopOnInteractionBooleanfalseautoPlay (if activated) gets stopped, after the user interacted with the sled. 
rewindBooleanfalseRewind sled, when you want to go beyond the first or last view. 
onSledEndfunctionnullCallback, that gets triggered after last view.
onAnimationStartfunctionnullCallback, that gets triggered when a sliding-animation starts.
onAnimationEndfunctionnullCallback, that gets triggered when a sliding-animation ends.

Controls

There is only one control-component for Arrows and Selecting-Dots. The prop select decides what the Control-element is: A string called next or prev will activate Arrow-functionality, a number Select-functionality.

You can easily style it via CSS. The default-styles are scoped to the class-name sled-progress-default. They are contained in the file dist/index.css. If you give it a custom className-prop, the default-class will be overridden and the Progress will be completely unstyled. Then you can copy the default-styles from here as a starting-point.

Control Props Overview:

NameTypeDefaultDescription
selectString or Number'next'Defines, if the Control has arrow- or dot-functionality. A number is the index of the target-view. A string can be 'prev' or 'next'
classNameStringDefault depends on select
styleString''If you provide a style and no preset, the default preset gets completely replaced. If you provide a style and a preset, the preset gets extended.

Arrow: Default-Design:

<Control
  select="next"
/>

Your Custom-Design:

<Control
  select="next"
  className="custom"
  style={{
    background: 'red'
  }}
>
  My custom arrow!
</Control>

Selection-Dot:

<Control
  select={1}
/>

Progress

react-sled has an Instagram-like progress-bar. You can easily style it via CSS. The default-styles are scoped to the class-name sled-progress-default. If you give it a custom className-prop, the Progress will be completely unstyled. You can copy the default-styles from here as a starting-point.

Progress Props Overview:

NameTypeDefaultDescription
classNameStringDefault depends on select
styleString''If you provide a style and no preset, the default preset gets completely replaced. If you provide a style and a preset, the preset gets extended.
<Progress
  className="my-progress"
  style={{ background: 'red' }}
/>

useSledStore

A hook, that exposes the plugin’s state-management. useSledStore is only useable inside the Sled-Component. It returns an Array with 2 elements:

  1. state of type object
  2. dispatch of type function

To-Do

  • Control animation by frame on drag
  • Infinity-Mode
  • Nice documentation with live examples (using Docz)
  • automated testing

Contributing

Every contribution is very much appreciated.

If you like react-sled, don't hesitate to star it on GitHub.

License

MIT © AndreasFaust

Thanks

This library is derived from the great work and especially this code-sandbox-example provided by Paul Henschel and the react-spring-team.

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.6

4 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.8

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

0.15.0

5 years ago

0.14.1

5 years ago

0.14.0

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago