BlogHow-toTheming

How to switch Palatian between light and dark, change the colour scheme, and turn on RTL or the top bar

Every look of Palatian is a data attribute on the html element: mode, scheme, accent metal, navigation, direction and more. Set them in the markup, from the Theme settings page or with the Palatian API, and add a scheme of your own in SCSS.

3 min readPerforks

The data attributes on the html element that set the mode, the colour scheme, the accent and the direction

Palatian keeps its appearance in attributes on <html>. Dark mode is data-bs-theme="dark", a scheme is data-scheme="olive", and so on. You can hard-code a look in the markup, let users choose on the Theme settings page, or set it from JavaScript. Whatever you choose is remembered in the browser's localStorage and re-applied before the first paint.

The attributes

AttributeValuesDefault
data-bs-themelight, darkfollows the operating system
data-schemelapis, olive, plum, ember, teallapis
data-accentgold, platinum, coppergold
data-panelpinned, hiddenpinned
data-navdock, top (horizontal bar with a mega panel)dock
dirltr, rtl (loads palatian.rtl.min.css)ltr
data-widthfluid, boxed (max 1360px)fluid
data-densitycomfortable, compactcomfortable
data-radiusrounded, soft, sharprounded
data-fontsm (15px), md (16px), lg (17px)md
data-motion, data-glow, data-glass, data-edgeon, offon

Fix a look in the markup

To ship the admin in one fixed look and skip the Theme settings page, set the attributes on the root element of every page (in src/partials/head.hbs if you build, or in each dist/*.html file if you do not):

<html lang="en" data-bs-theme="dark" data-scheme="teal" data-accent="platinum" data-density="compact">

For right-to-left, set dir="rtl" and link assets/css/palatian.rtl.min.css instead of palatian.min.css. The RTL stylesheet is generated by rtlcss at build time and ships in dist/.

Let users choose

The toolbar has a light/dark toggle, and the Theme settings page (theme-settings.html) exposes every option above: mode (light, dark or system), scheme, accent metal, panel, navigation, direction, width, density, radius, font size and the effects. Each change applies at once and is stored under palatian-prefs in localStorage (the mode under palatian-theme, the scheme under palatian-scheme). The small inline script in the head re-applies them before the first paint, so there is no flash of the wrong mode.

Any input with data-pl-pref="<key>" is wired automatically, so you can put a density switch or a scheme select on your own settings page. A button with data-pl-prefs-reset restores the defaults. Markup can also switch things directly: data-pl-theme="dark|light|toggle", data-pl-scheme="olive" and data-pl-panel on a button.

From JavaScript

Palatian.setTheme('dark')               // or 'light'
Palatian.getTheme()                     // 'dark' | 'light'
Palatian.setScheme('plum')              // 'lapis' | 'olive' | 'plum' | 'ember' | 'teal' | your own
Palatian.prefs.set('theme', 'system')   // follow the operating system again
Palatian.prefs.set('dir', 'rtl')        // swaps the stylesheet and sets dir="rtl"
Palatian.prefs.set('nav', 'top')
Palatian.prefs.all()                    // every current value
Palatian.prefs.reset()
document.addEventListener('pl:theme', e => { /* re-render anything colour-dependent */ })
document.addEventListener('pl:prefs', e => console.log(e.detail.key, e.detail.value))

prefs.set() returns false for a value it does not know; Palatian.prefs.options lists the allowed values per key. Charts made with the chart helpers re-render on pl:theme by themselves. The same calls work in the React edition.

Add a scheme of your own

Schemes are SCSS. The five colours are variables in src/scss/_variables.scss ($pl-lapis: #2e4cc8, $pl-olive, $pl-plum, $pl-ember, $pl-teal), and src/scss/_tokens.scss applies each one through the pl-scheme mixin, which re-points every Bootstrap component that bakes in the primary colour: buttons, links, checks, pills, pagination, progress, tabs, the calendar. One more line and a rebuild gives you a sixth:

html[data-scheme="brand"] { @include pl-scheme(#0e7490); }

Then add 'brand' to the scheme list in PREF_OPTIONS in src/js/prefs.js so the preferences accept it. To change the default scheme, edit $primary in _variables.scss and the first @include pl-scheme(...) line in _tokens.scss. The surfaces, fields and borders of light and dark mode are custom properties at the top of _tokens.scss; the accent metals are in _prefs.scss.

The typefaces are chosen in _variables.scss too: $headings-font-family, $font-family-sans-serif and $font-family-monospace, with the matching Google Fonts link in head.hbs. Building is described in running and building the HTML edition.