From 2a27b61e355f67c9f98fcf75fe66fa22df626c91 Mon Sep 17 00:00:00 2001 From: Narin Ratana Date: Thu, 15 Aug 2024 13:04:03 -0700 Subject: [PATCH 01/15] Add setDuration to context. Add screenreader check in .show() --- .../components/Snackbar/SnackbarProvider.tsx | 11 ++++++++-- .../src/components/Snackbar/useSnackbar.tsx | 20 +++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/components/src/components/Snackbar/SnackbarProvider.tsx b/packages/components/src/components/Snackbar/SnackbarProvider.tsx index 96b47262..70bafec2 100644 --- a/packages/components/src/components/Snackbar/SnackbarProvider.tsx +++ b/packages/components/src/components/Snackbar/SnackbarProvider.tsx @@ -8,6 +8,12 @@ import { useSnackbarDefaultOffset } from './useSnackbarDefaultOffset' type SnackbarContextType = { offset: number setOffset: (newOffset: number) => void + setDuration: (newDuration: number) => void +} + +export const SNACKBAR_DURATIONS = { + DEFAULT: 1000000000000, + SCREEN_READER: 5000 } export const SnackbarContext = createContext( @@ -20,12 +26,13 @@ export const SnackbarProvider: React.FC<{ children: ReactNode }> = ({ // TODO: Check global default setting const defaultOffset = useSnackbarDefaultOffset() const [offset, setOffset] = useState(defaultOffset) + const [duration, setDuration] = useState(SNACKBAR_DURATIONS.DEFAULT) return ( - + } diff --git a/packages/components/src/components/Snackbar/useSnackbar.tsx b/packages/components/src/components/Snackbar/useSnackbar.tsx index 0697e0ed..b17cc503 100644 --- a/packages/components/src/components/Snackbar/useSnackbar.tsx +++ b/packages/components/src/components/Snackbar/useSnackbar.tsx @@ -2,8 +2,9 @@ import { useContext } from 'react' import * as Toast from 'react-native-toast-notifications' -import { SnackbarContext } from './SnackbarProvider' +import { SNACKBAR_DURATIONS, SnackbarContext } from './SnackbarProvider' import { SnackbarOptions } from './Snackbar' +import { useIsScreenReaderEnabled } from '../../utils' import { useSnackbarDefaultOffset } from './useSnackbarDefaultOffset' /** @@ -15,23 +16,26 @@ export function useSnackbar() { const toast = Toast.useToast() const context = useContext(SnackbarContext) const defaultOffset = useSnackbarDefaultOffset() + const screenReaderEnabled = useIsScreenReaderEnabled() if (!context) { throw new Error('useSnackbar must be used within a SnackbarProvider') } - const { offset, setOffset } = context + const { setOffset, setDuration } = context const show = (message: string, snackbarOptions?: SnackbarOptions) => { toast.hideAll() - if (snackbarOptions?.offset) { - setOffset(snackbarOptions.offset) - } else if (offset !== defaultOffset) { - setOffset(defaultOffset) - } + setOffset(snackbarOptions?.offset ? snackbarOptions.offset : defaultOffset) - return toast.show(message, { data: snackbarOptions }) + if (screenReaderEnabled) { + setDuration( + screenReaderEnabled + ? SNACKBAR_DURATIONS.SCREEN_READER + : SNACKBAR_DURATIONS.DEFAULT, + ) + } else return toast.show(message, { data: snackbarOptions }) } return { From 63fa112a56e7258d792c06a142cb856f4ea41acb Mon Sep 17 00:00:00 2001 From: Narin Ratana Date: Thu, 15 Aug 2024 22:20:28 -0700 Subject: [PATCH 02/15] Add "With Action" variant to stories --- .../src/components/Snackbar/Snackbar.stories.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/components/src/components/Snackbar/Snackbar.stories.tsx b/packages/components/src/components/Snackbar/Snackbar.stories.tsx index 50856511..3111d513 100644 --- a/packages/components/src/components/Snackbar/Snackbar.stories.tsx +++ b/packages/components/src/components/Snackbar/Snackbar.stories.tsx @@ -49,7 +49,18 @@ const mobileComponentRenderer = (props: storyArgs) => { return