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

Tooltip: Fix positioning by anchoring to child element #41268

Merged
merged 17 commits into from
Jul 19, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `ComboboxControl`: use custom prefix when generating the instanceId ([#42134](https://github.com/WordPress/gutenberg/pull/42134).
- `Popover`: pass missing anchor ref to the `getAnchorRect` callback prop. ([#42076](https://github.com/WordPress/gutenberg/pull/42076)).
- `Popover`: call `getAnchorRect` callback prop even if `anchorRefFallback` has no value. ([#42329](https://github.com/WordPress/gutenberg/pull/42329)).
- Fix `ToolTip` position to ensure it is always positioned relative to the first child of the ToolTip. ([#41268](https://github.com/WordPress/gutenberg/pull/41268))

### Internal

Expand Down
46 changes: 37 additions & 9 deletions packages/components/src/tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
concatChildren,
useEffect,
useState,
useRef,
} from '@wordpress/element';
import { useDebounce } from '@wordpress/compose';
import { useDebounce, useMergeRefs } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -31,29 +32,45 @@ export const TOOLTIP_DELAY = 700;

const eventCatcher = <div className="event-catcher" />;

const getDisabledElement = ( { eventHandlers, child, childrenWithPopover } ) =>
cloneElement(
const getDisabledElement = ( {
eventHandlers,
child,
childrenWithPopover,
mergedRefs,
} ) => {
return cloneElement(
<span className="disabled-element-wrapper">
{ cloneElement( eventCatcher, eventHandlers ) }
{ cloneElement( child, {
children: childrenWithPopover,
ref: mergedRefs,
} ) }
</span>,
eventHandlers
{ ...eventHandlers }
);
};

const getRegularElement = ( { child, eventHandlers, childrenWithPopover } ) =>
cloneElement( child, {
const getRegularElement = ( {
child,
eventHandlers,
childrenWithPopover,
mergedRefs,
} ) => {
return cloneElement( child, {
...eventHandlers,
children: childrenWithPopover,
ref: mergedRefs,
} );
};

const addPopoverToGrandchildren = ( {
anchorRef,
grandchildren,
isOver,
offset,
position,
text,
shortcut,
text,
} ) =>
concatChildren(
grandchildren,
Expand All @@ -64,7 +81,8 @@ const addPopoverToGrandchildren = ( {
className="components-tooltip"
aria-hidden="true"
animate={ false }
offset={ 12 }
offset={ offset }
anchorRef={ anchorRef }
__unstableShift
>
{ text }
Expand Down Expand Up @@ -111,6 +129,13 @@ function Tooltip( props ) {
const [ isOver, setIsOver ] = useState( false );
const delayedSetIsOver = useDebounce( setIsOver, delay );

// Create a reference to the Tooltip's child, to be passed to the Popover
// so that the Tooltip can be correctly positioned. Also, merge with the
// existing ref for the first child, so that its ref is preserved.
const childRef = useRef( null );
const existingChildRef = Children.toArray( children )[ 0 ]?.ref;
const mergedChildRefs = useMergeRefs( [ childRef, existingChildRef ] );

const createMouseDown = ( event ) => {
// Preserve original child callback behavior.
emitToChild( children, 'onMouseDown', event );
Expand Down Expand Up @@ -214,10 +239,12 @@ function Tooltip( props ) {
: getRegularElement;

const popoverData = {
anchorRef: childRef,
isOver,
offset: 4,
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
position,
text,
shortcut,
text,
};
const childrenWithPopover = addPopoverToGrandchildren( {
grandchildren,
Expand All @@ -228,6 +255,7 @@ function Tooltip( props ) {
child,
eventHandlers,
childrenWithPopover,
mergedRefs: mergedChildRefs,
} );
}

Expand Down
41 changes: 28 additions & 13 deletions packages/components/src/tooltip/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,34 @@ export const _default = () => {
const position = select( 'Position', positionOptions, 'top center' );
const delay = number( 'Delay', 700 );
return (
<Tooltip text={ tooltipText } position={ position } delay={ delay }>
<div
style={ {
margin: '50px auto',
width: '200px',
padding: '20px',
textAlign: 'center',
border: '1px solid #ccc',
} }
>
Hover for more information
</div>
</Tooltip>
<>
<Tooltip text={ tooltipText } position={ position } delay={ delay }>
<div
style={ {
margin: '50px auto',
width: '200px',
padding: '20px',
textAlign: 'center',
border: '1px solid #ccc',
} }
>
Hover for more information
</div>
</Tooltip>
<Tooltip text={ tooltipText } position={ position } delay={ delay }>
<div
style={ {
margin: '50px auto',
width: 'min-content',
padding: '4px',
textAlign: 'center',
border: '1px solid #ccc',
} }
>
Small target
</div>
</Tooltip>
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/tooltip/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
z-index: z-index(".components-tooltip");

.components-popover__content {
min-width: 0;
min-width: min-content;
}
}

Expand Down