Skip to content

Commit

Permalink
feat: video component
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartek532 committed Jul 25, 2024
1 parent 42983ab commit 1df540a
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/resource/content/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Image } from "./components/image/Image";
import { Pre } from "./components/pre/Pre";
import { Quote } from "./components/quote/Quote";
import { Sandbox } from "./components/sandbox/Sandbox";
import { Video } from "./components/video/Video";

interface ContentProps {
readonly content: MDXRemoteSerializeResult;
Expand All @@ -32,6 +33,7 @@ const customMdxComponents = {
Image,
Link,
Quote,
Video,
Highlight,
Sandbox: ({ id }: { id: string }) => <Sandbox id={id} />,
pre: Pre,
Expand Down
57 changes: 57 additions & 0 deletions components/resource/content/components/video/Video.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import clsx from "clsx";
import { memo, useRef, useState } from "react";

import PauseIcon from "public/svg/pause.svg";
import PlayIcon from "public/svg/play.svg";

import styles from "./video.module.scss";

type VideoProps = { readonly alt?: string } & JSX.IntrinsicElements["video"];

export const Video = memo<VideoProps>(({ alt, ...props }: VideoProps) => {
const [paused, setPaused] = useState(!props.autoPlay);
const [controlsVisible, setControlsVisible] = useState(false);
const ref = useRef<HTMLVideoElement>(null);

const togglePlay = () => {
if (ref.current) {
if (ref.current.paused) {
setControlsVisible(false);
setPaused(false);
void ref.current.play();
} else {
setControlsVisible(true);
setPaused(true);
ref.current.pause();
}
}
};

return (
<figure className={styles.wrapper}>
<div className={styles.videoWrapper}>
<video
className={clsx(styles.video, props.className)}
{...props}
ref={ref}
onClick={togglePlay}
onMouseLeave={() => setControlsVisible(false)}
onMouseEnter={() => setControlsVisible(true)}
/>
<button
className={clsx(
styles.control,
styles.visible && {
[styles.visible]: controlsVisible,
},
)}
>
{paused ? <PlayIcon className={styles.icon} /> : <PauseIcon className={styles.icon} />}
</button>
</div>
{alt ? <figcaption className={styles.caption}>{alt}</figcaption> : null}
</figure>
);
});

Video.displayName = "Video";
57 changes: 57 additions & 0 deletions components/resource/content/components/video/video.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@use "styles/_mixins";

.wrapper {
width: auto;
height: auto;
margin: 4rem -1.2rem;
position: relative;
@include mixins.flex;
flex-flow: column wrap;
gap: 1.4rem;

@include mixins.mediaquery("sm") {
margin: 4rem -2rem;
}

.videoWrapper {
position: relative;

.video {
max-width: 100%;
border-radius: 2rem;
cursor: pointer;
}

.control {
position: absolute;
inset: 0;
width: 6.5rem;
height: 6.5rem;
border-radius: 50%;
border: 0 none;
transition: opacity 500ms ease;
background-color: rgba(0, 0, 0, 0.5);
margin: auto;
pointer-events: none;
opacity: 0;
color: var(--white-200);
@include mixins.flex;

&.visible {
opacity: 1;
}

.icon {
width: 50%;
}
}
}

.caption {
font-size: 1.4rem;
font-style: italic;
opacity: 0.6;
padding: 0 1rem;
text-align: center;
}
}
2 changes: 1 addition & 1 deletion public/svg/contribute.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/svg/fire.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/svg/pause.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/svg/play.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1df540a

Please sign in to comment.