summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/popup-position.ts
blob: be49532cf8e302d47a324bfbedc45cc4e8ffe5ca (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

export function calcPopupPosition(el: HTMLElement, props: {
	anchorElement?: HTMLElement | null;
	innerMargin: number;
	direction: 'top' | 'bottom' | 'left' | 'right';
	align: 'top' | 'bottom' | 'left' | 'right' | 'center';
	alignOffset?: number;
	x?: number;
	y?: number;
}): { top: number; left: number; transformOrigin: string; } {
	const contentWidth = el.offsetWidth;
	const contentHeight = el.offsetHeight;

	const HORIZONTAL_MARGIN = 1;

	let rect: DOMRect;

	if (props.anchorElement) {
		rect = props.anchorElement.getBoundingClientRect();
	}

	const calcPosWhenTop = () => {
		let left: number;
		let top: number;

		if (props.anchorElement) {
			left = rect.left + window.scrollX + (props.anchorElement.offsetWidth / 2);
			top = (rect.top + window.scrollY - contentHeight) - props.innerMargin;
		} else {
			left = props.x;
			top = (props.y - contentHeight) - props.innerMargin;
		}

		left -= (el.offsetWidth / 2);

		if (left + contentWidth - window.scrollX > window.innerWidth) {
			left = window.innerWidth - contentWidth + window.scrollX - HORIZONTAL_MARGIN;
		}

		left = Math.max(HORIZONTAL_MARGIN, left);

		return [left, top];
	};

	const calcPosWhenBottom = () => {
		let left: number;
		let top: number;

		if (props.anchorElement) {
			left = rect.left + window.scrollX + (props.anchorElement.offsetWidth / 2);
			top = (rect.top + window.scrollY + props.anchorElement.offsetHeight) + props.innerMargin;
		} else {
			left = props.x;
			top = (props.y) + props.innerMargin;
		}

		left -= (el.offsetWidth / 2);

		if (left + contentWidth - window.scrollX > window.innerWidth) {
			left = window.innerWidth - contentWidth + window.scrollX - HORIZONTAL_MARGIN;
		}

		left = Math.max(HORIZONTAL_MARGIN, left);

		return [left, top];
	};

	const calcPosWhenLeft = () => {
		let left: number;
		let top: number;

		if (props.anchorElement) {
			left = (rect.left + window.scrollX - contentWidth) - props.innerMargin;
			top = rect.top + window.scrollY + (props.anchorElement.offsetHeight / 2);
		} else {
			left = (props.x - contentWidth) - props.innerMargin;
			top = props.y;
		}

		left = Math.max(HORIZONTAL_MARGIN, left);

		top -= (el.offsetHeight / 2);

		if (top + contentHeight - window.scrollY > window.innerHeight) {
			top = window.innerHeight - contentHeight + window.scrollY - 1;
		}

		return [left, top];
	};

	const calcPosWhenRight = () => {
		let left: number;
		let top: number;

		if (props.anchorElement) {
			left = (rect.left + props.anchorElement.offsetWidth + window.scrollX) + props.innerMargin;

			if (props.align === 'top') {
				top = rect.top + window.scrollY;
				if (props.alignOffset != null) top += props.alignOffset;
			} else if (props.align === 'bottom') {
				// TODO
			} else { // center
				top = rect.top + window.scrollY + (props.anchorElement.offsetHeight / 2);
				top -= (el.offsetHeight / 2);
			}
		} else {
			left = props.x + props.innerMargin;
			top = props.y;
			top -= (el.offsetHeight / 2);
		}

		left = Math.max(HORIZONTAL_MARGIN, left);

		if (top + contentHeight - window.scrollY > window.innerHeight) {
			top = window.innerHeight - contentHeight + window.scrollY - 1;
		}

		return [left, top];
	};

	const calc = (): {
		left: number;
		top: number;
		transformOrigin: string;
	} => {
		switch (props.direction) {
			case 'top': {
				const [left, top] = calcPosWhenTop();

				// ツールチップを上に向かって表示するスペースがなければ下に向かって出す
				if (top - window.scrollY < 0) {
					const [left, top] = calcPosWhenBottom();
					return { left, top, transformOrigin: 'center top' };
				}

				return { left, top, transformOrigin: 'center bottom' };
			}

			case 'bottom': {
				const [left, top] = calcPosWhenBottom();
				// TODO: ツールチップを下に向かって表示するスペースがなければ上に向かって出す
				return { left, top, transformOrigin: 'center top' };
			}

			case 'left': {
				const [left, top] = calcPosWhenLeft();

				// ツールチップを左に向かって表示するスペースがなければ右に向かって出す
				if (left - window.scrollX < 0) {
					const [left, top] = calcPosWhenRight();
					return { left, top, transformOrigin: 'left center' };
				}

				return { left, top, transformOrigin: 'right center' };
			}

			case 'right': {
				const [left, top] = calcPosWhenRight();
				// TODO: ツールチップを右に向かって表示するスペースがなければ左に向かって出す
				return { left, top, transformOrigin: 'left center' };
			}
		}
	};

	return calc();
}