@bookbox/view-html
Version:
Bookbox view for html
31 lines (30 loc) • 1.2 kB
JavaScript
export const THEME_STORE_KEY = 'book-box-theme';
function getBookboxNodes(options) {
const { element, selector = '.book-box' } = options !== null && options !== void 0 ? options : {};
if (element)
return [element];
return Array.from(document.querySelectorAll(selector));
}
const darkClassName = 'book-box_theme-dark';
const sepiaClassName = 'book-box_theme-sepia';
export function setTheme(options) {
const { theme } = options;
for (const node of getBookboxNodes(options)) {
if (theme === 'dark')
node.classList.add(darkClassName);
if (theme === 'sepia')
node.classList.add(sepiaClassName);
}
}
/**
* set theme from localStorage
*/
export function setSavedTheme(options) {
const { storageKey } = options !== null && options !== void 0 ? options : {};
const savedTheme = localStorage.getItem(storageKey !== null && storageKey !== void 0 ? storageKey : THEME_STORE_KEY);
if (!savedTheme)
return;
if (savedTheme !== 'dark' && savedTheme !== 'light' && savedTheme !== 'sepia')
return;
setTheme(Object.assign({ theme: savedTheme }, (options !== null && options !== void 0 ? options : {})));
}