ant-design-vue/components/_util/hooks/useSize.ts

27 lines
823 B
TypeScript
Raw Normal View History

2021-05-13 04:57:42 +00:00
import { computed, ComputedRef, inject, provide, UnwrapRef } from 'vue';
import { ConfigProviderProps, defaultConfigProvider, SizeType } from '../../config-provider';
const sizeProvider = Symbol('SizeProvider');
2021-05-13 05:33:30 +00:00
const useProvideSize = <T = SizeType>(props: Record<any, any>): ComputedRef<T> => {
2021-05-13 04:57:42 +00:00
const configProvider = inject<UnwrapRef<ConfigProviderProps>>(
'configProvider',
defaultConfigProvider,
);
const size = computed<T>(() => props.size || configProvider.componentSize);
provide(sizeProvider, size);
return size;
};
const useInjectSize = <T = SizeType>(): ComputedRef<T> => {
const size: ComputedRef<T> = inject(
sizeProvider,
computed(() => ('default' as unknown) as T),
);
return size;
};
2021-05-13 05:33:30 +00:00
export { useInjectSize, sizeProvider, useProvideSize };
2021-05-13 04:57:42 +00:00
2021-05-13 05:33:30 +00:00
export default useProvideSize;