schlechtenburg/packages/core/lib/use-activation.ts
2021-03-08 16:29:35 +01:00

30 lines
661 B
TypeScript

import {
Ref,
ref,
inject,
computed,
} from 'vue';
export const SymActiveBlock = Symbol('Schlechtenburg active block');
export function useActivation(currentBlockId: string|null = null) {
const activeBlockId: Ref<string|null> = inject(SymActiveBlock, ref(null));
const isActive = computed(() => activeBlockId.value === currentBlockId);
const activate = (id?: string|null) => {
activeBlockId.value = id !== undefined ? id : currentBlockId;
};
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
return {
activeBlockId,
isActive,
activate,
requestActivation,
};
}