diff --git a/src/state/models/shell.ts b/src/state/models/shell.ts index 2dddb9a3..80ecbdd4 100644 --- a/src/state/models/shell.ts +++ b/src/state/models/shell.ts @@ -1,6 +1,14 @@ -import {makeAutoObservable} from 'mobx' +import {makeAutoObservable, runInAction} from 'mobx' import {ProfileViewModel} from './profile-view' +export class TabsSelectorModel { + name = 'tabs-selector' + + constructor() { + makeAutoObservable(this) + } +} + export class LinkActionsModel { name = 'link-actions' @@ -36,6 +44,7 @@ export class EditProfileModel { export class ShellModel { isModalActive = false activeModal: + | TabsSelectorModel | LinkActionsModel | SharePostModel | ComposePostModel @@ -48,6 +57,7 @@ export class ShellModel { openModal( modal: + | TabsSelectorModel | LinkActionsModel | SharePostModel | ComposePostModel diff --git a/src/view/com/modals/Modal.tsx b/src/view/com/modals/Modal.tsx index 73ac1446..f3a69d2a 100644 --- a/src/view/com/modals/Modal.tsx +++ b/src/view/com/modals/Modal.tsx @@ -1,4 +1,4 @@ -import React, {useRef} from 'react' +import React, {useRef, useEffect} from 'react' import {View} from 'react-native' import {observer} from 'mobx-react-lite' import BottomSheet from '@gorhom/bottom-sheet' @@ -7,11 +7,14 @@ import {createCustomBackdrop} from '../util/BottomSheetCustomBackdrop' import * as models from '../../../state/models/shell' +import * as TabsSelectorModal from './TabsSelector' import * as LinkActionsModal from './LinkActions' import * as SharePostModal from './SharePost.native' import * as ComposePostModal from './ComposePost' import * as EditProfile from './EditProfile' +const CLOSED_SNAPPOINTS = ['10%'] + export const Modal = observer(function Modal() { const store = useStores() const bottomSheetRef = useRef(null) @@ -25,12 +28,24 @@ export const Modal = observer(function Modal() { bottomSheetRef.current?.close() } - if (!store.shell.isModalActive) { - return - } + useEffect(() => { + if (store.shell.isModalActive) { + bottomSheetRef.current?.expand() + } else { + bottomSheetRef.current?.close() + } + }, [store.shell.isModalActive, bottomSheetRef]) - let snapPoints, element - if (store.shell.activeModal?.name === 'link-actions') { + let snapPoints: (string | number)[] = CLOSED_SNAPPOINTS + let element + if (store.shell.activeModal?.name === 'tabs-selector') { + snapPoints = TabsSelectorModal.snapPoints + element = ( + + ) + } else if (store.shell.activeModal?.name === 'link-actions') { snapPoints = LinkActionsModal.snapPoints element = ( ) } else { - return + element = } return ( {element} diff --git a/src/view/com/modals/TabsSelector.tsx b/src/view/com/modals/TabsSelector.tsx new file mode 100644 index 00000000..ef7d4705 --- /dev/null +++ b/src/view/com/modals/TabsSelector.tsx @@ -0,0 +1,307 @@ +import React, {createRef, useRef, useMemo} from 'react' +import {observer} from 'mobx-react-lite' +import { + ScrollView, + StyleSheet, + Text, + TouchableOpacity, + TouchableWithoutFeedback, + View, +} from 'react-native' +import {IconProp} from '@fortawesome/fontawesome-svg-core' +import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' +import Swipeable from 'react-native-gesture-handler/Swipeable' +import LinearGradient from 'react-native-linear-gradient' +import {useStores} from '../../../state' +import {s, colors, gradients} from '../../lib/styles' +import {match} from '../../routes' + +export const snapPoints = [500] + +export const Component = observer(() => { + const store = useStores() + const tabsRef = useRef(null) + const tabRefs = useMemo( + () => + Array.from({length: store.nav.tabs.length}).map(() => createRef()), + [store.nav.tabs.length], + ) + + // events + // = + + const onPressNewTab = () => { + store.nav.newTab('/') + onClose() + } + const onPressCloneTab = () => { + store.nav.newTab(store.nav.tab.current.url) + onClose() + } + const onPressShareTab = () => { + onClose() + // TODO + } + const onPressChangeTab = (tabIndex: number) => { + store.nav.setActiveTab(tabIndex) + onClose() + } + const onCloseTab = (tabIndex: number) => store.nav.closeTab(tabIndex) + const onNavigate = (url: string) => { + store.nav.navigate(url) + onClose() + } + const onClose = () => { + store.shell.closeModal() + } + const onLayout = () => { + // focus the current tab + const targetTab = tabRefs[store.nav.tabIndex] + if (tabsRef.current && targetTab.current) { + targetTab.current.measureLayout?.( + tabsRef.current, + (_left: number, top: number) => { + tabsRef.current?.scrollTo({y: top, animated: false}) + }, + () => {}, + ) + } + } + + // rendering + // = + + const FatMenuItem = ({ + icon, + label, + url, + gradient, + }: { + icon: IconProp + label: string + url: string + gradient: keyof typeof gradients + }) => ( + onNavigate(url)}> + + + + {label} + + ) + + const renderSwipeActions = () => { + return + } + + const currentTabIndex = store.nav.tabIndex + return ( + + + + + + + + + + + + + + + + + New tab + + + + + + + + Clone tab + + + + + + + + Share + + + + + + + {store.nav.tabs.map((tab, tabIndex) => { + const {icon} = match(tab.current.url) + const isActive = tabIndex === currentTabIndex + return ( + onCloseTab(tabIndex)}> + + onPressChangeTab(tabIndex)}> + + + + + onPressChangeTab(tabIndex)}> + + {tab.current.title || tab.current.url} + + + onCloseTab(tabIndex)}> + + + + + + + ) + })} + + + + ) +}) + +const styles = StyleSheet.create({ + section: { + borderBottomColor: colors.gray2, + borderBottomWidth: 1, + }, + sectionGrayBg: { + backgroundColor: colors.gray1, + }, + fatMenuItems: { + flexDirection: 'row', + marginTop: 10, + marginBottom: 10, + }, + fatMenuItem: { + width: 90, + alignItems: 'center', + marginRight: 6, + }, + fatMenuItemIconWrapper: { + borderRadius: 6, + width: 60, + height: 60, + justifyContent: 'center', + alignItems: 'center', + marginBottom: 5, + shadowColor: '#000', + shadowOpacity: 0.2, + shadowOffset: {width: 0, height: 2}, + shadowRadius: 2, + }, + fatMenuItemIcon: { + color: colors.white, + }, + fatMenuItemLabel: { + fontSize: 13, + }, + tabs: { + height: 240, + }, + tab: { + flexDirection: 'row', + backgroundColor: colors.gray1, + alignItems: 'center', + borderRadius: 4, + paddingLeft: 12, + paddingRight: 16, + marginBottom: 4, + }, + existing: { + borderColor: colors.gray4, + borderWidth: 1, + }, + active: { + backgroundColor: colors.white, + borderColor: colors.black, + borderWidth: 1, + }, + tabIcon: {}, + tabText: { + flex: 1, + paddingHorizontal: 10, + paddingVertical: 12, + fontSize: 16, + }, + tabTextActive: { + fontWeight: '500', + }, + tabClose: { + padding: 2, + }, + 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, + }, +}) diff --git a/src/view/shell/mobile/index.tsx b/src/view/shell/mobile/index.tsx index b752221f..2a981e10 100644 --- a/src/view/shell/mobile/index.tsx +++ b/src/view/shell/mobile/index.tsx @@ -23,9 +23,9 @@ import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {IconProp} from '@fortawesome/fontawesome-svg-core' import {useStores} from '../../../state' import {NavigationModel} from '../../../state/models/navigation' +import {TabsSelectorModel} from '../../../state/models/shell' import {match, MatchResult} from '../../routes' import {Modal} from '../../com/modals/Modal' -import {TabsSelectorModal} from './tabs-selector' import {LocationNavigator} from './location-navigator' import {createBackMenu, createForwardMenu} from './history-menu' import {createAccountsMenu} from './accounts-menu' @@ -106,7 +106,6 @@ const Btn = ({ export const MobileShell: React.FC = observer(() => { const store = useStores() - const tabSelectorRef = useRef<{open: () => void}>() const [isLocationMenuActive, setLocationMenuActive] = useState(false) const winDim = useWindowDimensions() const swipeGestureInterp = useSharedValue(0) @@ -129,15 +128,11 @@ export const MobileShell: React.FC = observer(() => { const onPressForward = () => store.nav.tab.goForward() const onPressHome = () => store.nav.navigate('/') const onPressNotifications = () => store.nav.navigate('/notifications') - const onPressTabs = () => tabSelectorRef.current?.open() + const onPressTabs = () => store.shell.openModal(new TabsSelectorModel()) const onLongPressBack = () => createBackMenu(store.nav.tab) const onLongPressForward = () => createForwardMenu(store.nav.tab) - const onNewTab = () => store.nav.newTab('/') - const onChangeTab = (tabIndex: number) => store.nav.setActiveTab(tabIndex) - const onCloseTab = (tabIndex: number) => store.nav.closeTab(tabIndex) - const goBack = () => store.nav.tab.goBack() const swipeGesture = Gesture.Pan() .onUpdate(e => { @@ -231,14 +226,6 @@ export const MobileShell: React.FC = observer(() => { - {isLocationMenuActive && ( 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', - }, -}) diff --git a/todos.txt b/todos.txt index 1273e977..2a37f685 100644 --- a/todos.txt +++ b/todos.txt @@ -2,6 +2,7 @@ Paul's todo list - General - Update to RN 0.70 + - Add close animation to tabs selector - Composer - Update the view after creating a post - Profile