Skip to content

Commit

Permalink
fix: use forwardRef for tooltip component
Browse files Browse the repository at this point in the history
  • Loading branch information
gndz07 authored Jan 7, 2025
1 parent bec2646 commit 8b4a072
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 38 deletions.
7 changes: 7 additions & 0 deletions components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Meta, StoryFn } from '@storybook/react';
import React from 'react';

import { modifyVariantsForStory } from '../../utils/modifyVariantsForStory';
import { Bubble as BubbleComponent } from '../Bubble';
import { Container } from '../Container';
import { Flex } from '../Flex';
import { Text } from '../Text';
Expand Down Expand Up @@ -75,4 +76,10 @@ NodeContent.argTypes = {
},
};

export const Bubble: StoryFn<typeof TooltipForStory> = (args) => (
<TooltipForStory {...args} content="This is a green bubble">
<BubbleComponent variant="green" size="large" />
</TooltipForStory>
);

export default Component;
39 changes: 22 additions & 17 deletions components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ const ArrowBox = styled(Box, {
color: '$tooltipContentBg',
});

export const TooltipContent = ({ css, multiline, children, ...props }: Partial<TooltipProps>) => {
export const TooltipContent = React.forwardRef<
React.ElementRef<typeof Content>,
Partial<TooltipProps>
>(({ css, multiline, children, ...props }, forwardedRef) => {
const isContentString = React.useMemo(() => typeof children === 'string', [children]);
return (
<Content css={css} side="top" align="center" sideOffset={5} {...props} multiline={multiline}>
<Content
css={css}
side="top"
align="center"
sideOffset={5}
{...props}
multiline={multiline}
ref={forwardedRef}
>
{isContentString ? (
<Text
size="1"
Expand All @@ -63,29 +74,23 @@ export const TooltipContent = ({ css, multiline, children, ...props }: Partial<T
</ArrowBox>
</Content>
);
};
});

export function Tooltip({
children,
content,
open,
defaultOpen,
onOpenChange,
multiline,
css,
...props
}: TooltipProps) {
return (
export const Tooltip = React.forwardRef<React.ElementRef<typeof Content>, TooltipProps>(
(
{ children, content, open, defaultOpen, onOpenChange, multiline, css, ...props },
forwardedRef,
) => (
<TooltipPrimitive.Root open={open} defaultOpen={defaultOpen} onOpenChange={onOpenChange}>
<TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger>
<TooltipPrimitive.Portal>
<TooltipContent css={css} multiline={multiline} {...props}>
<TooltipContent css={css} multiline={multiline} ref={forwardedRef} {...props}>
{content}
</TooltipContent>
</TooltipPrimitive.Portal>
</TooltipPrimitive.Root>
);
}
),
);

export const TooltipRoot = TooltipPrimitive.Root;
export const TooltipTrigger = TooltipPrimitive.Trigger;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"@radix-ui/react-tabs": "^1.0.1",
"@radix-ui/react-toggle": "^1.0.1",
"@radix-ui/react-toggle-group": "^1.0.1",
"@radix-ui/react-tooltip": "^1.0.7",
"@radix-ui/react-tooltip": "^1.1.6",
"@radix-ui/react-use-layout-effect": "^1.0.0",
"@radix-ui/react-visually-hidden": "^1.0.1",
"@rehookify/datepicker": "^6.6.7",
Expand Down
Loading

0 comments on commit 8b4a072

Please sign in to comment.