All jobs

Add a Dark Mode Toggle to a React Router App

Yes. Teammates can watch an agent add a cookie-backed dark mode toggle to a React Router app so the server render matches the choice, and correct it before the PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how the template handles colour today before adding a switch. In a React Router 7 framework-mode app shaped like the default template in remix-run/react-router-templates, app/app.css imports Tailwind 4 through the @tailwindcss/vite plugin, declares an Inter font in a theme block, and applies bg-white with a dark:bg-gray-950 variant to html and body, setting color-scheme dark inside a prefers-color-scheme media query. app/welcome/welcome.tsx swaps logo-light.svg for logo-dark.svg with dark:hidden and dark:block classes. So the app already has a dark appearance, but only the one the operating system picks; there is no toggle and nothing to remember.

Tailwind 4 keys the dark variant on the media query by default, and a toggle needs a class instead. The agent adds a custom-variant line to app/app.css so dark applies when a dark class is present on an ancestor, then has to decide what happens with no stored choice. Dropping the media query would make every first visit light, so it keeps system preference as the default: when no cookie is set the server renders no class and a short inline script in app/root.tsx reads matchMedia before paint and adds the class, which avoids the flash a client-side effect would cause. Once the visitor picks, a theme cookie wins, and react-router.config.ts has ssr set to true, so the loader in root.tsx reads the cookie on the server and the Layout component puts the class on the html element in the first response.

The toggle itself is a resource route. The agent adds app/routes/set-theme.ts with an action that validates the posted value, light, dark or system, writes the cookie with SameSite lax and a long max-age, and returns a 204, and registers it in app/routes.ts beside the index route with the route helper from @react-router/dev/routes. A ThemeToggle component uses useFetcher to post to it without navigation, reads the current value from useRouteLoaderData on the root route, and sits in the Welcome header for the template; the pull request notes where it belongs in a real app. The typecheck script runs react-router typegen before tsc, so the new route gets its own +types module and the loader's return type flows into the component.

Proof follows the repository's own checks. The .tests directory at the repository root runs Playwright against each template: test.default.ts runs pnpm typecheck, starts pnpm dev and later pnpm build and pnpm start, loads the page, waits for the React Router Docs and Join Discord links and asserts the page logged no errors. The agent runs the same three commands inside the template, then requests the built server with and without a theme cookie and checks the class attribute on the html element in the raw HTML, which is the point of doing this on the server. It screenshots the welcome page in both themes from the sandbox browser and attaches the images to the session, so a teammate reading the run can see the result and ask for a contrast change before the pull request opens.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "codex",
  "input": "Add a dark mode toggle to this React Router app. Read package.json, react-router.config.ts, vite.config.ts, app/app.css, app/root.tsx, app/routes.ts, app/routes/home.tsx and app/welcome/welcome.tsx first. Switch Tailwind's dark variant from the media query to a class with a custom-variant in app/app.css, keep system preference as the default when no choice is stored, and remember an explicit choice in a theme cookie so the server render puts the class on the html element with no flash. Add a resource route app/routes/set-theme.ts registered in app/routes.ts whose action validates light, dark or system and sets the cookie, a ThemeToggle component using useFetcher that reads the current value from the root loader, and a short inline script that applies the system choice before paint only when no cookie is set. Run pnpm typecheck, pnpm build and pnpm start, request the server with and without the cookie and check the html class in the response, screenshot the page in both themes, and open a pull request that explains the cookie and the no-cookie path and quotes the typecheck and build output.",
  "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 on its own branch that edits app/app.css, app/root.tsx and app/routes.ts, adds app/routes/set-theme.ts and a ThemeToggle component, and places the toggle in app/welcome/welcome.tsx. package.json is unchanged: the cookie helpers come from react-router itself and no new dependency is needed. The description explains the three states, light, dark and system, how the cookie and the inline script divide the work, and quotes the typecheck and build output from the sandbox.

Session artifacts hold the two screenshots, each with a download URL that works for an hour without the key; reading the session again issues a fresh one. The transcript keeps the raw server responses with and without the cookie, so a reviewer can see the class attribute on the html element in the first byte of HTML rather than trusting the description.

Before merging, a reviewer runs pnpm dev on the branch, toggles, reloads, and confirms no flash and that the choice survives; checks the Playwright test in .tests still passes because the page logs no console errors; and decides where the toggle should live beyond the welcome page, which is a follow-up on the same session rather than a new run.

Run it on every theme request

Send this request when someone asks for a theme switch on the app, with the repository in resources. One sandbox holds the branch, the dev server and the browser, and everyone in the org can read the run as it works. The starter, or an org admin, sends the correction as a follow-up; the console queues one sent mid-turn, and taking the browser directly is a five-minute lease that one person holds at a time.

When the turn finishes, the key's webhook receives run.completed or run.failed with data.session_url and data.events_url, for the runs that key started. The parked sandbox keeps the branch, so a designer who answers hours later about the gray-950 background gets the change pushed to the same pull request without a new clone.

The use case this job follows, with its automation and trigger: Steer a run as a team.

Why run it on Rush?

The bug in a dark mode toggle is a flash of the wrong theme on the first server render, and you only see it with a real server, a cookie and a browser. On a laptop that means a long-lived dev server and a screen share for every correction. Here the agent runs the built server in its sandbox, fetches the raw HTML with and without the cookie, screenshots both themes, and stays parked between comments so a teammate can read the run and send a change without a call.

The merge is still a person's. The pull request shows the three states and the pictures, and the transcript shows the typecheck, the build, the two raw responses and each follow-up that changed a colour or the cookie name. No other run shares that checkout.

Frequently asked questions

Why a cookie instead of localStorage?

Because the app renders on the server, with ssr true in react-router.config.ts. A cookie reaches the root loader, so the first HTML already carries the right class on the html element. localStorage is only readable after the page loads, which is the flash the toggle is meant to avoid.

What happens for a visitor who never clicked the toggle?

The system preference wins, as it does today. With no cookie the server renders no class, and a short inline script in root.tsx checks prefers-color-scheme before paint and adds the dark class when the system is dark. Picking a theme writes the cookie and that choice takes over.

Does this change Tailwind's dark: classes elsewhere in the app?

They keep working, but they now follow the class rather than the media query, because of the custom-variant line in app/app.css. Every existing dark: utility in welcome.tsx and root.tsx behaves the same once the class is set, which is why the agent screenshots both themes.

Will it add a dependency?

No. The cookie helpers ship with react-router, useFetcher and useRouteLoaderData are already in use in framework mode, and the toggle is a small component. package.json and the lockfile are unchanged.

More React Router jobs

The same job on other stacks

Keep reading