schlechtenburg/packages/core/lib/use-activation.ts

29 lines
639 B
TypeScript
Raw Normal View History

2020-12-30 01:32:46 +00:00
import {
Ref,
ref,
inject,
computed,
} from 'vue';
export const ActiveBlock = Symbol('Schlechtenburg active block');
export function useActivation(currentBlockId: string) {
const activeBlockId: Ref<string|null> = inject(ActiveBlock, ref(null));
const isActive = computed(() => activeBlockId.value === currentBlockId);
const activate = (blockId?: string|null) => {
activeBlockId.value = blockId !== undefined ? blockId : currentBlockId;
};
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
return {
isActive,
activate,
requestActivation,
};
}