11import * as React from 'react' ;
2- import { Animated } from 'react-native' ;
2+ import { View } from 'react-native' ;
3+ import { animated , useSpring } from 'react-spring/native' ;
34import { Omit } from 'ts-essentials' ;
45
5- import { Theme , withTheme } from '../../theme' ;
66import { Alert , AlertProps } from '../Alert' ;
77
8- // Animation taken from https://medium.com/@norbajunior /react-native-facebook-and-instagram-like-top-bar-notifications-with-animated-api-43c48d0443dd
8+ const AnimatedView = animated ( View ) ;
9+
910export type ToastId = string ;
1011
1112export interface ToastSettings extends Omit < AlertProps , 'onClose' > {
@@ -22,66 +23,35 @@ export interface ToastInstance extends ToastSettings {
2223 onRemove : ( ) => void ;
2324}
2425
25- export interface ToastProps extends ToastInstance {
26- theme : Theme ;
27- }
28-
29- export interface ToastState {
30- value : Animated . Value ;
31- }
32-
33- const DEFAULT_VALUE = 500 ;
34-
35- class ToastBase extends React . Component < ToastProps , ToastState > {
36- public closeTimer : number | null = null ;
37-
38- constructor ( props : ToastProps ) {
39- super ( props ) ;
40-
41- this . state = {
42- value : new Animated . Value ( - DEFAULT_VALUE ) ,
43- } ;
44- }
45-
46- public componentDidMount ( ) {
47- const { onRemove, duration = 3000 , offset = 16 } = this . props ;
48- const { value } = this . state ;
49-
50- Animated . sequence ( [
51- Animated . spring ( value , {
52- bounciness : 8 ,
53- speed : 25 ,
54- toValue : offset ,
55- } ) ,
56- Animated . delay ( duration ) ,
57- Animated . spring ( value , {
58- bounciness : 8 ,
59- speed : 25 ,
60- toValue : - DEFAULT_VALUE ,
61- } ) ,
62- ] ) . start ( ( ) => onRemove ( ) ) ;
63- }
64-
65- public render ( ) {
66- const {
67- component,
68- offset,
69- duration,
70- id,
71- onRemove,
72- ...toastSettings
73- } = this . props ;
74-
75- return (
76- < Animated . View
77- style = { {
78- transform : [ { translateY : this . state . value } ] ,
79- } }
80- >
81- { component || < Alert { ...toastSettings } onClose = { onRemove } /> }
82- </ Animated . View >
83- ) ;
84- }
85- }
86-
87- export const Toast = withTheme ( ToastBase ) ;
26+ // tslint:disable-next-line
27+ export interface ToastProps extends ToastInstance { }
28+
29+ export const Toast = ( props : ToastProps ) => {
30+ const {
31+ component,
32+ id,
33+ onRemove,
34+ duration = 3000 ,
35+ offset = 16 ,
36+ ...toastSettings
37+ } = props ;
38+
39+ const style = useSpring ( {
40+ config : { tension : 240 , friction : 24 } ,
41+ from : { translateY : - 500 } ,
42+ onRest : ( ) => onRemove ( ) ,
43+ to : async next => {
44+ // tslint:disable-next-line
45+ await next ( { translateY : 10 } ) ;
46+ // tslint:disable-next-line
47+ await next ( { translateY : - 500 , delay : duration } ) ;
48+ } ,
49+ } ) ;
50+
51+ return (
52+ // @ts -ignore
53+ < AnimatedView style = { { transform : [ { translateY : style . translateY } ] } } >
54+ { component || < Alert { ...toastSettings } onClose = { onRemove } /> }
55+ </ AnimatedView >
56+ ) ;
57+ } ;
0 commit comments