Skip to content

Commit 9b99037

Browse files
committed
Properly handle theme with next-themes
1 parent 78c75e1 commit 9b99037

7 files changed

Lines changed: 72 additions & 57 deletions

File tree

demo/components/content/code.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useAppContext } from 'components/context';
2-
import { CodeBlock, anOldHope, hybrid } from 'react-code-blocks';
1+
import { CopyBlock, anOldHope } from 'react-code-blocks';
32

43
// https://github.com/rajinwonderland/react-code-blocks
54

@@ -16,16 +15,14 @@ export const Code = ({
1615
showLineNumbers = true,
1716
wrapLongLines = true,
1817
}: Props) => {
19-
const [state] = useAppContext();
2018
return (
2119
<div className="text-sm font-fira">
22-
<CodeBlock
20+
<CopyBlock
2321
text={text}
2422
language={language}
2523
showLineNumbers={showLineNumbers}
2624
wrapLongLines={wrapLongLines}
27-
theme={state.theme === 'light' ? hybrid : anOldHope}
28-
//theme={tomorrowNight}
25+
theme={anOldHope}
2926
/>
3027
</div>
3128
);

demo/components/context.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import { createContext, useContext, useReducer } from 'react';
22

33
type State = {
44
drawerOpen: boolean;
5-
theme: 'light' | 'dark';
65
};
76

87
type Action =
98
| { type: 'setDrawerOpen'; open: boolean }
10-
| { type: 'toggleDrawer' }
11-
| { type: 'toggleTheme' };
9+
| { type: 'toggleDrawer' };
1210

1311
type Reducer = React.Reducer<State, Action>;
1412
type Dispatch = React.Dispatch<Action>;
@@ -22,9 +20,6 @@ const reducer = (state: State, action: Action): State => {
2220
case 'toggleDrawer': {
2321
return { ...state, drawerOpen: !state.drawerOpen };
2422
}
25-
case 'toggleTheme': {
26-
return { ...state, theme: state.theme === 'light' ? 'dark' : 'light' };
27-
}
2823
default: {
2924
return state;
3025
}
@@ -33,7 +28,6 @@ const reducer = (state: State, action: Action): State => {
3328

3429
const initialState: State = {
3530
drawerOpen: false,
36-
theme: 'dark',
3731
};
3832

3933
export const AppContext = createContext<Value>(undefined!);

demo/components/header/theme-button.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
import { useAppContext } from 'components/context';
2-
import { useCallback } from 'react';
1+
import { useCallback, useEffect, useState } from 'react';
2+
import { useTheme } from 'next-themes';
33
import { Button } from 'react-daisyui';
44
import { ButtonProps } from 'react-daisyui/dist/Button';
55

66
type Props = ButtonProps;
77

88
export const ThemeButton = (props: Props) => {
9-
const [state, dispatch] = useAppContext();
9+
const [mounted, setMounted] = useState(false);
10+
const { theme, setTheme } = useTheme();
1011

1112
const handleClick = useCallback(
12-
() => dispatch({ type: 'toggleTheme' }),
13-
[dispatch]
13+
() => setTheme(theme === 'dark' ? 'light' : 'dark'),
14+
[theme, setTheme]
1415
);
1516

17+
useEffect(() => setMounted(true), []);
18+
19+
if (!mounted || !theme) return null;
20+
1621
return (
1722
<Button {...props} shape="square" color="ghost" onClick={handleClick}>
18-
{state.theme === 'light' ? <SunnyIcon /> : <MoonIcon />}
23+
{theme === 'dark' ? <MoonIcon /> : <SunnyIcon />}
1924
</Button>
2025
);
2126
};

demo/components/layout.tsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useCallback } from 'react';
2-
import { Drawer, Menu, Theme } from 'react-daisyui';
3-
import { MenuItem, TitleButton } from './header';
42
import { useAppContext } from './context';
3+
import { Drawer, Menu } from 'react-daisyui';
4+
import { MenuItem, TitleButton } from './header';
55

66
const menuItems = [
77
{ name: 'Editor', url: '/editor' },
@@ -29,28 +29,26 @@ export const Layout = ({ children }: Props) => {
2929
);
3030

3131
return (
32-
<Theme dataTheme={state.theme}>
33-
<Drawer
34-
mobile
35-
open={state.drawerOpen}
36-
onClickOverlay={toggleDrawer}
37-
side={
38-
<aside className="flex flex-1 flex-col w-80 bg-base-100">
39-
<div className="navbar">
40-
<TitleButton />
41-
</div>
42-
<Menu className="flex-1 p-2 overflow-y-auto w-80">
43-
{menuItems.map((item) => (
44-
<MenuItem key={item.url} href={item.url} onClick={toggleDrawer}>
45-
{item.name}
46-
</MenuItem>
47-
))}
48-
</Menu>
49-
</aside>
50-
}
51-
>
52-
{children}
53-
</Drawer>
54-
</Theme>
32+
<Drawer
33+
mobile
34+
open={state.drawerOpen}
35+
onClickOverlay={toggleDrawer}
36+
side={
37+
<aside className="flex flex-1 flex-col w-80 bg-base-100">
38+
<div className="navbar">
39+
<TitleButton />
40+
</div>
41+
<Menu className="flex-1 p-2 overflow-y-auto w-80">
42+
{menuItems.map((item) => (
43+
<MenuItem key={item.url} href={item.url} onClick={toggleDrawer}>
44+
{item.name}
45+
</MenuItem>
46+
))}
47+
</Menu>
48+
</aside>
49+
}
50+
>
51+
{children}
52+
</Drawer>
5553
);
5654
};

demo/package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"daisyui": "^2.22.0",
1717
"immutability-helper": "^3.1.1",
1818
"next": "^12.2.4",
19+
"next-themes": "^0.2.0",
1920
"paper": "^0.12.15",
2021
"react": "^18.2.0",
2122
"react-code-blocks": "^0.0.9-0",

demo/pages/_app.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import '../styles/globals.css';
2-
import type { AppProps } from 'next/app';
32
import Head from 'next/head';
3+
import type { AppProps } from 'next/app';
4+
import { ThemeProvider } from 'next-themes';
45
import { Layout } from 'components/layout';
56
import { AppProvider } from 'components/context';
67

78
function MyApp({ Component, pageProps }: AppProps) {
89
return (
9-
<AppProvider>
10-
<Head>
11-
<title>react-paper-bindings</title>
12-
<meta
13-
name="description"
14-
content="Examples for react-paper-bindings library."
15-
/>
16-
</Head>
17-
<Layout>
18-
<Component {...pageProps} />
19-
</Layout>
20-
</AppProvider>
10+
<ThemeProvider>
11+
<AppProvider>
12+
<Head>
13+
<title>react-paper-bindings</title>
14+
<meta
15+
name="description"
16+
content="Examples for react-paper-bindings library."
17+
/>
18+
</Head>
19+
<Layout>
20+
<Component {...pageProps} />
21+
</Layout>
22+
</AppProvider>
23+
</ThemeProvider>
2124
);
2225
}
2326

0 commit comments

Comments
 (0)