Skip to content
Merged
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
44 changes: 30 additions & 14 deletions components/MdxContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ const Video = ({src, allow, width}: {

const [playing, setPlaying] = React.useState(false);

const backgroundRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
if (!playing) {
return;
if (playing) {
backgroundRef.current?.focus();
}

const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
close();
}
};

window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [playing]);

const className =
Expand All @@ -76,14 +69,37 @@ const Video = ({src, allow, width}: {
return (
<div
className={className}
onClick={width === "overlay" && playing ? close : undefined}
>
<div
className="video-overlay-background"
ref={backgroundRef}
tabIndex={-1}
onClick={close}
onBlur={() => {
if (playing) {
requestAnimationFrame(() => backgroundRef.current?.focus());
}
}}
onKeyDown={(e) => {
if (e.key === "Escape") {
e.preventDefault();
e.stopPropagation();
close();
}
}}
/>
<button
className="video-overlay-close"
onClick={close}
aria-label="Close video"
title="Close video"
>
×
</button>
<video
ref={videoRef}
controls playsInline
onClick={e => e.stopPropagation()}
onPlay={() => setPlaying(true)}
onPause={() => setPlaying(false)}
>
<source src={src} type="video/mp4"/>
</video>
Expand Down
67 changes: 52 additions & 15 deletions styles/globals.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ $gap: var(--gap);

.video-overlay {
position: relative;
isolation: isolate;

&::before {
content: "";
position: fixed;
inset: 0;
z-index: -1;
background: rgb(0 0 0 / 80%);
opacity: 0;
pointer-events: none;
transition: opacity 300ms ease;
}

&.visible {
position: fixed;
Expand All @@ -86,17 +74,31 @@ $gap: var(--gap);
align-items: center;
justify-content: center;
padding: 2rem;
isolation: isolate;
}

&::before {
opacity: 1;
.video-overlay-background {
position: absolute;
inset: 0;
z-index: 0;
background: rgb(0 0 0 / 80%);
opacity: 0;
transition: opacity 300ms ease;

&:focus {
outline: none;
}
}

&.visible .video-overlay-background {
opacity: 1;
}

video {
position: relative;
z-index: 1;
width: 100%;
max-height: 90vh;
z-index: 1001;
object-fit: contain;
transition: width 300ms ease, transform 300ms ease;
}
Expand All @@ -108,6 +110,41 @@ $gap: var(--gap);
}
}

.video-overlay-close {
display: none;

.visible & {
display: block;
position: fixed;
top: 1rem;
right: 1.5rem;
z-index: 2;

width: 2.5rem;
height: 2.5rem;
padding: 0;
border: 0;
border-radius: 50%;

background: rgb(0 0 0 / 50%);
color: white;
font-size: 2rem;
line-height: 1;
cursor: pointer;

&:hover {
background: rgb(0 0 0 / 80%);
}
}
}

@media (prefers-reduced-motion: reduce) {
.video-overlay .video-overlay-background,
.video-overlay video {
transition: none;
}
}

@media (prefers-reduced-motion: reduce) {
.video-overlay,
.video-overlay video {
Expand Down
Loading