Skip to content

Commit

Permalink
asChild
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Aug 16, 2023
1 parent ac59eeb commit 39db03e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
16 changes: 13 additions & 3 deletions packages/zeego/src/dropdown-menu/dropdown-menu.web.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { cloneElement } from 'react'
import {
ItemPrimitive,
MenuContentProps,
Expand Down Expand Up @@ -32,17 +32,27 @@ const Root = ({ children, onOpenChange }: MenuRootProps) => {
Root.displayName = MenuDisplayName.Root

const TriggerView = forwardRef<unknown, any>((props, ref) => {
if (props.asChild) {
const { children, ...rest } = props
return cloneElement(children, {
ref,
...rest,
onClickCapture: props.onPointerDown,
})
}
return (
<View ref={ref} {...props} onClickCapture={props.onPointerDown}>
{props.children}
</View>
)
})

const Trigger = ({ children, style }: MenuTriggerProps) => {
const Trigger = ({ children, style, asChild }: MenuTriggerProps) => {
return (
<DropdownMenu.Trigger asChild>
<TriggerView style={style}>{children}</TriggerView>
<TriggerView style={style} asChild={asChild}>
{children}
</TriggerView>
</DropdownMenu.Trigger>
)
}
Expand Down
9 changes: 7 additions & 2 deletions packages/zeego/src/menu/create-android-menu/index.android.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Children, ReactElement } from 'react'
import React, { Children, ReactElement, cloneElement } from 'react'

import { MenuView } from '@react-native-menu/menu'

Expand Down Expand Up @@ -34,7 +34,12 @@ import type {
import { View } from 'react-native'

const createAndroidMenu = (Menu: 'ContextMenu' | 'DropdownMenu') => {
const Trigger = create(({ children, style }: MenuTriggerProps) => {
const Trigger = create(({ children, style, asChild }: MenuTriggerProps) => {
if (asChild) {
return cloneElement(children, {
style,
})
}
return <View style={style}>{children}</View>
}, 'Trigger')

Expand Down
9 changes: 7 additions & 2 deletions packages/zeego/src/menu/create-ios-menu/index.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
MenuSubContentProps,
ContextMenuAuxliliaryProps,
} from '../types'
import React, { Children, ReactElement, useRef } from 'react'
import React, { Children, ReactElement, cloneElement, useRef } from 'react'
import {
flattenChildren,
pickChildren,
Expand All @@ -38,7 +38,12 @@ import { create } from '../display-names'
import type { ImageSystemConfig } from 'react-native-ios-context-menu/src/types/ImageItemConfig'

const createIosMenu = (Menu: 'ContextMenu' | 'DropdownMenu') => {
const Trigger = create(({ children, style }: MenuTriggerProps) => {
const Trigger = create(({ children, style, asChild }: MenuTriggerProps) => {
if (asChild) {
return cloneElement(children, {
style,
})
}
return <View style={style}>{children}</View>
}, 'Trigger')

Expand Down
1 change: 1 addition & 0 deletions packages/zeego/src/menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type ContextMenuAuxliliaryProps = Omit<
export type MenuTriggerProps = {
children: React.ReactElement
style?: ViewStyle
asChild?: boolean
/**
* Determine whether the menu should open on `press` or `longPress`. Defaults to `press` for `DropdownMenu` and `longPress` for `ContextMenu`.
*
Expand Down

1 comment on commit 39db03e

@vercel
Copy link

@vercel vercel bot commented on 39db03e Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.