-
-
Notifications
You must be signed in to change notification settings - Fork 734
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use buttons for sortable <th>s (#898)
* refactor: use buttons for sortable <th>s * refactor: announce sorting to screen readers * refactor: fix MenuItem padding override
- Loading branch information
Showing
11 changed files
with
191 additions
and
29 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
frontend/src/component/common/Announcer/AnnouncerContext/AnnouncerContext.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { render } from 'utils/testRenderer'; | ||
import { AnnouncerProvider } from 'component/common/Announcer/AnnouncerProvider/AnnouncerProvider'; | ||
import { AnnouncerContext } from 'component/common/Announcer/AnnouncerContext/AnnouncerContext'; | ||
import { useContext, useEffect } from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
|
||
test('AnnouncerContext', async () => { | ||
const TestComponent = () => { | ||
const { setAnnouncement } = useContext(AnnouncerContext); | ||
|
||
useEffect(() => { | ||
setAnnouncement('Foo'); | ||
setAnnouncement('Bar'); | ||
}, [setAnnouncement]); | ||
|
||
return null; | ||
}; | ||
|
||
render( | ||
<AnnouncerProvider> | ||
<TestComponent /> | ||
</AnnouncerProvider> | ||
); | ||
|
||
expect(screen.getByRole('status')).not.toHaveTextContent('Foo'); | ||
expect(screen.getByRole('status')).toHaveTextContent('Bar'); | ||
}); |
15 changes: 15 additions & 0 deletions
15
frontend/src/component/common/Announcer/AnnouncerContext/AnnouncerContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react'; | ||
|
||
export interface IAnnouncerContext { | ||
setAnnouncement: React.Dispatch<React.SetStateAction<string | undefined>>; | ||
} | ||
|
||
const setAnnouncementPlaceholder = () => { | ||
throw new Error('setAnnouncement called outside AnnouncerContext'); | ||
}; | ||
|
||
// AnnouncerContext announces messages to screen readers through a live region. | ||
// Call setAnnouncement to broadcast a new message to the screen reader. | ||
export const AnnouncerContext = React.createContext<IAnnouncerContext>({ | ||
setAnnouncement: setAnnouncementPlaceholder, | ||
}); |
13 changes: 13 additions & 0 deletions
13
frontend/src/component/common/Announcer/AnnouncerElement/AnnouncerElement.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
|
||
export const useStyles = makeStyles({ | ||
container: { | ||
clip: 'rect(0 0 0 0)', | ||
clipPath: 'inset(50%)', | ||
zIndex: -1, | ||
width: 1, | ||
height: 1, | ||
margin: -1, | ||
padding: 0, | ||
}, | ||
}); |
23 changes: 23 additions & 0 deletions
23
frontend/src/component/common/Announcer/AnnouncerElement/AnnouncerElement.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { ReactElement } from 'react'; | ||
import { useStyles } from 'component/common/Announcer/AnnouncerElement/AnnouncerElement.styles'; | ||
|
||
interface IAnnouncerElementProps { | ||
announcement?: string; | ||
} | ||
|
||
export const AnnouncerElement = ({ | ||
announcement, | ||
}: IAnnouncerElementProps): ReactElement => { | ||
const styles = useStyles(); | ||
|
||
return ( | ||
<div | ||
role="status" | ||
aria-live="polite" | ||
aria-atomic | ||
className={styles.container} | ||
> | ||
{announcement} | ||
</div> | ||
); | ||
}; |
27 changes: 27 additions & 0 deletions
27
frontend/src/component/common/Announcer/AnnouncerProvider/AnnouncerProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { ReactElement, useMemo, useState, ReactNode } from 'react'; | ||
import { AnnouncerContext } from '../AnnouncerContext/AnnouncerContext'; | ||
import { AnnouncerElement } from 'component/common/Announcer/AnnouncerElement/AnnouncerElement'; | ||
|
||
interface IAnnouncerProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const AnnouncerProvider = ({ | ||
children, | ||
}: IAnnouncerProviderProps): ReactElement => { | ||
const [announcement, setAnnouncement] = useState<string>(); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
setAnnouncement, | ||
}), | ||
[setAnnouncement] | ||
); | ||
|
||
return ( | ||
<AnnouncerContext.Provider value={value}> | ||
{children} | ||
<AnnouncerElement announcement={announcement} /> | ||
</AnnouncerContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters