From 73665f5606de4f3b12f9d8f15cba4d5c6641a8ff Mon Sep 17 00:00:00 2001 From: byAall <108778646+byAall@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:38:17 -0600 Subject: [PATCH] Draft, changing state (#155) * draft, changing state * component working properly * . * method name changed --- sample/index.tsx | 4 +- src/Alert/index.tsx | 92 ++++++++++++++++++++++++++++------------ src/resources/global.css | 31 ++++++++++++++ src/useAlertStore.ts | 35 +++++++++------ 4 files changed, 120 insertions(+), 42 deletions(-) diff --git a/sample/index.tsx b/sample/index.tsx index bf34dc9..a67beda 100644 --- a/sample/index.tsx +++ b/sample/index.tsx @@ -190,8 +190,8 @@ const Application = () => { - - + + void; +}) => ( +
p]:max-w-full [&>p]:m-0 -`; + mb-3 +`} + onClick={ereaseAlert} + > + {children} +
+); type AlertProps = { message?: string; + animation: string; + deleteAlert: () => void; + icon: ReactNode; }; -const AlertSuccess = ({ message = 'Changes Saved Successfully' }: AlertProps) => ( - - - {message} - -); - -const AlertError = ({ message = 'Something Went Wrong' }: AlertProps) => ( - - - {message} - -); +const AlertComponent = ({ message, animation, deleteAlert, icon }: AlertProps) => { + if (animation === AUTO_ANIMATE) + setTimeout(() => { + deleteAlert(); + }, 4500); + return ( + {} : deleteAlert}> + {icon} + {message} + + ); +}; export const Alert = () => { - const [isSuccess, message] = useAlertStore((state) => [state.isSuccess, state.message]); + const [activeAlerts, deleteAlerts] = useAlertStore((state) => [state.activeAlerts, state.deleteAlerts]); - return ( - <> - {isSuccess && } - {isSuccess === false && } - + const animationElection = (isTimed: boolean): string => + isTimed ? AUTO_ANIMATE : ' animate-[fadeIn_0.5s_ease-out_forwards] hover:bg-gray-400 '; + + return createPortal( +
+ {activeAlerts.map((alert) => ( + deleteAlerts(alert.id)} + icon={ + alert.isSuccess ? ( + + ) : ( + + ) + } + /> + ))} +
, + document.body, ); }; diff --git a/src/resources/global.css b/src/resources/global.css index 183f1c2..4405109 100644 --- a/src/resources/global.css +++ b/src/resources/global.css @@ -74,6 +74,37 @@ } } + + .alert-only-fade-in { + @keyframes fadeIn { + 0% { + transform: translateY(-40px); + visibility: visible; + opacity: 0; + } + 100% { + transform: translateY(0px); + visibility: visible; + opacity: 1; + } + } + } + + .alert-only-fade-out { + @keyframes fadeOut { + 0% { + transform: translateY(0px); + visibility: visible; + opacity: 1; + } + 100% { + transform: translateY(-40px); + visibility: hidden; + opacity: 0; + } + } + } + .animated-menu { -webkit-animation-duration: 600ms; animation-duration: 600ms; diff --git a/src/useAlertStore.ts b/src/useAlertStore.ts index c754be0..5d5e3c9 100644 --- a/src/useAlertStore.ts +++ b/src/useAlertStore.ts @@ -1,19 +1,28 @@ import { create } from 'zustand'; +import { Alert } from './Alert'; -interface AlertState { - message?: string; - isSuccess?: boolean; - setAlert: (isSuccess: boolean, message?: string) => void; +interface Alert { + id: number; + message: string; + isSuccess: boolean; + isTimed: boolean; } -export default create((set) => ({ - isSuccess: undefined, - message: undefined, - setAlert: (isSuccess: boolean, message?: string) => { - set(() => { - setTimeout(() => set({ isSuccess, message }), 200); +interface AlertList { + activeAlerts: Alert[]; + setAlert: (isSuccess: boolean, message: string, isTimed?: boolean) => void; + deleteAlerts: (alertId: number) => void; +} - return { message: undefined, isSuccess: undefined }; - }); - }, +export default create((set) => ({ + activeAlerts: [], + setAlert: (isSuccess: boolean, message: string, isTimed: boolean = true) => + set((state) => ({ + activeAlerts: [ + ...state.activeAlerts, + { isSuccess: isSuccess, isTimed: isTimed, message: message, id: Date.now() }, + ], + })), + deleteAlerts: (alertId: number) => + set((state) => ({ activeAlerts: state.activeAlerts.filter((alert) => alert.id !== alertId) })), }));