1 |
- {"version":3,"file":"defaults.mjs","names":["useToggleScope","computed","inject","provide","ref","shallowRef","unref","watchEffect","getCurrentInstance","injectSelf","mergeDeep","toKebabCase","DefaultsSymbol","Symbol","for","createDefaults","options","injectDefaults","defaults","Error","provideDefaults","injectedDefaults","providedDefaults","newDefaults","disabled","value","scoped","reset","root","properties","prev","len","Number","Infinity","i","propIsDefined","vnode","prop","props","internalUseDefaults","arguments","length","undefined","name","vm","type","__name","componentDefaults","_as","_props","Proxy","get","target","propValue","Reflect","filter","v","global","_subcomponentDefaults","subComponents","Object","entries","_ref","key","startsWith","toUpperCase","fromEntries","provideSubDefaults","useDefaults"],"sources":["../../src/composables/defaults.ts"],"sourcesContent":["// Composables\nimport { useToggleScope } from '@/composables/toggleScope'\n\n// Utilities\nimport { computed, inject, provide, ref, shallowRef, unref, watchEffect } from 'vue'\nimport { getCurrentInstance, injectSelf, mergeDeep, toKebabCase } from '@/util'\n\n// Types\nimport type { ComputedRef, InjectionKey, Ref, VNode } from 'vue'\nimport type { MaybeRef } from '@/util'\n\nexport type DefaultsInstance = undefined | {\n [key: string]: undefined | Record<string, unknown>\n global?: Record<string, unknown>\n}\n\nexport type DefaultsOptions = Partial<DefaultsInstance>\n\nexport const DefaultsSymbol: InjectionKey<Ref<DefaultsInstance>> = Symbol.for('vuetify:defaults')\n\nexport function createDefaults (options?: DefaultsInstance): Ref<DefaultsInstance> {\n return ref(options)\n}\n\nexport function injectDefaults () {\n const defaults = inject(DefaultsSymbol)\n\n if (!defaults) throw new Error('[Vuetify] Could not find defaults instance')\n\n return defaults\n}\n\nexport function provideDefaults (\n defaults?: MaybeRef<DefaultsInstance | undefined>,\n options?: {\n disabled?: MaybeRef<boolean | undefined>\n reset?: MaybeRef<number | string | undefined>\n root?: MaybeRef<boolean | string | undefined>\n scoped?: MaybeRef<boolean | undefined>\n }\n) {\n const injectedDefaults = injectDefaults()\n const providedDefaults = ref(defaults)\n\n const newDefaults = computed(() => {\n const disabled = unref(options?.disabled)\n\n if (disabled) return injectedDefaults.value\n\n const scoped = unref(options?.scoped)\n const reset = unref(options?.reset)\n const root = unref(options?.root)\n\n let properties = mergeDeep(providedDefaults.value, { prev: injectedDefaults.value })\n\n if (scoped) return properties\n\n if (reset || root) {\n const len = Number(reset || Infinity)\n\n for (let i = 0; i <= len; i++) {\n if (!properties || !('prev' in properties)) {\n break\n }\n\n properties = properties.prev\n }\n\n if (properties && typeof root === 'string' && root in properties) {\n properties = mergeDeep(mergeDeep(properties, { prev: properties }), properties[root])\n }\n\n return properties\n }\n\n return properties.prev\n ? mergeDeep(properties.prev, properties)\n : properties\n }) as ComputedRef<DefaultsInstance>\n\n provide(DefaultsSymbol, newDefaults)\n\n return newDefaults\n}\n\nfunction propIsDefined (vnode: VNode, prop: string) {\n return typeof vnode.props?.[prop] !== 'undefined' ||\n typeof vnode.props?.[toKebabCase(prop)] !== 'undefined'\n}\n\nexport function internalUseDefaults (\n props: Record<string, any> = {},\n name?: string,\n defaults = injectDefaults()\n) {\n const vm = getCurrentInstance('useDefaults')\n\n name = name ?? vm.type.name ?? vm.type.__name\n if (!name) {\n throw new Error('[Vuetify] Could not determine component name')\n }\n\n const componentDefaults = computed(() => defaults.value?.[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 componentDefaults.value?.[prop] ?? defaults.value?.global?.[prop] ?? propValue\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(([key]) => key.startsWith(key[0].toUpperCase()))\n if (subComponents.length) _subcomponentDefaults.value = Object.fromEntries(subComponents)\n }\n })\n\n function provideSubDefaults () {\n // If subcomponent defaults are provided, override any\n // subcomponents provided by the component's setup function.\n // This uses injectSelf so must be done after the original setup to work.\n useToggleScope(_subcomponentDefaults, () => {\n provideDefaults(mergeDeep(\n injectSelf(DefaultsSymbol)?.value ?? {},\n _subcomponentDefaults.value\n ))\n })\n }\n\n return { props: _props, provideSubDefaults }\n}\n\nexport function useDefaults<T extends Record<string, any>> (props: T, name?: string): T\nexport function useDefaults (props?: undefined, name?: string): Record<string, any>\nexport function useDefaults (\n props: Record<string, any> = {},\n name?: string,\n) {\n const { props: _props, provideSubDefaults } = internalUseDefaults(props, name)\n provideSubDefaults()\n return _props\n}\n"],"mappings":"AAAA;AAAA,SACSA,cAAc,6BAEvB;AACA,SAASC,QAAQ,EAAEC,MAAM,EAAEC,OAAO,EAAEC,GAAG,EAAEC,UAAU,EAAEC,KAAK,EAAEC,WAAW,QAAQ,KAAK;AAAA,SAC3EC,kBAAkB,EAAEC,UAAU,EAAEC,SAAS,EAAEC,WAAW,6BAE/D;AAWA,OAAO,MAAMC,cAAmD,GAAGC,MAAM,CAACC,GAAG,CAAC,kBAAkB,CAAC;AAEjG,OAAO,SAASC,cAAcA,CAAEC,OAA0B,EAAyB;EACjF,OAAOZ,GAAG,CAACY,OAAO,CAAC;AACrB;AAEA,OAAO,SAASC,cAAcA,CAAA,EAAI;EAChC,MAAMC,QAAQ,GAAGhB,MAAM,CAACU,cAAc,CAAC;EAEvC,IAAI,CAACM,QAAQ,EAAE,MAAM,IAAIC,KAAK,CAAC,4CAA4C,CAAC;EAE5E,OAAOD,QAAQ;AACjB;AAEA,OAAO,SAASE,eAAeA,CAC7BF,QAAiD,EACjDF,OAKC,EACD;EACA,MAAMK,gBAAgB,GAAGJ,cAAc,EAAE;EACzC,MAAMK,gBAAgB,GAAGlB,GAAG,CAACc,QAAQ,CAAC;EAEtC,MAAMK,WAAW,GAAGtB,QAAQ,CAAC,MAAM;IACjC,MAAMuB,QAAQ,GAAGlB,KAAK,CAACU,OAAO,EAAEQ,QAAQ,CAAC;IAEzC,IAAIA,QAAQ,EAAE,OAAOH,gBAAgB,CAACI,KA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