45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
|
|
/*
|
||
|
|
* @Author: RenXiaoDong
|
||
|
|
* @Date: 2025-08-20 23:17:49
|
||
|
|
*/
|
||
|
|
import { computed, defineComponent, inject, provide, shallowRef, triggerRef, unref, watch } from "vue";
|
||
|
|
import type { ComputedRef, InjectionKey } from "vue";
|
||
|
|
import { objectType } from "@/utils/type";
|
||
|
|
import type { BubbleContextProps } from "./types";
|
||
|
|
|
||
|
|
const BubbleContextKey: InjectionKey<ComputedRef<BubbleContextProps>> =
|
||
|
|
Symbol('BubbleContext');
|
||
|
|
|
||
|
|
export const globalBubbleContextApi = shallowRef<BubbleContextProps>();
|
||
|
|
|
||
|
|
export const useBubbleContextProvider = (value: ComputedRef<BubbleContextProps>) => {
|
||
|
|
provide(BubbleContextKey, value);
|
||
|
|
watch(
|
||
|
|
value,
|
||
|
|
() => {
|
||
|
|
globalBubbleContextApi.value = unref(value);
|
||
|
|
triggerRef(globalBubbleContextApi);
|
||
|
|
},
|
||
|
|
{ immediate: true, deep: true },
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const useBubbleContextInject = () => {
|
||
|
|
return inject(
|
||
|
|
BubbleContextKey,
|
||
|
|
computed(() => globalBubbleContextApi.value || {}),
|
||
|
|
);
|
||
|
|
};
|
||
|
|
export const BubbleContextProvider = defineComponent({
|
||
|
|
props: {
|
||
|
|
value: objectType<BubbleContextProps>(),
|
||
|
|
},
|
||
|
|
setup(props, { slots }) {
|
||
|
|
useBubbleContextProvider(computed(() => props.value));
|
||
|
|
return () => {
|
||
|
|
return slots.default?.();
|
||
|
|
};
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
export default BubbleContextProvider;
|