react-youtube icon indicating copy to clipboard operation
react-youtube copied to clipboard

Focus player on mount

Open ahmedu007 opened this issue 6 years ago • 3 comments

Hi,

Thankyou for the amazing package. The keyboard shortcuts work great too, however, they only work when the iframe is selected and in focus. I'm trying to focus the player on load so the user can use keys to play / pause videos and use other controls. I've tried using refs but they didnt work. Is there any other way of doing it?

Here is my sample code:

class Videos extends Component {
  constructor() {
    super();

    this.playerRef = React.createRef();
  }

  componentDidMount = () => {
   this.playerRef.current.focus();
  };

  // _onReady = e => console.log(e.target);

  render() {
    const opts = {
      height: "390",
      width: "640",
      playerVars: {
        // https://developers.google.com/youtube/player_parameters
        autoplay: 1,
      },
    };
    return (
        <YouTube videoId="2g811Eo7K8U" opts={opts} onReady={this._onReady} ref={this.playerRef} />
    );
  }
}

ahmedu007 avatar Dec 10 '18 16:12 ahmedu007

@ahmedu007 Did you ever find a solution to this?

BradyEdgar94 avatar Sep 24 '19 19:09 BradyEdgar94

No unfortunately :(

ahmedu007 avatar Oct 07 '19 11:10 ahmedu007

:( * 2

silnose avatar Oct 21 '20 03:10 silnose