summaryrefslogtreecommitdiff
path: root/packages/frontend/src/use/use-mutation-observer.ts
blob: 7b774022dc7a80c7b003515c16c568d6feb89941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { onUnmounted, watch } from 'vue';
import type { Ref } from 'vue';

export function useMutationObserver(targetNodeRef: Ref<HTMLElement | null | undefined>, options: MutationObserverInit, callback: MutationCallback): void {
	const observer = new MutationObserver(callback);

	watch(targetNodeRef, (targetNode) => {
		if (targetNode) {
			observer.observe(targetNode, options);
		}
	}, { immediate: true });

	onUnmounted(() => {
		observer.disconnect();
	});
}