By the end of this guide your app has its own accent color, and a light or dark mode that follows the system or the
user’s choice.
TILE’s look is its tokens, the named colors and sizes that the components read. To change the look, you set new token
values in your own stylesheet. You change no component, and you write no theme function of your own.
Theme and tokens explains how tokens are named and layered.
The theme declares its tokens in the theme cascade layer. Its dx-tokens layer comes next, and sets the values
that change with a surface or with a density, which is how compact a region’s controls are. Declare yours in
user-tokens, which comes after both, so yours win wherever both apply:
src/theme.css
1
@layer user-tokens {
2
:root {
3
/* A green accent: primary buttons, links and selection. */
--color-accent-bg-hover is a token of its own. If you override only --color-accent-bg, a green button turns blue
again under the pointer.
Import your stylesheet after the theme:
src/main.tsx
1
import'@dxos-theme';
2
import'@dxos/react-ui/theme.css';
3
import'./theme.css';
Override semantic tokens, such as accent-*, fg-* or a surface, rather than the scale. A semantic token is what the
components read. A scale value such as --color-blue-600 is also read by everything else that happens to use blue.
Give every override a light and a dark value with light-dark(), in that order. A plain value applies in both modes.
The components also read --dx-* variables, which hold their sizes and states. @dxos/react-ui/theme.css sets them
in the dx-components layer, which comes after user-tokens. Override one outside any layer, where your rule wins
over every layer:
The dark values apply under a dark class on the root element. ThemePlugin, which
Installation adds to the Vite config, sets the class for you. It puts a script in
the <head> of your page, which runs before the first paint, so the page never shows the wrong mode for a frame. The
script does what this code does:
1
// The dark values apply under a `dark` class on the root element.
2
const
const media: MediaQueryList
media =
var window: Window & typeof globalThis
The window property of a Window object points to the window object itself.
The Window interface's matchMedia() method returns a new MediaQueryList object that can then be used to determine if the document matches the media query string, as well as to monitor the document to detect when it matches (or stops matching) that media query.
The documentElement read-only property of the Document interface returns the Element that is the root element of the document (for example, the element for HTML documents).
The read-only classList property of the Element interface contains a live DOMTokenList collection representing the class attribute of the element. This can then be used to manipulate the class list.
The toggle() method of the DOMTokenList interface removes an existing token from the list and returns false. If the token doesn't exist it's added and the function returns true.
The matches read-only property of the MediaQueryList interface is a boolean value that returns true if the document currently matches the media query list, or false if not.
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
apply reads the media query only when the choice is 'system'. Call it when your app starts and each time the
choice changes.
Call it from a change listener on the media query too. ThemePlugin’s script sets the class from the system again
on every change, and a listener that you add later runs after the script’s.
Pass the same mode to Theme.Provider’s themeMode, so useThemeMode reports it to components that draw their own
canvas or chart. By default, themeMode is 'dark'.