feat: bubble组件封装
This commit is contained in:
45
src/components/xt-chat/Bubble/context.ts
Normal file
45
src/components/xt-chat/Bubble/context.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* @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;
|
||||
Reference in New Issue
Block a user