Skip to content

Instantly share code, notes, and snippets.

@outofthisworld
Last active May 21, 2019 13:36
Show Gist options
  • Select an option

  • Save outofthisworld/89bd258cf2041a0e08b6d8aee1bf6102 to your computer and use it in GitHub Desktop.

Select an option

Save outofthisworld/89bd258cf2041a0e08b6d8aee1bf6102 to your computer and use it in GitHub Desktop.
React pose slider
import React from "react";
import ReactDOM from "react-dom";
import { useState, useEffect, useRef } from "react";
import posed, { PoseGroup } from "react-pose";
import "./styles.css";
let AnimatableDiv = posed.div({
enter: {
y: 0,
x: 0,
opacity: 1,
rotate: "0deg",
transition: {
duration: 3000
}
},
exit: {
x: "-150%",
opacity: 0.5,
transition: {
duration: 3000,
delay: 1000
}
}
});
function Slider(props) {
let [sliderIndex, setSlideIndex] = useState(0);
let [isPlaying, setIsPlaying] = useState(true);
useEffect(
() => {
let cTimeout = null;
if (isPlaying) {
cTimeout = setTimeout(function() {
setSlideIndex((sliderIndex + 1) % props.children.length);
}, 3000);
}
return () => {
window.clearTimeout(cTimeout);
};
},
[isPlaying, sliderIndex, props.children.length]
);
return (
<div style={{ position: "relative", display: "inline-block" }}>
<PoseGroup>
{props.children.map(function(child, indx) {
return (
(indx === sliderIndex && (
<AnimatableDiv key={indx}>
{props.children[sliderIndex]}
</AnimatableDiv>
)) ||
null
);
})}
</PoseGroup>
<div style={{ position: "absolute", left: "10px", top: "50%" }}>&gt</div>
<div style={{ position: "absolute", right: "10px", top: "50%" }}>&lt</div>
<div
onClick={() => setIsPlaying(!isPlaying)}
style={{ position: "absolute", right: "50%", top: "80%" }}
>
{isPlaying ? "pause" : "play"}
</div>
</div>
);
}
function App() {
return (
<Slider>
<img key={1} src="http://www.placehold.it/400x200" />
<img key={3} src="http://www.placehold.it/400x200" />
<img key={4} src="http://www.placehold.it/400x200" />
<img key={5} src="http://www.placehold.it/400x200" />
<img key={6} src="http://www.placehold.it/400x200" />
<img key={7} src="http://www.placehold.it/400x200" />
</Slider>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment