The PrimeFaces 17 themes support light and dark mode, selected by the CSS color-scheme of the page.
LightSwitch sets the color-scheme to light or dark, automatically based on the OS settings
or with its JavaScript API, and can remember the selection on the server.
The widget methods light(), dark(), toggle() and automatic() switch
the whole site. Open the theme configurator: its color scheme buttons follow the switch. automatic="false" keeps the switch from applying the OS setting on page load, so the stored choice wins.
Put LightSwitch in your template like:
<html style="color-scheme: \#{empty userBean.colorScheme ? 'light dark' : userBean.colorScheme}">
...
<pe:lightSwitch selected="\#{userBean.colorScheme}"/>
userBean should be a session scoped bean with a String type colorScheme property
(with both getter and setter). Its value is light or dark. Rendering it on the html
element avoids a flash of the other mode on page load.
This showcase does not bind selected: the theme configurator (palette button at the right edge) keeps the
color scheme in the localStorage of the browser and applies it before the first paint, so the choice of the
configurator and of the demo below always agree and survive a reload.
Optionally, the light and dark attributes name a theme to switch to as well, e.g.
<pe:lightSwitch selected="\#{userBean.colorScheme}" light="aura" dark="metal"/>.
Then also bind the theme in your web.xml:
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>\#{userBean.theme}</param-value>
</context-param>
Migration from PrimeFaces Extensions 16: selected holds the color scheme
(light / dark) instead of a theme name, the light / dark
attributes have no default anymore (formerly saga-blue / arya-blue), and the
switch event carries the color scheme.
See also the PrimeFaces theming documentation.
CombinedResourceHandlerWhen a theme is switched with combined resources, we cannot find the theme link in the DOM tree. Therefore we need to add a node for the new theme. Important: this node will be added as the last element in the body.