From 2dbc20f7a87116a640e09810560bac1fc135a071 Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Wed, 6 Apr 2022 10:37:53 +0200 Subject: [PATCH] feat(system): add ref to popover and tooltip triggers --- .../src/system/popover/popover.tsx | 27 +++++++++++-------- .../src/system/tooltip/tooltip.tsx | 23 +++++++++++----- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/packages/status-react/src/system/popover/popover.tsx b/packages/status-react/src/system/popover/popover.tsx index e30965c..e284e5c 100644 --- a/packages/status-react/src/system/popover/popover.tsx +++ b/packages/status-react/src/system/popover/popover.tsx @@ -1,40 +1,45 @@ -import React, { forwardRef } from 'react' +import React, { cloneElement, forwardRef } from 'react' import * as Primitive from '@radix-ui/react-popover' import { Content } from './styles' +import type { PopoverContentProps } from '@radix-ui/react-popover' +import type { Ref } from 'react' + interface TriggerProps { children: [React.ReactElement, React.ReactElement] } -const PopoverTrigger = (props: TriggerProps) => { - const { children } = props +const PopoverTrigger = (props: TriggerProps, ref: Ref) => { + const { children, ...triggerProps } = props const [trigger, content] = children return ( - {trigger} + + {cloneElement(trigger, { ref, ...triggerProps })} + {content} ) } -interface PopoverProps { +const _PopoverTrigger = forwardRef(PopoverTrigger) + +interface PopoverProps extends PopoverContentProps { children: React.ReactNode } -const Popover = (props: PopoverProps, ref) => { - const { children } = props +const Popover = (props: PopoverProps) => { + const { children, ...contentProps } = props return ( - + {children} ) } -const _Popover = forwardRef(Popover) - -export { _Popover as Popover, PopoverTrigger } +export { Popover, _PopoverTrigger as PopoverTrigger } diff --git a/packages/status-react/src/system/tooltip/tooltip.tsx b/packages/status-react/src/system/tooltip/tooltip.tsx index ec32d5c..f1fc061 100644 --- a/packages/status-react/src/system/tooltip/tooltip.tsx +++ b/packages/status-react/src/system/tooltip/tooltip.tsx @@ -1,24 +1,33 @@ -import React from 'react' +import React, { cloneElement, forwardRef } from 'react' import * as Primitive from '@radix-ui/react-tooltip' import { Arrow, Content } from './styles' import type { TooltipContentProps } from '@radix-ui/react-tooltip' +import type { Ref } from 'react' interface Props { label: string - children: React.ReactNode + children: React.ReactElement side?: TooltipContentProps['side'] sideOffset?: TooltipContentProps['sideOffset'] } -const Tooltip = (props: Props) => { - const { children, label, side = 'top', sideOffset = 5 } = props +const Tooltip = (props: Props, ref: Ref) => { + const { + children, + label, + side = 'top', + sideOffset = 5, + ...triggerProps + } = props return ( - {children} + + {cloneElement(children, { ref, ...triggerProps })} + {label} @@ -27,5 +36,7 @@ const Tooltip = (props: Props) => { ) } -export { Tooltip } +const _Tooltip = forwardRef(Tooltip) + +export { _Tooltip as Tooltip } export type { Props as TooltipProps }