|
| 1 | +import { ToastRegionInstance, ToastType } from '@primereact/types/shared/toast'; |
| 2 | +import { Button } from 'primereact/button'; |
| 3 | +import { toast, Toast } from 'primereact/toast'; |
| 4 | + |
| 5 | +function PositionToast({ position = 'bottom-right' }: { position: 'top-left' | 'top-right' | 'top-center' | 'bottom-left' | 'bottom-right' | 'bottom-center' }) { |
| 6 | + return ( |
| 7 | + <Toast position={position} group={position}> |
| 8 | + <Toast.Portal> |
| 9 | + <Toast.Region> |
| 10 | + {({ toast }: ToastRegionInstance) => |
| 11 | + toast?.toasts.map((toast: ToastType) => ( |
| 12 | + <Toast.Item key={toast.id} data={toast}> |
| 13 | + <div className="flex items-start gap-2"> |
| 14 | + <Toast.Icon /> |
| 15 | + <div className="flex-1"> |
| 16 | + <Toast.Title className="mb-1 -mt-0.5" /> |
| 17 | + <Toast.Description /> |
| 18 | + <Toast.Action as={Button} size="small" className="mt-4" /> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | + <Toast.Close as={Button} iconOnly severity={'secondary'} variant="text" size="small" className={'absolute top-2 right-2'}> |
| 22 | + <i className="pi pi-times"></i> |
| 23 | + </Toast.Close> |
| 24 | + </Toast.Item> |
| 25 | + )) |
| 26 | + } |
| 27 | + </Toast.Region> |
| 28 | + </Toast.Portal> |
| 29 | + </Toast> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +function PositionDemo() { |
| 34 | + const createToast = (group: string) => { |
| 35 | + toast({ |
| 36 | + title: 'Changes saved', |
| 37 | + description: 'Are you sure you would like to remove this user? This action cannot be undone.', |
| 38 | + group |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="card flex flex-wrap items-center justify-center gap-4"> |
| 44 | + <Button onClick={() => createToast('top-left')} variant="outlined"> |
| 45 | + Top Left |
| 46 | + </Button> |
| 47 | + <Button onClick={() => createToast('top-center')} variant="outlined"> |
| 48 | + Top Center |
| 49 | + </Button> |
| 50 | + <Button onClick={() => createToast('top-right')} variant="outlined"> |
| 51 | + Top Right |
| 52 | + </Button> |
| 53 | + <Button onClick={() => createToast('bottom-left')} variant="outlined"> |
| 54 | + Bottom Left |
| 55 | + </Button> |
| 56 | + <Button onClick={() => createToast('bottom-center')} variant="outlined"> |
| 57 | + Bottom Center |
| 58 | + </Button> |
| 59 | + <Button onClick={() => createToast('bottom-right')} variant="outlined"> |
| 60 | + Bottom Right |
| 61 | + </Button> |
| 62 | + |
| 63 | + <PositionToast position="top-left" /> |
| 64 | + <PositionToast position="top-right" /> |
| 65 | + <PositionToast position="top-center" /> |
| 66 | + <PositionToast position="bottom-left" /> |
| 67 | + <PositionToast position="bottom-right" /> |
| 68 | + <PositionToast position="bottom-center" /> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +export default PositionDemo; |
0 commit comments