385969e9f5
* fix(frontend): 直前のパターンを記録するように * fix(frontend): フォーカス/タブ移動に関する挙動を調整 (#226) Cherry-pick commit e8c030673326871edf3623cf2b8675d68f9e1b13 Co-authored-by: taiyme <53635909+taiyme@users.noreply.github.com> * focusのデザイン修正 * move scripts * Modalにfocus trapを追加 * 記録するホットキーはレートリミット式にする * escキーのハンドリングをMkModalに統一 * fix * enterで子メニューを開けるように * lint * fix focus trap * improve switch accessibility * 一部のmodalのフォーカストラップが外れない問題を修正 * fix * fix * Revert "記録するホットキーはレートリミット式にする" This reverts commit 40a7509286a87911ad4cc06d9482e8a2e5d0e7e8. * Revert "fix(frontend): 直前のパターンを記録するように" This reverts commit 5372b2594023952cff34aa62253ed4efef15b5dd. * Revert "Revert "fix(frontend): 直前のパターンを記録するように"" This reverts commit a9bb52e799e110927ad92cd8f26af980819334e1. * Revert "Revert "記録するホットキーはレートリミット式にする"" This reverts commit bdac34273e0bc5f13604c7e2f9fa6b1321a0df3d. * 試験的にCypressでのFocustrapを無効化 * fix * fix focus-trap * Update Changelog * ✌️ * fix focustrap invocation logic * スクロールがsticky headerを考慮するように * 🎨 * スタイルの微調整 * 🎨 * remove deprecated key aliases * focusElementが足りなかったので修正 * preview系にfocus時スタイルが足りなかったので修正 * `returnFocusElement` -> `returnFocusTo` * lint * Update packages/frontend/src/components/MkModalWindow.vue * Apply suggestions from code review Co-authored-by: taiy <53635909+taiyme@users.noreply.github.com> * keydownイベントをまとめる * use correct pesudo-element selector * fix * rename --------- Co-authored-by: taiyme <53635909+taiyme@users.noreply.github.com> Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
80 lines
2.8 KiB
TypeScript
80 lines
2.8 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
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;
|
|
}
|
|
scrollContainer.scrollTo({ top: scrollTo, behavior: 'instant' });
|
|
}
|
|
|
|
if (document.activeElement !== element) {
|
|
element.focus({ preventScroll: true });
|
|
}
|
|
};
|