Skip to content

Commit

Permalink
example: fix RSC dup render bug, next-themes
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Feb 25, 2024
1 parent d4def6d commit 0620b98
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 66 deletions.
67 changes: 36 additions & 31 deletions examples/default-provider/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import type { Metadata } from "next";
import Link from "next/link";
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import * as fs from 'node:fs/promises';

import type { Metadata } from 'next';
import Link from 'next/link';
import { DM_Sans, JetBrains_Mono } from 'next/font/google';
import { Providers } from './providers';
import Nav from "./nav";
import "./globals.css";
import Nav from './nav';
import './globals.css';

const dmSans = DM_Sans({ subsets: ['latin'], variable: '--sans' });
const jetBrainsMono = JetBrains_Mono({ subsets: ['latin'], variable: '--mono' });

export const metadata: Metadata = {
title: "next-video",
description: "Next Video solves the hard problems with embedding, storing, streaming, and customizing video in your Next.js app.",
title: 'next-video',
description:
'Next Video solves the hard problems with embedding, storing, streaming, and customizing video in your Next.js app.',
};

export default function RootLayout({
const fileDir = dirname(fileURLToPath(import.meta.url));
const themeScript = await fs.readFile(`${fileDir}/theme-toggle.js`, 'utf-8');

export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
Expand All @@ -25,29 +32,27 @@ export default function RootLayout({
className={`${dmSans.variable} ${jetBrainsMono.variable}`}
>
<body>
<Providers>
<header>
<Link className="logo" href="/">
<code>next-video</code>
<span> playground</span>
</Link>
<Nav />
</header>
<aside>
<nav>
<ul>
<li>
<Link href="/">Basic example</Link>
</li>
<li>
<Link href="/slotted-poster">Slotted Poster</Link>
</li>
</ul>
</nav>
</aside>

{children}
</Providers>
<script dangerouslySetInnerHTML={{ __html: themeScript }} />
<header>
<Link className="logo" href="/">
<code>next-video</code>
<span> playground</span>
</Link>
<Nav />
</header>
<aside>
<nav>
<ul>
<li>
<Link href="/">Basic example</Link>
</li>
<li>
<Link href="/slotted-poster">Slotted Poster</Link>
</li>
</ul>
</nav>
</aside>
{children}
</body>
</html>
);
Expand Down
7 changes: 0 additions & 7 deletions examples/default-provider/app/nav.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
'use client';

import { useTheme } from 'next-themes';

export default function Nav() {
const { theme, setTheme } = useTheme()

return <nav>
<button
onClick={() => setTheme(theme === "light" ? "dark" : "light")}
className="theme-toggle"
id="theme-toggle"
title="Toggles light & dark"
Expand Down
4 changes: 2 additions & 2 deletions examples/default-provider/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Metadata } from 'next'
import { Metadata } from 'next';
import Video from 'next-video';
import getStarted from '/videos/get-started.mp4';

export const metadata: Metadata = {
title: 'next-video - Basic example',
}
};

export default function Page() {
return (
Expand Down
11 changes: 0 additions & 11 deletions examples/default-provider/app/providers.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion examples/default-provider/app/slotted-poster/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import getStartedPoster from '/images/get-started-poster.jpg';

export const metadata: Metadata = {
title: 'next-video - Slotted poster image',
}
};

export default function Page() {
return (
Expand Down
39 changes: 39 additions & 0 deletions examples/default-provider/app/theme-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const getColorPreference = () => {
if (globalThis.localStorage?.getItem('theme-preference')) {
return localStorage.getItem('theme-preference');
} else {
return globalThis.matchMedia?.('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
};

const setPreference = () => {
localStorage.setItem('theme-preference', `${theme.value}`);
reflectPreference();
};

const reflectPreference = () => {
globalThis.document?.firstElementChild?.setAttribute('data-theme', `${theme.value}`);
globalThis.document?.querySelector('#theme-toggle')?.setAttribute('aria-label', `${theme.value}`);
};

const theme = {
value: getColorPreference(),
};

reflectPreference();

globalThis.onload = () => {
reflectPreference();

document.querySelector('#theme-toggle')?.addEventListener('click', (e) => {
theme.value = theme.value === 'light' ? 'dark' : 'light';
setPreference();
});
};

globalThis
.matchMedia?.('(prefers-color-scheme: dark)')
.addEventListener('change', ({ matches: isDark }) => {
theme.value = isDark ? 'dark' : 'light';
setPreference();
});
4 changes: 2 additions & 2 deletions examples/default-provider/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withNextVideo } from "next-video/process";
import { withNextVideo } from 'next-video/process';
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default withNextVideo(nextConfig);
export default withNextVideo(nextConfig);
11 changes: 0 additions & 11 deletions examples/default-provider/package-lock.json

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

1 change: 0 additions & 1 deletion examples/default-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"next": "14.1.0",
"next-themes": "^0.2.1",
"next-video": "^0.15.0",
"open-props": "^1.6.19",
"react": "^18",
Expand Down

0 comments on commit 0620b98

Please sign in to comment.