Skip to content

Commit 749105d

Browse files
authored
feat: Add userAction.click to prevent pause/play when player is clicked (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.
1 parent 11228cf commit 749105d

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

docs/guides/options.md

+35
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [techCanOverridePoster](#techcanoverrideposter)
4646
* [techOrder](#techorder)
4747
* [userActions](#useractions)
48+
* [userActions.click](#useractionsclick)
4849
* [userActions.doubleClick](#useractionsdoubleclick)
4950
* [userActions.hotkeys](#useractionshotkeys)
5051
* [userActions.hotkeys.fullscreenKey](#useractionshotkeysfullscreenkey)
@@ -443,6 +444,40 @@ Defines the order in which Video.js techs are preferred. By default, this means
443444

444445
> Type: `Object`
445446
447+
### `userActions.click`
448+
449+
> Type: `boolean|function`
450+
451+
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.
452+
453+
```js
454+
videojs('my-player', {
455+
userActions: {
456+
click: false
457+
}
458+
});
459+
```
460+
461+
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`):
462+
463+
```js
464+
function myClickHandler(event) = {
465+
// `this` is the player in this context
466+
467+
if (this.isFullscreen()) {
468+
this.exitFullscreen();
469+
} else {
470+
this.requestFullscreen();
471+
}
472+
};
473+
474+
videojs('my-player', {
475+
userActions: {
476+
click: myClickHandler
477+
}
478+
});
479+
```
480+
446481
### `userActions.doubleClick`
447482

448483
> Type: `boolean|function`

sandbox/userAction-click.html.example

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Video.js Sandbox</title>
6+
<link href="../dist/video-js.css" rel="stylesheet" type="text/css">
7+
<script src="../dist/video.js"></script>
8+
</head>
9+
<body>
10+
<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;">
11+
<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>
12+
<pre>npm start</pre>
13+
<pre>open http://localhost:9999/sandbox/index.html</pre>
14+
</div>
15+
16+
<video-js
17+
id="vid1"
18+
controls
19+
preload="auto"
20+
width="640"
21+
height="264"
22+
poster="https://vjs.zencdn.net/v/oceans.png">
23+
<source src="https://vjs.zencdn.net/v/oceans.mp4" type="video/mp4">
24+
<source src="https://vjs.zencdn.net/v/oceans.webm" type="video/webm">
25+
<source src="https://vjs.zencdn.net/v/oceans.ogv" type="video/ogg">
26+
<track kind="captions" src="../docs/examples/shared/example-captions.vtt" srclang="en" label="English">
27+
<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>
28+
</video-js>
29+
30+
<script>
31+
var vid = document.getElementById('vid1');
32+
function myClickHandler(event) {
33+
console.log("Yowser! Single Click Action!");
34+
if (this.isFullscreen()) {
35+
this.exitFullscreen();
36+
} else {
37+
this.requestFullscreen();
38+
}
39+
};
40+
var player = videojs(vid, {
41+
userActions: {
42+
click: myClickHandler
43+
}
44+
});
45+
</script>
46+
47+
</body>
48+
</html>

src/js/player.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1936,10 +1936,26 @@ class Player extends Component {
19361936
return;
19371937
}
19381938

1939-
if (this.paused()) {
1940-
silencePromise(this.play());
1941-
} else {
1942-
this.pause();
1939+
if (
1940+
this.options_ === undefined ||
1941+
this.options_.userActions === undefined ||
1942+
this.options_.userActions.click === undefined ||
1943+
this.options_.userActions.click !== false
1944+
) {
1945+
1946+
if (
1947+
this.options_ !== undefined &&
1948+
this.options_.userActions !== undefined &&
1949+
typeof this.options_.userActions.click === 'function'
1950+
) {
1951+
1952+
this.options_.userActions.click.call(this, event);
1953+
1954+
} else if (this.paused()) {
1955+
silencePromise(this.play());
1956+
} else {
1957+
this.pause();
1958+
}
19431959
}
19441960
}
19451961

0 commit comments

Comments
 (0)