Skip to content

Commit

Permalink
Refactor the Popover component to use the floatingUI library (#40740)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <ntsekouras@outlook.com>
Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
  • Loading branch information
3 people committed May 18, 2022
1 parent b499487 commit 8b14921
Show file tree
Hide file tree
Showing 83 changed files with 487 additions and 2,484 deletions.
6 changes: 0 additions & 6 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,6 @@
"markdown_source": "../packages/components/src/flex/flex/README.md",
"parent": "components"
},
{
"title": "Flyout",
"slug": "flyout",
"markdown_source": "../packages/components/src/flyout/flyout/README.md",
"parent": "components"
},
{
"title": "FocalPointPicker",
"slug": "focal-point-picker",
Expand Down
26 changes: 24 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ function BlockAlignmentMatrixControl( props ) {
} = props;

const icon = <AlignmentMatrixControl.Icon value={ value } />;
const className = 'block-editor-block-alignment-matrix-control';
const popoverClassName = `${ className }__popover`;
const isAlternate = true;

return (
<Dropdown
position="bottom right"
className={ className }
popoverProps={ { className: popoverClassName, isAlternate } }
popoverProps={ { isAlternate: true } }
renderToggle={ ( { onToggle, isOpen } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ function BlockPopoverInbetween( {
left: previousRect ? previousRect.right : nextRect.right,
right: previousRect ? previousRect.left : nextRect.left,
bottom: nextRect ? nextRect.top : previousRect.bottom,
height: 0,
width: 0,
ownerDocument,
};
}
Expand All @@ -116,6 +118,8 @@ function BlockPopoverInbetween( {
left: previousRect ? previousRect.left : nextRect.left,
right: previousRect ? previousRect.right : nextRect.right,
bottom: nextRect ? nextRect.top : previousRect.bottom,
height: 0,
width: 0,
ownerDocument,
};
}
Expand All @@ -126,6 +130,8 @@ function BlockPopoverInbetween( {
left: previousRect ? previousRect.left : nextRect.right,
right: nextRect ? nextRect.right : previousRect.left,
bottom: previousRect ? previousRect.bottom : nextRect.bottom,
height: 0,
width: 0,
ownerDocument,
};
}
Expand All @@ -135,6 +141,8 @@ function BlockPopoverInbetween( {
left: previousRect ? previousRect.right : nextRect.left,
right: nextRect ? nextRect.left : previousRect.right,
bottom: previousRect ? previousRect.bottom : nextRect.bottom,
height: 0,
width: 0,
ownerDocument,
};
}, [ previousElement, nextElement ] );
Expand All @@ -155,7 +163,6 @@ function BlockPopoverInbetween( {
return (
<Popover
ref={ popoverScrollRef }
noArrow
animate={ false }
getAnchorRect={ getAnchorRect }
focusOnMount={ false }
Expand Down
17 changes: 1 addition & 16 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Popover } from '@wordpress/components';
import { getScrollContainer } from '@wordpress/dom';
import { useMemo } from '@wordpress/element';

/**
Expand Down Expand Up @@ -50,33 +49,19 @@ export default function BlockPopover( {
bottom: lastSelectedElement,
};

const { ownerDocument } = selectedElement;
const stickyBoundaryElement =
ownerDocument.defaultView.frameElement ||
getScrollContainer( selectedElement ) ||
ownerDocument.body;

return (
<Popover
ref={ popoverScrollRef }
noArrow
animate={ false }
position="top right left"
focusOnMount={ false }
anchorRef={ anchorRef }
__unstableStickyBoundaryElement={
__unstableCoverTarget ? undefined : stickyBoundaryElement
}
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
__unstableBoundaryParent
// Observe movement for block animations (especially horizontal).
__unstableObserveElement={ selectedElement }
// Used to safeguard sticky position behavior against cases where it would permanently
// obscure specific sections of a block.
__unstableEditorCanvasWrapper={ __unstableContentRef?.current }
__unstableForcePosition={ __unstableCoverTarget }
__unstableForcePosition
{ ...props }
className={ classnames(
'block-editor-block-popover',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
width: max-content;
background: none;
border: none;
outline: none;
box-shadow: none;
overflow-y: visible;

Expand Down
41 changes: 2 additions & 39 deletions packages/block-editor/src/components/block-switcher/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
font-size: 14px;
}

// Indent the popover to match the button position.
.block-editor-block-switcher__popover {
margin-left: 6px;
}

.components-button.block-editor-block-switcher__no-switcher-icon {
display: flex;

Expand Down Expand Up @@ -93,43 +88,11 @@
min-width: 300px;
}

// We keep the min width the same for the border to work.
.components-popover.block-editor-block-switcher__popover .components-popover__content > div {
min-width: auto;
display: flex;
background: $white;
padding: 0;

.components-menu-group {
margin: 0;
}
}

.block-editor-block-switcher__popover .components-popover__content {

.block-editor-block-styles {
margin: 0 -3px; // Remove the panel body padding while keeping it for the title.
}

// Hide the bottom border on the last panel so it stacks with the popover.
.components-panel__body {
border: 0;

// Elevate this so the hover style is visible.
position: relative;
z-index: 1;
}

.components-panel__body + .components-panel__body {
border-top: $border-width solid $gray-200;
}
}

.block-editor-block-switcher__popover__preview__parent {
.block-editor-block-switcher__popover__preview__container {
position: absolute;
top: -$grid-unit-15;
left: calc(100% + #{$grid-unit-40});
left: calc(100% + #{$grid-unit-20});
}
}

Expand All @@ -138,7 +101,6 @@

// Position correctly. Needs specificity.
&.components-popover {
margin-left: $grid-unit-05;
margin-top: $grid-unit-15 - $border-width;
}

Expand All @@ -151,6 +113,7 @@
border: $border-width solid $gray-900;
background: $white;
border-radius: $radius-block-ui;
outline: none;
}

.block-editor-block-switcher__preview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,16 @@ export default function ColorGradientSettingsDropdown( {
__experimentalIsRenderedInSidebar,
...props
} ) {
const dropdownPosition = __experimentalIsRenderedInSidebar
? 'bottom left'
: undefined;

const dropdownClassName = __experimentalIsItemGroup
? 'block-editor-panel-color-gradient-settings__dropdown'
: 'block-editor-tools-panel-color-gradient-settings__dropdown';
let popoverProps;
if ( __experimentalIsRenderedInSidebar ) {
popoverProps = {
placement: 'left-start',
offset: 36,
};
}

return (
// Only wrap with `ItemGroup` if these controls are being rendered
Expand Down Expand Up @@ -170,7 +173,7 @@ export default function ColorGradientSettingsDropdown( {
{ ...props }
>
<Dropdown
position={ dropdownPosition }
popoverProps={ popoverProps }
className={ dropdownClassName }
contentClassName="block-editor-panel-color-gradient-settings__dropdown-content"
renderToggle={ renderToggle( toggleSettings ) }
Expand Down
20 changes: 1 addition & 19 deletions packages/block-editor/src/components/colors-gradients/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,7 @@
}

.block-editor-panel-color-gradient-settings__dropdown-content .components-popover__content {
& > div {
width: $sidebar-width;
}
}

@include break-medium() {
.block-editor-panel-color-gradient-settings__dropdown-content {
.components-popover__content {
margin-right: #{ math.div($sidebar-width, 2) + $grid-unit-20 } !important;
}

&.is-from-top .components-popover__content {
margin-top: #{ -($grid-unit-60 + $grid-unit-15) } !important;
}

&.is-from-bottom .components-popover__content {
margin-bottom: #{ -($grid-unit-60 + $grid-unit-15) } !important;
}
}
width: $sidebar-width;
}

.block-editor-panel-color-gradient-settings__dropdown:last-child > div {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $popover-padding: $grid-unit-20;
$swatch-columns: math.floor(math.div($popover-width + $swatch-gap - 2 * $popover-padding, $swatch-size + $swatch-gap));

.block-editor-duotone-control__popover {
> .components-popover__content > div {
> .components-popover__content {
padding: $popover-padding;
width: $popover-width;
}
Expand All @@ -34,9 +34,3 @@ $swatch-columns: math.floor(math.div($popover-width + $swatch-gap - 2 * $popover
margin: $grid-unit-20 0;
font-size: $helptext-font-size;
}

// Better align the popover under the swatch.
// @todo: when the positioning for popovers gets refactored, this can presumably be removed.
.block-editor-duotone-control__popover:not([data-y-axis="middle"][data-x-axis="right"]) > .components-popover__content {
margin-left: -14px;
}
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $block-inserter-tabs-height: 44px;
.block-editor-inserter__popover.is-quick {
.components-popover__content {
border: none;
outline: none;

.block-editor-inserter__quick-inserter > * {
border-left: $border-width solid $gray-400;
Expand Down Expand Up @@ -310,7 +311,7 @@ $block-inserter-tabs-height: 44px;
border-top: $border-width solid $gray-300;
}

.block-editor-inserter__popover.is-quick > .components-popover__content > div {
.block-editor-inserter__popover.is-quick > .components-popover__content {
padding: 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export default function ListViewDropIndicator( {

return (
<Popover
noArrow
animate={ false }
getAnchorRect={ getAnchorRect }
focusOnMount={ false }
Expand Down
3 changes: 2 additions & 1 deletion packages/block-editor/src/components/list-view/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,11 @@ $block-navigation-max-indent: 8;
}

// Reset some popover defaults for the drop indicator.
.block-editor-list-view-drop-indicator:not([data-y-axis="middle"][data-x-axis="right"]) > .components-popover__content {
.block-editor-list-view-drop-indicator > .components-popover__content {
margin-left: 0;
border: none;
box-shadow: none;
outline: none;
}

.block-editor-list-view-placeholder {
Expand Down
14 changes: 12 additions & 2 deletions packages/block-editor/src/components/navigable-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ function hasFocusWithin( container ) {
function focusFirstTabbableIn( container ) {
const [ firstTabbable ] = focus.tabbable.find( container );
if ( firstTabbable ) {
firstTabbable.focus();
firstTabbable.focus( {
// When focusing newly mounted toolbars,
// the position of the popover is often not right on the first render
// This prevents the layout shifts when focusing the dialogs.
preventScroll: true,
} );
}
}

Expand Down Expand Up @@ -119,7 +124,12 @@ function useToolbarFocus(
const items = getAllToolbarItemsIn( ref.current );
const index = initialIndex || 0;
if ( items[ index ] && hasFocusWithin( ref.current ) ) {
items[ index ].focus();
items[ index ].focus( {
// When focusing newly mounted toolbars,
// the position of the popover is often not right on the first render
// This prevents the layout shifts when focusing the dialogs.
preventScroll: true,
} );
}
} );
}
Expand Down
Loading

0 comments on commit 8b14921

Please sign in to comment.