Skip to content

Commit

Permalink
Stop theme-irrelevant videos from loading
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Aug 21, 2024
1 parent 235ae72 commit c3b3527
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
59 changes: 45 additions & 14 deletions src/components/elements/video-player/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
'use client';

import * as React from "react";
import { useTheme } from "next-themes";

import { useMounted } from '@/lib/hooks/use-mounted';

import { StyledIframe } from "./video-player.styles";

const urlOptions = new URLSearchParams({
Expand All @@ -14,23 +21,47 @@ export const VideoPlayer = (props: {
lightId: string,
aspectRatio: string
}) => {
const { isMounted } = useMounted();
const { resolvedTheme: theme } = useTheme();

const darkUrl = `https://iframe.mediadelivery.net/embed/${props.libraryId}/${props.darkId}?${urlOptions}`;
const lightUrl = `https://iframe.mediadelivery.net/embed/${props.libraryId}/${props.lightId}?${urlOptions}`;

const darkVisibility = isMounted
? theme === 'dark'
: undefined;
const lightVisibility = isMounted
? theme === 'light'
: undefined;

return <>
<StyledIframe
src={lightUrl}
data-hide-on-theme="dark"
aspectRatio={props.aspectRatio}
allow="autoplay;picture-in-picture;"
allowFullScreen={true}
/>
<StyledIframe
src={darkUrl}
data-hide-on-theme="light"
aspectRatio={props.aspectRatio}
allow="autoplay;picture-in-picture;"
allowFullScreen={true}
/>
{ (!isMounted || theme === 'dark') &&
<StyledIframe
src={darkUrl}

loading="lazy"
// Hide if wrong theme
data-hide-on-theme="light"
$visible={darkVisibility}

$aspectRatio={props.aspectRatio}
allow="autoplay;picture-in-picture;"
allowFullScreen={true}
/>
}
{ (!isMounted || theme === 'light') &&
<StyledIframe
src={lightUrl}

loading="lazy"
// Hide if wrong theme
data-hide-on-theme="dark"
$visible={lightVisibility}

$aspectRatio={props.aspectRatio}
allow="autoplay;picture-in-picture;"
allowFullScreen={true}
/>
}
</>;
};
24 changes: 22 additions & 2 deletions src/components/elements/video-player/video-player.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
import { styled } from '@/styles';

export const StyledIframe = styled.iframe<{
aspectRatio: string
$aspectRatio: string,
$visible: boolean | undefined
}>`
width: 100%;
aspect-ratio: ${props => props.aspectRatio};
aspect-ratio: ${props => props.$aspectRatio};
border: none;
${p => p.$visible === undefined &&
// During SSR, we render both and use CSS only to show the right default
`
@media (prefers-color-scheme: dark) {
&[data-hide-on-theme="dark"] { display: none; }
}
@media (prefers-color-scheme: light) {
&[data-hide-on-theme="light"] { display: none; }
}
`}
${p => p.$visible === false &&
// Once rendering is active, we render only the right one, but then we also
// make it hidden until the content is actually ready.
`
visibility: hidden;
`}
`;

0 comments on commit c3b3527

Please sign in to comment.