Skip to content

Commit ded314f

Browse files
committed
Add page title change announcer for screen readers
[DISCO-181]
1 parent dee3a2b commit ded314f

File tree

5 files changed

+241
-141
lines changed

5 files changed

+241
-141
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
3+
export default function announcePageTitle(title: string) {
4+
const el = document.getElementById('page-title-confirmation');
5+
6+
el?.dispatchEvent(new window.CustomEvent('page-title', {
7+
detail: title
8+
}));
9+
}
10+
11+
export function PageTitleConfirmation() {
12+
const [docTitle, setDocTitle] = React.useState('');
13+
const ref = React.useRef(null);
14+
15+
React.useEffect(
16+
() => {
17+
const el = ref.current as unknown as Element; // it's not null
18+
const listener = ((e: CustomEvent) => {
19+
setDocTitle(e.detail);
20+
}) as EventListener;
21+
22+
el.addEventListener('page-title', listener);
23+
return () => el.removeEventListener('page-title', listener);
24+
},
25+
[]
26+
);
27+
28+
return (
29+
<div id="page-title-confirmation" ref={ref} className="sr-only" aria-live="polite">
30+
{`Loaded page "${docTitle}"`}
31+
</div>
32+
);
33+
}

src/app/components/shell/header/header.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import JITLoad from '~/helpers/jit-load';
33
import {useStickyData} from '../shared.js';
44
import Menus from './menus/menus';
55
import $ from '~/helpers/$';
6+
import { PageTitleConfirmation } from './announce-page-title';
67
import './header.scss';
78

89
function doSkipToContent(event) {
910
event.preventDefault();
1011
const mainEl = document.getElementById('main');
11-
const focusableItems = Array.from(mainEl.querySelectorAll($.focusable));
12-
13-
if (focusableItems.length > 0) {
14-
const target = focusableItems[0];
12+
const target = mainEl.querySelector($.focusable);
1513

14+
if (target) {
1615
$.scrollTo(target);
1716
target.focus();
1817
}
@@ -24,12 +23,14 @@ function SkipToContent() {
2423
);
2524
}
2625

26+
2727
export default function Header() {
2828
const stickyData = useStickyData();
2929

3030
return (
3131
<div className="page-header">
3232
<SkipToContent />
33+
<PageTitleConfirmation />
3334
<JITLoad importFn={() => import('./sticky-note/sticky-note.js')} stickyData={stickyData} />
3435
<Menus />
3536
</div>

src/app/helpers/data.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
export function useToggle(
44
initialState?: boolean
55
): [boolean, (state?: boolean) => boolean];
6+
7+
export function htmlToText(
8+
html: string
9+
): string;

src/app/helpers/use-document-head.js

-137
This file was deleted.

0 commit comments

Comments
 (0)