เบื่อไหม? กับเว็บที่สว่างจ้าจนตาล้า 😵💫 ถ้าคุณอยากให้ผู้ใช้เลือกได้ว่าอยากใช้ โหมดสว่าง (Light Mode) หรือ โหมดมืด (Dark Mode) ใน Next.js เรามีวิธีง่ายๆ ให้คุณลองทำ!
ในบทความนี้ เราจะมาเจาะลึก การทำ Dark Mode ใน Next.js แบบครบทุกวิธี ทั้งใช้ CSS Variables, useState, localStorage และไลบรารีเสริม เช่น Next Themes กับ Tailwind CSS พร้อมตัวอย่างโค้ดที่เอาไปใช้ได้เลย! 🚀
🔥 ทำไมต้องมี Dark Mode?
✅ ข้อดีของ Dark Mode
- ช่วยลดอาการล้าสายตา 👀 (สำหรับคนที่ใช้เว็บตอนกลางคืน)
- ประหยัดแบตเตอรี่ 🔋 (โดยเฉพาะจอ OLED)
- ดูเท่! 😎 หลายเว็บใหญ่ๆ ก็ใช้ เช่น YouTube, Twitter
🛠 วิธีทำ Dark Mode ใน Next.js
🚀 1. ใช้ CSS Variables + useState
วิธีนี้เหมาะกับ:
✅ เว็บที่ไม่ซับซ้อนมาก
✅ ไม่ต้องใช้ไลบรารีเสริม
👨💻 ขั้นตอนการทำ
1️⃣ สร้าง globals.css และกำหนด Theme
แก้ไขไฟล์ styles/globals.css
:root {
--bg-color: #ffffff;
--text-color: #000000;
}
.dark-mode {
--bg-color: #121212;
--text-color: #ffffff;
}
body {
background-color: var(--bg-color);
color: var(--text-color);
transition: background 0.3s, color 0.3s;
}
2️⃣ เพิ่มปุ่มสลับโหมดใน index.js
เปิดไฟล์ pages/index.js แล้วเพิ่มโค้ดนี้
import { useState } from "react";
export default function Home() {
const [darkMode, setDarkMode] = useState(false);
const toggleDarkMode = () => {
setDarkMode(!darkMode);
document.body.classList.toggle("dark-mode");
};
return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>Dark Mode in Next.js 🌙</h1>
<button
onClick={toggleDarkMode}
style={{
padding: "10px 20px",
fontSize: "18px",
cursor: "pointer",
background: darkMode ? "#fff" : "#333",
color: darkMode ? "#000" : "#fff",
border: "none",
borderRadius: "5px",
}}
>
{darkMode ? "Switch to Light Mode ☀️" : "Switch to Dark Mode 🌙"}
</button>
</div>
);
}
🛠 2. ใช้ localStorage จำค่าโหมด
ปัญหา: ถ้ารีเฟรชหน้าเว็บ ค่าโหมดที่เลือกไว้จะหายไป
ทางแก้: ใช้ localStorage จำค่าการตั้งค่าของผู้ใช้
อัปเดตโค้ด index.js
import { useState, useEffect } from "react";
export default function Home() {
const [darkMode, setDarkMode] = useState(false);
useEffect(() => {
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
setDarkMode(true);
document.body.classList.add("dark-mode");
}
}, []);
const toggleDarkMode = () => {
setDarkMode(!darkMode);
if (!darkMode) {
document.body.classList.add("dark-mode");
localStorage.setItem("theme", "dark");
} else {
document.body.classList.remove("dark-mode");
localStorage.setItem("theme", "light");
}
};
return (
<div style={{ textAlign: "center", marginTop: "50px" }}>
<h1>Dark Mode in Next.js 🌙</h1>
<button
onClick={toggleDarkMode}
style={{
padding: "10px 20px",
fontSize: "18px",
cursor: "pointer",
background: darkMode ? "#fff" : "#333",
color: darkMode ? "#000" : "#fff",
border: "none",
borderRadius: "5px",
}}
>
{darkMode ? "Switch to Light Mode ☀️" : "Switch to Dark Mode 🌙"}
</button>
</div>
);
}
✅ ข้อดี:
- จำค่าธีมได้หลังจากรีเฟรช
🎨 3. ใช้ next-themes (แนะนำ!)
✅ ข้อดีของ next-themes
- ใช้ง่ายมาก (ไม่ต้องเขียนโค้ดเยอะ)
- รองรับ ระบบ Dark Mode อัตโนมัติจาก OS
🛠 ติดตั้ง next-themes
npm install next-themes
👨💻 ใช้งาน next-themes
1️⃣ แก้ไข _app.js
import { ThemeProvider } from "next-themes";
export default function MyApp({ Component, pageProps }) {
return (
<ThemeProvider attribute="class">
<Component {...pageProps} />
</ThemeProvider>
);
}
2️⃣ แก้ไข index.js
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
export default function Home() {
const { theme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return null; // ป้องกัน Hydration Error
return (
<div className="p-5 text-center">
<h1 className="text-2xl">Dark Mode with next-themes 🌙</h1>
<button
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
className="mt-3 px-4 py-2 text-white bg-black rounded"
>
{theme === "dark" ? "Switch to Light Mode ☀️" : "Switch to Dark Mode 🌙"}
</button>
</div>
);
}
🔥 ง่ายสุดๆ แค่เรียก setTheme() แล้วปล่อยให้ next-themes จัดการให้!
🏁 สรุป
| วิธี | ข้อดี | ข้อเสีย |
|---|---|---|
| CSS Variables + useState | ง่าย, ไม่ต้องใช้ไลบรารี | รีเฟรชแล้วค่าหาย |
| localStorage | จำค่าธีมได้หลังรีเฟรช | ต้องใช้ useEffect |
| next-themes (แนะนำ) | รองรับ Dark Mode อัตโนมัติ | ต้องติดตั้งไลบรารี |
🎯 ถ้าคุณอยากทำ Dark Mode แบบง่ายสุด 👉 ใช้ next-themes
🎯 ถ้าอยากควบคุมเองทั้งหมด 👉 ใช้ CSS Variables + localStorage
🚀 ลองทำเลย แล้วเว็บของคุณจะดูโปรขึ้น 300%!! 😆