Skip to content
x4x4x4js2.3.29

Themes and CSS

In x4js, appearance is not decided in code: it lives in CSS. Each component brings its structure and its behaviour, and the stylesheet does the rest.

Each component carries a class named after it, and those of its base classes.

Component Classes carried by the element
Button x4button, x4
VBox x4vbox, x4box, x4
MainView, which extends VBox mainview, x4vbox, x4box, x4

A class can declare its prefix: with static classPrefix = "app"; in MainView, the element carries appmainview instead of mainview.

So you target a component, provided or your own, without declaring anything. The cls property adds your own classes when two elements of the same type must be told apart.

.mainview {
padding: 12px;
gap: 8px;
.x4button {
min-width: 90px;
}
}

Colors are not hard-coded in the components. They go through CSS variables, organised in three levels:

Level Examples Role
Primary scale --color-primary-a0 … --color-primary-a100 From a0, the background, to a100, the text, through the base color. The greys are mixes of a0 and a100.
Meaning variables --accent-background, --border, --text-primary, --background-primary What a color means in the interface.
Component variables --button-background, --listbox-border, --chart-color-1 The fine tuning of one component.

Each level builds on the previous one. Depending on what you want to change, act at the right level.

Redefine the primary scale, once per theme. All components follow.

:root {
--color-primary-a40: #3d8fbd;
--color-primary-a50: #126e9e;
--color-primary-a60: #0e5a80;
// … the other shades of the scale
}
:root {
--border: #cfdae0;
--accent-background: #126e9e;
}
.x4button {
--button-background: #002a40;
--button-color: white;
}

The list of the variables of each component is in the API reference, on the page of its class.

A theme is a set of values for these variables, turned on by a class on <html>. x4js provides two: the light theme, by default, and a dark theme.

import { Application } from 'x4js';
Application.setTheme( "dark" ); // puts the class x4theme-dark on <html>
Application.setTheme( "" ); // back to the light theme

Application.getTheme( ) gives the name of the current theme ("" for the light theme).

For a theme of your own, redefine the scale, and the meaning variables if needed, under html.x4theme-<name>, then turn it on by its name:

html.x4theme-pink {
--color-primary-a0: #fff5f8;
// … a5 to a90 …
--color-primary-a100: #3a1020;
}
Application.setTheme( "pink" );

For a dark theme, the scale goes from dark (a0) to light (a100): the greys follow by themselves. Add color-scheme: dark in the same block, so that the native parts (choice lists, scroll bars) follow too.

There is no “theme” object to handle in TypeScript: do not create one to duplicate what CSS already does.

The font and its size are two variables, --font-name and --font-size (14px by default), set on :root. To change the size of the text, change --font-size, not the font size of <html>: the components follow --font-size.

:root {
--font-size: 13px;
}

Sometimes a value depends on data: a color picked by the user, a computed width. A component can then read or write a CSS variable.

panel.setStyleVariable( "--panel-accent", color );
const height = panel.getStyleVariable( "--row-height" );
  1. No cosmetics in code. Colors, margins and fonts go in the stylesheet.
  2. No fighting with CSS. If you need negative margins or !important, look for the mistake in the CSS.
  3. CSS dresses, it does not remove a behaviour. To remove the close button of a dialog, configure the component through its properties (closable: false), do not hide it in CSS.