-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: show status div when table results changes
- Loading branch information
1 parent
7b506e4
commit cc89c5b
Showing
10 changed files
with
189 additions
and
23 deletions.
There are no files selected for viewing
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
81 changes: 81 additions & 0 deletions
81
src/common/components/accessibilityNotificationContext/AccessibilityNotificationContext.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,81 @@ | ||
import uniqueId from 'lodash/uniqueId'; | ||
import React, { | ||
createContext, | ||
FC, | ||
PropsWithChildren, | ||
useCallback, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
import styles from './accessibilityNotification.module.scss'; | ||
|
||
export type SetAccessibilityTextFn = (text: string) => void; | ||
|
||
export type AccessibilityNotificationProps = { id: string; text: string }; | ||
|
||
export type AccessibilityNotificationContextProps = { | ||
setAccessibilityText: SetAccessibilityTextFn; | ||
}; | ||
|
||
export const AccessibilityNotificationContext = createContext< | ||
AccessibilityNotificationContextProps | undefined | ||
>(undefined); | ||
|
||
export const AccessibilityNotificationProvider: FC<PropsWithChildren> = ({ | ||
children, | ||
}) => { | ||
const [notifications, setNotifications] = useState< | ||
AccessibilityNotificationProps[] | ||
>([]); | ||
|
||
const removeNotification = (notificationId: string) => { | ||
setNotifications((items) => | ||
items.filter(({ id }) => id !== notificationId) | ||
); | ||
}; | ||
|
||
const updateNotificationText = (text: string, notificationId: string) => | ||
setNotifications((items) => | ||
items.map((notification) => | ||
notification.id === notificationId | ||
? { ...notification, text } | ||
: notification | ||
) | ||
); | ||
|
||
const setAccessibilityText = useCallback((text: string) => { | ||
const notificationId = uniqueId('accessibility-notification-'); | ||
|
||
setNotifications((items) => [...items, { id: notificationId, text: '' }]); | ||
// Change notification text after 100ms to force screen reader | ||
// to read the notification | ||
setTimeout(() => { | ||
updateNotificationText(text, notificationId); | ||
}, 100); | ||
|
||
// Clear notification area after 1000ms | ||
setTimeout(() => { | ||
removeNotification(notificationId); | ||
}, 1000); | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
setAccessibilityText, | ||
}), | ||
[setAccessibilityText] | ||
); | ||
|
||
return ( | ||
<AccessibilityNotificationContext.Provider value={value}> | ||
{notifications.map(({ text, id }) => ( | ||
<output key={id} className={styles.accessibilityNotification}> | ||
{text} | ||
</output> | ||
))} | ||
|
||
{children} | ||
</AccessibilityNotificationContext.Provider> | ||
); | ||
}; |
5 changes: 5 additions & 0 deletions
5
src/common/components/accessibilityNotificationContext/accessibilityNotification.module.scss
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,5 @@ | ||
@import '../../../styles/mixins.scss'; | ||
|
||
.accessibilityNotification { | ||
@include hidden-from-screen; | ||
} |
24 changes: 24 additions & 0 deletions
24
...components/accessibilityNotificationContext/hooks/useAccessibilityNotificationContext.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,24 @@ | ||
/* eslint @typescript-eslint/explicit-function-return-type: 0 */ | ||
import { useContext } from 'react'; | ||
|
||
import { | ||
AccessibilityNotificationContext, | ||
AccessibilityNotificationContextProps, | ||
} from '../AccessibilityNotificationContext'; | ||
|
||
export const useAccessibilityNotificationContext = | ||
(): AccessibilityNotificationContextProps => { | ||
const context = useContext< | ||
AccessibilityNotificationContextProps | undefined | ||
>(AccessibilityNotificationContext); | ||
|
||
/* istanbul ignore next */ | ||
if (!context) { | ||
throw new Error( | ||
// eslint-disable-next-line max-len | ||
'AccessibilityNotificationContext context is undefined, please verify you are calling useAccessibilityNotificationContext() as child of a <AccessibilityNotificationProvider> component.' | ||
); | ||
} | ||
|
||
return context; | ||
}; |
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,25 @@ | ||
/* eslint-disable max-len */ | ||
import { useTranslation } from 'next-i18next'; | ||
import { FC, useEffect } from 'react'; | ||
|
||
import { useAccessibilityNotificationContext } from '../accessibilityNotificationContext/hooks/useAccessibilityNotificationContext'; | ||
|
||
type Props = { count: number; loading: boolean }; | ||
|
||
const SearchStatus: FC<Props> = ({ count, loading }) => { | ||
const { t } = useTranslation('common'); | ||
const { setAccessibilityText } = useAccessibilityNotificationContext(); | ||
|
||
useEffect(() => { | ||
if (!loading) { | ||
setAccessibilityText( | ||
count | ||
? t('common:table.accessibility.resultsFoundText', { count }) | ||
: t('common:table.accessibility.noResultsFound') | ||
); | ||
} | ||
}, [count, loading, setAccessibilityText, t]); | ||
return null; | ||
}; | ||
|
||
export default SearchStatus; |
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