about summary refs log tree commit diff
path: root/src/view/com/util/Toast.web.tsx
blob: 6b99b30bf388e73530c1cbd5c87d8891baa0ae9d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Note: the dataSet properties are used to leverage custom CSS in public/index.html
 */

import {useEffect, useState} from 'react'
import {Pressable, StyleSheet, Text, View} from 'react-native'

import {
  convertLegacyToastType,
  getToastTypeStyles,
  getToastWebAnimationStyles,
  type LegacyToastType,
  TOAST_TYPE_TO_ICON,
  TOAST_WEB_KEYFRAMES,
  type ToastType,
} from '#/view/com/util/Toast.style'
import {atoms as a, useTheme} from '#/alf'

const DURATION = 3500

interface ActiveToast {
  text: string
  type: ToastType
}
type GlobalSetActiveToast = (_activeToast: ActiveToast | undefined) => void

// globals
// =
let globalSetActiveToast: GlobalSetActiveToast | undefined
let toastTimeout: NodeJS.Timeout | undefined

// components
// =
type ToastContainerProps = {}
export const ToastContainer: React.FC<ToastContainerProps> = ({}) => {
  const [activeToast, setActiveToast] = useState<ActiveToast | undefined>()
  const [isExiting, setIsExiting] = useState(false)

  useEffect(() => {
    globalSetActiveToast = (t: ActiveToast | undefined) => {
      if (!t && activeToast) {
        setIsExiting(true)
        setTimeout(() => {
          setActiveToast(t)
          setIsExiting(false)
        }, 200)
      } else {
        setActiveToast(t)
        setIsExiting(false)
      }
    }
  }, [activeToast])

  useEffect(() => {
    const styleId = 'toast-animations'
    if (!document.getElementById(styleId)) {
      const style = document.createElement('style')
      style.id = styleId
      style.textContent = TOAST_WEB_KEYFRAMES
      document.head.appendChild(style)
    }
  }, [])

  const t = useTheme()

  const toastTypeStyles = getToastTypeStyles(t)
  const toastStyles = activeToast
    ? toastTypeStyles[activeToast.type]
    : toastTypeStyles.default

  const IconComponent = activeToast
    ? TOAST_TYPE_TO_ICON[activeToast.type]
    : TOAST_TYPE_TO_ICON.default

  const animationStyles = getToastWebAnimationStyles()

  return (
    <>
      {activeToast && (
        <View
          style={[
            styles.container,
            {
              backgroundColor: toastStyles.backgroundColor,
              borderColor: toastStyles.borderColor,
              ...(isExiting
                ? animationStyles.exiting
                : animationStyles.entering),
            },
          ]}>
          <View
            style={[
              styles.iconContainer,
              {
                backgroundColor: 'transparent',
              },
            ]}>
            <IconComponent
              fill={toastStyles.iconColor}
              size="sm"
              style={styles.icon}
            />
          </View>
          <Text
            style={[
              styles.text,
              a.text_sm,
              a.font_bold,
              {color: toastStyles.textColor},
            ]}>
            {activeToast.text}
          </Text>
          <Pressable
            style={styles.dismissBackdrop}
            accessibilityLabel="Dismiss"
            accessibilityHint=""
            onPress={() => {
              setActiveToast(undefined)
            }}
          />
        </View>
      )}
    </>
  )
}

// methods
// =

export function show(
  text: string,
  type: ToastType | LegacyToastType = 'default',
) {
  if (toastTimeout) {
    clearTimeout(toastTimeout)
  }

  globalSetActiveToast?.({text, type: convertLegacyToastType(type)})
  toastTimeout = setTimeout(() => {
    globalSetActiveToast?.(undefined)
  }, DURATION)
}

const styles = StyleSheet.create({
  container: {
    // @ts-ignore web only
    position: 'fixed',
    left: 20,
    bottom: 20,
    // @ts-ignore web only
    width: 'calc(100% - 40px)',
    maxWidth: 380,
    padding: 20,
    flexDirection: 'row',
    alignItems: 'center',
    borderRadius: 10,
    borderWidth: 1,
  },
  dismissBackdrop: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
  },
  iconContainer: {
    width: 32,
    height: 32,
    borderRadius: 16,
    alignItems: 'center',
    justifyContent: 'center',
    flexShrink: 0,
  },
  icon: {
    flexShrink: 0,
  },
  text: {
    marginLeft: 10,
  },
})