Skip to content

Commit

Permalink
feat: dark mode in ant design and can use tailwind
Browse files Browse the repository at this point in the history
  • Loading branch information
Li-Ninja committed May 19, 2024
1 parent 8a14271 commit 92b83b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GoogleTagManager } from '@next/third-parties/google';
import classNames from 'classnames';
import type { Metadata } from 'next';
import { Noto_Sans_SC as noto } from 'next/font/google';
import AntdRegistry from '@/component/global/antdRegistry';
import Header from '@/component/global/header';
import 'tw-elements-react/dist/css/tw-elements-react.min.css';
import './globals.css';
Expand All @@ -23,8 +24,10 @@ export default function RootLayout({
return (
<html lang="en">
<body className={classNames('mx-2 mt-2', font.className)}>
<Header />
{children}
<AntdRegistry>
<Header />
{children}
</AntdRegistry>
</body>
{process.env.isProduction === 'true' && gtmId && <GoogleTagManager gtmId={gtmId} />}
</html>
Expand Down
41 changes: 41 additions & 0 deletions src/component/global/antdRegistry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';

import {
StyleProvider, createCache, extractStyle,
} from '@ant-design/cssinjs';
import type Entity from '@ant-design/cssinjs/es/Cache';
import {
ConfigProvider, theme,
} from 'antd';
import { useServerInsertedHTML } from 'next/navigation';
import React from 'react';

export default function AntdRegistry({ children }: React.PropsWithChildren) {
const isServerInserted = React.useRef<boolean>(false);
const cache = React.useMemo<Entity>(() => createCache(), []);
const { darkAlgorithm } = theme;

useServerInsertedHTML(() => {
if (isServerInserted.current) {
return;
}

isServerInserted.current = true;
return (
<style
id="antd"
dangerouslySetInnerHTML={{ __html: extractStyle(cache, true) }}
/>
);
});
return (
<StyleProvider cache={cache}>
<ConfigProvider
theme={{
algorithm: darkAlgorithm,
}}>
{children}
</ConfigProvider>
</StyleProvider>
);
}

0 comments on commit 92b83b3

Please sign in to comment.