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(false) const [snapPoints, setSnapPoints] = useState([100]) const bottomSheetRef = useRef(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 ( {tabs.map((tab, tabIndex) => { const {icon} = match(tab.current.url) const isActive = tabIndex === currentTabIndex return ( onPressChangeTab(tabIndex)}> onPressChangeTab(tabIndex)}> {tab.current.title || tab.current.url} onCloseTab(tabIndex)}> ) })} New tab ) }) 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', }, })