Skip to content

Commit

Permalink
Update useAnchorRef usage in FormatToolbarContainer, use anchor prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Aug 30, 2022
1 parent 9f6f943 commit ac5c6ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,32 @@ import BlockControls from '../block-controls';
import FormatToolbar from './format-toolbar';
import { store as blockEditorStore } from '../../store';

function InlineSelectionToolbar( { value, anchorRef, activeFormats } ) {
function InlineSelectionToolbar( {
value,
editableContentRef,
activeFormats,
} ) {
const lastFormat = activeFormats[ activeFormats.length - 1 ];
const lastFormatType = lastFormat?.type;
const settings = useSelect(
( select ) => select( richTextStore ).getFormatType( lastFormatType ),
[ lastFormatType ]
);
const selectionRef = useAnchorRef( {
ref: anchorRef,
const popoverAnchor = useAnchorRef( {
ref: editableContentRef,
value,
settings,
} );

return <InlineToolbar anchorRef={ selectionRef } />;
return <InlineToolbar popoverAnchor={ popoverAnchor } />;
}

function InlineToolbar( { anchorRef } ) {
function InlineToolbar( { popoverAnchor } ) {
return (
<Popover
position="top center"
focusOnMount={ false }
anchorRef={ anchorRef }
anchor={ popoverAnchor }
className="block-editor-rich-text__inline-format-toolbar"
__unstableSlotName="block-toolbar"
>
Expand All @@ -51,14 +55,14 @@ function InlineToolbar( { anchorRef } ) {
);
}

const FormatToolbarContainer = ( { inline, anchorRef, value } ) => {
const FormatToolbarContainer = ( { inline, editableContentRef, value } ) => {
const hasInlineToolbar = useSelect(
( select ) => select( blockEditorStore ).getSettings().hasInlineToolbar,
[]
);

if ( inline ) {
return <InlineToolbar anchorRef={ anchorRef } />;
return <InlineToolbar popoverAnchor={ editableContentRef.current } />;
}

if ( hasInlineToolbar ) {
Expand All @@ -70,7 +74,7 @@ const FormatToolbarContainer = ( { inline, anchorRef, value } ) => {

return (
<InlineSelectionToolbar
anchorRef={ anchorRef }
editableContentRef={ editableContentRef }
value={ value }
activeFormats={ activeFormats }
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ function RichTextWrapper(
{ isSelected && hasFormats && (
<FormatToolbarContainer
inline={ inlineToolbar }
anchorRef={ anchorRef }
editableContentRef={ anchorRef }
value={ value }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ function RichTextWrapper(
{ nestedIsSelected && hasFormats && (
<FormatToolbarContainer
inline={ inlineToolbar }
anchorRef={ fallbackRef.current }
editableContentRef={ fallbackRef }
/>
) }
{ nestedIsSelected && <RemoveBrowserShortcuts /> }
Expand Down

0 comments on commit ac5c6ed

Please sign in to comment.