# Add a Dark Mode Toggle to a Phoenix LiveView App

Yes. A team watches an agent switch your LiveView app's Tailwind build to class-based dark mode, add a toggle that remembers its choice, and steers the palette.

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

## What the agent does

The agent begins with how the app's styles are built, because dark mode in Tailwind is a configuration choice before it is a design one. In an app like fly-apps/live_beats, `config/config.exs` runs Tailwind 3.2.7 through the `tailwind` Hex package with `assets/tailwind.config.js` and `css/app.css` as inputs, and that config lists `content` globs over `../lib/*_web/**/*.*ex`, loads `@tailwindcss/forms`, adds `phx-click-loading` and similar variants through plugins, and sets no `darkMode` key. Tailwind 3 then uses the `media` strategy, so the nine `dark:` classes already in the codebase, six in `live/player_live.ex` for the now-playing text, one in `live/profile_live/song_entry_component.ex` and two on the progress bar in `components/core_components.ex`, follow the operating system and nothing else. Every other surface is light only: the sidebars in `layouts/live.html.heex` use `bg-gray-100` and `bg-white`, `sign_in_live.ex` sits on `bg-gray-50`, `title_bar` in the components has `border-gray-200` and `text-gray-900`, and the settings form has `divide-gray-200` and `text-gray-700` labels.

It switches the config to `darkMode: "class"`, which keeps the existing `dark:` classes working but hands control to a class on the root element, and adds a few lines of inline script to the `<head>` of `layouts/root.html.heex`, before the stylesheet, that read `localStorage.theme`, fall back to `prefers-color-scheme`, and set the class on `document.documentElement`. Because `app.js` is loaded with `defer`, a hook would run after first paint and the page would flash light; the inline script is what prevents that. The toggle itself goes into the account dropdown that `components/layouts.ex` renders in both the mobile and desktop sidebars, next to Settings and Sign out, as a button with `phx-click={JS.dispatch("toggle-theme")}`; a listener in `assets/js/app.js`, beside the `Sortable`, `Flash` and `Menu` hooks, flips the class and stores the choice. No LiveView event, no server round trip and nothing to persist in the database.

Then the palette. The agent adds `dark:` counterparts across `live.html.heex`, `sign_in_live.ex`, `settings_live.ex`, `profile_live.ex`, `title_bar`, the flash and modal components and the song table, using one gray scale consistently so a teammate can ask for a different one in a sentence. It boots the app in its sandbox with a PostgreSQL server, placeholder values for `LIVE_BEATS_GITHUB_CLIENT_ID` and `LIVE_BEATS_GITHUB_CLIENT_SECRET`, which `config/dev.exs` reads at boot, and `mix phx.server`, then opens the sign-in page and the profile page in its browser in both themes and takes screenshots, so the team is looking at rendered pages rather than class names while the run is still open.

A teammate in the org reads the transcript as it goes and the run's starter, or an org admin, steers it with a follow-up: a different gray, a toggle in the title bar instead of the dropdown, or leaving the player's existing classes exactly as they were. The agent applies the change, rebuilds the CSS with `mix assets.deploy` to confirm the purge keeps every `dark:` class it uses, runs `mix test`, whose LiveView tests select on text and not on colors, and attaches the final screenshots to the pull request.

## Start it with one API call

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

```json
{
  "agent": "codex",
  "input": "Add a dark mode toggle to this Phoenix LiveView app. Read config/config.exs, assets/tailwind.config.js, assets/js/app.js, the layouts under lib/live_beats_web/components/layouts, core_components.ex and the live views first. Switch Tailwind to class-based dark mode, add an inline script in the root layout that applies the saved or system preference before the stylesheet loads so there is no flash, add a toggle in the account dropdown that dispatches a JS event handled in app.js and remembers the choice in localStorage, and add dark variants across the sidebars, sign-in page, settings form, profile page, title bar, flash and modal. Keep the player's existing dark classes working. Start the app with a local database and placeholder GitHub OAuth values, screenshot the sign-in and profile pages in both themes, run mix test, and open a pull request with the screenshots.",
  "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 changing `assets/tailwind.config.js`, `layouts/root.html.heex`, `assets/js/app.js`, `components/layouts.ex`, `layouts/live.html.heex`, `core_components.ex` and the live views that gained `dark:` variants, with four screenshots in the description: sign-in and profile, light and dark. No migration, no new dependency and no change to `mix.exs`.

The description explains the three moving parts in a paragraph each, the config strategy, the inline script and the dispatched event, lists the follow-ups the team sent during the run and what changed for each, and quotes the `mix test` summary. It also names the surfaces it left light on purpose, such as the GitHub sign-in button's brand color.

Before merging, a reviewer flips the toggle on a phone-width window to check the mobile sidebar, reloads to confirm the choice sticks with no flash, and compares the two screenshots of the settings form for a label that lost contrast.

## Run it on every design change

A designer or an engineer starts the run in the console, in the Project that has the repository, or with this body from a server; everyone in the org can open the run and read its transcript while it works. The run's starter, or an org admin, sends a follow-up through `POST /o/{org}/sessions/{id}/messages` once the turn has finished, and the parked sandbox wakes with the app, the database and the built CSS exactly where they were.

Send a `request_id`, a UUID, in the body so a retried start returns the run it already began. When the last turn ends, `GET /o/{org}/sessions/{id}` names the pull request, and the transcript keeps every screenshot and command the team looked at while steering.

## Why run it on Rush?

Dark mode touches dozens of templates and one line of config, and the one line decides whether the dozens work: with the default `media` strategy a toggle does nothing, and with `class` but no early script every load flashes white. The agent reads the Tailwind pipeline and the deferred script tag before it edits a template, and shows the team rendered pages rather than a diff of class names.

On Rush the app runs in the agent's own sandbox with its own database and browser, so the screenshots are of the real pages and the team can change its mind mid-run without anyone's laptop in the loop. The transcript keeps every follow-up and what it changed, which is the review trail for a visual change that a diff alone does not give.

## Frequently asked questions

### Why change darkMode to class instead of keeping media?

With the `media` strategy Tailwind applies `dark:` classes only when the operating system is dark, so a toggle in the app has nothing to flip. The `class` strategy applies them when the root element carries the `dark` class, which the inline script and the toggle control, and the existing `dark:` classes in the player keep working unchanged.

### Why an inline script rather than a LiveView hook?

`app.js` is loaded with `defer`, so a hook runs after the first paint and a returning dark-mode user would see a light flash on every page load. A few lines inline in the `<head>`, before the stylesheet, set the class before anything is drawn.

### Is the choice stored per user on the server?

No. It lives in `localStorage` in the browser, which is enough for a preference that is about the screen in front of the user. Storing it on the account is a follow-up: a field on the user, a select in settings and a line in the inline script that reads a data attribute the layout renders.

### How does the team steer the colors?

By reading the transcript and the screenshots while the run is open and sending a follow-up such as a different gray scale or a toggle in the title bar. The agent applies it, rebuilds the CSS, takes new screenshots and continues; each follow-up and its result stay in the transcript next to the pull request.

## More Phoenix LiveView jobs

- [Set Up GitHub Actions CI for a Phoenix LiveView App](https://getrush.ai/run/add-github-actions-ci/phoenix-liveview/)
- [Add Health Checks to a Phoenix LiveView App](https://getrush.ai/run/add-health-checks/phoenix-liveview/)
- [Add i18n with Gettext to a Phoenix LiveView App from a Linear Issue](https://getrush.ai/run/add-i18n/phoenix-liveview/)

## The same job on other stacks

- [Add Dark Mode to a React App](https://getrush.ai/run/add-dark-mode/react/)
- [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/)

## 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/)
