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

Rich text popovers: move to block tools to fix tab order #34956

Merged
merged 4 commits into from
Oct 8, 2021
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
7 changes: 5 additions & 2 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ export default function BlockTools( {
ref={ usePopoverScroll( __unstableContentRef ) }
/>
{ children }
{ /* Forward compatibility: a place to render block tools behind the
content so it can be tabbed to properly. */ }
{ /* Used for inline rich text popovers. */ }
<Popover.Slot
name="__unstable-block-tools-after"
ref={ usePopoverScroll( __unstableContentRef ) }
/>
</InsertionPoint>
</div>
);
Expand Down
20 changes: 12 additions & 8 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '@wordpress/rich-text';
import deprecated from '@wordpress/deprecated';
import { BACKSPACE, DELETE } from '@wordpress/keycodes';
import { Popover } from '@wordpress/components';

/**
* Internal dependencies
Expand Down Expand Up @@ -307,14 +308,17 @@ function RichTextWrapper(
{ isSelected && (
<keyboardShortcutContext.Provider value={ keyboardShortcuts }>
<inputEventContext.Provider value={ inputEvents }>
{ children && children( { value, onChange, onFocus } ) }
<FormatEdit
value={ value }
onChange={ onChange }
onFocus={ onFocus }
formatTypes={ formatTypes }
forwardedRef={ anchorRef }
/>
<Popover.__unstableSlotNameProvider value="__unstable-block-tools-after">
{ children &&
children( { value, onChange, onFocus } ) }
<FormatEdit
value={ value }
onChange={ onChange }
onFocus={ onFocus }
formatTypes={ formatTypes }
forwardedRef={ anchorRef }
/>
</Popover.__unstableSlotNameProvider>
</inputEventContext.Provider>
</keyboardShortcutContext.Provider>
) }
Expand Down
10 changes: 8 additions & 2 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
useState,
useLayoutEffect,
forwardRef,
createContext,
useContext,
} from '@wordpress/element';
import { getRectangleFromRange } from '@wordpress/dom';
import deprecated from '@wordpress/deprecated';
Expand Down Expand Up @@ -39,6 +41,8 @@ import { getAnimateClassName } from '../animate';
*/
const SLOT_NAME = 'Popover';

const slotNameContext = createContext();

function computeAnchorRect(
anchorRefFallback,
anchorRect,
Expand Down Expand Up @@ -269,7 +273,8 @@ const Popover = (
const containerRef = useRef();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ animateOrigin, setAnimateOrigin ] = useState();
const slot = useSlot( __unstableSlotName );
const slotName = useContext( slotNameContext ) || __unstableSlotName;
const slot = useSlot( slotName );
const isExpanded = expandOnMobile && isMobileViewport;
const [ containerResizeListener, contentSize ] = useResizeObserver();
noArrow = isExpanded || noArrow;
Expand Down Expand Up @@ -568,7 +573,7 @@ const Popover = (
);

if ( slot.ref ) {
content = <Fill name={ __unstableSlotName }>{ content }</Fill>;
content = <Fill name={ slotName }>{ content }</Fill>;
}

if ( anchorRef || anchorRect ) {
Expand All @@ -592,5 +597,6 @@ function PopoverSlot( { name = SLOT_NAME }, ref ) {
}

PopoverContainer.Slot = forwardRef( PopoverSlot );
PopoverContainer.__unstableSlotNameProvider = slotNameContext.Provider;

export default PopoverContainer;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
createNewPost,
clickBlockToolbarButton,
clickButton,
pressKeyWithModifier,
} from '@wordpress/e2e-test-utils';

describe( 'adding inline tokens', () => {
Expand Down Expand Up @@ -59,5 +60,20 @@ describe( 'adding inline tokens', () => {
`<!-- wp:paragraph -->\\s*<p>a <img class="wp-image-\\d+" style="width:\\s*10px;?" src="[^"]+\\/${ filename }\\.png" alt=""\\/?><\\/p>\\s*<!-- \\/wp:paragraph -->`
);
expect( await getEditedPostContent() ).toMatch( regex );

await pressKeyWithModifier( 'shift', 'ArrowLeft' );
await page.waitForSelector(
'.block-editor-format-toolbar__image-popover'
);
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
await page.keyboard.type( '20' );
await page.keyboard.press( 'Enter' );

// Check the content.
const regex2 = new RegExp(
`<!-- wp:paragraph -->\\s*<p>a <img class="wp-image-\\d+" style="width:\\s*20px;?" src="[^"]+\\/${ filename }\\.png" alt=""\\/?><\\/p>\\s*<!-- \\/wp:paragraph -->`
);
expect( await getEditedPostContent() ).toMatch( regex2 );
} );
} );