BlowfishCMS
A small content management system for OpenBSD
version 0.45.6 · ISC · 2026-12-03 · releases
95 lines · 4.5 KB · download this file
// Loaded in the <head> without "defer", so the visitor's chosen theme is set
// before the page is drawn (no flash of the default theme).
(() => {
// The server lists the available themes in <meta name="site-themes">, generated
// from the theme editor. Visitors choose among enabled themes; ?theme=id
// previews any theme (including hidden ones) without saving the choice.
// A variant's "pair" is its theme's other shown variant, light or dark,
// which the switch beside the theme menu (#theme-mode) changes to.
let info = {default: 'modern', themes: {modern: {scheme: 'light', surfaces: true, enabled: true}}};
try {
const meta = document.querySelector('meta[name="site-themes"]');
if (meta) info = JSON.parse(meta.content);
} catch {}
const known = value => typeof value === 'string' && Object.prototype.hasOwnProperty.call(info.themes, value);
const usable = value => known(value) && info.themes[value].enabled;
const schemeOf = value => (known(value) && info.themes[value].scheme === 'dark') ? 'dark' : 'light';
const root = document.documentElement;
let preview = null;
try { const asked = new URLSearchParams(location.search).get('theme'); if (known(asked)) preview = asked; } catch {}
let selected = info.default;
// A choice is only honoured while visitors may choose (the site's theme menu is on).
if (info.picker !== false) { try { const saved = localStorage.getItem('bf-theme'); if (usable(saved)) selected = saved; } catch {} }
if (preview) selected = preview;
// Sets data-theme (which variant's CSS applies), data-scheme (light/dark)
// and data-surfaces on <html>.
const mark = value => {
const theme = info.themes[value] || {};
root.dataset.theme = value;
root.dataset.scheme = theme.scheme === 'dark' ? 'dark' : 'light';
root.dataset.surfaces = theme.surfaces ? 'themed' : 'original';
};
mark(selected);
// The menu has one option per theme; its data-variants lists the theme's
// variants, so the option showing is the one holding the chosen variant.
const optionFor = (picker, value) => [...picker.options].find(option => option.value === value || (option.dataset.variants || '').split(' ').includes(value));
const apply = value => {
selected = known(value) ? value : info.default;
mark(selected);
const picker = document.getElementById('theme-picker');
if (picker) {
let option = optionFor(picker, selected);
if (!option) {
option = new Option(`${selected} (preview)`, selected);
option.dataset.preview = '1';
picker.add(option);
}
picker.value = option.value;
}
// The switch: shown when this theme has a shown variant of the other
// scheme; pressed while the dark one is on.
const mode = document.getElementById('theme-mode');
if (mode) {
const pair = info.themes[selected]?.pair;
mode.hidden = !usable(pair);
mode.setAttribute('aria-pressed', schemeOf(selected) === 'dark' ? 'true' : 'false');
}
};
const choose = value => {
apply(value);
if (usable(selected)) { try { localStorage.setItem('bf-theme', selected); } catch {} }
};
document.addEventListener('DOMContentLoaded', () => {
apply(selected);
const label = document.querySelector('.theme-picker');
const mode = document.getElementById('theme-mode');
const menu = document.querySelector('.site-nav__menu');
const toggle = document.querySelector('.site-nav__toggle');
const mobile = window.matchMedia('(max-width: 640px)');
// On narrow screens the theme menu and its switch move into the navigation menu.
const placePicker = () => {
if (!label || !menu || !toggle) return;
const parts = mode ? [label, mode] : [label];
if (mobile.matches) menu.append(...parts);
else toggle.before(...parts);
};
placePicker();
mobile.addEventListener('change', placePicker);
// Choosing a theme keeps light or dark: its variant of the scheme now on,
// when it has one.
document.getElementById('theme-picker')?.addEventListener('change', event => {
let value = event.target.value;
const pair = info.themes[value]?.pair;
if (schemeOf(value) !== schemeOf(selected) && usable(pair)) value = pair;
choose(value);
});
mode?.addEventListener('click', () => {
const pair = info.themes[selected]?.pair;
if (usable(pair)) choose(pair);
});
});
// A theme chosen in another tab applies here too.
window.addEventListener('storage', event => {
if (event.key === 'bf-theme' && !preview && usable(event.newValue)) apply(event.newValue);
});
})();