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

30 lines
644 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');
2021-03-07 17:47:28 +00:00
export function useActivation(currentBlockId?: string) {
2020-12-30 01:32:46 +00:00
const activeBlockId: Ref<string|null> = inject(ActiveBlock, ref(null));
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,
};
}