feat(system): pass open state to children in ContextMenu

This commit is contained in:
Pavel Prichodko 2022-03-31 15:13:44 +02:00
parent b662c253f3
commit 43ab8e6240
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
1 changed files with 5 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import React from 'react'
import React, { cloneElement, useState } from 'react'
import * as Primitive from '@radix-ui/react-context-menu'
@ -18,12 +18,14 @@ interface TriggerProps extends ContextMenuTriggerProps {
const ContextMenuTrigger = (props: TriggerProps) => {
const { children, ...triggerProps } = props
const [open, setOpen] = useState(false)
const [trigger, menu] = children
return (
<Primitive.Root>
<Primitive.Root onOpenChange={setOpen}>
<Primitive.Trigger {...triggerProps} asChild>
{trigger}
{cloneElement(trigger, { 'data-open': open })}
</Primitive.Trigger>
{menu}
</Primitive.Root>