Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

feat: implement custom accessible tooltips with keyboard shortcuts for the video player (#4787)

IEBqp352796fbaaec
diff --git a/web/components/video/VideoJS/VideoJS.module.scss b/web/components/video/VideoJS/VideoJS.module.scss
index 9a77133e47..dbf43f8019 100644
--- a/web/components/video/VideoJS/VideoJS.module.scss
+++ b/web/components/video/VideoJS/VideoJS.module.scss
@@ -2,4 +2,70 @@
.player {
width: 100%;
+ position: relative;
+ overflow: hidden;
+
+ @media (hover: hover) and (pointer: fine) {
+ :global(.vjs-control-bar .vjs-control:hover .vjs-control-text) {
+ /* This ensures the text isn't hidden by default VJS accessibility rules */
+ display: block !important;
+ visibility: visible !important;
+ width: auto !important;
+ height: auto !important;
+ /* stylelint-disable-next-line property-no-deprecated */
+ clip: auto !important; /* necessary to make the control text visible */
+ margin: 0 !important;
+
+ /* Base tooltip styles */
+ position: absolute;
+ bottom: 45px;
+ left: 50%;
+ transform: translateX(-50%);
+ white-space: nowrap;
+ z-index: 1000;
+ padding: 5px 10px;
+ background: rgb(20 20 20 / 90%);
+ color: #fff;
+ border-radius: 4px;
+ font-size: 12px;
+ line-height: 1.2;
+ pointer-events: none; /* Prevents tooltip from flickering when mouse hits it */
+
+ /* If there's no text content, it stays invisible */
+ &:empty {
+ display: none !important;
+ }
+ }
+
+ /* Prevent the "Mute"/"Unmute" tooltip from showing when interacting with the volume slider.
+ * Since the slider is part of the volume panel container, hovering it technically
+ * counts as hovering the mute control context. We force-hide the tooltip
+ * unless the cursor is explicitly over the mute button icon itself.
+ */
+ :global(.vjs-volume-panel:hover .vjs-mute-control:not(:hover) .vjs-control-text) {
+ display: none !important;
+ }
+
+ /* Move the first and last element slightly right and left to prevent them from overflowing respectively */
+ :global(.vjs-control:first-child:hover .vjs-control-text) {
+ left: 0;
+ transform: translateX(6px);
+
+ &::after {
+ left: 15%;
+ }
+ }
+
+ :global(.vjs-control:last-child:hover .vjs-control-text) {
+ left: auto;
+ right: 0;
+ transform: translateX(-6px);
+
+ &::after {
+ left: auto;
+ right: 15%;
+ transform: translateX(50%);
+ }
+ }
+ }
}
diff --git a/web/components/video/VideoJS/VideoJS.tsx b/web/components/video/VideoJS/VideoJS.tsx
index 6120056c17..83bcf5322e 100644
--- a/web/components/video/VideoJS/VideoJS.tsx
+++ b/web/components/video/VideoJS/VideoJS.tsx
@@ -1,11 +1,23 @@
import React, { FC } from 'react';
import videojs from 'video.js';
import type VideoJsPlayer from 'video.js/dist/types/player';
+import { useTranslation } from 'next-export-i18n';
import styles from './VideoJS.module.scss';
require('video.js/dist/video-js.css');
+const SHORTCUT_SUFFIXES: Record<string, string> = {
+ Play: ' (Space)',
+ Pause: ' (Space)',
+ Mute: ' (m)',
+ Unmute: ' (m)',
+ Fullscreen: ' (f)',
+ 'Non-Fullscreen': ' (f)',
+ 'Picture-in-Picture': ' (i)',
+ 'Exit Picture-in-Picture': ' (i)',
+};
+
export type VideoJSProps = {
options: any;
onReady: (player: VideoJsPlayer, vjsInstance: typeof videojs) => void;
@@ -14,14 +26,30 @@ export type VideoJSProps = {
export const VideoJS: FC<VideoJSProps> = ({ options, onReady }) => {
const videoRef = React.useRef<HTMLVideoElement | null>(null);
const playerRef = React.useRef<VideoJsPlayer | null>(null);
+ const { t } = useTranslation();
+
+ const addShortcutsToLanguage = (vjs: typeof videojs, langCode: string) => {
+ const updates: Record<string, string> = {};
+ Object.keys(SHORTCUT_SUFFIXES).forEach(key => {
+ const currentLabel = key;
+ const suffix = SHORTCUT_SUFFIXES[key];
+ updates[key] = t(`${currentLabel}${suffix}`);
+ });
+ vjs.addLanguage(langCode, updates);
+ };
React.useEffect(() => {
// Make sure Video.js player is only initialized once
if (!playerRef.current) {
const videoElement = videoRef.current;
+ addShortcutsToLanguage(videojs, 'en');
+ const finalOptions = {
+ ...options,
+ noUITitleAttributes: true, // Prevents videojs from adding a title attribute to UI elements, thus preventing "double tooltips".
+ };
// eslint-disable-next-line no-multi-assign
- const player: VideoJsPlayer = (playerRef.current = videojs(videoElement, options, () => {
+ const player: VideoJsPlayer = (playerRef.current = videojs(videoElement, finalOptions, () => {
console.debug('player is ready');
return onReady && onReady(player, videojs);
}));