Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add live region for navigation changes #2214

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import theme from '../theme';
import AccessibilityButtonsWrapper from './AccessibilityButtonsWrapper';
import NavBar from './NavBar';
import OnEsc from './OnEsc';
import PageTitleConfirmation from './PageTitleConfirmation';

// tslint:disable-next-line:variable-name
const Layout: SFC = ({ children }) => <AccessibilityButtonsWrapper>
<NavBar />
<OnEsc />
<PageTitleConfirmation />
<ErrorModal />
<ErrorBoundary>
{children}
Expand Down
33 changes: 33 additions & 0 deletions src/app/components/PageTitleConfirmation.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { renderToDom } from '../../test/reactutils';
// import { act } from 'react-dom/test-utils';
import { initialState } from '../reducer';
import createTestStore from '../../test/createTestStore';
import PageTitleConfirmation from './PageTitleConfirmation';
import TestContainer from '../../test/TestContainer';
import { setHead } from '../head/actions';

describe('PageTitleConfirmation', () => {
it('skips blank, skips first non-blank, announces subsequent', async() => {
const state = {
...initialState,
};

state.head.title = 'initial';
TomWoodward marked this conversation as resolved.
Show resolved Hide resolved
const store = createTestStore(state);

store.dispatch(setHead({...initialState.head, title: ''}));
const component = renderToDom(
<TestContainer store={store}>
<PageTitleConfirmation className='any' />
</TestContainer>
);

expect(component.node.textContent).toBe('');
store.dispatch(setHead({...initialState.head, title: 'initial'}));
TomWoodward marked this conversation as resolved.
Show resolved Hide resolved
expect(component.node.textContent).toBe('');
store.dispatch(setHead({...initialState.head, title: 'something'}));
expect(component.node.textContent).toBe('Loaded page something');
component.unmount();
});
});
37 changes: 37 additions & 0 deletions src/app/components/PageTitleConfirmation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { hiddenButAccessible } from '../theme';
import { AppState } from '../types';
import styled from 'styled-components';
import { connect } from 'react-redux';

function PageTitleConfirmation({
className,
title,
}: {
className: string;
title: string;
}) {
const skipFirst = React.useRef(true);
const message = React.useMemo(() => {
if (!title) {
return '';
}
if (skipFirst.current) {
skipFirst.current = false;
return '';
}
return `Loaded page ${title}`;
}, [title]);

return (
<div id='page-title-confirmation' className={className} aria-live='polite'>
{message}
</div>
);
}

export default connect(({ head: { title } }: AppState) => ({ title }))(styled(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a selector for the title https://github.com/openstax/rex-web/blob/main/src/app/head/selectors.ts#L19

using it will affect nobody's life but it follows the convention. when you have derived values in selectors it does make a difference because they're memoized based on the input state and will help reduce react re-renders

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a auto-focus to main when its content changes, and also revised this to use the selector.

PageTitleConfirmation
)`
${hiddenButAccessible}
`);
40 changes: 40 additions & 0 deletions src/app/content/components/__snapshots__/Content.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ Array [
/>
</a>
</div>
</div>,
.c0 {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
-webkit-clip: rect(0,0,0,0);
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}

<div
aria-live="polite"
className="c0"
id="page-title-confirmation"
>

</div>,
.c4 {
display: inline-block;
Expand Down Expand Up @@ -4865,6 +4885,26 @@ Array [
/>
</a>
</div>
</div>,
.c0 {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
-webkit-clip: rect(0,0,0,0);
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}

<div
aria-live="polite"
className="c0"
id="page-title-confirmation"
>

</div>,
.c4 {
display: inline-block;
Expand Down
20 changes: 20 additions & 0 deletions src/app/developer/components/__snapshots__/Home.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ Array [
/>
</a>
</div>
</div>,
.c0 {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
-webkit-clip: rect(0,0,0,0);
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}

<div
aria-live="polite"
className="c0"
id="page-title-confirmation"
>

</div>,
.c19 {
display: inline-block;
Expand Down
12 changes: 12 additions & 0 deletions src/app/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export interface ColorSet {
darkest?: string;
}

export const hiddenButAccessible = `
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
`;

const textColors = {
black: '#000',
default: '#424242',
Expand Down
Loading