Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 2d4df0a

Browse files
Brian OchanTigge
authored andcommitted
feat(toast): allow user to provide custom toastOptions
- allows a user to provide custom toastOptions which will be merged with the DEFAULT_TOAST_OPTIONS
1 parent 7128435 commit 2d4df0a

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

packages/core/src/Practical.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ const PracticalContext = createContext<PracticalContextType>({
2424
rootEl: undefined,
2525
})
2626

27-
interface ToastsOptions {
27+
export interface ToastsOptions {
2828
/**
2929
* Position of toasts on the page.
3030
*/
31-
readonly placement: ToastsPlacement
31+
readonly placement?: ToastsPlacement
3232
/**
3333
* Determine if toasts should be always on top (above any other modal layers),
3434
* or not (above the main application layer but below any other modal layers).
3535
*
3636
* @default true
3737
*/
38-
readonly alwaysOnTop: boolean
38+
readonly alwaysOnTop?: boolean
3939
/**
4040
* Default toast durations
4141
*/
42-
readonly defaultDurations: SimpleToastsDurations
42+
readonly defaultDurations?: SimpleToastsDurations
4343
}
4444

45-
const DEFAULT_TOASTS_OPTIONS: ToastsOptions = {
45+
const DEFAULT_TOASTS_OPTIONS: Required<ToastsOptions> = {
4646
placement: { justify: 'right', top: '0' },
4747
alwaysOnTop: true,
4848
defaultDurations: {
@@ -65,19 +65,24 @@ interface PracticalProviderProps extends SimpleToastsDurations {
6565

6666
export const PracticalProvider: React.FC<PracticalProviderProps> = ({
6767
theme = defaultTheme,
68-
toastsOptions = DEFAULT_TOASTS_OPTIONS,
68+
toastsOptions,
6969
children,
7070
}) => {
7171
const [rootEl, rootRef] = useState<HTMLDivElement | null>(null)
7272

73+
const mergedOptions = {
74+
...DEFAULT_TOASTS_OPTIONS,
75+
...toastsOptions,
76+
}
77+
7378
return (
7479
<ThemeProvider theme={theme}>
7580
<PracticalContext.Provider value={{ rootEl }}>
76-
<ToastsProvider {...toastsOptions.defaultDurations}>
81+
<ToastsProvider {...mergedOptions.defaultDurations}>
7782
<PracticalRoot id="practical-root" ref={rootRef}>
7883
<Layer>{children}</Layer>
79-
<Layer zIndex={toastsOptions.alwaysOnTop ? 1 : 0}>
80-
<ToastsAnchor placement={toastsOptions.placement} />
84+
<Layer zIndex={mergedOptions.alwaysOnTop ? 1 : 0}>
85+
<ToastsAnchor placement={mergedOptions.placement} />
8186
</Layer>
8287
</PracticalRoot>
8388
</ToastsProvider>

0 commit comments

Comments
 (0)