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

feat: add player only export #261

Merged
merged 4 commits into from
Jun 3, 2024
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
21 changes: 21 additions & 0 deletions examples/default-provider/app/(default)/player-only/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Metadata } from 'next';
import Player from 'next-video/player';

export const metadata: Metadata = {
title: 'next-video - Player only',
};

export default function Page() {
return (
<>
<section>
<Player
style={{ display: 'grid', width: '100%', aspectRatio: '16/9' }}
src="https://storage.googleapis.com/muxdemofiles/mux.mp4"
poster="https://image.mux.com/jxEf6XiJs6JY017pSzpv8Hd6tTbdAOecHTq4FiFAn564/thumbnail.webp"
blurDataURL='data:image/webp;base64,UklGRlAAAABXRUJQVlA4IEQAAACwAQCdASoQAAkAAQAcJZwAAueBHFYwAP7+sPJ01xp5AM+XuhDsRQ67ZYXXhHDkrqsIkUGjQSCMuENc5y3Qg0o9pZgAAA=='
/>
</section>
</>
);
}
3 changes: 3 additions & 0 deletions examples/default-provider/app/sidebar-nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default function SidebarNav() {
<li>
<Link className={`link ${pathname === '/string-source' ? 'active' : ''}`} href="/string-source">String video source</Link>
</li>
<li>
<Link className={`link ${pathname === '/player-only' ? 'active' : ''}`} href="/player-only">Player only</Link>
</li>
</ul>
</nav>
}
11 changes: 7 additions & 4 deletions examples/default-provider/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { withNextVideo } from 'next-video/process';

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = (phase, { defaultConfig }) => {
return {
...defaultConfig,
};
};

export default async function config() {
return withNextVideo(nextConfig);
}
export default withNextVideo(nextConfig);

// Amazon S3 example
// export default withNextVideo(nextConfig, {
Expand Down
20 changes: 10 additions & 10 deletions examples/default-provider/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
"main": "./dist/components/video.js",
"exports": {
".": "./dist/components/video.js",
"./player": "./dist/components/players/default-player.js",
"./process": {
"import": "./dist/process.js",
"require": "./dist/cjs/process.js",
"default": "./dist/process.js"
},
"./background-video": "./dist/components/background-video.js",
"./background-player": "./dist/components/players/background-player.js",
"./request-handler": "./dist/request-handler.js",
"./video-types/*": "./video-types/*.d.ts",
"./dist/cjs/*": "./dist/cjs/*",
Expand Down
5 changes: 1 addition & 4 deletions src/components/background-video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import { forwardRef } from 'react';

import { BackgroundPlayer } from './players/background-player.js';
import BackgroundPlayer from './players/background-player.js';
import Video from './video.js';

import type { VideoProps } from './types.js';

const BackgroundVideo = forwardRef((props: VideoProps, forwardedRef) => {
const { className } = props;

return <Video
ref={forwardedRef}
as={BackgroundPlayer}
thumbnailTime={0}
className={`${className ? `${className} ` : ''}next-video-bg`}
{...props}
/>;
});
Expand Down
88 changes: 51 additions & 37 deletions src/components/players/background-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ import MuxVideo from '@mux/mux-video-react';
import { getPlaybackId, getPosterURLFromPlaybackId } from '../../providers/mux/transformer.js';
import { svgBlurImage } from '../utils.js';

export type BackgroundPlayerProps = Omit<DefaultPlayerProps, 'src'>;

export const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, forwardedRef: any) => {
let { style, children, asset, controls, poster, blurDataURL, onPlaying, onLoadStart, ...rest } =
allProps;
export type BackgroundPlayerProps = DefaultPlayerProps;

const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, forwardedRef: any) => {
let {
style,
className,
children,
asset,
controls,
poster,
blurDataURL,
onPlaying,
onLoadStart,
...rest
} = allProps;

const slottedPoster = Children.toArray(children).find((child) => {
return typeof child === 'object' && 'type' in child && child.props.slot === 'poster';
Expand Down Expand Up @@ -57,6 +67,8 @@ export const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, for
const showCustomBlur = isCustomPoster && blurDataURL !== asset?.blurDataURL;

if (showGeneratedBlur || showCustomBlur) {
imgStyleProps.width = '100%';
imgStyleProps.height = '100%';
imgStyleProps.color = 'transparent';
imgStyleProps.backgroundSize = 'cover';
imgStyleProps.backgroundPosition = 'center';
Expand All @@ -75,21 +87,19 @@ export const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, for
<style>{
/* css */`
.next-video-bg {
aspect-ratio: initial;
width: 100%;
height: 100%;
display: grid;
}

.next-video-bg [data-next-video],
.next-video-bg-video,
.next-video-bg-poster,
.next-video-bg-text {
grid-area: 1 / 1;
min-height: 0;
}

.next-video-bg-poster {
width: 100%;
height: 100%;
position: relative;
object-fit: cover;
pointer-events: none;
Expand All @@ -100,7 +110,7 @@ export const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, for
opacity: 0;
}

.next-video-bg [data-next-video] {
.next-video-bg-video {
object-fit: cover;
}

Expand All @@ -112,34 +122,38 @@ export const BackgroundPlayer = forwardRef((allProps: BackgroundPlayerProps, for
}
`
}</style>
<MuxVideo
ref={forwardedRef}
style={{ ...style }}
onPlaying={(event) => {
onPlaying?.(event as any);
setPosterHidden(true);
}}
onLoadStart={(event) => {
onLoadStart?.(event as any);
setPosterHidden(false);
}}
muted={true}
autoPlay={true}
loop={true}
{...props}
/>
{poster && (
<img
className="next-video-bg-poster"
src={isCustomPoster ? poster : undefined}
srcSet={srcSet}
style={imgStyleProps}
hidden={posterHidden}
decoding="async"
aria-hidden="true"
<div className={`${className ? `${className} ` : ''}next-video-bg`} style={{ ...style }}>
<MuxVideo
ref={forwardedRef}
className="next-video-bg-video"
onPlaying={(event) => {
onPlaying?.(event as any);
setPosterHidden(true);
}}
onLoadStart={(event) => {
onLoadStart?.(event as any);
setPosterHidden(false);
}}
muted={true}
autoPlay={true}
loop={true}
{...props}
/>
)}
<div className="next-video-bg-text">{children}</div>
{poster && (
<img
className="next-video-bg-poster"
src={isCustomPoster ? poster : undefined}
srcSet={srcSet}
style={imgStyleProps}
hidden={posterHidden}
decoding="async"
aria-hidden="true"
/>
)}
<div className="next-video-bg-text">{children}</div>
</div>
</>
);
});

export default BackgroundPlayer;
8 changes: 7 additions & 1 deletion src/components/players/default-player.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client';

import { forwardRef, Suspense, lazy, Children, isValidElement } from 'react';
import { getPlaybackId, getPosterURLFromPlaybackId } from '../../providers/mux/transformer.js';
import { svgBlurImage } from '../utils.js';
Expand All @@ -11,7 +13,7 @@ export type DefaultPlayerRefAttributes = MuxPlayerRefAttributes;

export type DefaultPlayerProps = Omit<MuxPlayerProps, 'src'> & PlayerProps;

export const DefaultPlayer = forwardRef((allProps: DefaultPlayerProps, forwardedRef: any) => {
const DefaultPlayer = forwardRef((allProps: DefaultPlayerProps, forwardedRef: any) => {
let {
style,
children,
Expand Down Expand Up @@ -64,6 +66,8 @@ export const DefaultPlayer = forwardRef((allProps: DefaultPlayerProps, forwarded
const showCustomBlur = isCustomPoster && blurDataURL !== asset?.blurDataURL;

if (showGeneratedBlur || showCustomBlur) {
imgStyleProps.width = '100%';
imgStyleProps.height = '100%';
imgStyleProps.color = 'transparent';
imgStyleProps.backgroundSize = 'cover';
imgStyleProps.backgroundPosition = 'center';
Expand Down Expand Up @@ -104,3 +108,5 @@ export const DefaultPlayer = forwardRef((allProps: DefaultPlayerProps, forwarded
</Suspense>
);
});

export default DefaultPlayer;
5 changes: 1 addition & 4 deletions src/components/video.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { forwardRef, useState } from 'react';
import { DefaultPlayer } from './players/default-player.js';
import DefaultPlayer from './players/default-player.js';
import { Alert } from './alert.js';
import { createVideoRequest, defaultLoader } from './video-loader.js';
import * as transformers from '../providers/transformers.js';
Expand Down Expand Up @@ -85,9 +85,6 @@ const NextVideo = forwardRef((props: VideoProps, forwardedRef) => {
[data-next-video] img {
object-fit: var(--media-object-fit, contain);
object-position: var(--media-object-position, center);
background: center / var(--media-object-fit, contain) no-repeat transparent;
width: 100%;
height: 100%;
}
`
}</style>
Expand Down
9 changes: 6 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const videoConfigDefault: VideoConfigComplete = {

/**
* The video config is set in `next.config.js` and passed to the `withNextVideo` function.
* The video config is then stored as an environment variable __NEXT_VIDEO_OPTS.
* The video config is then stored in `serverRuntimeConfig`.
*/
export async function getVideoConfig(): Promise<VideoConfigComplete> {
let nextConfig: NextConfig | undefined = getConfig();
Expand All @@ -91,10 +91,13 @@ async function importConfig(file: string) {
const fileUrl = pathToFileURL(absFilePath).href;

const mod = await import(/* webpackIgnore: true */ fileUrl);
const config: (() => NextConfig) | NextConfig | undefined = mod?.default;
const config:
| ((phase: string | undefined, opts: any) => Promise<NextConfig>)
| NextConfig
| undefined = mod?.default;

if (typeof config === 'function') {
return config();
return config(process.env.NEXT_PHASE, { defaultConfig: {} });
}
return config;
}
26 changes: 15 additions & 11 deletions src/with-next-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ import { fileURLToPath } from 'node:url';
import { videoConfigDefault } from './config.js';
import type { VideoConfig } from './config.js';

export async function withNextVideo(nextConfig: any, videoConfig?: VideoConfig) {
if (typeof nextConfig === 'function') {
return async (...args: any[]) => {
const nextConfigResult = await Promise.resolve(nextConfig(...args));
return withNextVideo(nextConfigResult, videoConfig);
};
}

export function withNextVideo(nextConfig: any, videoConfig?: VideoConfig) {
const videoConfigComplete = Object.assign({}, videoConfigDefault, videoConfig);
const { path, folder, provider } = videoConfigComplete;

// env VARS have to be set before the async function return!!

// Don't use `process.env` here because Next.js replaces public env vars during build.
env['NEXT_PUBLIC_VIDEO_OPTS'] = JSON.stringify({ path, provider });
env['__NEXT_VIDEO_OPTS'] = JSON.stringify(videoConfigComplete);
env['NEXT_PUBLIC_VIDEO_OPTS'] = JSON.stringify({ path, folder, provider });

// We should probably switch to using `phase` here, just a bit concerned about backwards compatibility.
if (process.argv[2] === 'dev') {
// Don't use `process.env` here because Next.js replaces public env vars during build.
env['NEXT_PUBLIC_DEV_VIDEO_OPTS'] = JSON.stringify({ path, folder, provider });
}

if (typeof nextConfig === 'function') {
return async (...args: any[]) => {
const nextConfigResult = await nextConfig(...args);
return withNextVideo(nextConfigResult, videoConfig);
};
}

// We should probably switch to using `phase` here, just a bit concerned about backwards compatibility.
if (process.argv[2] === 'dev') {
const VIDEOS_PATH = join(process.cwd(), folder);
const TMP_PUBLIC_VIDEOS_PATH = join(process.cwd(), 'public', `_next-video`);

await symlinkDir(VIDEOS_PATH, TMP_PUBLIC_VIDEOS_PATH);
symlinkDir(VIDEOS_PATH, TMP_PUBLIC_VIDEOS_PATH);

process.on('exit', async () => {
await fs.unlink(TMP_PUBLIC_VIDEOS_PATH);
Expand Down