Skip to content

Commit 51f10d7

Browse files
authored
Feature/passing though events to slider (#145)
* passing through events on Slider Component A goal of the project is to pass through all props when possible. We forgot to pass through event handlers on the slider component. * updated packages to silence rollup warnings Needed to update some rollup plugins after recent update to rollup. These plugins were using deprecated rollup hooks. * passing through props to slider Tray tag There was a request to pass through props to the Tray tag in slider. * Update Slider.jsx (#146)
1 parent ff8f12c commit 51f10d7

File tree

5 files changed

+126
-43
lines changed

5 files changed

+126
-43
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ Let's make a simple carousel with three slides, a next button, and a back button
5959
```JSX
6060
import React from 'react';
6161
import { CarouselProvider, Slider, Slide, ButtonBack, ButtonNext } from 'pure-react-carousel';
62-
```
62+
```
6363

6464
3. Using Webpack or Rollup? Does your Webpack config have a loader for "css" files? If so, import the css as well.
6565

6666
```JSX
6767
import React from 'react';
6868
import { CarouselProvider, Slider, Slide, ButtonBack, ButtonNext } from 'pure-react-carousel';
6969
import 'pure-react-carousel/dist/react-carousel.es.css';
70-
```
70+
```
7171

7272
4. Put a CarouselProvider component in your code. This allows the other carousel components to communicate with each other. The only required properties are the orientation, naturalSlideWidth, and naturalSlideHeight. The naturalSlideWidth and naturalSlideHeight are used to create an aspect ratio for each slide. Since the carousel is responsive by default, it will stretch to fill in the width of it's parent container. The CarouselProvider must also have children. We'll add the children in the next step.
7373

@@ -129,7 +129,7 @@ Let's make a simple carousel with three slides, a next button, and a back button
129129
naturalSlideWidth={100}
130130
naturalSlideHeight={125}
131131
totalSlides={3}
132-
>
132+
>
133133
<Slider>
134134
<Slide index={0}>I am the first Slide.</Slide>
135135
<Slide index={1}>I am the second Slide.</Slide>
@@ -144,9 +144,9 @@ Let's make a simple carousel with three slides, a next button, and a back button
144144
```
145145

146146

147-
That's it. You have a super basic Carousel.
147+
That's it. You have a super basic Carousel.
148148

149-
There are other components you can add, like ButtonFirst, ButtonLast, an Image component, and even an ImageWithZoom component that zooms on mouse hover or finger tap.
149+
There are other components you can add, like ButtonFirst, ButtonLast, an Image component, and even an ImageWithZoom component that zooms on mouse hover or finger tap.
150150

151151
Obviously, you can customize the heck out of the layout. If you need to bury your Slider component in 18 parent divs, go for it. It will still do it's job. Feel free to add the className property to any of the Components to further customize your carousel. Or, hook into the many BEM named default CSS class names built into the carousel components.
152152

@@ -215,6 +215,7 @@ A Slider is a viewport that masks slides. The Slider component must wrap one or
215215
| onMasterSpinner | [function&#124;null] | null | No | Optional callback function that is called when the Master Spinner is visible. Requires that &lt;CarouselProvider /> set hasMasterSpinner to true |
216216
| spinner | function | null | No | Optional inline JSX (aka "render props") to render your own custom spinner. Example `() => <MyCustomSpinnerComponent />` If left blank, the default spinner is used. |
217217
| style | object | {} | No | Optional css styles to add to the Slider. Note: internal css properties take precedence over any styles specified in the styles object |
218+
| trayProps | object | {} | No | Any props you want to attach to the slider tray with the exception of className and style. The className prop is handled via classNameTray prop above. Style is used internally. Any event handlers like onMouseDown or others are called after any of our internal event handlers. |
218219
| trayTag | string | 'ul' | No | The HTML tag to used for the tray (the thing that holds all the slides and moves the slides back and forth.) |
219220

220221
#### The Slider component creates the following pseudo HTML by default.
@@ -428,7 +429,7 @@ Use this HOC to pass CarouselProvider state properties as props to a component.
428429
</CarouselProvider>
429430
```
430431

431-
WithStore has two arguments:
432+
WithStore has two arguments:
432433

433434
`WithStore([component], [mapstateToProps])`
434435

package-lock.json

Lines changed: 34 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@
8585
"rollup-plugin-commonjs": "^9.2.0",
8686
"rollup-plugin-eslint": "^5.0.0",
8787
"rollup-plugin-img": "^1.1.0",
88-
"rollup-plugin-livereload": "^0.6.0",
88+
"rollup-plugin-livereload": "^1.0.0",
8989
"rollup-plugin-multi-entry": "^2.0.1",
90-
"rollup-plugin-node-resolve": "^3.4.0",
90+
"rollup-plugin-node-resolve": "^4.2.3",
9191
"rollup-plugin-postcss": "^2.0.3",
9292
"rollup-plugin-replace": "^2.1.0",
93-
"rollup-plugin-serve": "^0.6.0",
93+
"rollup-plugin-serve": "^1.0.1",
9494
"rollup-plugin-uglify": "^6.0.0",
9595
"rollup-plugin-uglify-es": "0.0.1",
9696
"rollup-watch": "^4.3.1",

src/Slider/Slider.jsx

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Slider = class Slider extends React.Component {
1919
disableAnimation: PropTypes.bool,
2020
disableKeyboard: PropTypes.bool,
2121
dragEnabled: PropTypes.bool.isRequired,
22+
dragStep: PropTypes.number,
2223
hasMasterSpinner: PropTypes.bool.isRequired,
2324
interval: PropTypes.number.isRequired,
2425
isPageScrollLocked: PropTypes.bool.isRequired,
@@ -35,11 +36,11 @@ const Slider = class Slider extends React.Component {
3536
slideTraySize: PropTypes.number.isRequired,
3637
spinner: PropTypes.func,
3738
step: PropTypes.number.isRequired,
38-
dragStep: PropTypes.number,
3939
style: PropTypes.object,
4040
tabIndex: PropTypes.number,
4141
totalSlides: PropTypes.number.isRequired,
4242
touchEnabled: PropTypes.bool.isRequired,
43+
trayProps: PropTypes.shape({}),
4344
trayTag: PropTypes.string,
4445
visibleSlides: PropTypes.number,
4546
}
@@ -51,14 +52,15 @@ const Slider = class Slider extends React.Component {
5152
classNameTrayWrap: null,
5253
disableAnimation: false,
5354
disableKeyboard: false,
55+
dragStep: 1,
5456
moveThreshold: 0.1,
5557
onMasterSpinner: null,
5658
spinner: null,
5759
style: {},
5860
tabIndex: null,
61+
trayProps: {},
5962
trayTag: 'ul',
6063
visibleSlides: 1,
61-
dragStep: 1,
6264
}
6365

6466
static slideSizeInPx(orientation, sliderTrayWidth, sliderTrayHeight, totalSlides) {
@@ -91,6 +93,7 @@ const Slider = class Slider extends React.Component {
9193
this.handleOnTouchStart = this.handleOnTouchStart.bind(this);
9294
this.playBackward = this.playBackward.bind(this);
9395
this.playForward = this.playForward.bind(this);
96+
this.callCallback = this.callCallback.bind(this);
9497

9598
this.state = {
9699
cancelNextClick: false,
@@ -214,14 +217,26 @@ const Slider = class Slider extends React.Component {
214217
this.sliderTrayElement = el;
215218
}
216219

220+
callCallback(propName, ev) {
221+
const { trayProps } = this.props;
222+
if (trayProps && typeof trayProps[propName] === 'function') {
223+
ev.persist();
224+
trayProps[propName](ev);
225+
}
226+
}
227+
217228
handleOnMouseDown(ev) {
218-
if (!this.props.dragEnabled) return;
229+
if (!this.props.dragEnabled) {
230+
this.callCallback('onMouseDown', ev);
231+
return;
232+
}
219233
ev.preventDefault();
220234
this.onDragStart({
221235
screenX: ev.screenX,
222236
screenY: ev.screenY,
223237
mouseDrag: true,
224238
});
239+
this.callCallback('onMouseDown', ev);
225240
}
226241

227242
handleOnMouseMove(ev) {
@@ -238,14 +253,21 @@ const Slider = class Slider extends React.Component {
238253
}
239254

240255
handleOnClickCapture(ev) {
241-
if (!this.state.cancelNextClick) return;
256+
if (!this.state.cancelNextClick) {
257+
this.callCallback('onClickCapture', ev);
258+
return;
259+
}
242260
ev.stopPropagation();
243261
ev.preventDefault();
244262
this.setState({ cancelNextClick: false });
263+
this.callCallback('onClickCapture', ev);
245264
}
246265

247266
handleOnTouchStart(ev) {
248-
if (!this.props.touchEnabled) return;
267+
if (!this.props.touchEnabled) {
268+
this.callCallback('onTouchStart', ev);
269+
return;
270+
}
249271

250272
if (this.props.orientation === 'vertical') {
251273
ev.preventDefault();
@@ -258,6 +280,7 @@ const Slider = class Slider extends React.Component {
258280
screenY: touch.screenY,
259281
touchDrag: true,
260282
});
283+
this.callCallback('onTouchStart', ev);
261284
}
262285

263286
handleDocumentScroll() {
@@ -273,12 +296,16 @@ const Slider = class Slider extends React.Component {
273296
if (
274297
!this.props.touchEnabled
275298
|| (this.props.lockOnWindowScroll && this.isDocumentScrolling)
276-
) return;
299+
) {
300+
this.callCallback('onTouchMove', ev);
301+
return;
302+
}
277303

278304
window.cancelAnimationFrame.call(window, this.moveTimer);
279305

280306
const touch = ev.targetTouches[0];
281307
this.onDragMove(touch.screenX, touch.screenY);
308+
this.callCallback('onTouchMove', ev);
282309
}
283310

284311
forward() {
@@ -418,12 +445,14 @@ const Slider = class Slider extends React.Component {
418445
this.sliderElement.focus();
419446
}
420447

421-
handleOnTouchEnd() {
448+
handleOnTouchEnd(ev) {
422449
this.endTouchMove();
450+
this.callCallback('onTouchEnd', ev);
423451
}
424452

425-
handleOnTouchCancel() {
453+
handleOnTouchCancel(ev) {
426454
this.endTouchMove();
455+
this.callCallback('onTouchCancel', ev);
427456
}
428457

429458
endTouchMove() {
@@ -484,6 +513,7 @@ const Slider = class Slider extends React.Component {
484513
tabIndex,
485514
totalSlides,
486515
touchEnabled,
516+
trayProps,
487517
trayTag: TrayTag,
488518
visibleSlides,
489519
...props
@@ -550,6 +580,23 @@ const Slider = class Slider extends React.Component {
550580
// remove `dragStep` and `step` from Slider div, since it isn't a valid html attribute
551581
const { dragStep, step, ...rest } = props;
552582

583+
// filter out some tray props before passing them in. We will process event handlers in the
584+
// trayProps object as callbacks to OUR event handlers. Ref is needed by us. Style and
585+
// className are in the main props. Can't merge them into trayProps without causing a breaking
586+
// change.
587+
const {
588+
className: ignoreClassName,
589+
onClickCapture,
590+
onMouseDown,
591+
onTouchCancel,
592+
onTouchEnd,
593+
onTouchMove,
594+
onTouchStart,
595+
ref: ignoreRef,
596+
style: ignoreStyle,
597+
...filteredTrayProps
598+
} = trayProps;
599+
553600
return (
554601
<div
555602
ref={(el) => { this.sliderElement = el; }}
@@ -572,6 +619,7 @@ const Slider = class Slider extends React.Component {
572619
onTouchCancel={this.handleOnTouchCancel}
573620
onMouseDown={this.handleOnMouseDown}
574621
onClickCapture={this.handleOnClickCapture}
622+
{...filteredTrayProps}
575623
>
576624
{children}
577625
</TrayTag>

0 commit comments

Comments
 (0)