# Add Dark Mode to a React App

Yes. An agent wires the dark palette your CSS already defines to a theme store, a toggle in the layout and the system preference, with tests, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-dark-mode/react/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the design system already has before adding anything. In an app like alan2207/bulletproof-react under `apps/react-vite`, `src/index.css` defines the shadcn-style palette twice: HSL variables for `--background`, `--foreground`, `--primary`, `--muted`, `--border` and the rest on `:root`, and a second set under a `.dark` selector that nothing applies. `tailwind.config.cjs` maps those variables to `background`, `foreground`, `primary` and the other colour names through `hsl(var(--…))`, but has no `darkMode` setting, so `dark:` variants do not compile. The `Button` in `src/components/ui/button/button.tsx` and the layouts already use `bg-background`, `text-primary-foreground` and `border-input`, so most of the app would follow a class on `<html>` the moment it exists.

The agent sets `darkMode: 'class'` in `tailwind.config.cjs`, then adds a theme store. The app already depends on `zustand`, and `src/testing/setup-tests.ts` mocks it with `vi.mock('zustand')` so stores reset between tests, so the store goes in `src/hooks/use-theme.ts` next to `use-disclosure.ts`: a `theme` of `light`, `dark` or `system`, persisted under one localStorage key, with an effect that resolves `system` through `matchMedia('(prefers-color-scheme: dark)')`, toggles the `dark` class on `document.documentElement` and follows the media query when it changes. `src/main.tsx` applies the stored value before `createRoot` renders so the first paint is already the right colour, and `index.html` gets a `<meta name="color-scheme">` beside the existing `theme-color`.

The toggle goes where a user looks for it. `src/components/layouts/dashboard-layout.tsx` has a user `DropdownMenu` with Your Profile and Sign Out items; the agent adds a Theme group using the `DropdownMenuCheckboxItem` that `src/components/ui/dropdown/dropdown.tsx` already exports, one item each for Light, Dark and System, with `lucide-react` icons as the layout's other items use. Hard-coded greys that ignore the palette get replaced with the semantic names: the `bg-white`, `text-gray-900` and `border-gray-200` in `src/app/routes/app/profile.tsx` and the `text-gray-900` heading in `content-layout.tsx` become `bg-card`, `text-foreground` and `border-border`, while the black sidebar, which is dark in both modes, stays as it is. File and folder names stay kebab-case because `.eslintrc.cjs` enforces that with `eslint-plugin-check-file`.

Tests follow the existing `__tests__` folders: `src/hooks/__tests__/use-theme.test.ts` checks that choosing Dark adds the class and writes localStorage and that System follows a stubbed `matchMedia`, and a test beside `dashboard-layout.tsx` renders the layout with `rtlRender` from `src/testing/test-utils.tsx`, opens the menu and switches the theme. The agent runs `yarn lint`, `yarn check-types`, `yarn test` and `yarn build`, the four steps in `.github/workflows/react-vite-ci.yml`, then `yarn dev` with the mock server and takes a screenshot of the dashboard in each mode for the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add dark mode to the React app under apps/react-vite and open a pull request. Read apps/react-vite/src/index.css, tailwind.config.cjs, index.html, src/main.tsx, src/hooks/use-disclosure.ts, src/testing/setup-tests.ts, src/components/ui/dropdown/dropdown.tsx, src/components/layouts/dashboard-layout.tsx, src/components/layouts/content-layout.tsx, src/app/routes/app/profile.tsx and .eslintrc.cjs first. Set darkMode to class in tailwind.config.cjs. Add src/hooks/use-theme.ts as a zustand store with theme light, dark or system persisted in localStorage, resolving system through matchMedia and toggling the dark class on the html element, and apply the stored theme in src/main.tsx before rendering. Add a color-scheme meta tag to index.html. In dashboard-layout.tsx add Light, Dark and System items to the user dropdown with DropdownMenuCheckboxItem and lucide icons. Replace bg-white, text-gray-900 and border-gray-200 in profile.tsx and content-layout.tsx with the semantic palette names from tailwind.config.cjs; leave the black sidebar as it is. Keep every new file and folder kebab-case. Add Vitest tests for the hook and for switching the theme from the dropdown using rtlRender from src/testing/test-utils.tsx. Run yarn lint, yarn check-types, yarn test and yarn build, then run the app with the mock server and take a screenshot of the dashboard in light and in dark mode. Paste the command output and attach the screenshots in the pull request. Do not change apps/nextjs-app or apps/nextjs-pages.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request under `apps/react-vite` with one line in `tailwind.config.cjs`, a new `src/hooks/use-theme.ts` and its test, a theme section in the dropdown in `dashboard-layout.tsx` with a test, a few lines in `src/main.tsx` and `index.html`, and class swaps in `profile.tsx` and `content-layout.tsx`. The description pastes the output of lint, type check, tests and build, and two screenshots of the dashboard, one per mode, taken against the mock server.

The `react-vite-ci.yml` workflow runs the same four commands on the pull request, so the new tests run beside the existing ones, and the Playwright job in that workflow still passes because no route or selector changed.

A reviewer checks that the first paint uses the stored theme rather than flashing light, that System follows the operating system and updates when it changes, that no component still hard-codes a grey that disappears on a dark background, and that the `nextjs-app` and `nextjs-pages` workspaces are untouched.

## Run it on every design request in the backlog

A team that keeps several React front ends on the same component library starts one run per repository with `POST /o/{org}/sessions`, this prompt and the repository in `resources`. The agent reads each app's own `index.css` and Tailwind config, so an app whose palette has no `.dark` block gets one generated from its light values, and each run opens its own pull request with its own screenshots.

The key's `webhook_url` receives `run.completed` for each run it starts, with `data.session_url` for the pull request link.

## Why run it on Rush?

Dark mode is a sweep across every component for hard-coded colours, and the misses show up only on screen. In a Rush sandbox the agent runs the app against the mock server, switches the theme in a real browser and takes screenshots, and those screenshots sit in the pull request beside the test output, so a reviewer sees a dark dashboard rather than a diff of class names.

The same request works across every React app a team maintains, each in its own sandbox with lint, type check, tests and build run before anyone opens the diff.

## Frequently asked questions

### Why a zustand store and not React Context?

The app already depends on `zustand`, `src/testing/setup-tests.ts` mocks it so stores reset between tests, and the theme is read in `main.tsx` before any component renders. A store with a persisted key fits those three facts; a context would need a provider above `AppProvider` and still need storage of its own.

### Will the page flash light before going dark?

No. `src/main.tsx` applies the stored theme to the `html` element before `createRoot` renders, and the `color-scheme` meta tag tells the browser which default form controls to draw. The test for the hook covers the initial read from localStorage.

### Does it change the Next.js apps in the repository?

No. The prompt limits the run to `apps/react-vite`, and the pull request touches nothing under `apps/nextjs-app` or `apps/nextjs-pages`. A second run with the same prompt and that path can do the same there.

### What about the black sidebar?

It is black in both modes by design, so the agent leaves its `bg-black` and `text-gray-300` classes alone. Only surfaces that are white in light mode, like the profile card and page headings, move to the semantic `card` and `foreground` names so they follow the palette.

## More React jobs

- [Add Error Boundaries to a React App](https://getrush.ai/run/add-error-boundaries/react/)
- [Add i18n to a React App from a Linear Issue](https://getrush.ai/run/add-i18n/react/)
- [Add Optimistic Updates to a React App](https://getrush.ai/run/add-optimistic-ui/react/)

## The same job on other stacks

- [Add a Dark Mode Toggle to a React Router App](https://getrush.ai/run/add-dark-mode/react-router/)
- [Add Dark Mode to a Reflex App](https://getrush.ai/run/add-dark-mode/reflex/)
- [Add Dark Mode to a SolidJS App](https://getrush.ai/run/add-dark-mode/solidjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
