vue-admin-core
Version:
A Component Library for Vue 3
42 lines (39 loc) • 1.15 kB
JavaScript
import { defineComponent, toRef, provide, readonly, inject, ref } from 'vue';
const createContext = (defaultValue) => {
const injectKey = Symbol();
return {
Provider: defineComponent({
name: "ContextProvider",
props: {
value: {
type: [Object, Array, String, Number, Boolean]
}
},
setup(props, { slots }) {
const value = toRef(props, "value", defaultValue);
provide(injectKey, readonly(value));
return () => {
var _a;
return (_a = slots == null ? void 0 : slots.default) == null ? void 0 : _a.call(slots);
};
}
}),
Consumer: defineComponent({
name: "ContextConsumer",
setup(_props, { slots }) {
const value = inject(injectKey);
return () => {
var _a;
return (_a = slots == null ? void 0 : slots.default) == null ? void 0 : _a.call(slots, value);
};
}
}),
injectKey
};
};
const useContext = (context) => {
const key = context.injectKey;
return inject(key, ref(null));
};
export { createContext, useContext };
//# sourceMappingURL=create-context.mjs.map