> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Dark mode

Your app follows the phone's light or dark appearance with no code. While the phone is dark, AngularLynx gives the **`page`** element a `dark` class.

- **Your stylesheet's dark values** go under that class, or in a `prefers-color-scheme` query as on the web.
- **`LynxTheme`** gives you the appearance as signals, and lets the user choose their own.

## Switch colors with CSS variables

Define your colors twice: light values on `page`, and dark values on `page.dark`.

```css title="src/styles.css"
page {
  --surface: #ffffff;
  --text: #1a1a1a;
  --border: #e0e0e0;
  background-color: var(--surface);
}

page.dark {
  --surface: #1a1a1a;
  --text: #f0f0f0;
  --border: #333333;
}
```

Then use the variables. Nothing in the component switches them:

```typescript title="src/app/app.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-root',
  imports: [LYNX_ELEMENTS],
  template: `
    <view class="app-card">
      <text class="app-card-text">Hello in either theme</text>
    </view>
  `,
  styles: `
    .app-card {
      margin: 16px;
      border-width: 1px;
      border-style: solid;
      border-color: var(--border);
      border-radius: 12px;
      padding: 16px;
    }
    .app-card-text {
      color: var(--text);
    }
  `,
})
export class App {}
```

- **`page` is the root element of every Lynx app** (see [Glossary](/guide/glossary.md#page)). AngularLynx creates it, and adds and removes its `dark` class as the appearance changes.
- **CSS variables inherit**, unlike other properties on Lynx. Every element reads the `page.dark` values once the class is on, and the light ones otherwise.
- **The first frame is already in the right theme.** The class is set before your app renders, so a dark phone never sees a flash of light.
- **Give your text a color.** Lynx draws text black unless a style says otherwise, and black text vanishes on a dark background.
- **Rules under `page.dark` work too**, such as `page.dark .app-card { … }`. They restyle when the class changes, as on the web. Variables keep each color in one place.

## Use `prefers-color-scheme` and Tailwind's `dark:`

CSS written for the web works as it is. The build turns each `@media (prefers-color-scheme: dark)` block into rules under `page.dark`, so it follows the app's appearance:

```css title="src/styles.css"
@media (prefers-color-scheme: dark) {
  /* Becomes page.dark { … } */
  :root {
    --surface: #1a1a1a;
  }
  /* Becomes page.dark .app-card { … } */
  .app-card {
    border-color: #333333;
  }
}
```

Tailwind's `dark:` variant writes the same query, so `dark:` classes work too:

```html
<view class="bg-white dark:bg-zinc-900">
  <text class="text-zinc-900 dark:text-white">Hello in either theme</text>
</view>
```

- **It works in global and component styles alike**, and with Tailwind 3 and 4. See [Tailwind CSS](/guide/tailwindcss.md#dark-mode).
- **`(prefers-color-scheme: light)` becomes `page:not(.dark)`.**
- **It follows the app's appearance, not only the phone's:** `setColorScheme('light')` keeps `dark:` classes off on a dark phone.
- **A query with another condition is Lynx's to evaluate.** One such as `(prefers-color-scheme: dark) and (min-width: 640px)` is left as it is. Lynx answers its color scheme from the one the host app gives it.
  - **The apps `ng build --platform` makes** build the view with the phone's scheme. On every switch, they send the app's appearance: the phone's, or the one `setColorScheme()` chose.
  - **A choice made at startup**, such as `provideColorScheme('light')` on a dark phone, reaches Lynx once the bundle runs. So such a query can briefly answer with the phone's scheme first.
  - **Another host app** answers it with the scheme it builds the view with (light if it sets none). It follows later changes only if it calls Lynx's `updateColorScheme`.

## Where the appearance comes from

AngularLynx doesn't ask the phone directly. It reads a `theme` value from the [**host app**](/guide/glossary.md#host-app), the native app that runs your bundle. The host app passes it in its **global props**, values it shares with every bundle it runs (see [GlobalData](/guide/glossary.md#globaldata)). The value is `'Dark'` or `'Light'`.

| Where the app runs                                | What it follows                                                  |
| ------------------------------------------------- | ---------------------------------------------------------------- |
| Apps from `ng build --platform`                   | The phone's appearance setting, as it changes                    |
| [Lynx Explorer](/guide/glossary.md#lynx-explorer) | The phone's appearance setting when the bundle opens (see below) |
| [Web preview](/guide/glossary.md#web-preview)     | The browser's `prefers-color-scheme`, as it changes              |
| Your own host app                                 | Whatever it passes as `theme`                                    |

- **The apps that `ng build --platform` makes send the new theme as soon as the phone switches**, so the app restyles while it's open.
- **Lynx Explorer on iOS sends the theme once.** Switch the phone's appearance and open the bundle again to see the other theme. On Android it follows the switch.
- **A host app's `theme` wins over the browser's preference**, so a page that embeds your app in a `<lynx-view>` can set the theme itself through `globalProps`.

## Let the user choose

An Appearance setting usually has three choices: follow the system, always light, always dark. `setColorScheme()` takes the same three.

```typescript title="src/app/appearance-setting.ts"
import { Component, inject } from '@angular/core';
import {
  LYNX_ELEMENTS,
  type LynxColorScheme,
  LynxTheme,
} from '@blotch/angular-lynx';

@Component({
  selector: 'app-appearance-setting',
  imports: [LYNX_ELEMENTS],
  template: `
    @for (scheme of schemes; track scheme) {
      <view (bindtap)="theme.setColorScheme(scheme)">
        <text class="app-text">
          {{ scheme }}{{ theme.colorScheme() === scheme ? ' ✓' : '' }}
        </text>
      </view>
    }
  `,
})
export class AppearanceSetting {
  protected readonly theme = inject(LynxTheme);
  protected readonly schemes: readonly LynxColorScheme[] = [
    'system',
    'light',
    'dark',
  ];
}
```

- **`'system'`** (the default) follows the phone. **`'light'`** and **`'dark'`** keep to one whatever the phone says.
- **The choice lasts until the app closes.** Store it, such as with [`LynxLocalStorage`](/guide/persistent-storage.md), and set it again when the app starts.

## Keep to one appearance

An app with no dark design yet can stay light on every phone. Set it in your app config, and it applies from the first frame:

```typescript title="src/app/app.config.ts"
import { type ApplicationConfig } from '@angular/core';
import { provideColorScheme, provideRenderer } from '@blotch/angular-lynx';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRenderer(),
    provideColorScheme('light'), // [!code highlight]
  ],
};
```

An app whose stylesheet has no `page.dark` rule doesn't need this: the class is there, and nothing reads it.

## With the UI components

The [UI components](/dolan/index.md) theme keeps its light values in `default.css` and its dark values in `dark.css`, under `page.dark`. Import both and the components follow the phone. Classes like `bg-background` and `text-foreground` follow it too. See [Theming](/dolan/theming.md#dark-mode).

Icons and charts draw with SVG, which can't read CSS, so they take their colors from the appearance instead, and switch with it by themselves.

On Android 12 and later, [Dynamic Color](/dolan/dynamic-color.md) gives the components the colors of the user's wallpaper, in both themes.

## Read the appearance in TypeScript

```typescript title="src/app/status-bar.ts"
import { Component, computed, inject } from '@angular/core';
import { LYNX_ELEMENTS, LynxTheme } from '@blotch/angular-lynx';

@Component({
  selector: 'app-status-bar',
  imports: [LYNX_ELEMENTS],
  template: `<image [src]="logo()" class="app-logo" />`,
})
export class StatusBar {
  readonly #theme = inject(LynxTheme);

  protected readonly logo = computed(() =>
    this.#theme.appearance() === 'dark' ? 'logo-dark.png' : 'logo.png',
  );
}
```

- **`appearance()`** is a signal of `'light' | 'dark'`: what the app is drawn in. It's the user's choice, or the phone's while that's `'system'`.
- **`colorScheme()`** is the choice itself: `'system' | 'light' | 'dark'`.
- **`theme()`** and **`isDarkMode()`** are the phone's own appearance, whatever the app chose.

Use them for things CSS can't reach, such as picking an image or a chart palette. For colors, prefer the CSS variables above. The whole page restyles from one class change.

## Limitations

- **In your own host app, a `prefers-color-scheme` query with another condition follows the host's scheme.** That's the one it builds the view with, light if it sets none. It changes only when the host calls Lynx's `updateColorScheme`. See [above](#use-prefers-color-scheme-and-tailwinds-dark).
- **Your own native app has to send each change.** The app follows whatever theme the host app sends. See [Pass data from your native app](/guide/embedding/host-data.md#send-the-data-from-native-code).

## Related

- [`LynxTheme` API](/api/angular-lynx/device/theme.md)
- [CSS on Lynx](/guide/styling/index.md)
- [Theming the UI components](/dolan/theming.md)
- [Dynamic Color](/dolan/dynamic-color.md): the wallpaper's colors on Android
- [Pass data from your native app](/guide/embedding/host-data.md): how a host app sets global props
