Skip to content

Commit

Permalink
test server action
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Feb 19, 2024
1 parent 3a1e93e commit 85087cc
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/zero-nextjs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const theme = extendTheme({
},
},
},
getSelector: (colorScheme) => (colorScheme ? `.theme-${colorScheme}` : ':root'),
});

module.exports = withZeroPlugin({}, { theme });
7 changes: 7 additions & 0 deletions examples/zero-nextjs/src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use server';
import { cookies } from 'next/headers';

// eslint-disable-next-line import/prefer-default-export
export async function setTheme(theme: string) {
cookies().set('theme', theme);
}
4 changes: 3 additions & 1 deletion examples/zero-nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import type { Metadata } from 'next';
import { cookies } from 'next/headers';
import '@mui/zero-runtime/styles.css';
import { css } from '@mui/zero-runtime';

Expand All @@ -11,8 +12,9 @@ export const metadata: Metadata = {
};

export default function RootLayout(props: { children: React.ReactNode }) {
const mode = cookies().get('theme')?.value || 'light';
return (
<html lang="en">
<html lang="en" className={`theme-${mode}`}>
<body
className={css(({ theme }) => ({
color: 'hsl(var(--palette-foreground))',
Expand Down
4 changes: 4 additions & 0 deletions examples/zero-nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react';
import { cookies } from 'next/headers';
import { Kalnia, Josefin_Sans } from 'next/font/google';
import { styled, css, keyframes } from '@mui/zero-runtime';
import ThemeToggle from '@/components/ThemeToggle';

const kalnia = Kalnia({ subsets: ['latin'] });
const josefin = Josefin_Sans({ subsets: ['latin'] });
Expand Down Expand Up @@ -200,6 +202,8 @@ export default function Home() {
Roadmap
</Link>
</div>

<ThemeToggle value={cookies().get('theme')?.value || 'light'} />
</main>
);
}
24 changes: 24 additions & 0 deletions examples/zero-nextjs/src/components/ThemeToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';
import * as React from 'react';
import { css } from '@mui/zero-runtime';
import { setTheme } from '../app/actions';

export default function ThemeToggle({ value }: { value: string }) {
return (
<select
value={value}
className={css({
position: 'fixed',
top: '1rem',
right: '1rem',
zIndex: 1000,
})}
onChange={async (event) => {
await setTheme(event.target.value);
}}
>
<option value="light">light</option>
<option value="dark">dark</option>
</select>
);
}
Loading

0 comments on commit 85087cc

Please sign in to comment.