Skip to content

Commit

Permalink
refactor: use buttons for sortable <th>s (#898)
Browse files Browse the repository at this point in the history
* refactor: use buttons for sortable <th>s

* refactor: announce sorting to screen readers

* refactor: fix MenuItem padding override
  • Loading branch information
olav authored Apr 22, 2022
1 parent 629df7e commit 5288438
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 29 deletions.
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');
});
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,
});
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,
},
});
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>
);
};
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>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export const useStyles = makeStyles(theme => ({
},
},
},
sortButton: {
all: 'unset',
'&:focus-visible, &:active': {
outline: 'revert',
},
},
}));
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactNode } from 'react';
import React, { ReactNode, useContext } from 'react';
import { TableCell } from '@material-ui/core';
import classnames from 'classnames';
import {
Expand All @@ -9,6 +9,7 @@ import {
import { IUsersSort, UsersSortType } from 'hooks/useUsersSort';
import ConditionallyRender from 'component/common/ConditionallyRender';
import { useStyles } from 'component/common/Table/TableCellSortable/TableCellSortable.styles';
import { AnnouncerContext } from 'component/common/Announcer/AnnouncerContext/AnnouncerContext';

// Add others as needed, e.g. UsersSortType | FeaturesSortType
type SortType = UsersSortType;
Expand All @@ -29,23 +30,37 @@ export const TableCellSortable = ({
setSort,
children,
}: ITableCellSortableProps) => {
const { setAnnouncement } = useContext(AnnouncerContext);
const styles = useStyles();

const ariaSort =
sort.type === name
? sort.desc
? 'descending'
: 'ascending'
: undefined;

const cellClassName = classnames(
className,
styles.tableCellHeaderSortable,
sort.type === name && 'sorted'
);

const onSortClick = () => {
setSort(prev => ({
desc: !Boolean(prev.desc),
type: name,
}));
setAnnouncement(
`Sorted table by ${name}, ${sort.desc ? 'ascending' : 'descending'}`
);
};

return (
<TableCell
className={classnames(
className,
styles.tableCellHeaderSortable,
sort.type === name && 'sorted'
)}
onClick={() =>
setSort(prev => ({
desc: !Boolean(prev.desc),
type: name,
}))
}
>
{children}
<TableCell aria-sort={ariaSort} className={cellClassName}>
<button className={styles.sortButton} onClick={onSortClick}>
{children}
</button>
<ConditionallyRender
condition={sort.type === name}
show={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ export const useStyles = makeStyles(theme => ({
color: theme.palette.primary.main,
fontWeight: theme.fontWeight.bold,
},
sortButton: {
all: 'unset',
'&:focus-visible, &:active': {
outline: 'revert',
},
},
}));
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState, useEffect, useContext } from 'react';
import {
Table,
TableBody,
Expand All @@ -18,6 +18,7 @@ import {
import PaginateUI from 'component/common/PaginateUI/PaginateUI';
import StringTruncator from 'component/common/StringTruncator/StringTruncator';
import { createGlobalStateHook } from 'hooks/useGlobalState';
import { AnnouncerContext } from 'component/common/Announcer/AnnouncerContext/AnnouncerContext';
interface IFeatureToggleListNewProps {
features: IFeatureToggleListItem[];
loading: boolean;
Expand Down Expand Up @@ -83,6 +84,7 @@ const FeatureToggleListNew = ({
projectId,
}: IFeatureToggleListNewProps) => {
const styles = useStyles();
const { setAnnouncement } = useContext(AnnouncerContext);
const [sortOpt, setSortOpt] = useFeatureToggLeProjectSort();
const [sortedFeatures, setSortedFeatures] = useState(
sortList([...features], sortOpt)
Expand Down Expand Up @@ -116,6 +118,12 @@ const FeatureToggleListNew = ({
setSortOpt(newSortOpt);
setSortedFeatures(sortList([...features], newSortOpt));
setPageIndex(0);

setAnnouncement(
`Sorted table by ${field}, ${
sortOpt.direction ? 'ascending' : 'descending'
}`
);
};

const getEnvironments = () => {
Expand Down Expand Up @@ -163,6 +171,14 @@ const FeatureToggleListNew = ({
});
};

const ariaSort = (field: string) => {
return field === sortOpt.field
? sortOpt.direction
? 'ascending'
: 'descending'
: undefined;
};

return (
<>
<Table>
Expand All @@ -176,13 +192,15 @@ const FeatureToggleListNew = ({
styles.tableCellHeaderSortable
)}
align="left"
aria-sort={ariaSort('lastSeenAt')}
>
<span
<button
data-loading
onClick={() => updateSort('lastSeenAt')}
className={styles.sortButton}
>
Last use
</span>
</button>
</TableCell>
<TableCell
className={classnames(
Expand All @@ -192,13 +210,15 @@ const FeatureToggleListNew = ({
styles.tableCellHeaderSortable
)}
align="center"
aria-sort={ariaSort('type')}
>
<span
<button
data-loading
onClick={() => updateSort('type')}
className={styles.sortButton}
>
Type
</span>
</button>
</TableCell>
<TableCell
className={classnames(
Expand All @@ -208,13 +228,15 @@ const FeatureToggleListNew = ({
styles.tableCellHeaderSortable
)}
align="left"
aria-sort={ariaSort('name')}
>
<span
<button
data-loading
onClick={() => updateSort('name')}
className={styles.sortButton}
>
Name
</span>
</button>
</TableCell>
<TableCell
className={classnames(
Expand All @@ -224,13 +246,15 @@ const FeatureToggleListNew = ({
styles.tableCellHeaderSortable
)}
align="left"
aria-sort={ariaSort('createdAt')}
>
<span
<button
data-loading
onClick={() => updateSort('createdAt')}
className={styles.sortButton}
>
Created
</span>
</button>
</TableCell>
{getEnvironments().map((env: any) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const useStyles = makeStyles(theme => ({
color: '#000',
height: '100%',
width: '100%',
padding: '0.5rem 1rem',
'&&': {
// Override MenuItem's built-in padding.
padding: '0.5rem 1rem',
},
},
}));
11 changes: 7 additions & 4 deletions frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import AccessProvider from 'component/providers/AccessProvider/AccessProvider';
import { getBasePath } from 'utils/formatPath';
import { FeedbackCESProvider } from 'component/feedback/FeedbackCESContext/FeedbackCESProvider';
import UIProvider from 'component/providers/UIProvider/UIProvider';
import { AnnouncerProvider } from 'component/common/Announcer/AnnouncerProvider/AnnouncerProvider';

ReactDOM.render(
<DndProvider backend={HTML5Backend}>
<UIProvider>
<AccessProvider>
<Router basename={`${getBasePath()}`}>
<MainThemeProvider>
<FeedbackCESProvider>
<ScrollTop />
<Route path="/" component={App} />
</FeedbackCESProvider>
<AnnouncerProvider>
<FeedbackCESProvider>
<ScrollTop />
<Route path="/" component={App} />
</FeedbackCESProvider>
</AnnouncerProvider>
</MainThemeProvider>
</Router>
</AccessProvider>
Expand Down

0 comments on commit 5288438

Please sign in to comment.