From feb14f24f497de44c49b2d7228f624c97680dabe Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Tue, 24 May 2022 15:08:57 +1000 Subject: [PATCH 01/17] ToolTip: Fix positioning by anchoring to child element --- packages/components/src/tooltip/index.js | 48 +++++++++++++++------- packages/components/src/tooltip/style.scss | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/packages/components/src/tooltip/index.js b/packages/components/src/tooltip/index.js index 7bf2d4af2907dc..4343cdcd788779 100644 --- a/packages/components/src/tooltip/index.js +++ b/packages/components/src/tooltip/index.js @@ -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 @@ -31,22 +32,37 @@ export const TOOLTIP_DELAY = 700; const eventCatcher =
; -const getDisabledElement = ( { eventHandlers, child, childrenWithPopover } ) => - cloneElement( +const DisabledElement = ( { + eventHandlers, + child, + childrenWithPopover, + anchorRef, +} ) => { + const ref = useMergeRefs( [ anchorRef, child?.ref ] ); + return cloneElement( { cloneElement( eventCatcher, eventHandlers ) } { cloneElement( child, { children: childrenWithPopover, } ) } , - eventHandlers + { ...eventHandlers, ref } ); +}; -const getRegularElement = ( { child, eventHandlers, childrenWithPopover } ) => - cloneElement( child, { +const RegularElement = ( { + child, + eventHandlers, + childrenWithPopover, + anchorRef, +} ) => { + const ref = useMergeRefs( [ anchorRef, child?.ref ] ); + return cloneElement( child, { ...eventHandlers, children: childrenWithPopover, + ref, } ); +}; const addPopoverToGrandchildren = ( { grandchildren, @@ -54,6 +70,7 @@ const addPopoverToGrandchildren = ( { position, text, shortcut, + anchorRef, } ) => concatChildren( grandchildren, @@ -110,6 +127,7 @@ function Tooltip( props ) { const [ isMouseDown, setIsMouseDown ] = useState( false ); const [ isOver, setIsOver ] = useState( false ); const delayedSetIsOver = useDebounce( setIsOver, delay ); + const childRef = useRef( null ); const createMouseDown = ( event ) => { // Preserve original child callback behavior. @@ -209,11 +227,10 @@ function Tooltip( props ) { const child = Children.only( children ); const { children: grandchildren, disabled } = child.props; - const getElementWithPopover = disabled - ? getDisabledElement - : getRegularElement; + const ElementWithPopover = disabled ? DisabledElement : RegularElement; const popoverData = { + anchorRef: childRef, isOver, position, text, @@ -224,11 +241,14 @@ function Tooltip( props ) { ...popoverData, } ); - return getElementWithPopover( { - child, - eventHandlers, - childrenWithPopover, - } ); + return ( + + ); } export default Tooltip; diff --git a/packages/components/src/tooltip/style.scss b/packages/components/src/tooltip/style.scss index 3de494fa60359e..9a874246568f6b 100644 --- a/packages/components/src/tooltip/style.scss +++ b/packages/components/src/tooltip/style.scss @@ -2,7 +2,7 @@ z-index: z-index(".components-tooltip"); .components-popover__content { - min-width: 0; + min-width: min-content; } } From 7e7715ce8523fca3034c1439e4467859a938986e Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Tue, 24 May 2022 15:36:04 +1000 Subject: [PATCH 02/17] Fix left/right issue --- packages/components/src/popover/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index e93442a70f1b20..a463de013d3d7c 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -55,9 +55,9 @@ const positionToPlacement = ( position ) => { if ( [ 'top', 'bottom' ].includes( x ) ) { let suffix = ''; - if ( ( !! z && z === 'left' ) || y === 'right' ) { + if ( ( !! z && z === 'left' ) || y === 'left' ) { suffix = '-start'; - } else if ( ( !! z && z === 'right' ) || y === 'left' ) { + } else if ( ( !! z && z === 'right' ) || y === 'right' ) { suffix = '-end'; } return x + suffix; From a52ee640fbf3459b990d3f7d22d741367a480c79 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Tue, 24 May 2022 16:08:15 +1000 Subject: [PATCH 03/17] Switch post schedule and post visiblity to 'bottom right' for consistency --- .../edit-post/src/components/sidebar/post-schedule/index.js | 2 +- .../edit-post/src/components/sidebar/post-visibility/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/edit-post/src/components/sidebar/post-schedule/index.js b/packages/edit-post/src/components/sidebar/post-schedule/index.js index 7c9021894e26f3..dfd0bd83d0e060 100644 --- a/packages/edit-post/src/components/sidebar/post-schedule/index.js +++ b/packages/edit-post/src/components/sidebar/post-schedule/index.js @@ -18,7 +18,7 @@ export default function PostSchedule() { { __( 'Publish' ) } ( Date: Tue, 24 May 2022 16:34:31 +1000 Subject: [PATCH 04/17] Move left/right transformation to the ToolTip component --- packages/components/src/popover/index.js | 4 +-- packages/components/src/tooltip/index.js | 35 +++++++++++++++++-- .../components/sidebar/post-schedule/index.js | 2 +- .../sidebar/post-visibility/index.js | 2 +- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index a463de013d3d7c..e93442a70f1b20 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -55,9 +55,9 @@ const positionToPlacement = ( position ) => { if ( [ 'top', 'bottom' ].includes( x ) ) { let suffix = ''; - if ( ( !! z && z === 'left' ) || y === 'left' ) { + if ( ( !! z && z === 'left' ) || y === 'right' ) { suffix = '-start'; - } else if ( ( !! z && z === 'right' ) || y === 'right' ) { + } else if ( ( !! z && z === 'right' ) || y === 'left' ) { suffix = '-end'; } return x + suffix; diff --git a/packages/components/src/tooltip/index.js b/packages/components/src/tooltip/index.js index 4343cdcd788779..30622215d4aba1 100644 --- a/packages/components/src/tooltip/index.js +++ b/packages/components/src/tooltip/index.js @@ -32,6 +32,33 @@ export const TOOLTIP_DELAY = 700; const eventCatcher =
; +/** + * Convert a position string to a placement string. This logic + * is borrowed from the Popover component, with one key difference: + * + * Here, the 'left' position translates to the '-start' placement and + * the 'right' position translates to the '-end' placement. This ensures + * that the ToolTip is placed at the expected left/right location. + * + * @param {string} position A position string such as 'bottom left' + * @return {string} A placement string such as 'bottom-start' + */ +const positionToPlacement = ( position ) => { + const [ x, y, z ] = position.split( ' ' ); + + if ( [ 'top', 'bottom' ].includes( x ) ) { + let suffix = ''; + if ( ( !! z && z === 'left' ) || y === 'left' ) { + suffix = '-start'; + } else if ( ( !! z && z === 'right' ) || y === 'right' ) { + suffix = '-end'; + } + return x + suffix; + } + + return y; +}; + const DisabledElement = ( { eventHandlers, child, @@ -67,7 +94,7 @@ const RegularElement = ( { const addPopoverToGrandchildren = ( { grandchildren, isOver, - position, + placement, text, shortcut, anchorRef, @@ -77,7 +104,7 @@ const addPopoverToGrandchildren = ( { isOver && (