Skip to content

Commit

Permalink
Merge pull request #55 from alexstine/fix/announcer-page-focus
Browse files Browse the repository at this point in the history
Try fixing focus loss on pages by placing focus on announcer
  • Loading branch information
azdak authored Aug 15, 2024
2 parents ea2294d + a111625 commit d47913b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/components/layout/announcer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { useLocation, matchPath } from 'react-router-dom';

const routeNames: Record<string, string> = {
Expand Down Expand Up @@ -33,20 +33,21 @@ const getPageName = (pathname: string) => {

const Announcer: React.FC = () => {
const location = useLocation();
const announcer = useRef<HTMLDivElement>(null);

useEffect(() => {
const pageName = getPageName(location.pathname);
const announcer = document.getElementById('page-announcer');
if (announcer) {
announcer.textContent = `${pageName} page loaded`;
if (announcer.current) {
announcer.current.textContent = `${pageName} page loaded`;
announcer.current.focus({ preventScroll: true });
}
}, [location]);

return (
<div
ref={announcer}
tabIndex={-1}
id="page-announcer"
aria-live="assertive"
aria-atomic="true"
className="sr-only"
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Header = () => {
<header className="border-b border-gray-300 bg-white">
<MaxWidthWrapper>
<div className="flex flex-col items-center justify-between gap-3 py-3 md:h-16 md:flex-row">
<Link to="/reports" replace aria-label="Go to homepage" rel="home">
<Link id="header-home-link-focus" to="/reports" replace aria-label="Go to homepage" rel="home">
<img
src="/equalify.svg"
className="h-auto w-28 md:w-32"
Expand Down

0 comments on commit d47913b

Please sign in to comment.