Skip to content

gjc14/remix-shadcn-darkmode

Repository files navigation

Remix Shadcn Darkmode

Welcome! This is a demo of how to control dark, light, and system themes in Remix SSR without flash. Taking advantage of client-hints and learnt a lot from 2024 Remix dark mode.

Flow

1. Get client hint

Using @epic-web/client-hints (I copied only color-schema part of this package) to get prefers-color-scheme before sending the first response. (Which will be stored in the cookie with request)

2. Add theme

Read client hint cookie from loader and directly add theme to <html> in /app/root.tsx/Layout component, if there are no custom-theme cookie set.

*Fallbakc theme is set in useCookieTheme()

3. Set theme and cookie

Set theme responsively by changing theme context (Using React Context, instruction: Shadcn Dark mode(vite)). Then set custom-theme cookie when user manually choose either dark or light, delete when system is chosen. Finally revalidate() to get new cookie set.


Welcome to Remix!

Development

Run the dev server:

npm run dev

Deployment

First, build your app for production:

npm run build

Then run the app in production mode:

npm start

Now you'll need to pick a host to deploy it to.

DIY

If you're familiar with deploying Node applications, the built-in Remix app server is production-ready.

Make sure to deploy the output of npm run build

  • build/server
  • build/client

Styling

This template comes with Tailwind CSS already configured for a simple default starting experience. You can use whatever css framework you prefer. See the Vite docs on css for more information.