about summary refs log tree commit diff
path: root/src/view/shell/mobile/TabsSelector.tsx
blob: 921a0c85b51750da2e571365d8ff1dbc2d79ed9a (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import React, {createRef, useRef, useMemo, useState} from 'react'
import {observer} from 'mobx-react-lite'
import {
  Animated,
  ScrollView,
  Share,
  StyleSheet,
  TouchableWithoutFeedback,
  View,
} from 'react-native'
import {useSafeAreaInsets} from 'react-native-safe-area-context'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {Text} from '../../com/util/text/Text'
import Swipeable from 'react-native-gesture-handler/Swipeable'
import {useStores} from '../../../state'
import {s, colors} from '../../lib/styles'
import {toShareUrl} from '../../../lib/strings'
import {match} from '../../routes'
import {useAnimatedValue} from '../../lib/hooks/useAnimatedValue'

const TAB_HEIGHT = 42

export const TabsSelector = observer(
  ({
    active,
    tabMenuInterp,
    onClose,
  }: {
    active: boolean
    tabMenuInterp: Animated.Value
    onClose: () => void
  }) => {
    const store = useStores()
    const insets = useSafeAreaInsets()
    const [closingTabIndex, setClosingTabIndex] = useState<number | undefined>(
      undefined,
    )
    const closeInterp = useAnimatedValue(0)
    const tabsContainerRef = useRef<View>(null)
    const tabsRef = useRef<ScrollView>(null)
    const tabRefs = useMemo(
      () =>
        Array.from({length: store.nav.tabs.length}).map(() =>
          createRef<View>(),
        ),
      [store.nav.tabs.length],
    )

    const wrapperAnimStyle = {
      transform: [
        {
          translateY: tabMenuInterp.interpolate({
            inputRange: [0, 1.0],
            outputRange: [320, 0],
          }),
        },
      ],
    }

    // events
    // =

    const onPressNewTab = () => {
      store.nav.newTab('/')
      onClose()
    }
    const onPressCloneTab = () => {
      store.nav.newTab(store.nav.tab.current.url)
      onClose()
    }
    const onPressShareTab = () => {
      onClose()
      Share.share({url: toShareUrl(store.nav.tab.current.url)})
    }
    const onPressChangeTab = (tabIndex: number) => {
      store.nav.setActiveTab(tabIndex)
      onClose()
    }
    const onCloseTab = (tabIndex: number) => {
      setClosingTabIndex(tabIndex)
      closeInterp.setValue(0)
      Animated.timing(closeInterp, {
        toValue: 1,
        duration: 300,
        useNativeDriver: false,
      }).start(() => {
        setClosingTabIndex(undefined)
        store.nav.closeTab(tabIndex)
      })
    }
    const onLayout = () => {
      // focus the current tab
      const targetTab = tabRefs[store.nav.tabIndex]
      if (tabsContainerRef.current && tabsRef.current && targetTab.current) {
        targetTab.current.measureLayout?.(
          tabsContainerRef.current,
          (_left: number, top: number) => {
            tabsRef.current?.scrollTo({y: top, animated: false})
          },
          () => {},
        )
      }
    }

    // rendering
    // =

    const renderSwipeActions = () => {
      return <View style={[s.p2]} />
    }

    const currentTabIndex = store.nav.tabIndex
    const closingTabAnimStyle = {
      height: Animated.multiply(TAB_HEIGHT, Animated.subtract(1, closeInterp)),
      opacity: Animated.subtract(1, closeInterp),
      marginBottom: Animated.multiply(4, Animated.subtract(1, closeInterp)),
    }

    if (!active) {
      return <View testID="emptyView" />
    }

    return (
      <Animated.View
        testID="tabsSelectorView"
        style={[
          styles.wrapper,
          {bottom: insets.bottom + 55},
          wrapperAnimStyle,
        ]}>
        <View onLayout={onLayout}>
          <View style={[s.p10, styles.section]}>
            <View style={styles.btns}>
              <TouchableWithoutFeedback
                testID="shareButton"
                onPress={onPressShareTab}>
                <View style={[styles.btn]}>
                  <View style={styles.btnIcon}>
                    <FontAwesomeIcon size={16} icon="share" />
                  </View>
                  <Text style={styles.btnText}>Share</Text>
                </View>
              </TouchableWithoutFeedback>
              <TouchableWithoutFeedback
                testID="cloneButton"
                onPress={onPressCloneTab}>
                <View style={[styles.btn]}>
                  <View style={styles.btnIcon}>
                    <FontAwesomeIcon size={16} icon={['far', 'clone']} />
                  </View>
                  <Text style={styles.btnText}>Clone tab</Text>
                </View>
              </TouchableWithoutFeedback>
              <TouchableWithoutFeedback
                testID="newTabButton"
                onPress={onPressNewTab}>
                <View style={[styles.btn]}>
                  <View style={styles.btnIcon}>
                    <FontAwesomeIcon size={16} icon="plus" />
                  </View>
                  <Text style={styles.btnText}>New tab</Text>
                </View>
              </TouchableWithoutFeedback>
            </View>
          </View>
          <View
            ref={tabsContainerRef}
            style={[s.p10, styles.section, styles.sectionGrayBg]}>
            <ScrollView ref={tabsRef} style={styles.tabs}>
              {store.nav.tabs.map((tab, tabIndex) => {
                const {icon} = match(tab.current.url)
                const isActive = tabIndex === currentTabIndex
                const isClosing = closingTabIndex === tabIndex
                return (
                  <Swipeable
                    key={tab.id}
                    testID="tabsSwipable"
                    renderLeftActions={renderSwipeActions}
                    renderRightActions={renderSwipeActions}
                    leftThreshold={100}
                    rightThreshold={100}
                    onSwipeableWillOpen={() => onCloseTab(tabIndex)}>
                    <Animated.View
                      style={[
                        styles.tabOuter,
                        isClosing ? closingTabAnimStyle : undefined,
                      ]}>
                      <Animated.View
                        // HOTFIX
                        // TabsSelector.test.tsx snapshot fails if the
                        // ref was set like this: ref={tabRefs[tabIndex]}
                        ref={(ref: any) => (tabRefs[tabIndex] = ref)}
                        style={[
                          styles.tab,
                          styles.existing,
                          isActive && styles.active,
                        ]}>
                        <TouchableWithoutFeedback
                          testID="changeTabButton"
                          onPress={() => onPressChangeTab(tabIndex)}>
                          <View style={styles.tabInner}>
                            <View style={styles.tabIcon}>
                              <FontAwesomeIcon size={20} icon={icon} />
                            </View>
                            <Text
                              ellipsizeMode="tail"
                              numberOfLines={1}
                              suppressHighlighting={true}
                              style={[
                                styles.tabText,
                                isActive && styles.tabTextActive,
                              ]}>
                              {tab.current.title || tab.current.url}
                            </Text>
                          </View>
                        </TouchableWithoutFeedback>
                        <TouchableWithoutFeedback
                          testID="closeTabButton"
                          onPress={() => onCloseTab(tabIndex)}>
                          <View style={styles.tabClose}>
                            <FontAwesomeIcon
                              size={14}
                              icon="x"
                              style={styles.tabCloseIcon}
                            />
                          </View>
                        </TouchableWithoutFeedback>
                      </Animated.View>
                    </Animated.View>
                  </Swipeable>
                )
              })}
            </ScrollView>
          </View>
        </View>
      </Animated.View>
    )
  },
)

const styles = StyleSheet.create({
  wrapper: {
    position: 'absolute',
    width: '100%',
    height: 320,
    borderTopColor: colors.gray2,
    borderTopWidth: 1,
    backgroundColor: '#fff',
    opacity: 1,
  },
  section: {
    borderBottomColor: colors.gray2,
    borderBottomWidth: 1,
  },
  sectionGrayBg: {
    backgroundColor: colors.gray1,
  },
  tabs: {
    height: 240,
  },
  tabOuter: {
    height: TAB_HEIGHT + 4,
    overflow: 'hidden',
  },
  tab: {
    flexDirection: 'row',
    height: TAB_HEIGHT,
    backgroundColor: colors.gray1,
    alignItems: 'center',
    borderRadius: 4,
  },
  tabInner: {
    flexDirection: 'row',
    flex: 1,
    alignItems: 'center',
    paddingLeft: 12,
    paddingVertical: 12,
  },
  existing: {
    borderColor: colors.gray4,
    borderWidth: 1,
  },
  active: {
    backgroundColor: colors.white,
    borderColor: colors.black,
    borderWidth: 1,
  },
  tabIcon: {},
  tabText: {
    flex: 1,
    paddingHorizontal: 10,
    fontSize: 16,
  },
  tabTextActive: {
    fontWeight: '500',
  },
  tabClose: {
    paddingVertical: 16,
    paddingRight: 16,
  },
  tabCloseIcon: {
    color: '#655',
  },
  btns: {
    flexDirection: 'row',
    paddingTop: 2,
  },
  btn: {
    flexDirection: 'row',
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: colors.gray1,
    borderRadius: 4,
    marginRight: 5,
    paddingLeft: 12,
    paddingRight: 16,
    paddingVertical: 10,
  },
  btnIcon: {
    marginRight: 8,
  },
  btnText: {
    fontWeight: '500',
    fontSize: 16,
  },
})