Last active
April 7, 2023 05:51
-
-
Save felixcatto/41ac1b3b942775116ea57eedab96b400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [notifications, setNotifications] = React.useState([]); | |
const addNotification = () => { | |
setNotifications(mountNotification); | |
setTimeout(startAddAnimation, 0); | |
setTimeout(deleteNotification, autoremoveTimeout); | |
}; | |
const deleteNotification = id => () => { | |
setNotifications(startRemoveAnimation); | |
setTimeout(removeNotification, animationDuration); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const $notifications = createStore({ | |
list: [], | |
autoremoveTimeout: 10_000, | |
animationDuration: 400, | |
}) | |
// Add Flow | |
sample({ | |
source: $notifications, | |
clock: actions.addNotification, | |
fn: mountNotification, | |
target: $notifications, | |
}); | |
sample({ | |
source: $notifications, | |
clock: delay({ source: actions.addNotification, timeout: 0 }), | |
fn: startAddAnimation, | |
target: $notifications, | |
}); | |
sample({ | |
source: $notifications, | |
clock: delay({ source: actions.addNotification, timeout: $notifications.map(el => el.autoremoveTimeout) }), | |
fn: startRemoveAnimation, | |
target: $notifications, | |
}); | |
sample({ | |
source: $notifications, | |
clock: delay({ | |
source: actions.addNotification, | |
timeout: $notifications.map(el => el.autoremoveTimeout + el.animationDuration), | |
}), | |
fn: removeNotification, | |
target: $notifications, | |
}); | |
// Remove Flow | |
sample({ | |
source: $notifications, | |
clock: actions.removeNotification, | |
fn: startRemoveAnimation, | |
target: $notifications, | |
}); | |
sample({ | |
source: $notifications, | |
clock: delay({ source: actions.removeNotification, timeout: $notifications.map(el => el.animationDuration) }), | |
fn: removeNotification, | |
target: $notifications, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment