Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changeSlide props #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-fluid-gallery": "link:..",
"react-fluid-gallery": "file:..",
"react-github-corner": "^2.3.0",
"react-scripts": "^1.1.4"
},
Expand Down
21 changes: 20 additions & 1 deletion example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component } from 'react'

import FluidGallery from 'react-fluid-gallery'
import FluidGallery, { changeSlideEnum } from 'react-fluid-gallery'
import GitHubCorner from 'react-github-corner'

import video1 from './assets/0.mp4'
Expand All @@ -22,17 +22,36 @@ const images = [
]

export default class App extends Component {

constructor() {
super()
this.state = {
changeSlide: undefined
}
}

onClickPrev = () => {
this.setState({changeSlide: changeSlideEnum.prev});
}

onClickNext = () => {
this.setState({changeSlide: changeSlideEnum.next});
}

render () {
return (
<div
style={{
height: '100vh'
}}
>
<button onClick={this.onClickNext}> Click to go to next slide</button>
<button onClick={this.onClickPrev}> Click to go to prev slide</button>
<FluidGallery
slides={images}
startAt={0}
onChange={this._onChange}
changeSlide={this.state.changeSlide}
/>

<div
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"gh-pages": "^1.2.0",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts": "^1.1.4"
"react-scripts": "^1.1.4",
"yarn": "^1.22.17"
},
"files": [
"dist"
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default class App extends Component {
| `slides` | `Array<string>` | **required** | Array of image / video URLs to use for the gallery slides. |
| `startAt` | number | random | Default slide to show. |
| `onChange` | function(index: number) | undefined | Optional callback when the active slide is changed. |
| `changeSlide` | `changeSlideEnum.next \| changeSlideEnum.prev` | undefined | Passing down `next` will change the active slide to the next, whereas passing down `prev` will change the active slide to the previous. This is useful if you want to control the slide transition in the consuming application by means other than manually swiping / scrolling. See an example of this in ./example. |
| `...` | ... | undefined | Any other props are applied to the root element. |

## Credits
Expand Down
23 changes: 21 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import FluidGallery from './fluid-gallery'

const noop = () => undefined

export const changeSlideEnum = {
next: 'next',
prev: 'prev'
}

class ReactFluidGallery extends Component {
static propTypes = {
slides: PropTypes.arrayOf(PropTypes.string).isRequired,
Expand All @@ -20,12 +25,14 @@ class ReactFluidGallery extends Component {
size: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
})
}),
changeSlide: PropTypes.oneOf(Object.keys(changeSlideEnum))
}

static defaultProps = {
onChange: noop,
style: { }
style: { },
changeSlide: undefined,
}

_current = (typeof this.props.startAt !== 'undefined'
Expand All @@ -51,12 +58,24 @@ class ReactFluidGallery extends Component {
}
}

shouldComponentUpdate(nextProps) {
const { changeSlide } = nextProps
if (changeSlide) {
const multiplier = changeSlide === changeSlideEnum.prev ? -1 : 1
const deltaY = this._canvas.height * 1.5 * multiplier
this._gallery.onScroll({ deltaY })
return false
}
return true
}

render() {
const {
slides,
startAt,
onChange,
style,
changeSlide,
...rest
} = this.props

Expand Down