Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/enhancement/603-AddUseMemoAndUse…
Browse files Browse the repository at this point in the history
…CallbackToUseSnackbarHook' into test-branch
  • Loading branch information
oddballdave committed Feb 13, 2025
2 parents 6aa08ef + d53a777 commit 5226545
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@department-of-veterans-affairs/mobile-component-library",
"version": "0.30.0",
"version": "0.30.1-alpha.0",
"description": "VA Design System Mobile Component Library",
"main": "src/index.tsx",
"scripts": {
Expand Down
65 changes: 36 additions & 29 deletions packages/components/src/components/Snackbar/useSnackbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from 'react'
import { useCallback, useContext, useMemo } from 'react'

import * as Toast from 'react-native-toast-notifications'

Expand All @@ -22,32 +22,39 @@ export function useSnackbar() {
throw new Error('useSnackbar must be used within a SnackbarProvider')
}

const show = (message: string, snackbarOptions?: SnackbarOptions) => {
const { offset, setOffset } = context

const customOffset = snackbarOptions?.offset

// Custom offset if provided, else default
const newOffset = customOffset === undefined ? defaultOffset : customOffset

// Only call setOffset if different from current to avoid re-render
if (newOffset !== offset) {
setOffset(newOffset)
}

// Auto-dismiss if screen reader is on and there is no action button
const duration =
screenReaderEnabled && !snackbarOptions?.onActionPressed
? SNACKBAR_DURATIONS.SCREEN_READER
: SNACKBAR_DURATIONS.DEFAULT

toast.hideAll()
toast.show(message, { data: snackbarOptions, duration })
}

return {
show,
hide: toast.hideAll,
isOpen: toast.isOpen,
}
const { offset, setOffset } = context

const show = useCallback(
(message: string, snackbarOptions?: SnackbarOptions) => {
const customOffset = snackbarOptions?.offset

// Custom offset if provided, else default
const newOffset =
customOffset === undefined ? defaultOffset : customOffset

// Only call setOffset if different from current to avoid re-render
if (newOffset !== offset) {
setOffset(newOffset)
}

// Auto-dismiss if screen reader is on and there is no action button
const duration =
screenReaderEnabled && !snackbarOptions?.onActionPressed
? SNACKBAR_DURATIONS.SCREEN_READER
: SNACKBAR_DURATIONS.DEFAULT

toast.hideAll()
toast.show(message, { data: snackbarOptions, duration })
},
[defaultOffset, offset, screenReaderEnabled, setOffset, toast],
)

return useMemo(
() => ({
show,
hide: toast.hideAll,
isOpen: toast.isOpen,
}),
[show, toast],
)
}

0 comments on commit 5226545

Please sign in to comment.