Skip to content

Commit

Permalink
[Tooltip] Fix trackCursorAxis prop
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Oct 17, 2024
1 parent 7445aad commit 60e1021
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/data/components/tooltip/tooltip.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ It automatically positions a wrapper element that can be styled or contain a cus

## Cursor following

The tooltip can follow the cursor on both axes or one axis using the `followCursorAxis` prop on `Tooltip.Root`. Possible values are: `none` (default), `both`, `x`, or `y`.
The tooltip can follow the cursor on both axes or one axis using the `trackCursorAxis` prop on `Tooltip.Root`. Possible values are: `none` (default), `both`, `x`, or `y`.

<Demo demo="UnstyledTooltipFollowCursor" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import type { Boundary, Padding, VirtualElement, FloatingRootContext } from '@fl
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useAnchorPositioning } from '../../utils/useAnchorPositioning';
import type { GenericHTMLProps } from '../../utils/types';
import { useTooltipRootContext } from '../Root/TooltipRootContext';

export function useTooltipPositioner(
params: useTooltipPositioner.Parameters,
): useTooltipPositioner.ReturnValue {
const { open = false, keepMounted = false, trackCursorAxis = 'none' } = params;

const { triggerElement } = useTooltipRootContext();

const {
positionerStyles,
arrowStyles,
Expand All @@ -17,7 +20,10 @@ export function useTooltipPositioner(
arrowUncentered,
renderedSide,
renderedAlignment,
} = useAnchorPositioning(params);
} = useAnchorPositioning({
...params,
triggerElement,
});

const getPositionerProps: useTooltipPositioner.ReturnValue['getPositionerProps'] =
React.useCallback(
Expand Down
10 changes: 7 additions & 3 deletions packages/mui-base/src/utils/useAnchorPositioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface UseAnchorPositioningParameters {
mounted?: boolean;
trackAnchor?: boolean;
nodeId?: string;
triggerElement?: Element | null;
}

interface UseAnchorPositioningReturnValue {
Expand Down Expand Up @@ -86,6 +87,7 @@ export function useAnchorPositioning(
arrowPadding = 5,
mounted = true,
trackAnchor = true,
triggerElement,
nodeId,
} = params;

Expand Down Expand Up @@ -210,10 +212,12 @@ export function useAnchorPositioning(

if (resolvedAnchor) {
const unwrappedElement = isRef(resolvedAnchor) ? resolvedAnchor.current : resolvedAnchor;
refs.setPositionReference(unwrappedElement);
registeredPositionReferenceRef.current = unwrappedElement;
if (unwrappedElement !== triggerElement) {
refs.setPositionReference(unwrappedElement);
registeredPositionReferenceRef.current = unwrappedElement;
}
}
}, [refs, anchor]);
}, [refs, anchor, triggerElement]);

React.useEffect(() => {
// Refs from parent components are set after useLayoutEffect runs and are available in useEffect.
Expand Down

0 comments on commit 60e1021

Please sign in to comment.