Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tooltip: render non-string content outside of the Text component #557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
migrate to passing a textAs prop
  • Loading branch information
evanfrawley committed Aug 13, 2024
commit 85fc6ac7dd317cf460bdd62165094cbaa443e6b7
27 changes: 15 additions & 12 deletions apps/playground/app/sink/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,21 @@ export default function Sink() {
</Button>
</Tooltip>

<Tooltip content={
<DataList.Root my="1">
<DataList.Item>
<DataList.Label style={{ color: 'var(--gray-5)'}}>Name</DataList.Label>
<DataList.Value style={{ color: 'var(--gray-1)'}}>Susan Kare</DataList.Value>
</DataList.Item>
<DataList.Item>
<DataList.Label style={{ color: 'var(--gray-5)'}}>Email</DataList.Label>
<DataList.Value style={{ color: 'var(--gray-1)'}}>susan.kare@apple.com</DataList.Value>
</DataList.Item>
</DataList.Root>
}>
<Tooltip
textAs="div"
content={
<DataList.Root my="1">
<DataList.Item>
<DataList.Label style={{ color: 'var(--gray-5)'}}>Name</DataList.Label>
<DataList.Value style={{ color: 'var(--gray-1)'}}>Susan Kare</DataList.Value>
</DataList.Item>
<DataList.Item>
<DataList.Label style={{ color: 'var(--gray-5)'}}>Email</DataList.Label>
<DataList.Value style={{ color: 'var(--gray-1)'}}>susan.kare@apple.com</DataList.Value>
</DataList.Item>
</DataList.Root>
}
>
<Button variant="solid" size="1">
Non-string ReactNode
</Button>
Expand Down
3 changes: 3 additions & 0 deletions packages/radix-ui-themes/src/components/tooltip.props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { widthPropDefs } from '../props/width.props.js';
import { textPropDefs } from './text.props.js';

import type { PropDef, GetPropDefTypes } from '../props/prop-def.js';

Expand All @@ -7,11 +8,13 @@ const tooltipPropDefs = {
width: widthPropDefs.width,
minWidth: widthPropDefs.minWidth,
maxWidth: { ...widthPropDefs.maxWidth, default: '360px' },
textAs: textPropDefs['as']
} satisfies {
width: PropDef<string>;
minWidth: PropDef<string>;
maxWidth: PropDef<string>;
content: PropDef<React.ReactNode>;
textAs: PropDef<string>;
};

type TooltipOwnProps = GetPropDefTypes<typeof tooltipPropDefs & typeof widthPropDefs>;
Expand Down
11 changes: 4 additions & 7 deletions packages/radix-ui-themes/src/components/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Tooltip = React.forwardRef<TooltipElement, TooltipProps>((props, forwarded
content,
container,
forceMount,
textAs,
...tooltipContentProps
} = extractProps(props, tooltipPropDefs);
const rootProps = { open, defaultOpen, onOpenChange, delayDuration, disableHoverableContent };
Expand All @@ -47,13 +48,9 @@ const Tooltip = React.forwardRef<TooltipElement, TooltipProps>((props, forwarded
ref={forwardedRef}
className={classNames('rt-TooltipContent', className)}
>
{typeof content === 'string' ? (
<Text as="p" className="rt-TooltipText" size="1">
{content}
</Text>
) : (
<>{content}</>
)}
<Text as={textAs ?? 'p'} className="rt-TooltipText" size="1">
{content}
</Text>
<TooltipPrimitive.Arrow className="rt-TooltipArrow" />
</TooltipPrimitive.Content>
</Theme>
Expand Down