
이번 글을 통해 배워갈 내용
- Tailwind CSS 사용 시 Next JS 다크모드 Next JS 라이트 모드 적용 꿀 방법
1.
next-themes 라이브러리 추가 (35kb 정도 함)
yarn add next-themes
https://www.npmjs.com/package/next-themes
next-themes
An abstraction for themes in your Next.js app.. Latest version: 0.2.1, last published: 7 months ago. Start using next-themes in your project by running `npm i next-themes`. There are 141 other projects in the npm registry using next-themes.
www.npmjs.com
2.
tailwind.config.js 에 darkMode:class, 추가
const { fontFamily } = require('tailwindcss/defaultTheme');
module.exports = {
...
darkMode: 'class',
...
};
3.
_app.ts 에 ThemeProvider 추가
import { ThemeProvider } from 'next-themes';
export default function App({ Component, pageProps }: AppProps) {
return (
<ThemeProvider attribute="class">
<Component {...pageProps} />
</ThemeProvider>
);
4.
코드에 적용
export default function Home() {
const { systemTheme, theme, setTheme } = useTheme();
const currentTheme = theme === 'system' ? systemTheme : theme;
const toggleDarkMode = () => {
if (theme == 'dark') {
setTheme('light');
} else {
setTheme('dark');
}
};
return (
<main className="dark:bg-darkBlue dark:text-white">
<div onClick={toggleDarkMode}>
<div className={`${theme == 'dark' ? 'hidden' : ''} w-5 h-5`} >
라이트
</div>
<div className={`${theme == 'dark' ? '' : 'hidden'} w-5 h-5`}>
다크
</div>
</div>
</main>
)
}
참조 및 인용
https://tailwindcss.com/docs/dark-mode
Dark Mode - Tailwind CSS
Using Tailwind CSS to style your site in dark mode.
tailwindcss.com
블로그 추천 포스트
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'Javascript > NextJs' 카테고리의 다른 글
NextJs 14.1.0에서 TailwindCss NotoSans Font 설정하기 (0) | 2024.01.20 |
---|---|
Nextjs Tailwind 에서 aos tos 대신 scroll animation 직접 구현해보기 (0) | 2024.01.15 |
Nextjs Tailwind CSS에 사용자 지정 글꼴 적용하기 23년 최신 방법 (0) | 2023.04.08 |
Next Js에서 Do not use <img> 오류 해결 하는 2가지 방법 (0) | 2021.08.07 |

이번 글을 통해 배워갈 내용
- Tailwind CSS 사용 시 Next JS 다크모드 Next JS 라이트 모드 적용 꿀 방법
1.
next-themes 라이브러리 추가 (35kb 정도 함)
yarn add next-themes
https://www.npmjs.com/package/next-themes
next-themes
An abstraction for themes in your Next.js app.. Latest version: 0.2.1, last published: 7 months ago. Start using next-themes in your project by running `npm i next-themes`. There are 141 other projects in the npm registry using next-themes.
www.npmjs.com
2.
tailwind.config.js 에 darkMode:class, 추가
const { fontFamily } = require('tailwindcss/defaultTheme');
module.exports = {
...
darkMode: 'class',
...
};
3.
_app.ts 에 ThemeProvider 추가
import { ThemeProvider } from 'next-themes';
export default function App({ Component, pageProps }: AppProps) {
return (
<ThemeProvider attribute="class">
<Component {...pageProps} />
</ThemeProvider>
);
4.
코드에 적용
export default function Home() {
const { systemTheme, theme, setTheme } = useTheme();
const currentTheme = theme === 'system' ? systemTheme : theme;
const toggleDarkMode = () => {
if (theme == 'dark') {
setTheme('light');
} else {
setTheme('dark');
}
};
return (
<main className="dark:bg-darkBlue dark:text-white">
<div onClick={toggleDarkMode}>
<div className={`${theme == 'dark' ? 'hidden' : ''} w-5 h-5`} >
라이트
</div>
<div className={`${theme == 'dark' ? '' : 'hidden'} w-5 h-5`}>
다크
</div>
</div>
</main>
)
}
참조 및 인용
https://tailwindcss.com/docs/dark-mode
Dark Mode - Tailwind CSS
Using Tailwind CSS to style your site in dark mode.
tailwindcss.com
블로그 추천 포스트
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'Javascript > NextJs' 카테고리의 다른 글
NextJs 14.1.0에서 TailwindCss NotoSans Font 설정하기 (0) | 2024.01.20 |
---|---|
Nextjs Tailwind 에서 aos tos 대신 scroll animation 직접 구현해보기 (0) | 2024.01.15 |
Nextjs Tailwind CSS에 사용자 지정 글꼴 적용하기 23년 최신 방법 (0) | 2023.04.08 |
Next Js에서 Do not use <img> 오류 해결 하는 2가지 방법 (0) | 2021.08.07 |