about summary refs log tree commit diff
path: root/src/view/shell/mobile/tabs-selector.tsx
blob: 10651ba1f159d073dad831afc5ce3b0213b3009b (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
import React, {forwardRef, useState, useImperativeHandle, useRef} from 'react'
import {StyleSheet, Text, TouchableWithoutFeedback, View} from 'react-native'
import BottomSheet from '@gorhom/bottom-sheet'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {s} from '../../lib/styles'
import {NavigationTabModel} from '../../../state/models/navigation'
import {createCustomBackdrop} from '../../com/util/BottomSheetCustomBackdrop'
import {match} from '../../routes'

const TAB_HEIGHT = 38
const TAB_SPACING = 5
const BOTTOM_MARGIN = 70

export const TabsSelectorModal = forwardRef(function TabsSelectorModal(
  {
    onNewTab,
    onChangeTab,
    onCloseTab,
    tabs,
    currentTabIndex,
  }: {
    onNewTab: () => void
    onChangeTab: (tabIndex: number) => void
    onCloseTab: (tabIndex: number) => void
    tabs: NavigationTabModel[]
    currentTabIndex: number
  },
  ref,
) {
  const [isOpen, setIsOpen] = useState<boolean>(false)
  const [snapPoints, setSnapPoints] = useState<number[]>([100])
  const bottomSheetRef = useRef<BottomSheet>(null)

  useImperativeHandle(ref, () => ({
    open() {
      setIsOpen(true)
      setSnapPoints([
        (tabs.length + 1) * (TAB_HEIGHT + TAB_SPACING) + BOTTOM_MARGIN,
      ])
      bottomSheetRef.current?.expand()
    },
  }))

  const onShareBottomSheetChange = (snapPoint: number) => {
    if (snapPoint === -1) {
      setIsOpen(false)
    }
  }
  const onPressNewTab = () => {
    onNewTab()
    onClose()
  }
  const onPressChangeTab = (tabIndex: number) => {
    onChangeTab(tabIndex)
    onClose()
  }
  const onClose = () => {
    setIsOpen(false)
    bottomSheetRef.current?.close()
  }
  return (
    <BottomSheet
      ref={bottomSheetRef}
      index={-1}
      snapPoints={snapPoints}
      enablePanDownToClose
      backdropComponent={isOpen ? createCustomBackdrop(onClose) : undefined}
      onChange={onShareBottomSheetChange}>
      <View style={s.p10}>
        {tabs.map((tab, tabIndex) => {
          const {icon} = match(tab.current.url)
          const isActive = tabIndex === currentTabIndex
          return (
            <View
              key={tabIndex}
              style={[styles.tab, styles.existing, isActive && styles.active]}>
              <TouchableWithoutFeedback
                onPress={() => onPressChangeTab(tabIndex)}>
                <View style={styles.tabIcon}>
                  <FontAwesomeIcon size={16} icon={icon} />
                </View>
              </TouchableWithoutFeedback>
              <TouchableWithoutFeedback
                onPress={() => onPressChangeTab(tabIndex)}>
                <Text
                  style={[styles.tabText, isActive && styles.tabTextActive]}>
                  {tab.current.title || tab.current.url}
                </Text>
              </TouchableWithoutFeedback>
              <TouchableWithoutFeedback onPress={() => onCloseTab(tabIndex)}>
                <View style={styles.tabClose}>
                  <FontAwesomeIcon
                    size={16}
                    icon="x"
                    style={styles.tabCloseIcon}
                  />
                </View>
              </TouchableWithoutFeedback>
            </View>
          )
        })}
        <TouchableWithoutFeedback onPress={onPressNewTab}>
          <View style={[styles.tab, styles.create]}>
            <View style={styles.tabIcon}>
              <FontAwesomeIcon size={16} icon="plus" />
            </View>
            <Text style={styles.tabText}>New tab</Text>
          </View>
        </TouchableWithoutFeedback>
      </View>
    </BottomSheet>
  )
})

const styles = StyleSheet.create({
  tab: {
    flexDirection: 'row',
    width: '100%',
    borderRadius: 4,
    height: TAB_HEIGHT,
    marginBottom: TAB_SPACING,
  },
  existing: {
    borderColor: '#000',
    borderWidth: 1,
  },
  create: {
    backgroundColor: '#F8F3F3',
  },
  active: {
    backgroundColor: '#faf0f0',
    borderColor: '#f00',
    borderWidth: 1,
  },
  tabIcon: {
    paddingTop: 10,
    paddingBottom: 10,
    paddingLeft: 15,
    paddingRight: 10,
  },
  tabText: {
    flex: 1,
    paddingTop: 10,
    paddingBottom: 10,
  },
  tabTextActive: {
    fontWeight: 'bold',
  },
  tabClose: {
    paddingTop: 10,
    paddingBottom: 10,
    paddingLeft: 10,
    paddingRight: 15,
  },
  tabCloseIcon: {
    color: '#655',
  },
})