Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(react-media): add two new stories for videos #175

Merged
merged 1 commit into from
Sep 15, 2023
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
5 changes: 5 additions & 0 deletions apps/storybook/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ declare module "*.mp3" {
export default value;
}

declare module "*.mp4" {
const value: string;
export default value;
}

declare module "*.jpg" {
const value: string;
export default value;
Expand Down
48 changes: 45 additions & 3 deletions apps/storybook/stories/react-media/MediaProvider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
timecode,
} from "@codedazur/react-components";
import { Meta, StoryObj } from "@storybook/react";
import { FunctionComponent, useMemo } from "react";
import { FunctionComponent, useMemo, useRef } from "react";
import { Bar } from "@apps/storybook/components/Bar";
import { List } from "@apps/storybook/components/List";
import { WithCenter } from "@apps/storybook/decorators/WithCenter";
Expand All @@ -46,6 +46,9 @@ import seaOfStars from "./artworks/sea-of-stars.jpg";
import alienated from "./tracks/alienated.mp3";
import meteorites from "./tracks/meteorites.mp3";
import tabulaRasa from "./tracks/tabula-rasa.mp3";
import bigBuckBunny from "./videos/big-buck-bunny.mp4";
import ships from "./videos/ships.mp4";
import guitar from "./videos/guitar.mp4";
import { DebugOverlay } from "../../components/DebugOverlay";

const meta: Meta = {
Expand Down Expand Up @@ -112,6 +115,40 @@ export const WithVolumeControls = () => (
</MediaProvider>
);

export const Video = () => {
const videoRef = useRef<HTMLVideoElement>(null);
return (
<MediaProvider tracks={[bigBuckBunny]} element={videoRef}>
<Stack justify={"center"} align={"center"}>
<video
style={{ height: "15rem", width: "25rem" }}
controls
ref={videoRef}
/>
</Stack>

<MediaDebugOverlay tracks cursor isPlaying volume time duration />
</MediaProvider>
);
};
export const MultipleVideos = () => {
const videoRef = useRef<HTMLVideoElement>(null);
return (
<MediaProvider tracks={[bigBuckBunny, ships, guitar]} element={videoRef}>
<Stack justify={"center"} align={"center"}>
<video
style={{ height: "15rem", width: "25rem" }}
controls
ref={videoRef}
/>
</Stack>
<TrackList />
<PlaylistControls />
<MediaDebugOverlay tracks cursor isPlaying volume time duration />
</MediaProvider>
);
};

const VolumeControls = () => (
<Bar>
<VolumeUpIcon />
Expand Down Expand Up @@ -472,6 +509,11 @@ const attributions = {
"https://soundcloud.com/purrplecat/tabula-rasa",
"Licensed under CC BY-SA 3.0",
],
[ships]: ["Stock video by Videezy", "http://videezy.com/"],
[guitar]: [
"Video by Nino Souza",
"https://www.pexels.com/video/rock-audio-pedal-banda-4142301/",
],
};

const TrackAttributionOverlay = () => {
Expand All @@ -488,7 +530,7 @@ const TrackAttributionOverlay = () => {
<AbsorbPointer>
<Opacity opacity={0.5}>
<Column style={{ fontSize: "smaller", lineHeight: "1.5em" }}>
{attributions[source].map((line) => (
{attributions[source]?.map((line) => (
<Text key={line}>{line}</Text>
))}
</Column>
Expand Down Expand Up @@ -527,7 +569,7 @@ const MediaDebugOverlay: FunctionComponent<MediaDebugOverlayProps> = ({
.map(([key]) => key) as Array<
keyof ReturnType<typeof useMediaProgress>
>,
[keyObject]
[keyObject],
);

return <DebugOverlay value={pick(media, keys)} />;
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 1 addition & 4 deletions packages/react-media/hooks/useMedia.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useContext } from "react";
import {
mediaContext,
MediaContext,
} from "../providers/MediaContext";
import { mediaContext, MediaContext } from "../providers/MediaContext";
import { MediaTrack } from "../providers/MediaProvider";

export const useMedia = <T extends MediaTrack>(): MediaContext<T> =>
Expand Down
4 changes: 2 additions & 2 deletions packages/react-media/providers/MediaProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { MediaContext, mediaContext } from "./MediaContext";

export type MediaTrack = string | { source: string };


interface MediaProviderProps {
element?: MaybeRef<HTMLMediaElement>;
tracks?: MediaTrack[];
Expand Down Expand Up @@ -51,11 +50,12 @@ export function MediaProvider({
);
const trackRef = useSynchronizedRef(track);

const elementRef: MaybeRef<HTMLMediaElement> = useRef(
const internalElementRef: MaybeRef<HTMLMediaElement> = useRef(
typeof window !== "undefined"
? window.document.createElement("video")
: null,
);
const elementRef = initialElement ?? internalElementRef;

const [isPlaying, setIsPlaying] = useState<boolean>(false);
const isPlayingRef = useSynchronizedRef(isPlaying);
Expand Down