Skip to content

Commit

Permalink
focus first keyboard focusable element after scroll to top article
Browse files Browse the repository at this point in the history
  • Loading branch information
kabeaty committed Aug 28, 2023
1 parent b7eedaa commit 8d651bd
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
FC,
FunctionComponent,
useCallback,
useMemo,
} from "react";

import { useCoralContext } from "coral-framework/lib/bootstrap";
Expand Down Expand Up @@ -57,12 +58,37 @@ const CommentsLinks: FunctionComponent<Props> = ({
showGoToProfile,
}) => {
const { renderWindow, customScrollContainer } = useCoralContext();

// Find first keyboard focusable element for accessibility
const firstKeyboardFocusableElement = useMemo(() => {
const container = customScrollContainer ?? renderWindow.document;
const visibleFocusableElements: Element[] = [];
const focusableElements = container.querySelectorAll(
'a[href], button, input, textarea, select, details, [tabindex]:not([tabindex="-1"])'
);
focusableElements.forEach((el) => {
if (
!el.hasAttribute("disabled") &&
!el.getAttribute("aria-hidden") &&
!el.getAttribute("hidden")
) {
visibleFocusableElements.push(el);
}
});
return visibleFocusableElements[0];
}, [renderWindow, customScrollContainer]);

const root = useShadowRootOrDocument();
const onGoToArticleTop = useCallback(() => {
if (customScrollContainer) {
customScrollContainer.scrollTo({ top: 0 });
}
renderWindow.scrollTo({ top: 0 });
// programmatically apply focus to first keyboard focusable element
// after scroll for accessibility
if (firstKeyboardFocusableElement instanceof HTMLElement) {
firstKeyboardFocusableElement.focus();
}
}, [renderWindow, customScrollContainer]);
const onGoToCommentsTop = useCallback(() => {
scrollToBeginning(root, renderWindow, customScrollContainer);
Expand Down

0 comments on commit 8d651bd

Please sign in to comment.