Skip to content

Commit

Permalink
fix: click menu outside to close
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Aug 17, 2024
1 parent 9a05762 commit 8b43bab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
19 changes: 11 additions & 8 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '../theme/unistyles'

import { PortalProvider } from '@gorhom/portal'
import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native'
import { useMigrations } from 'drizzle-orm/expo-sqlite/migrator'
import { useDrizzleStudio } from 'expo-drizzle-studio-plugin'
Expand Down Expand Up @@ -111,14 +112,16 @@ export default function Root() {
},
}}
>
<ToastProvider offsetTop={100} duration={1000} placement="top">
<ThemeProvider
value={UnistylesRuntime.colorScheme === 'light' ? DefaultTheme : DarkTheme}
>
{__DEV__ && <DrizzleStudio />}
<Slot />
</ThemeProvider>
</ToastProvider>
<PortalProvider>
<ToastProvider offsetTop={100} duration={1000} placement="top">
<ThemeProvider
value={UnistylesRuntime.colorScheme === 'light' ? DefaultTheme : DarkTheme}
>
{__DEV__ && <DrizzleStudio />}
<Slot />
</ThemeProvider>
</ToastProvider>
</PortalProvider>
</SWRConfig>
)
}
38 changes: 38 additions & 0 deletions components/menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Portal } from '@gorhom/portal'
import type { MenuComponentProps } from '@react-native-menu/menu'
import { MenuView } from '@react-native-menu/menu'
import { useState } from 'react'
import type { ViewStyle } from 'react-native'
import { Dimensions, Pressable } from 'react-native'

export type MenuProps = MenuComponentProps

export default function Menu({ children, ...props }: MenuProps) {
const [isOpen, setIsOpen] = useState(false)

const onPressAction: MenuComponentProps['onPressAction'] = ({ nativeEvent }) => {
setIsOpen(false)
props.onPressAction?.({ nativeEvent })
}

return (
<MenuView {...props} onPressAction={onPressAction}>
<Pressable onPress={() => setIsOpen(true)}>{children}</Pressable>
<Portal>
{isOpen && <Pressable style={absoluteViewStyle} onPress={() => setIsOpen(false)} />}
</Portal>
</MenuView>
)
}

const screenSize = Dimensions.get('screen')

const absoluteViewStyle: ViewStyle = {
position: 'absolute',
top: 0,
left: 0,
width: screenSize.width,
height: screenSize.height,

zIndex: 999,
}
7 changes: 4 additions & 3 deletions components/view-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { MenuView } from '@react-native-menu/menu'
import { useRouter } from 'expo-router'
import { useAtom } from 'jotai'

Expand All @@ -7,11 +6,13 @@ import type { TabViewIndex } from '~/atom/layout'
import { viewLayoutMapAtom } from '~/atom/layout'
import { Iconify } from '~/components'

import Menu from './menu'

export function ViewActions({ view }: { view: TabViewIndex }) {
const [viewLayoutMap, setViewLayoutMap] = useAtom(viewLayoutMapAtom)
const router = useRouter()
return (
<MenuView
<Menu
actions={[
{
id: 'mark-as-read',
Expand Down Expand Up @@ -59,6 +60,6 @@ export function ViewActions({ view }: { view: TabViewIndex }) {
<Iconify
icon="mingcute:more-2-fill"
/>
</MenuView>
</Menu>
)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"web": "expo start --web"
},
"dependencies": {
"@gorhom/portal": "^1.0.14",
"@radix-ui/colors": "^3.0.0",
"@react-native-menu/menu": "^1.1.2",
"@react-navigation/elements": "^1.3.31",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b43bab

Please sign in to comment.