2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-07-12 02:25:44 -05:00
|
|
|
import { getScrollPosition, getScrollContainer, getStickyBottom, getStickyTop } from '@/scripts/scroll.js';
|
|
|
|
import { getElementOrNull, getNodeOrNull } from '@/scripts/get-dom-node-or-null.js';
|
|
|
|
|
|
|
|
type MaybeHTMLElement = EventTarget | Node | Element | HTMLElement;
|
|
|
|
|
|
|
|
export const isFocusable = (input: MaybeHTMLElement | null | undefined): input is HTMLElement => {
|
|
|
|
if (input == null || !(input instanceof HTMLElement)) return false;
|
|
|
|
|
|
|
|
if (input.tabIndex < 0) return false;
|
|
|
|
if ('disabled' in input && input.disabled === true) return false;
|
|
|
|
if ('readonly' in input && input.readonly === true) return false;
|
|
|
|
|
|
|
|
if (!input.ownerDocument.contains(input)) return false;
|
|
|
|
|
|
|
|
const style = window.getComputedStyle(input);
|
|
|
|
if (style.display === 'none') return false;
|
|
|
|
if (style.visibility === 'hidden') return false;
|
|
|
|
if (style.opacity === '0') return false;
|
|
|
|
if (style.pointerEvents === 'none') return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const focusPrev = (input: MaybeHTMLElement | null | undefined, self = false, scroll = true) => {
|
|
|
|
const element = self ? input : getElementOrNull(input)?.previousElementSibling;
|
|
|
|
if (element == null) return;
|
|
|
|
if (isFocusable(element)) {
|
|
|
|
focusOrScroll(element, scroll);
|
|
|
|
} else {
|
|
|
|
focusPrev(element, false, scroll);
|
2020-02-08 08:52:40 -06:00
|
|
|
}
|
2024-07-12 02:25:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const focusNext = (input: MaybeHTMLElement | null | undefined, self = false, scroll = true) => {
|
|
|
|
const element = self ? input : getElementOrNull(input)?.nextElementSibling;
|
|
|
|
if (element == null) return;
|
|
|
|
if (isFocusable(element)) {
|
|
|
|
focusOrScroll(element, scroll);
|
|
|
|
} else {
|
|
|
|
focusNext(element, false, scroll);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const focusParent = (input: MaybeHTMLElement | null | undefined, self = false, scroll = true) => {
|
|
|
|
const element = self ? input : getNodeOrNull(input)?.parentElement;
|
|
|
|
if (element == null) return;
|
|
|
|
if (isFocusable(element)) {
|
|
|
|
focusOrScroll(element, scroll);
|
|
|
|
} else {
|
|
|
|
focusParent(element, false, scroll);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const focusOrScroll = (element: HTMLElement, scroll: boolean) => {
|
|
|
|
if (scroll) {
|
|
|
|
const scrollContainer = getScrollContainer(element) ?? document.documentElement;
|
|
|
|
const scrollContainerTop = getScrollPosition(scrollContainer);
|
|
|
|
const stickyTop = getStickyTop(element, scrollContainer);
|
|
|
|
const stickyBottom = getStickyBottom(element, scrollContainer);
|
|
|
|
const top = element.getBoundingClientRect().top;
|
|
|
|
const bottom = element.getBoundingClientRect().bottom;
|
|
|
|
|
|
|
|
let scrollTo = scrollContainerTop;
|
|
|
|
if (top < stickyTop) {
|
|
|
|
scrollTo += top - stickyTop;
|
|
|
|
} else if (bottom > window.innerHeight - stickyBottom) {
|
|
|
|
scrollTo += bottom - window.innerHeight + stickyBottom;
|
2020-02-08 08:52:40 -06:00
|
|
|
}
|
2024-07-12 02:25:44 -05:00
|
|
|
scrollContainer.scrollTo({ top: scrollTo, behavior: 'instant' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.activeElement !== element) {
|
|
|
|
element.focus({ preventScroll: true });
|
2020-02-08 08:52:40 -06:00
|
|
|
}
|
2024-07-12 02:25:44 -05:00
|
|
|
};
|