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-schemequery as on the web. LynxThemegives 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.
Then use the variables. Nothing in the component switches them:
pageis the root element of every Lynx app (see Glossary). AngularLynx creates it, and adds and removes itsdarkclass as the appearance changes.- CSS variables inherit, unlike other properties on Lynx. Every element reads the
page.darkvalues 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.darkwork too, such aspage.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:
Tailwind's dark: variant writes the same query, so dark: classes work too:
- It works in global and component styles alike, and with Tailwind 3 and 4. See Tailwind CSS.
(prefers-color-scheme: light)becomespage:not(.dark).- It follows the app's appearance, not only the phone's:
setColorScheme('light')keepsdark: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 --platformmakes build the view with the phone's scheme. On every switch, they send the app's appearance: the phone's, or the onesetColorScheme()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.
- The apps
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'.
- The apps that
ng build --platformmakes 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
themewins over the browser's preference, so a page that embeds your app in a<lynx-view>can set the theme itself throughglobalProps.
Let the user choose
An Appearance setting usually has three choices: follow the system, always light, always dark. setColorScheme() takes the same three.
'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:
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
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()andisDarkMode()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-schemequery 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'supdateColorScheme. 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.
Related
LynxThemeAPI- CSS on Lynx
- Theming the UI components
- Dynamic Color: the wallpaper's colors on Android
- Pass data from your native app: how a host app sets global props
