Files
breadcrumb-the-shire/web/js/app-boot.js

56 lines
2.0 KiB
JavaScript
Raw Normal View History

/**
* Render-blocking boot script runs synchronously before first paint.
*
* Intentionally a classic IIFE (not an ES module) so the browser executes it
* immediately while parsing <head>. This prevents layout flicker by applying
* persisted UI state (sidebar collapsed, contrast mode) before any content
* is rendered. Uses `var` for broadest compatibility in the sync path.
*/
2026-02-11 19:28:12 +01:00
(function () {
var root = document.documentElement;
root.classList.remove('no-js');
root.classList.add('js');
root.classList.add('aside-pending');
var normalizeToken = function (value) {
return String(value == null ? '' : value)
.trim()
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, '-')
.replace(/^-+|-+$/g, '');
};
var buildStorageKey = function (scope, key) {
var namespace = normalizeToken('app-ui') || 'app-ui';
var version = normalizeToken('v1') || 'v1';
var normalizedScope = normalizeToken(scope || '') || 'global';
var normalizedKey = normalizeToken(key || '');
if (!normalizedKey) {
return '';
}
return namespace + ':' + version + ':' + normalizedScope + ':state:' + normalizedKey;
};
2026-02-11 19:28:12 +01:00
try {
var sidebarCollapsedKey = buildStorageKey('sidebar', 'sidebar-collapsed');
var sidebarHiddenKey = buildStorageKey('sidebar', 'sidebar-hidden');
var contrastKey = buildStorageKey('contrast', 'contrast-mode');
if (window.innerWidth >= 768) {
if (sidebarCollapsedKey && window.localStorage.getItem(sidebarCollapsedKey) === '1') {
root.classList.add('sidebar-collapsed');
}
if (sidebarHiddenKey && window.localStorage.getItem(sidebarHiddenKey) === '1') {
root.classList.add('sidebar-hidden');
}
2026-02-11 19:28:12 +01:00
}
var contrastValue = contrastKey ? window.localStorage.getItem(contrastKey) : null;
2026-02-11 19:28:12 +01:00
if (contrastValue === 'high') {
root.dataset.contrast = 'high';
} else {
root.dataset.contrast = 'normal';
}
} catch (e) {
// ignore storage errors
}
})();