UNPKG

bootstrap-vue-next

Version:

BootstrapVueNext is an early and lovely component library for Vue 3 & Nuxt 3 based on Bootstrap 5 and Typescript.

93 lines (92 loc) 3.49 kB
import { inject, ref, computed, shallowRef, watchEffect, getCurrentInstance as getCurrentInstance$1, provide } from "vue"; import { f as defaultsKey } from "./keys-DUCx0SNv.mjs"; function injectSelf(key, vm = getCurrentInstance("injectSelf")) { const { provides } = vm; if (provides && key in provides) { return provides[key]; } return void 0; } function getCurrentInstance(name, message) { const vm = getCurrentInstance$1(); if (!vm) { throw new Error(`[Bvn] ${name} ${"must be called from inside a setup function"}`); } return vm; } const toKebabCase = (str = "") => str.replace(/[^a-z]/gi, "-").replace(/\B([A-Z])/g, "-$1").toLowerCase(); const isObject = (obj) => obj !== null && typeof obj === "object" && !Array.isArray(obj); function mergeDeep(source = {}, target = {}, arrayFn) { const out = {}; for (const key in source) { out[key] = source[key]; } for (const key in target) { const sourceProperty = source[key]; const targetProperty = target[key]; if (isObject(sourceProperty) && isObject(targetProperty)) { out[key] = mergeDeep(sourceProperty, targetProperty); continue; } out[key] = targetProperty; } return out; } const propIsDefined = (vnode, prop) => { var _a, _b; return typeof ((_a = vnode.props) == null ? void 0 : _a[prop]) !== "undefined" || typeof ((_b = vnode.props) == null ? void 0 : _b[toKebabCase(prop)]) !== "undefined"; }; function internalUseDefaults(props = {}, name) { const defaults = inject(defaultsKey, ref({})); const vm = getCurrentInstance("useDefaults"); name = name ?? vm.type.name ?? vm.type.__name; if (!name) { throw new Error("[Bvn] Could not determine component name"); } const componentDefaults = computed(() => { var _a; return (_a = defaults.value) == null ? void 0 : _a[props._as ?? name]; }); const _props = new Proxy(props, { get(target, prop) { var _a, _b, _c, _d; const propValue = Reflect.get(target, prop); if (prop === "class" || prop === "style") { return [(_a = componentDefaults.value) == null ? void 0 : _a[prop], propValue].filter((v) => v != null); } else if (typeof prop === "string" && !propIsDefined(vm.vnode, prop)) { return ((_b = componentDefaults.value) == null ? void 0 : _b[prop]) ?? ((_d = (_c = defaults.value) == null ? void 0 : _c.global) == null ? void 0 : _d[prop]) ?? propValue; } return propValue; } }); const _subcomponentDefaults = shallowRef(); watchEffect(() => { if (componentDefaults.value) { const subComponents = Object.entries(componentDefaults.value).filter( ([key]) => key.startsWith(key[0].toUpperCase()) ); _subcomponentDefaults.value = subComponents.length ? Object.fromEntries(subComponents) : void 0; } else { _subcomponentDefaults.value = void 0; } }); function provideSubDefaults() { const injected = injectSelf(defaultsKey, vm); provide( defaultsKey, computed( () => _subcomponentDefaults.value ? mergeDeep((injected == null ? void 0 : injected.value) ?? {}, _subcomponentDefaults.value) : injected == null ? void 0 : injected.value ) ); } return { props: _props, provideSubDefaults }; } function useDefaults(props, name) { const { props: _props, provideSubDefaults } = internalUseDefaults(props, name); provideSubDefaults(); return _props; } export { useDefaults as u }; //# sourceMappingURL=useDefaults-CzkRF2AY.mjs.map