about summary refs log tree commit diff
path: root/src/view/com/util/Toast.tsx
blob: c7134febe6ddbf4dbe8b31489bbcb55d69ba5250 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import RootSiblings from 'react-native-root-siblings'
import React from 'react'
import {Animated, StyleSheet, View} from 'react-native'
import {Props as FontAwesomeProps} from '@fortawesome/react-native-fontawesome'
import {Text} from './text/Text'
import {colors} from 'lib/styles'
import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnimatedValue} from 'lib/hooks/useAnimatedValue'

const TIMEOUT = 4e3

export function show(
  message: string,
  _icon: FontAwesomeProps['icon'] = 'check',
) {
  const item = new RootSiblings(<Toast message={message} />)
  setTimeout(() => {
    item.destroy()
  }, TIMEOUT)
}

function Toast({message}: {message: string}) {
  const theme = useTheme()
  const pal = usePalette('default')
  const interp = useAnimatedValue(0)

  React.useEffect(() => {
    Animated.sequence([
      Animated.timing(interp, {
        toValue: 1,
        duration: 150,
        useNativeDriver: true,
      }),
      Animated.delay(3700),
      Animated.timing(interp, {
        toValue: 0,
        duration: 150,
        useNativeDriver: true,
      }),
    ]).start()
  })

  const opacityStyle = {opacity: interp}
  return (
    <View style={styles.container} pointerEvents="none">
      <Animated.View
        style={[
          pal.view,
          pal.border,
          styles.toast,
          theme.colorScheme === 'dark' && styles.toastDark,
          opacityStyle,
        ]}>
        <Text type="lg-medium" style={pal.text}>
          {message}
        </Text>
      </Animated.View>
    </View>
  )
}

const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    top: 60,
    left: 0,
    right: 0,
    alignItems: 'center',
  },
  toast: {
    paddingHorizontal: 18,
    paddingVertical: 10,
    borderRadius: 24,
    borderWidth: 1,
    shadowColor: '#000',
    shadowOpacity: 0.1,
    shadowOffset: {width: 0, height: 4},
    marginHorizontal: 6,
  },
  toastDark: {
    backgroundColor: colors.gray6,
    shadowOpacity: 0.5,
  },
})