Skip to content

Commit

Permalink
fix(tooltip): fix re-render errors when removing the label
Browse files Browse the repository at this point in the history
Remove render short circuit when label is falsy to avoid react hook re-render errors
  • Loading branch information
gcornut committed Sep 13, 2024
1 parent ad5386f commit 8f8d52a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Tooltip`: fix re-render errors when removing the label.

## [3.9.0][] - 2024-09-03

### Added
Expand Down
8 changes: 4 additions & 4 deletions packages/lumx-react/src/components/tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ const ARROW_SIZE = 8;
*/
export const Tooltip: Comp<TooltipProps, HTMLDivElement> = forwardRef((props, ref) => {
const { label, children, className, delay, placement, forceOpen, ...forwardedProps } = props;
// Disable in SSR or without a label.
if (!DOCUMENT || !label) {
return <TooltipContextProvider>{children}</TooltipContextProvider>;
// Disable in SSR.
if (!DOCUMENT) {
return <>{children}</>;
}

const id = useId();
Expand All @@ -87,7 +87,7 @@ export const Tooltip: Comp<TooltipProps, HTMLDivElement> = forwardRef((props, re

const position = attributes?.popper?.['data-popper-placement'] ?? placement;
const { isOpen: isActivated, onPopperMount } = useTooltipOpen(delay, anchorElement);
const isOpen = isActivated || forceOpen;
const isOpen = (isActivated || forceOpen) && !!label;
const wrappedChildren = useInjectTooltipRef(children, setAnchorElement, isOpen, id, label);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export const useInjectTooltipRef = (
setAnchorElement: (e: HTMLDivElement) => void,
isOpen: boolean | undefined,
id: string,
label: string,
label?: string | null | false,
): ReactNode => {
// Only add description when open
const describedBy = isOpen ? id : undefined;

return useMemo(() => {
if (!label) return children;

// Non-disabled element
if (React.isValidElement(children) && children.props.disabled !== true && children.props.isDisabled !== true) {
const ref = mergeRefs((children as any).ref, setAnchorElement);
Expand Down

0 comments on commit 8f8d52a

Please sign in to comment.