Search for a command to run...
Zaidan shipped with dark mode support out of the box. All the tools are included in the registry, let's see how to enable it in your app.
To start using the dark mode feature, you first need to add the items to your project:
npx shadcn@latest add @zaidan/color-modepnpx shadcn add @zaidan/color-modeyarn dlx shadcn@latest add @zaidan/color-modebunx shadcn@latest add @zaidan/color-modeThen, you need to wrap your applications in the ColorModeProvider and set the initial mode (we will show you later how to detect it automatically):
1import { ColorModeProvider } from "@components/color-mode";2
3function RootComponent() {4 const colorMode = "light";5
6 return (7 <html lang="en" class={colorMode}>8 <head>{/* ... */}</head>9 <body>10 <ColorModeProvider initialColorMode={colorMode}>11 {/* Your app */}12 </ColorModeProvider>13 </body>14 </html>15 );16}Then use the useColorMode hook to access the current mode or toggle it from anywhere in your app:
1import { useColorMode } from "@components/color-mode";2
3function ThemeToggle() {4 const { colorMode, toggleColorMode } = useColorMode();5
6 return (7 <button onClick={toggleColorMode}>8 {colorMode() === "light" ? "Switch to Dark" : "Switch to Light"}9 </button>10 );11}That's it! You now have a working dark mode toggle in your app. You can define dark: modifiers in your Tailwind CSS classes to style elements based on the current mode.
Zaidan provides client-side utility to automatically detect the user's preferred color mode either from the cookie or from the system preference
On the server side (SSR), you'll need to come up with your own solution, since it's depends on your framework and setup. We will show you how we did it on Zaidan with Tanstack Start:
1import { createIsomorphicFn } from "@tanstack/solid-start";2import { getCookie } from "@tanstack/solid-start/server";3import { getClientColorMode, ZAIDAN_COLOR_MODE_COOKIE_KEY } from "@components/color-mode";4
5// Tanstack Start provides powerful utilities to create function that work on server & client6const getColorMode = createIsomorphicFn()7 .server(() => getCookie(ZAIDAN_COLOR_MODE_COOKIE_KEY) ?? "light") // Use your framework's cookie parsing utility to read the color mode from cookies8 .client(() => getClientColorMode());9
10function RootComponent() {11 const colorMode = getColorMode();12
13 return (14 <html lang="en" class={colorMode}>15 <head>{/* ... */}</head>16 <body>17 <ColorModeProvider initialColorMode={colorMode}>18 {/* Your app */}19 </ColorModeProvider>20 </body>21 </html>22 );23}