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: div-in-p issue in tooltip #4053

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/components/tooltip/src/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Children, cloneElement, isValidElement} from "react";
import {getTransformOrigins} from "@nextui-org/aria-utils";
import {mergeProps} from "@react-aria/utils";

import {UseTooltipProps, useTooltip} from "./use-tooltip";
import {UseTooltipProps, isLazyElement, useTooltip} from "./use-tooltip";

export interface TooltipProps extends Omit<UseTooltipProps, "disableTriggerFocus" | "backdrop"> {}

Expand Down Expand Up @@ -41,8 +41,10 @@ const Tooltip = forwardRef<"div", TooltipProps>((props, ref) => {

if (childrenNum !== 1) throw new Error();

if (!isValidElement(children)) {
if (!isValidElement(children) && !isLazyElement(children)) {
trigger = <p {...getTriggerProps()}>{children}</p>;
} else if (isLazyElement(children)) {
trigger = <div {...getTriggerProps()}>{children}</div>;
} else {
const child = children as React.ReactElement & {
ref?: React.Ref<any>;
Expand Down
17 changes: 16 additions & 1 deletion packages/components/tooltip/src/use-tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {OverlayTriggerProps} from "@react-types/overlays";
import type {HTMLMotionProps} from "framer-motion";
import type {OverlayOptions} from "@nextui-org/aria-utils";

import {ReactNode, Ref, useId, useImperativeHandle} from "react";
import {ReactElement, ReactNode, Ref, useId, useImperativeHandle} from "react";
import {useTooltipTriggerState} from "@react-stately/tooltip";
import {mergeProps} from "@react-aria/utils";
import {useTooltip as useReactAriaTooltip, useTooltipTrigger} from "@react-aria/tooltip";
Expand Down Expand Up @@ -91,6 +91,8 @@ export type UseTooltipProps = Props &
OverlayOptions &
PopoverVariantProps;

export type LazyElement = ReactElement | ReactNode | null | undefined;

export function useTooltip(originalProps: UseTooltipProps) {
const globalContext = useProviderContext();
const [props, variantProps] = mapPropsVariants(originalProps, popover.variantKeys);
Expand Down Expand Up @@ -294,4 +296,17 @@ export function useTooltip(originalProps: UseTooltipProps) {
};
}

export function isLazyElement(element: LazyElement): boolean {
if (typeof element !== "object" || element === null) {
return false;
}

// Check if it's a ReactElement and has the $$typeof property of React.lazy
const reactElement = element as ReactNode & {
$$typeof: symbol;
};

return reactElement.$$typeof === Symbol.for("react.lazy");
}
Comment on lines +299 to +310
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add JSDoc documentation and improve type safety

The function implementation is correct but could benefit from better documentation and type safety.

  1. Add JSDoc documentation:
+/**
+ * Checks if the given element is a React lazy component.
+ * @param element - The element to check
+ * @returns true if the element is a React lazy component, false otherwise
+ */
export function isLazyElement(element: LazyElement): boolean {
  1. Make it a type guard for better type safety:
-export function isLazyElement(element: LazyElement): boolean {
+export function isLazyElement(element: LazyElement): element is ReactElement {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export function isLazyElement(element: LazyElement): boolean {
if (typeof element !== "object" || element === null) {
return false;
}
// Check if it's a ReactElement and has the $$typeof property of React.lazy
const reactElement = element as ReactNode & {
$$typeof: symbol;
};
return reactElement.$$typeof === Symbol.for("react.lazy");
}
/**
* Checks if the given element is a React lazy component.
* @param element - The element to check
* @returns true if the element is a React lazy component, false otherwise
*/
export function isLazyElement(element: LazyElement): element is ReactElement {
if (typeof element !== "object" || element === null) {
return false;
}
// Check if it's a ReactElement and has the $$typeof property of React.lazy
const reactElement = element as ReactNode & {
$$typeof: symbol;
};
return reactElement.$$typeof === Symbol.for("react.lazy");
}


export type UseTooltipReturn = ReturnType<typeof useTooltip>;