|
| 1 | +"use client"; |
| 2 | +import React, { ReactNode, useState } from "react"; |
| 3 | + |
| 4 | +import Button from "../button/index.js"; |
| 5 | +import Icon from "../icon/index.js"; |
| 6 | +import IconButton from "../icon-button/index.js"; |
| 7 | + |
| 8 | +import { ToastOptions, toast } from "react-hot-toast"; |
| 9 | +import { useSwipeable } from "react-swipeable"; |
| 10 | +import { twMerge } from "tailwind-merge"; |
| 11 | + |
| 12 | +import { Toaster, ToasterProps } from "react-hot-toast"; |
| 13 | + |
| 14 | +export type SnackbarProviderProps = ToasterProps |
| 15 | +export const SnackbarProvider = (props: SnackbarProviderProps) => { |
| 16 | + return ( |
| 17 | + <> |
| 18 | + <Toaster {...props} /> |
| 19 | + {props.children} |
| 20 | + </> |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +export type SnackbarProps = { |
| 25 | + icon?: React.ReactNode; |
| 26 | + variant?: string; |
| 27 | + children: React.ReactNode; |
| 28 | + showClose?: boolean; |
| 29 | + onAction?: () => void; |
| 30 | + onClose?: () => void; |
| 31 | + actionText?: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export const Snackbar = ({ |
| 35 | + icon, |
| 36 | + variant, |
| 37 | + children, |
| 38 | + showClose, |
| 39 | + onAction, |
| 40 | + onClose, |
| 41 | + actionText, |
| 42 | +}: SnackbarProps) => { |
| 43 | + const [translateX, setTranslateX] = useState(0); |
| 44 | + const [isSwiped, setIsSwiped] = useState(false); |
| 45 | + |
| 46 | + // Handle swipe |
| 47 | + const handlers = useSwipeable({ |
| 48 | + onSwiping: (eventData) => { |
| 49 | + setTranslateX(eventData.deltaX); // Update the translateX value dynamically |
| 50 | + if (Math.abs(eventData.deltaX) > 100) { |
| 51 | + setIsSwiped(true); // Swipe threshold for dismissing |
| 52 | + } else { |
| 53 | + setIsSwiped(false); // Swipe threshold for dismissing |
| 54 | + } |
| 55 | + }, |
| 56 | + onSwiped: (eventData) => { |
| 57 | + if (Math.abs(eventData.deltaX) > 100) { |
| 58 | + setIsSwiped(true); // Swipe threshold for dismissing |
| 59 | + setTimeout(() => { |
| 60 | + onClose?.(); |
| 61 | + }, 300); // Delay to allow animation to complete |
| 62 | + } else { |
| 63 | + setTranslateX(0); // Reset if swipe is not sufficient |
| 64 | + setIsSwiped(false); // Swipe threshold for dismissing |
| 65 | + } |
| 66 | + }, |
| 67 | + preventScrollOnSwipe: true, |
| 68 | + trackTouch: true, |
| 69 | + trackMouse: false, |
| 70 | + }); |
| 71 | + |
| 72 | + const _showClose = showClose || !actionText; |
| 73 | + const _variant = variant |
| 74 | + ? variant |
| 75 | + : (actionText?.length ?? 0) > 10 || actionText?.includes(" ") |
| 76 | + ? "extended" |
| 77 | + : "standard"; |
| 78 | + const useLongerAction = _variant === "extended"; |
| 79 | + |
| 80 | + return ( |
| 81 | + <div |
| 82 | + {...handlers} |
| 83 | + style={{ |
| 84 | + transform: `translateX(${translateX}px)`, // Dynamically apply the translation |
| 85 | + opacity: isSwiped ? 1 - Math.abs(translateX / (384 / 2)) : 1, // Fade out when dismissed |
| 86 | + transition: |
| 87 | + translateX === 0 |
| 88 | + ? "all 0.5s" |
| 89 | + : "transform 0.02s linear, opacity 0.1s linear", |
| 90 | + }} |
| 91 | + className={twMerge( |
| 92 | + "relative font-regular flex items-start gap-x-5 justify-evenly rounded-md bg-[#313033] py-4 pl-5 pr-3 text-[#F4EFF4] shadow-lg text-sm min-w-80 sm:min-w-96 max-w-96 w-screen transition-all", |
| 93 | + useLongerAction ? "flex-col" : "flex-row", |
| 94 | + isSwiped ? "opacity-0" : "opacity-100" |
| 95 | + )} |
| 96 | + > |
| 97 | + {icon && <span className="text-white">{icon}</span>} |
| 98 | + <span |
| 99 | + className={twMerge("flex flex-1 flex-wrap", useLongerAction && "pr-10")} |
| 100 | + > |
| 101 | + {children} |
| 102 | + </span> |
| 103 | + <div |
| 104 | + className={twMerge( |
| 105 | + useLongerAction ? "w-full flex-1" : "w-fit", |
| 106 | + "flex flex-row justify-end items-center gap-3" |
| 107 | + )} |
| 108 | + > |
| 109 | + {actionText && ( |
| 110 | + <Button |
| 111 | + onClick={() => { |
| 112 | + onAction?.(); |
| 113 | + onClose?.(); |
| 114 | + }} |
| 115 | + variant="text" |
| 116 | + id="action" |
| 117 | + > |
| 118 | + {actionText} |
| 119 | + </Button> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + {_showClose && ( |
| 123 | + <IconButton |
| 124 | + className={twMerge(useLongerAction && "absolute top-2 right-2")} |
| 125 | + onClick={onClose} |
| 126 | + > |
| 127 | + <Icon>close</Icon> |
| 128 | + </IconButton> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +const showSnackbar = ( |
| 135 | + message: string | ReactNode, |
| 136 | + options?: ToastOptions & Partial<SnackbarProps> |
| 137 | +) => { |
| 138 | + const _options = options; |
| 139 | + const { icon, style = {}, ...toastOptions } = _options || {}; |
| 140 | + const toastId = toast( |
| 141 | + <Snackbar |
| 142 | + showClose={_options?.showClose} |
| 143 | + onClose={() => { |
| 144 | + toast.dismiss(toastId); |
| 145 | + }} |
| 146 | + {...toastOptions} |
| 147 | + > |
| 148 | + {message} |
| 149 | + </Snackbar>, |
| 150 | + { |
| 151 | + position: "bottom-center", |
| 152 | + style: { |
| 153 | + padding: "0rem 1rem", |
| 154 | + margin: 0, |
| 155 | + boxShadow: "none", |
| 156 | + background: "transparent", |
| 157 | + display: "flex", |
| 158 | + justifyContent: "center", |
| 159 | + alignItems: "center", |
| 160 | + ...style, |
| 161 | + }, |
| 162 | + icon, |
| 163 | + ...toastOptions, |
| 164 | + } |
| 165 | + ); |
| 166 | + |
| 167 | + return toastId; |
| 168 | +}; |
| 169 | + |
| 170 | +const snackbar: typeof toast & { |
| 171 | + show: ( |
| 172 | + message: string | ReactNode, |
| 173 | + options?: ToastOptions & Partial<SnackbarProps> |
| 174 | + ) => void; |
| 175 | +} = toast; |
| 176 | +snackbar.show = showSnackbar; |
| 177 | + |
| 178 | +export { snackbar }; |
0 commit comments