summaryrefslogtreecommitdiff
path: root/src/client/directives/follow-append.ts
blob: 26f9e9f82b0e25db2a1bd181a5e9e6c07d560733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Directive } from 'vue';
import { getScrollContainer, getScrollPosition } from '@/scripts/scroll';

export default {
	mounted(src, binding, vn) {
		const ro = new ResizeObserver((entries, observer) => {
			const pos = getScrollPosition(src);
			const container = getScrollContainer(src);
			const viewHeight = container.clientHeight;
			const height = container.scrollHeight;
			if (pos + viewHeight > height - 32) {
				container.scrollTop = height;
			}
		});

		ro.observe(src);

		// TODO: 新たにプロパティを作るのをやめMapを使う
		src._ro_ = ro;
	},

	unmounted(src, binding, vn) {
		src._ro_.unobserve(src);
	}
} as Directive;