bootstrap-vue-next
Version:
Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development
1 lines • 6.7 kB
Source Map (JSON)
{"version":3,"file":"useDefaults-CCWS15M8.mjs","names":[],"sources":["../src/composables/useDefaults.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n// Utilities\nimport {\n getCurrentInstance as _getCurrentInstance,\n type ComponentInternalInstance,\n computed,\n inject,\n type InjectionKey,\n provide,\n ref,\n shallowRef,\n type VNode,\n watchEffect,\n} from 'vue'\nimport {defaultsKey} from '../utils/keys'\nimport type {BvnComponentProps} from '../types/BootstrapVueOptions'\n\n// Code here was taken from\n// https://github.com/vuetifyjs/vuetify/blob/8ed87310890e2b6c8ad2a626a02c17f9467cbb60/packages/vuetify/src/composables/defaults.ts#L138\n// Thanks to the Vuetify team. Vue does not make this easy\n\nfunction injectSelf<T>(key: InjectionKey<T> | string, vm?: ComponentInternalInstance): T | undefined\nfunction injectSelf(key: InjectionKey<any> | string, vm = getCurrentInstance('injectSelf')) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n const {provides} = vm\n\n if (provides && (key as string | symbol) in provides) {\n // TS doesn't allow symbol as index type\n return provides[key as string]\n }\n return undefined\n}\n\nfunction getCurrentInstance(name: string, message?: string) {\n const vm = _getCurrentInstance()\n\n if (!vm) {\n throw new Error(`[Bvn] ${name} ${message || 'must be called from inside a setup function'}`)\n }\n\n return vm\n}\n\nconst toKebabCase = (str = '') =>\n str\n .replace(/[^a-z]/gi, '-')\n .replace(/\\B([A-Z])/g, '-$1')\n .toLowerCase()\nconst isObject = (obj: unknown): obj is Record<string, any> =>\n obj !== null && typeof obj === 'object' && !Array.isArray(obj)\n\nfunction mergeDeep(\n source: Record<string, any> = {},\n target: Record<string, any> = {},\n arrayFn?: (a: unknown[], b: unknown[]) => unknown[]\n) {\n const out: Record<string, any> = {}\n\n for (const key in source) {\n out[key] = source[key]\n }\n\n for (const key in target) {\n const sourceProperty = source[key]\n const targetProperty = target[key]\n\n // Only continue deep merging if\n // both properties are objects\n if (isObject(sourceProperty) && isObject(targetProperty)) {\n out[key] = mergeDeep(sourceProperty, targetProperty, arrayFn)\n\n continue\n }\n\n if (Array.isArray(sourceProperty) && Array.isArray(targetProperty) && arrayFn) {\n out[key] = arrayFn(sourceProperty, targetProperty)\n\n continue\n }\n\n out[key] = targetProperty\n }\n\n return out\n}\n\nconst propIsDefined = (vnode: VNode, prop: string) =>\n typeof vnode.props?.[prop] !== 'undefined' ||\n typeof vnode.props?.[toKebabCase(prop)] !== 'undefined'\n\nfunction internalUseDefaults(props: Record<string, any> = {}, name?: string) {\n const defaults = inject(defaultsKey, ref({}))\n const vm = getCurrentInstance('useDefaults')\n\n name = name ?? vm.type.name ?? vm.type.__name\n if (!name) {\n // Should never happen\n throw new Error('[Bvn] Could not determine component name')\n }\n\n const componentDefaults = computed(() => (defaults.value as any)?.[props._as ?? name])\n const _props = new Proxy(props, {\n get(target, prop) {\n const propValue = Reflect.get(target, prop)\n if (prop === 'class' || prop === 'style') {\n return [componentDefaults.value?.[prop], propValue].filter((v) => v != null)\n } else if (typeof prop === 'string' && !propIsDefined(vm.vnode, prop)) {\n return (\n componentDefaults.value?.[prop] ?? (defaults.value as any)?.global?.[prop] ?? propValue\n )\n }\n return propValue\n },\n })\n\n const _subcomponentDefaults = shallowRef()\n watchEffect(() => {\n if (componentDefaults.value) {\n const subComponents = Object.entries(componentDefaults.value).filter(\n ([key]) => key[0] !== undefined && key.startsWith(key[0].toUpperCase())\n )\n _subcomponentDefaults.value = subComponents.length\n ? Object.fromEntries(subComponents)\n : undefined\n } else {\n _subcomponentDefaults.value = undefined\n }\n })\n\n function provideSubDefaults() {\n const injected = injectSelf(defaultsKey, vm)\n provide(\n defaultsKey,\n computed(() =>\n _subcomponentDefaults.value\n ? mergeDeep(injected?.value ?? {}, _subcomponentDefaults.value)\n : injected?.value\n ) as any\n )\n }\n\n return {props: _props, provideSubDefaults}\n}\n\nexport function useDefaults<T extends Record<string, any>>(\n props: T,\n name: keyof BvnComponentProps\n): T {\n const {props: _props, provideSubDefaults} = internalUseDefaults(props, name)\n provideSubDefaults()\n return _props as T\n}\n"],"mappings":";;;AAsBA,SAAS,WAAW,KAAiC,KAAK,qBAAmB,aAAa,EAAE;CAG1F,MAAM,EAAC,aAAY;AAEnB,KAAI,YAAa,OAA2B,SAE1C,QAAO,SAAS;;AAKpB,SAAS,qBAAmB,MAAc,SAAkB;CAC1D,MAAM,KAAK,oBAAqB;AAEhC,KAAI,CAAC,GACH,OAAM,IAAI,MAAM,SAAS,KAAK,GAAG,WAAW,gDAAgD;AAG9F,QAAO;;AAGT,IAAM,eAAe,MAAM,OACzB,IACG,QAAQ,YAAY,IAAI,CACxB,QAAQ,cAAc,MAAM,CAC5B,aAAa;AAClB,IAAM,YAAY,QAChB,QAAQ,QAAQ,OAAO,QAAQ,YAAY,CAAC,MAAM,QAAQ,IAAI;AAEhE,SAAS,UACP,SAA8B,EAAE,EAChC,SAA8B,EAAE,EAChC,SACA;CACA,MAAM,MAA2B,EAAE;AAEnC,MAAK,MAAM,OAAO,OAChB,KAAI,OAAO,OAAO;AAGpB,MAAK,MAAM,OAAO,QAAQ;EACxB,MAAM,iBAAiB,OAAO;EAC9B,MAAM,iBAAiB,OAAO;AAI9B,MAAI,SAAS,eAAe,IAAI,SAAS,eAAe,EAAE;AACxD,OAAI,OAAO,UAAU,gBAAgB,gBAAgB,QAAQ;AAE7D;;AAGF,MAAI,MAAM,QAAQ,eAAe,IAAI,MAAM,QAAQ,eAAe,IAAI,SAAS;AAC7E,OAAI,OAAO,QAAQ,gBAAgB,eAAe;AAElD;;AAGF,MAAI,OAAO;;AAGb,QAAO;;AAGT,IAAM,iBAAiB,OAAc,SACnC,OAAO,MAAM,QAAQ,UAAU,eAC/B,OAAO,MAAM,QAAQ,YAAY,KAAK,MAAM;AAE9C,SAAS,oBAAoB,QAA6B,EAAE,EAAE,MAAe;CAC3E,MAAM,WAAW,O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