benwiley4000 / react-gif-player

📽 A GIF component that moves when YOU want it to!
https://benwiley4000.github.io/react-gif-player/
MIT License
94 stars 32 forks source link

Support gif playlists #25

Closed AAverin closed 5 years ago

AAverin commented 5 years ago

Would be nice to display a list of gifs one after another in a playlist manner – useful to showcase product features

benwiley4000 commented 5 years ago

Hm, not sure how generic this use case is. Would it be difficult to do it by having a list of gifs available in the parent component and store the selected gif as component state in the parent?

benwiley4000 commented 5 years ago

@AAverin what do you think about my suggestion?

AAverin commented 5 years ago

@benwiley4000 Hey Ben. I switched my solution to YouTube embeds for now, because gifs were too heavy and they were loading with the page. In general yes, I think everybody can wrap around the player to support playlist, or it could be another separate component, provided by the library

benwiley4000 commented 5 years ago

Alright thanks for the update!

For the record, here's an example of what a custom component could look like. I think it's a bit too specific to be included with the library though, unless I start getting many requests for this sort of thing.

class GifPlaylist extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedIndex: 0,
    };
  }

  next() {
    this.setState(state => {
      if (state.selectedIndex + 1 === this.props.playlist.length) {
        return {
          selectedIndex: 0,
        };
      }
      return {
        selectedIndex: state.selectedIndex + 1,
      };
    });
  }

  render() {
    const selected = this.props.playlist[this.state.selectedIndex];
    return (
      <>
        <button onClick={() => this.next()}>Next</button>
        <GifPlayer gif={selected.gif} still={selected.still} />
      </>
    );
  }
}