For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/dark-mode.md.

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.

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:

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). 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:

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:

<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.
  • (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, 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). The value is 'Dark' or 'Light'.

Where the app runsWhat it follows
Apps from ng build --platformThe phone's appearance setting, as it changes
Lynx ExplorerThe phone's appearance setting when the bundle opens (see below)
Web previewThe browser's prefers-color-scheme, as it changes
Your own host appWhatever 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.

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, 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:

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'),
  ],
};

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 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.

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 gives the components the colors of the user's wallpaper, in both themes.

Read the appearance in TypeScript

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.
  • 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.