Skip to content

Commit

Permalink
Add YouTube iframe fullscreen behaviour for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
arelra committed Sep 30, 2024
1 parent d620494 commit e805cc5
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions dotcom-rendering/src/components/YoutubeAtom/YoutubeAtomPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css } from '@emotion/react';
import { css, Global } from '@emotion/react';
import type { Participations } from '@guardian/ab-core';
import { buildImaAdTagUrl } from '@guardian/commercial';
import type { ConsentState } from '@guardian/libs';
Expand Down Expand Up @@ -74,6 +74,19 @@ const imaPlayerStyles = css`
height: 100%;
`;

const fullscreenStyles = (id: string) => css`
html {
overflow: hidden;
}
iframe#${id} {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
}
`;

/**
* Dispatches a custom play event so that other players listening
* for this event will stop playing
Expand Down Expand Up @@ -398,6 +411,7 @@ export const YoutubeAtomPlayer = ({
});

const [playerReady, setPlayerReady] = useState<boolean>(false);
const [isFullscreen, setIsFullscreen] = useState<boolean>(false);
const playerReadyCallback = useCallback(() => setPlayerReady(true), []);
const playerListeners = useRef<PlayerListeners>([]);
/**
Expand Down Expand Up @@ -472,8 +486,7 @@ export const YoutubeAtomPlayer = ({
from: 'YoutubeAtomPlayer fullscreen',
videoId,
});
// For Android only, iOS will stub the method
void getVideoClient().fullscreen();
setIsFullscreen((prev) => !prev);
}
},
},
Expand Down Expand Up @@ -644,17 +657,27 @@ export const YoutubeAtomPlayer = ({
};
}, []);

useEffect(() => {
if (renderingTarget === 'Apps') {
console.log('fullscreen', isFullscreen);
// For Android only, iOS will stub the method
// void getVideoClient().fullscreen(isFullscreen);
}
}, [isFullscreen, renderingTarget]);

/**
* An element for the YouTube player to hook into the dom
*/
return (
<div
id={id}
data-atom-id={id}
data-testid={id}
data-atom-type="youtube"
title={title}
css={enableAds && imaPlayerStyles}
></div>
<>
{isFullscreen && <Global styles={fullscreenStyles(id)} />}
<div
id={id}
data-testid={id}
data-atom-type="youtube"
title={title}
css={enableAds && imaPlayerStyles}
></div>
</>
);
};

0 comments on commit e805cc5

Please sign in to comment.