123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- // Composables
- import { useToggleScope } from "./toggleScope.mjs"; // Utilities
- import { computed, inject, provide, ref, shallowRef, unref, watchEffect } from 'vue';
- import { getCurrentInstance, injectSelf, mergeDeep, toKebabCase } from "../util/index.mjs"; // Types
- export const DefaultsSymbol = Symbol.for('vuetify:defaults');
- export function createDefaults(options) {
- return ref(options);
- }
- export function injectDefaults() {
- const defaults = inject(DefaultsSymbol);
- if (!defaults) throw new Error('[Vuetify] Could not find defaults instance');
- return defaults;
- }
- export function provideDefaults(defaults, options) {
- const injectedDefaults = injectDefaults();
- const providedDefaults = ref(defaults);
- const newDefaults = computed(() => {
- const disabled = unref(options?.disabled);
- if (disabled) return injectedDefaults.value;
- const scoped = unref(options?.scoped);
- const reset = unref(options?.reset);
- const root = unref(options?.root);
- let properties = mergeDeep(providedDefaults.value, {
- prev: injectedDefaults.value
- });
- if (scoped) return properties;
- if (reset || root) {
- const len = Number(reset || Infinity);
- for (let i = 0; i <= len; i++) {
- if (!properties || !('prev' in properties)) {
- break;
- }
- properties = properties.prev;
- }
- if (properties && typeof root === 'string' && root in properties) {
- properties = mergeDeep(mergeDeep(properties, {
- prev: properties
- }), properties[root]);
- }
- return properties;
- }
- return properties.prev ? mergeDeep(properties.prev, properties) : properties;
- });
- provide(DefaultsSymbol, newDefaults);
- return newDefaults;
- }
- function propIsDefined(vnode, prop) {
- return typeof vnode.props?.[prop] !== 'undefined' || typeof vnode.props?.[toKebabCase(prop)] !== 'undefined';
- }
- export function internalUseDefaults() {
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- let name = arguments.length > 1 ? arguments[1] : undefined;
- let defaults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : injectDefaults();
- const vm = getCurrentInstance('useDefaults');
- name = name ?? vm.type.name ?? vm.type.__name;
- if (!name) {
- throw new Error('[Vuetify] Could not determine component name');
- }
- const componentDefaults = computed(() => defaults.value?.[props._as ?? name]);
- const _props = new Proxy(props, {
- get(target, prop) {
- const propValue = Reflect.get(target, prop);
- if (prop === 'class' || prop === 'style') {
- return [componentDefaults.value?.[prop], propValue].filter(v => v != null);
- } else if (typeof prop === 'string' && !propIsDefined(vm.vnode, prop)) {
- return componentDefaults.value?.[prop] ?? defaults.value?.global?.[prop] ?? propValue;
- }
- return propValue;
- }
- });
- const _subcomponentDefaults = shallowRef();
- watchEffect(() => {
- if (componentDefaults.value) {
- const subComponents = Object.entries(componentDefaults.value).filter(_ref => {
- let [key] = _ref;
- return key.startsWith(key[0].toUpperCase());
- });
- if (subComponents.length) _subcomponentDefaults.value = Object.fromEntries(subComponents);
- }
- });
- function provideSubDefaults() {
- // If subcomponent defaults are provided, override any
- // subcomponents provided by the component's setup function.
- // This uses injectSelf so must be done after the original setup to work.
- useToggleScope(_subcomponentDefaults, () => {
- provideDefaults(mergeDeep(injectSelf(DefaultsSymbol)?.value ?? {}, _subcomponentDefaults.value));
- });
- }
- return {
- props: _props,
- provideSubDefaults
- };
- }
- export function useDefaults() {
- let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- let name = arguments.length > 1 ? arguments[1] : undefined;
- const {
- props: _props,
- provideSubDefaults
- } = internalUseDefaults(props, name);
- provideSubDefaults();
- return _props;
- }
- //# sourceMappingURL=defaults.mjs.map
|