Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/jwplayer/players/shared/JwPlayerWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import JWEvents from 'jwplayer/players/shared/JWEvents';
import addBaseTrackingEvents from 'jwplayer/players/shared/addBaseTrackingEvents';
import slugify from 'jwplayer/utils/slugify';
import getSponsoredVideos from 'utils/getSponsoredVideos';
import { isInViewportCheck } from 'jwplayer/utils/utils';

interface WindowJWPlayer extends Window {
jwplayer?: JWPlayerApi;
Expand Down Expand Up @@ -100,9 +101,13 @@ const JwPlayerWrapper: React.FC<JwPlayerWrapperProps> = ({
...configWithoutImage,
});

playerInstance.on(JWEvents.AD_PAUSE, ({ pauseReason, viewable }: JWPauseEvent) => {
playerInstance.on(JWEvents.AD_PAUSE, ({ pauseReason }: JWPauseEvent) => {
const playerElement = document.getElementById('featured-video__player');
// The 'viewable' property from the JWPauseEvent is not reliable
const isInViewport = isInViewportCheck(playerElement);

// Keep playing the ad when the user closed the mini player
if (viewable === 0 && pauseReason === 'external') {
if (isInViewport === false && pauseReason === 'external') {
playerInstance.play();
}
});
Expand Down
13 changes: 13 additions & 0 deletions src/jwplayer/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ export const isInGallery = () => {
}
return false;
};

export const isInViewportCheck = (element: HTMLElement) => {
if (element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
return false;
};