summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/focus.ts
blob: ea6ee61c8838196abe8fed8f1323392bef5da0b2 (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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

export function focusPrev(el: Element | null, self = false, scroll = true) {
	if (el == null) return;
	if (!self) el = el.previousElementSibling;
	if (el) {
		if (el.hasAttribute('tabindex')) {
			(el as HTMLElement).focus({
				preventScroll: !scroll,
			});
		} else {
			focusPrev(el.previousElementSibling, true);
		}
	}
}

export function focusNext(el: Element | null, self = false, scroll = true) {
	if (el == null) return;
	if (!self) el = el.nextElementSibling;
	if (el) {
		if (el.hasAttribute('tabindex')) {
			(el as HTMLElement).focus({
				preventScroll: !scroll,
			});
		} else {
			focusPrev(el.nextElementSibling, true);
		}
	}
}