UNPKG

bootstrap-vue-next

Version:

Seamless integration of Vue 3, Bootstrap 5, and TypeScript for modern, type-safe UI development

1 lines 3.72 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../src/composables/useProvideDefaults/index.ts"],"sourcesContent":["import {computed, inject, provide, type Ref, unref} from 'vue'\nimport {defaultsKey} from '../../utils/keys'\nimport type {BvnComponentProps} from '../../types/BootstrapVueOptions'\nimport type {BAppProps} from '../../types/ComponentProps'\n\nexport const useProvideDefaults = (\n defaults: BAppProps['defaults'] | Ref<BAppProps['defaults']>,\n mergeDefaults: BAppProps['mergeDefaults']\n) => {\n // Inject existing defaults from parent (could be from plugins)\n const injectedDefaults = inject(defaultsKey, null)\n\n // Merge injected defaults with prop defaults (prop takes priority)\n const mergedDefaults = computed<Partial<BvnComponentProps>>(() => {\n const _defaults = unref(defaults)\n if (!injectedDefaults) {\n return _defaults ?? {}\n }\n const merged = {...injectedDefaults.value} as Partial<BvnComponentProps>\n\n if (_defaults) {\n // If mergeDefaults is a function, call it with\n if (mergeDefaults) {\n if (typeof mergeDefaults === 'function') {\n return mergeDefaults(merged, _defaults) as Partial<BvnComponentProps>\n } else if (mergeDefaults === true) {\n return deepMerge(merged, _defaults)\n }\n return Object.assign(merged, _defaults)\n }\n return _defaults\n }\n\n return merged\n })\n\n // Provide the merged defaults to child components\n provide(defaultsKey, mergedDefaults)\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n return (\n value !== null &&\n typeof value === 'object' &&\n value.constructor === Object &&\n Object.prototype.toString.call(value) === '[object Object]'\n )\n}\n\nfunction deepMerge<T extends Record<string, unknown>, S extends Record<string, unknown>>(\n target: T,\n source: S,\n visited = new WeakSet()\n): T & S {\n // Handle circular references\n if (visited.has(source)) {\n return target as T & S\n }\n visited.add(source)\n\n // Create a new object to avoid mutating the target\n const result: Record<string, unknown> = {...target}\n\n for (const key in source) {\n const sourceValue = source[key]\n const targetValue = target[key]\n\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n // Recursively merge plain objects\n result[key] = deepMerge(\n targetValue as Record<string, unknown>,\n sourceValue as Record<string, unknown>,\n visited\n )\n } else if (Array.isArray(sourceValue)) {\n // Copy arrays to avoid mutation\n result[key] = [...sourceValue]\n } else {\n // Replace value for all other types (primitives, class instances, Vue refs, etc.)\n result[key] = sourceValue\n }\n }\n\n return result as T & S\n}\n"],"mappings":";;;AAKA,IAAa,sBACX,UACA,kBACG;CAEH,MAAM,mBAAmB,OAAO,aAAa,KAAK;AA2BlD,SAAQ,aAxBe,eAA2C;EAChE,MAAM,YAAY,MAAM,SAAS;AACjC,MAAI,CAAC,iBACH,QAAO,aAAa,EAAE;EAExB,MAAM,SAAS,EAAC,GAAG,iBAAiB,OAAM;AAE1C,MAAI,WAAW;AAEb,OAAI,eAAe;AACjB,QAAI,OAAO,kBAAkB,WAC3B,QAAO,cAAc,QAAQ,UAAU;aAC9B,kBAAkB,KAC3B,QAAO,UAAU,QAAQ,UAAU;AAErC,WAAO,OAAO,OAAO,QAAQ,UAAU;;AAEzC,UAAO;;AAGT,SAAO;GACP,CAGkC;;AAGtC,SAAS,cAAc,OAAkD;AACvE,QACE,UAAU,QACV,OAAO,UAAU,YACjB,MAAM,gBAAgB,UACtB,OAAO,UAAU,SAAS,KAAK,MAAM,KAAK;;AAI9C,SAAS,UACP,QACA,QACA,0BAAU,IAAI,SAAS,EAChB;AAEP,KAAI,QAAQ,IAAI,OAAO,CACrB,QAAO;AAET,SAAQ,IAAI,OAAO;CAGnB,MAAM,SAAkC,EAAC,GAAG,QAAO;AAEnD,MAAK,MAAM,OAAO,QAAQ;EACxB,MAAM,cAAc,OAAO;EAC3B,MAAM,cAAc,OAAO;AAE3B,MAAI,cAAc,YAAY,IAAI,cAAc,YAAY,CAE1D,QAAO,OAAO,UACZ,aACA,aACA,QACD;WACQ,MAAM,QAAQ,YAAY,CAEnC,QAAO,OAAO,CAAC,GAAG,YAAY;MAG9B,QAAO,OAAO;;AAIlB,QAAO"}