UNPKG

@kelvininc/ui-components

Version:
37 lines (36 loc) 1.64 kB
import { setMode } from "@stencil/core"; import { get } from "lodash-es"; import { StyleMode } from "../types"; import { DEFAULT_CONFIG } from "./config"; export const initialize = (userConfig = {}) => { var _a, _b; const defaultStyleMode = get(userConfig, 'styleMode', StyleMode.Night); const { document: doc, window: win } = window; doc.body.setAttribute('mode', defaultStyleMode); const isKvElement = (elm) => elm.tagName && elm.tagName.startsWith('KV-'); const isAllowedStyleModeValue = (elmMode) => Object.values(StyleMode).includes(elmMode); setMode((elm) => { while (elm) { const elmMode = elm.mode || elm.getAttribute('mode'); if (elmMode) { if (isAllowedStyleModeValue(elmMode)) { return elmMode; } else if (isKvElement(elm)) { console.warn(`Invalid kv-component mode: ${elmMode}, expected: ${Object.values(StyleMode).join(',')}`); } } // TODO: check with more time why cant obtain the parentElement // elm = elm.parentElement; elm = doc.body; } return defaultStyleMode; }); if (typeof win === 'undefined') { return; } const instance = (win.KvUiComponents = (_a = win.KvUiComponents) !== null && _a !== void 0 ? _a : {}); const actualConfig = (instance.config = (_b = instance.config) !== null && _b !== void 0 ? _b : {}); win.KvUiComponents.config = Object.assign(Object.assign(Object.assign({}, DEFAULT_CONFIG), actualConfig), userConfig); }; export default initialize;