Skip to content

Commit

Permalink
feat: Add userAction.click to prevent pause/play when player is click…
Browse files Browse the repository at this point in the history
…ed (videojs#7495)

Pass `false` as `userAction.click` to disable the default click-to-play behavior. Alternatively, pass in a function, to enable custom behavior.

Fixes videojs#7123.
  • Loading branch information
rberger authored and edirub committed Jun 8, 2023
1 parent 0dc6ca8 commit 70118ab
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
35 changes: 35 additions & 0 deletions docs/guides/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* [techCanOverridePoster](#techcanoverrideposter)
* [techOrder](#techorder)
* [userActions](#useractions)
* [userActions.click](#useractionsclick)
* [userActions.doubleClick](#useractionsdoubleclick)
* [userActions.hotkeys](#useractionshotkeys)
* [userActions.hotkeys.fullscreenKey](#useractionshotkeysfullscreenkey)
Expand Down Expand Up @@ -443,6 +444,40 @@ Defines the order in which Video.js techs are preferred. By default, this means

> Type: `Object`
### `userActions.click`

> Type: `boolean|function`
Controls how clicking on the player/tech operates. If set to `false`, clicking is disabled and will no longer cause the player to toggle between paused and playing.

```js
videojs('my-player', {
userActions: {
click: false
}
});
```

If undefined or set to `true`, clicking is enabled and toggles the player between paused and play. To override the default click handling, set `userActions.click` to a function which accepts a `click` event (in this example it will request Full Screen, the same as a `userAction.doubleClick`):

```js
function myClickHandler(event) = {
// `this` is the player in this context

if (this.isFullscreen()) {
this.exitFullscreen();
} else {
this.requestFullscreen();
}
};

videojs('my-player', {
userActions: {
click: myClickHandler
}
});
```

### `userActions.doubleClick`

> Type: `boolean|function`
Expand Down
48 changes: 48 additions & 0 deletions sandbox/userAction-click.html.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Video.js Sandbox</title>
<link href="../dist/video-js.css" rel="stylesheet" type="text/css">
<script src="../dist/video.js"></script>
</head>
<body>
<div style="background-color:#eee; border: 1px solid #777; padding: 10px; margin-bottom: 20px; font-size: .8em; line-height: 1.5em; font-family: Verdana, sans-serif;">
<p>You can use /sandbox/ for writing and testing your own code. Nothing in /sandbox/ will get checked into the repo, except files that end in .example (so don't edit or add those files). To get started run `npm start` and open the index.html</p>
<pre>npm start</pre>
<pre>open http://localhost:9999/sandbox/index.html</pre>
</div>

<video-js
id="vid1"
controls
preload="auto"
width="640"
height="264"
poster="https://vjs.zencdn.net/v/oceans.png">
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm">
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg">
<track kind="captions" src="../docs/examples/shared/example-captions.vtt" srclang="en" label="English">
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video-js>

<script>
var vid = document.getElementById('vid1');
function myClickHandler(event) {
console.log("Yowser! Single Click Action!");
if (this.isFullscreen()) {
this.exitFullscreen();
} else {
this.requestFullscreen();
}
};
var player = videojs(vid, {
userActions: {
click: myClickHandler
}
});
</script>

</body>
</html>
24 changes: 20 additions & 4 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1936,10 +1936,26 @@ class Player extends Component {
return;
}

if (this.paused()) {
silencePromise(this.play());
} else {
this.pause();
if (
this.options_ === undefined ||
this.options_.userActions === undefined ||
this.options_.userActions.click === undefined ||
this.options_.userActions.click !== false
) {

if (
this.options_ !== undefined &&
this.options_.userActions !== undefined &&
typeof this.options_.userActions.click === 'function'
) {

this.options_.userActions.click.call(this, event);

} else if (this.paused()) {
silencePromise(this.play());
} else {
this.pause();
}
}
}

Expand Down

0 comments on commit 70118ab

Please sign in to comment.