From 6d54e045f68fef15b7a56073e5d490d47fd34a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Fri, 7 May 2021 16:24:50 +0300 Subject: [PATCH] Forward refs --- .../components/block-tools/block-popover.js | 111 +++++++++--------- .../components/block-tools/insertion-point.js | 9 +- .../block-tools/use-popover-scroll.js | 7 +- packages/components/src/popover/index.js | 88 ++++++++------ .../src/slot-fill/bubbles-virtually/slot.js | 24 ++-- packages/components/src/slot-fill/index.js | 12 +- 6 files changed, 141 insertions(+), 110 deletions(-) diff --git a/packages/block-editor/src/components/block-tools/block-popover.js b/packages/block-editor/src/components/block-tools/block-popover.js index 6c637e0331b2a2..dc803f7670aba6 100644 --- a/packages/block-editor/src/components/block-tools/block-popover.js +++ b/packages/block-editor/src/components/block-tools/block-popover.js @@ -178,6 +178,7 @@ function BlockPopover( { return ( -
- { ( shouldShowContextualToolbar || isToolbarForced ) && ( -
- -
- ) } - { ( shouldShowContextualToolbar || isToolbarForced ) && ( - { - initialToolbarItemIndexRef.current = index; - } } - // Resets the index whenever the active block changes so - // this is not persisted. See https://github.com/WordPress/gutenberg/pull/25760#issuecomment-717906169 - key={ clientId } - /> - ) } - { shouldShowBreadcrumb && ( - + +
+ ) } + { ( shouldShowContextualToolbar || isToolbarForced ) && ( + { + initialToolbarItemIndexRef.current = index; + } } + // Resets the index whenever the active block changes so + // this is not persisted. See https://github.com/WordPress/gutenberg/pull/25760#issuecomment-717906169 + key={ clientId } + /> + ) } + { shouldShowBreadcrumb && ( + + ) } + { showEmptyBlockSideInserter && ( +
+ - ) } - { showEmptyBlockSideInserter && ( -
- -
- ) } -
+ + ) }
); } diff --git a/packages/block-editor/src/components/block-tools/insertion-point.js b/packages/block-editor/src/components/block-tools/insertion-point.js index 380ef7c75cd484..134430c301a61e 100644 --- a/packages/block-editor/src/components/block-tools/insertion-point.js +++ b/packages/block-editor/src/components/block-tools/insertion-point.js @@ -16,7 +16,6 @@ import { } from '@wordpress/element'; import { Popover } from '@wordpress/components'; import { isRTL } from '@wordpress/i18n'; -import { useMergeRefs } from '@wordpress/compose'; /** * Internal dependencies @@ -166,10 +165,7 @@ function InsertionPointPopover( { }; }, [ previousElement, nextElement ] ); - const mergedRefs = useMergeRefs( [ - ref, - usePopoverScroll( __unstableContentRef ), - ] ); + const popoverScrollRef = usePopoverScroll( __unstableContentRef ); if ( ! previousElement ) { return null; @@ -215,6 +211,7 @@ function InsertionPointPopover( { // See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus return (
{ - function onWheel( { deltaX, deltaY } ) { + if ( ! scrollableRef ) { + return; + } + + function onWheel( event ) { + const { deltaX, deltaY } = event; scrollableRef.current.scrollBy( deltaX, deltaY ); } node.addEventListener( 'wheel', onWheel ); diff --git a/packages/components/src/popover/index.js b/packages/components/src/popover/index.js index 5833a836056363..b0e8d4bccb7a37 100644 --- a/packages/components/src/popover/index.js +++ b/packages/components/src/popover/index.js @@ -6,7 +6,12 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { useRef, useState, useLayoutEffect } from '@wordpress/element'; +import { + useRef, + useState, + useLayoutEffect, + forwardRef, +} from '@wordpress/element'; import { getRectangleFromRange } from '@wordpress/dom'; import { ESCAPE } from '@wordpress/keycodes'; import deprecated from '@wordpress/deprecated'; @@ -223,36 +228,39 @@ function getAnchorDocument( anchor ) { return anchor.ownerDocument; } -const Popover = ( { - headerTitle, - onClose, - onKeyDown, - children, - className, - noArrow = true, - isAlternate, - // Disable reason: We generate the `...contentProps` rest as remainder - // of props which aren't explicitly handled by this component. - /* eslint-disable no-unused-vars */ - position = 'bottom right', - range, - focusOnMount = 'firstElement', - anchorRef, - shouldAnchorIncludePadding, - anchorRect, - getAnchorRect, - expandOnMobile, - animate = true, - onClickOutside, - onFocusOutside, - __unstableStickyBoundaryElement, - __unstableSlotName = SLOT_NAME, - __unstableObserveElement, - __unstableBoundaryParent, - __unstableForcePosition, - /* eslint-enable no-unused-vars */ - ...contentProps -} ) => { +const Popover = ( + { + headerTitle, + onClose, + onKeyDown, + children, + className, + noArrow = true, + isAlternate, + // Disable reason: We generate the `...contentProps` rest as remainder + // of props which aren't explicitly handled by this component. + /* eslint-disable no-unused-vars */ + position = 'bottom right', + range, + focusOnMount = 'firstElement', + anchorRef, + shouldAnchorIncludePadding, + anchorRect, + getAnchorRect, + expandOnMobile, + animate = true, + onClickOutside, + onFocusOutside, + __unstableStickyBoundaryElement, + __unstableSlotName = SLOT_NAME, + __unstableObserveElement, + __unstableBoundaryParent, + __unstableForcePosition, + /* eslint-enable no-unused-vars */ + ...contentProps + }, + ref +) => { const anchorRefFallback = useRef( null ); const contentRef = useRef( null ); const containerRef = useRef(); @@ -474,6 +482,7 @@ const Popover = ( { const focusOnMountRef = useFocusOnMount( focusOnMount ); const focusOutsideProps = useFocusOutside( handleOnFocusOutside ); const mergedRefs = useMergeRefs( [ + ref, containerRef, focusOnMount ? constrainedTabbingRef : null, focusOnMount ? focusReturnRef : null, @@ -616,10 +625,19 @@ const Popover = ( { return { content }; }; -const PopoverContainer = Popover; +const PopoverContainer = forwardRef( Popover ); + +function PopoverSlot( { name = SLOT_NAME }, ref ) { + return ( + + ); +} -PopoverContainer.Slot = ( { name = SLOT_NAME } ) => ( - -); +PopoverContainer.Slot = forwardRef( PopoverSlot ); export default PopoverContainer; diff --git a/packages/components/src/slot-fill/bubbles-virtually/slot.js b/packages/components/src/slot-fill/bubbles-virtually/slot.js index ace175e4b5a59e..5075677c3e173b 100644 --- a/packages/components/src/slot-fill/bubbles-virtually/slot.js +++ b/packages/components/src/slot-fill/bubbles-virtually/slot.js @@ -1,19 +1,23 @@ /** * WordPress dependencies */ -import { useRef, useLayoutEffect, useContext } from '@wordpress/element'; +import { + useRef, + useLayoutEffect, + useContext, + forwardRef, +} from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; /** * Internal dependencies */ import SlotFillContext from './slot-fill-context'; -export default function Slot( { - name, - fillProps = {}, - as: Component = 'div', - ...props -} ) { +function Slot( + { name, fillProps = {}, as: Component = 'div', ...props }, + forwardedRef +) { const registry = useContext( SlotFillContext ); const ref = useRef(); @@ -34,5 +38,9 @@ export default function Slot( { registry.updateSlot( name, fillProps ); } ); - return ; + return ( + + ); } + +export default forwardRef( Slot ); diff --git a/packages/components/src/slot-fill/index.js b/packages/components/src/slot-fill/index.js index 8c61fa2a444c3b..f91461a3385274 100644 --- a/packages/components/src/slot-fill/index.js +++ b/packages/components/src/slot-fill/index.js @@ -1,3 +1,8 @@ +/** + * WordPress dependencies + */ +import { forwardRef } from '@wordpress/element'; + /** * Internal dependencies */ @@ -20,13 +25,12 @@ export function Fill( props ) { ); } - -export function Slot( { bubblesVirtually, ...props } ) { +export const Slot = forwardRef( ( { bubblesVirtually, ...props }, ref ) => { if ( bubblesVirtually ) { - return ; + return ; } return ; -} +} ); export function Provider( { children, ...props } ) { return (