2022-11-14 03:59:26 +00:00
|
|
|
import { inject, computed, isRef, provide, reactive } from "vue";
|
|
|
|
const LevelInjectionKey = Symbol("menuLevelKey");
|
|
|
|
function provideLevel(level) {
|
|
|
|
const computedLevel = computed(() => isRef(level) ? level.value : level);
|
2024-09-24 09:04:44 +00:00
|
|
|
provide(
|
|
|
|
LevelInjectionKey,
|
|
|
|
reactive({
|
|
|
|
level: computedLevel
|
|
|
|
})
|
|
|
|
);
|
2022-11-14 03:59:26 +00:00
|
|
|
}
|
|
|
|
function useLevel(props) {
|
|
|
|
const { provideNextLevel } = props || {};
|
|
|
|
const levelContext = inject(LevelInjectionKey);
|
|
|
|
const level = computed(() => levelContext.level || 1);
|
|
|
|
if (provideNextLevel) {
|
|
|
|
const nextLevel = computed(() => level.value + 1);
|
|
|
|
provideLevel(nextLevel);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
level
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export { provideLevel as p, useLevel as u };
|