@@ -16,6 +16,22 @@ export interface ToastProviderProps {
16
16
* they are cleared manually.
17
17
*/
18
18
noTimeout ?: CommonToastProps [ 'message' ] | CommonToastProps [ 'message' ] [ ] ;
19
+ /**
20
+ * Specify the location where the toasts will appear from.
21
+ * @default "top-right"
22
+ */
23
+ position ?: 'top-left' | 'top' | 'top-right' | 'bottom-left' | 'bottom' | 'bottom-right' ;
24
+ /**
25
+ * If provided, will position the toast this number of pixels away from the
26
+ * edge of the screen. This only applies on the y axis, you will have to use
27
+ * `className` to add any other styles.
28
+ */
29
+ customPadding ?: number ;
30
+ /**
31
+ * Custom classname for the div that wraps the toast. This can be used to add
32
+ * custom padding or override the animations.
33
+ */
34
+ className ?: string ;
19
35
}
20
36
21
37
interface ToastQueueItem extends AddToast {
@@ -24,7 +40,14 @@ interface ToastQueueItem extends AddToast {
24
40
25
41
let toastIdSequence = 1 ;
26
42
27
- export const ToastProvider : FC < ToastProviderProps > = ( { defaultTimeout, noTimeout, children } ) => {
43
+ export const ToastProvider : FC < ToastProviderProps > = ( {
44
+ defaultTimeout,
45
+ noTimeout,
46
+ position = 'top-right' ,
47
+ customPadding,
48
+ className,
49
+ children,
50
+ } ) => {
28
51
const [ toasts , setToasts ] = useState < ToastQueueItem [ ] > ( [ ] ) ;
29
52
const [ closing , setClosing ] = useState ( false ) ;
30
53
@@ -103,13 +126,29 @@ export const ToastProvider: FC<ToastProviderProps> = ({ defaultTimeout, noTimeou
103
126
[ add , remove , toasts ] ,
104
127
) ;
105
128
129
+ const padding = useMemo ( ( ) => {
130
+ if ( customPadding === undefined ) return undefined ;
131
+ const isTop = [ 'top-left' , 'top' , 'top-right' ] . includes ( position ) ;
132
+ return customPadding !== undefined
133
+ ? {
134
+ paddingTop : isTop ? customPadding : undefined ,
135
+ paddingBottom : isTop ? undefined : customPadding ,
136
+ }
137
+ : undefined ;
138
+ } , [ customPadding , position ] ) ;
139
+
106
140
const toastProviderClass = classnames ( 'ui__toastProvider' , { 'ui__toastProvider--closing' : closing } ) ;
141
+ const toastClass = classnames (
142
+ 'ui__toastProvider__toast' ,
143
+ `ui__toastProvider__toast--position-${ position } ` ,
144
+ className ,
145
+ ) ;
107
146
108
147
return (
109
148
< ToastContext . Provider value = { value } >
110
149
{ toastId && (
111
150
< Overlay >
112
- < div className = "ui__toastProvider__toast" key = { toastId } >
151
+ < div className = { toastClass } key = { toastId } style = { padding } >
113
152
< Toast
114
153
{ ...toastProps }
115
154
role = { toastProps . message === 'error' ? 'alert' : 'status' }
0 commit comments