Skip to content

Theming and Dark Mode

A good mobile theme does three things: it gives screens semantic names instead of raw colors, it follows the user’s light or dark preference, and it never flashes the wrong scheme while the app starts.

PAM Native splits that work into three layers:

  • Theme registers semantic classes (surface, card, heading, button-primary…) and per-component defaults, checked against WCAG contrast when the theme is built.
  • DesignTokens provides the spacing, radius, type and motion scale.
  • Appearance owns the light/dark preference natively, so the window, the splash screen and PHP’s first frame all agree.

Set the theme once in your entry point, before App::run():

index.php
<?php
declare(strict_types=1);
use App\Hello;
use Pam\Native\App;
use Pam\Native\Theme;
use Pam\Native\ThemeMode;
require __DIR__.'/vendor/autoload.php';
App::components(__DIR__.'/src', __DIR__.'/.pam-native/components');
App::theme(Theme::adaptive(ThemeMode::Light));
App::run(new Hello());

Theme::adaptive() accepts ThemeMode::Light, ThemeMode::Dark or ThemeMode::HighContrast. Theme::pamLab(), used by new projects, is the dark palette.

The theme does two things:

  1. It registers semantic classes you can use from templates (class="card") or PHP (->class('card')).
  2. It applies defaults to every Screen, Text, Button and Input (background, text color, minimum touch height, radius), so an unstyled tree already looks consistent.
Class Purpose
surface, surface-muted Screen and secondary backgrounds
card Muted surface with border, 12 dp radius and 16 dp padding
text-primary, text-muted, heading, label, metric Text roles
accent, danger Filled action colors with readable foregrounds
button-primary, button-secondary Buttons with a 48 dp minimum height
input Text field surface, border and placeholder color
focus-ring A 3 dp focus border
src/Components/BalanceCard.pam.php
<template>
<Column class="card">
<Text class="label">Balance</Text>
<Text class="metric">{{ $balance }}</Text>
<Button class="button-primary" label="Add money" @press="topUp" />
</Column>
</template>

DesignTokens constants keep spacing and type consistent in typed PHP trees:

Constant Value
Space1, Space2, Space3, Space4, Space6, Space8 4, 8, 12, 16, 24, 32 dp
RadiusSmall, RadiusMedium, RadiusLarge 8, 12, 20 dp
TextLabel, TextBody, TextTitle, TextHeadline 14, 16, 20, 24
TouchTarget 48 dp
MotionFastMs, MotionStandardMs 150, 240 ms
use Pam\Native\DesignTokens;
use Pam\Native\Style;
$cardStyle = new Style(
padding: DesignTokens::Space4,
gap: DesignTokens::Space2,
borderRadius: DesignTokens::RadiusMedium,
);

In stylesheets, declare your own compiled tokens with @tokens and reuse them with var(); see Styling. pam style tokens src/app.css generated/style turns them into typed PHP, Kotlin and Swift constants.

Declare the window colors for both schemes in pam-native.json. The build generates the Android DayNight resources and the iOS launch colors from them, so the starting window and splash screen already use the right scheme:

pam-native.json
{
"appearance": {
"defaultMode": "system",
"light": { "background": "#F8FAFC" },
"dark": { "background": "#0F172A" }
}
}
Field Default Purpose
defaultMode system Preference used until the app calls Appearance::set(): system, light or dark.
light.background, dark.background #FFFFFF, #121212 Window background painted before the first PHP frame.
*.statusBar, *.navigationBar the background Bar colors; icon contrast follows their luminance.
*.splashBackground the background Android 12+ splash screen background.
legacyStartingWindow theme none skips the Android 8–11 starting window, which cannot follow a persisted override.

Prefer CSS over passing a $dark flag through every component. The query follows the effective appearance and restyles at runtime without remounting:

src/app.css
.screen { background-color: #F8FAFC; }
.title { color: #0F172A; }
.card { background-color: #FFFFFF; border: 1px solid #E2E8F0; }
@media (prefers-color-scheme: dark) {
.screen { background-color: #0F172A; }
.title { color: #F8FAFC; }
.card { background-color: #1E293B; border-color: #334155; }
}

Write the dark overrides directly inside the media block. Custom properties resolve when the sheet compiles, so redefining a --variable inside @media does not change rules outside it.

Appearance reads and changes the preference. Every read is synchronous: the host exports the persisted mode and effective scheme before PHP starts, so the very first render already knows the answer.

use Pam\Native\Appearance;
use Pam\Native\AppearanceMode;
use Pam\Native\UserInterfaceAppearance;
Appearance::mode(); // AppearanceMode::System, Light or Dark (the persisted preference)
Appearance::current(); // UserInterfaceAppearance::Light or Dark (the effective scheme)
Appearance::isDark(); // bool
Appearance::system(); // ?UserInterfaceAppearance: the OS scheme, when observable

Appearance::set() restyles the tree in place (components are not remounted), notifies listeners and asks the host to persist the choice:

src/Screens/AppearanceSettings.php
<?php
declare(strict_types=1);
namespace App\Screens;
use Pam\Native\Appearance;
use Pam\Native\AppearanceMode;
use Pam\Native\Component;
use Pam\Native\Element;
use Pam\Native\Style;
use Pam\Native\UI\Button;
use Pam\Native\UI\Column;
use Pam\Native\UI\Screen;
use Pam\Native\UI\Text;
final class AppearanceSettings extends Component
{
public function render(): Element
{
$current = Appearance::mode();
return Screen::make(
Column::make(
Text::make('Appearance')->class('heading'),
...array_map(
fn (AppearanceMode $mode): Button => Button::make($mode->name)
->class($mode === $current ? 'button-primary' : 'button-secondary')
->onPress(fn () => Appearance::set($mode)),
AppearanceMode::cases(),
),
)->style(new Style(padding: 24, gap: 12)),
);
}
}

AppearanceMode is an int-backed enum: System = 1, Light = 2, Dark = 3. Pass a completion closure to know whether the native write succeeded: Appearance::set($mode, fn (bool $persisted) => ...).

When you use a Theme palette instead of CSS queries, swap it when the effective scheme changes:

index.php
use Pam\Native\App;
use Pam\Native\Appearance;
use Pam\Native\AppearanceMode;
use Pam\Native\Theme;
use Pam\Native\ThemeMode;
use Pam\Native\UserInterfaceAppearance;
$themeFor = static fn (UserInterfaceAppearance $scheme): Theme => Theme::adaptive(
$scheme === UserInterfaceAppearance::Dark ? ThemeMode::Dark : ThemeMode::Light,
);
App::theme($themeFor(Appearance::current()));
Appearance::onChange(static function (UserInterfaceAppearance $current, AppearanceMode $mode) use ($themeFor): void {
App::theme($themeFor($current));
});

Appearance::onChange() returns a subscription id; pass it to Appearance::unsubscribe() when a component that subscribed goes away.

With edge-to-edge windows your screen paints behind the bars, so pick icon colors that contrast with what is under them. Light icons go on dark backgrounds:

use Pam\Native\StatusBarAppearance;
use Pam\Native\UI\StatusBar;
$bar = StatusBar::make(color: 0xFF0F172A, appearance: StatusBarAppearance::Light)->animated();

Only the active route contributes status-bar configuration, so hidden screens in a stack never override the visible one. Keep content out of the bars themselves; see Safe Areas.

ThemeMode::HighContrast is a black-and-white palette with a yellow accent and focus ring. Offer it as a user setting, and never use color as the only signal of state: pair it with text, an icon or an accessibility state. See Accessibility.