UNPKG

@bookbox/view-html

Version:

Bookbox view for html

36 lines (35 loc) 1.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setSavedTheme = exports.setTheme = exports.THEME_STORE_KEY = void 0; exports.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'; 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); } } exports.setTheme = setTheme; /** * set theme from localStorage */ function setSavedTheme(options) { const { storageKey } = options !== null && options !== void 0 ? options : {}; const savedTheme = localStorage.getItem(storageKey !== null && storageKey !== void 0 ? storageKey : exports.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 : {}))); } exports.setSavedTheme = setSavedTheme;