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

30 lines
661 B
TypeScript
Raw Normal View History

2020-12-30 01:32:46 +00:00
import {
Ref,
ref,
inject,
computed,
} from 'vue';
2021-03-08 15:29:35 +00:00
export const SymActiveBlock = Symbol('Schlechtenburg active block');
export function useActivation(currentBlockId: string|null = null) {
const activeBlockId: Ref<string|null> = inject(SymActiveBlock, ref(null));
2020-12-30 01:32:46 +00:00
const isActive = computed(() => activeBlockId.value === currentBlockId);
2021-03-07 17:47:28 +00:00
const activate = (id?: string|null) => {
activeBlockId.value = id !== undefined ? id : currentBlockId;
2020-12-30 01:32:46 +00:00
};
const requestActivation = () => {
if (activeBlockId.value) {
return;
}
activate();
};
return {
2021-03-07 17:47:28 +00:00
activeBlockId,
2020-12-30 01:32:46 +00:00
isActive,
activate,
requestActivation,
};
}