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

Make the Popover scrolling and position behavior adapt to the content changes #23159

Merged
merged 1 commit into from
Jun 16, 2020
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
18 changes: 7 additions & 11 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ function InserterMenu( {
const blocksTab = (
<>
<div className="block-editor-inserter__block-list">
<div className="block-editor-inserter__scrollable">
<InserterBlockList
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
filterValue={ filterValue }
/>
</div>
<InserterBlockList
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
filterValue={ filterValue }
/>
</div>
{ showInserterHelpPanel && (
<div className="block-editor-inserter__tips">
Expand All @@ -102,9 +100,7 @@ function InserterMenu( {
);

const patternsTab = (
<div className="block-editor-inserter__scrollable">
<BlockPatterns onInsert={ onInsert } filterValue={ filterValue } />
</div>
<BlockPatterns onInsert={ onInsert } filterValue={ filterValue } />
);

// Disable reason (no-autofocus): The inserter menu is a modal display, not one which
Expand Down
45 changes: 23 additions & 22 deletions packages/block-editor/src/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ $block-inserter-tabs-height: 44px;
}
}

.block-editor-inserter__popover > .components-popover__content {
@include break-medium {
overflow-y: visible;
height: 100vh;
padding: 0;
.block-editor-inserter__popover .block-editor-inserter__menu {
margin: -$grid-unit-15;

.block-editor-inserter__search {
top: -$grid-unit-15;
}

.block-editor-inserter__tabs .components-tab-panel__tabs {
top: $grid-unit-10 + $grid-unit-20 + $grid-unit-60 - $grid-unit-15;
}

.block-editor-inserter__main-area {
overflow: visible;
height: auto;
}
}

Expand All @@ -43,8 +52,7 @@ $block-inserter-tabs-height: 44px;

.block-editor-inserter__main-area {
width: auto;
display: flex;
flex-direction: column;
overflow-y: auto;
height: 100%;
@include break-medium {
width: $block-inserter-width;
Expand All @@ -65,9 +73,10 @@ $block-inserter-tabs-height: 44px;

.block-editor-inserter__search {
padding: $grid-unit-20;
position: sticky;
top: 0;
background: $white;
z-index: 1;
flex-shrink: 0;
position: relative;

input[type="search"].block-editor-inserter__search-input {
@include input-control;
Expand Down Expand Up @@ -103,11 +112,15 @@ $block-inserter-tabs-height: 44px;

.block-editor-inserter__tabs {
display: flex;
flex-grow: 1;
flex-direction: column;
margin-top: -$grid-unit-10;

.components-tab-panel__tabs {
position: sticky;
// Computed based off the search input height and paddings
top: $grid-unit-10 + $grid-unit-20 + $grid-unit-60;
background: $white;
z-index: 1;
border-bottom: $border-width solid $light-gray-500;

.components-tab-panel__tabs-item {
Expand Down Expand Up @@ -147,18 +160,6 @@ $block-inserter-tabs-height: 44px;
position: relative;
}

// This extra div is needed because
// flex grow and overflow auto doesn't work well together.
.block-editor-inserter__scrollable {
overflow: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
overflow-y: scroll;
}

.block-editor-inserter__popover .block-editor-block-types-list {
margin: 0 -8px;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useRef, useState, useEffect } from '@wordpress/element';
import { focus, getRectangleFromRange } from '@wordpress/dom';
import { ESCAPE } from '@wordpress/keycodes';
import deprecated from '@wordpress/deprecated';
import { useViewportMatch } from '@wordpress/compose';
import { useViewportMatch, useResizeObserver } from '@wordpress/compose';
import { close } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -260,11 +260,11 @@ const Popover = ( {
const anchorRefFallback = useRef( null );
const contentRef = useRef( null );
const containerRef = useRef();
const contentRect = useRef();
const isMobileViewport = useViewportMatch( 'medium', '<' );
const [ animateOrigin, setAnimateOrigin ] = useState();
const slot = useSlot( __unstableSlotName );
const isExpanded = expandOnMobile && isMobileViewport;
const [ containerResizeListener, contentSize ] = useResizeObserver();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for the moment (IE11) but this hook is widely used in Gutenberg already and it works pretty well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also already have an interval running, so we could use ResizeObserver and have the interval as a fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why such complexity while the hook is already there and works properly? I'd rather find a way to remove the interval personally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, would be great if we could remove the interval


noArrow = isExpanded || noArrow;

Expand Down Expand Up @@ -299,10 +299,6 @@ const Popover = ( {
return;
}

if ( ! contentRect.current ) {
contentRect.current = contentRef.current.getBoundingClientRect();
}

let relativeOffsetTop = 0;

// If there is a positioned ancestor element that is not the body,
Expand Down Expand Up @@ -343,7 +339,7 @@ const Popover = ( {
contentWidth,
} = computePopoverPosition(
anchor,
contentRect.current,
contentSize,
position,
__unstableSticky,
containerRef.current,
Expand Down Expand Up @@ -481,6 +477,7 @@ const Popover = ( {
anchorRef,
shouldAnchorIncludePadding,
position,
contentSize,
__unstableSticky,
__unstableAllowVerticalSubpixelPosition,
__unstableAllowHorizontalSubpixelPosition,
Expand Down Expand Up @@ -603,7 +600,10 @@ const Popover = ( {
className="components-popover__content"
tabIndex="-1"
>
{ children }
<div style={ { position: 'relative' } }>
{ containerResizeListener }
{ children }
</div>
</div>
</IsolatedEventContainer>
) }
Expand Down
55 changes: 55 additions & 0 deletions packages/components/src/popover/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
*/
import { boolean, select, text } from '@storybook/addon-knobs';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { DraggableWrapper } from './_utils';
import Popover from '../';
import Button from '../../button';

export default { title: 'Components/Popover', component: Popover };

Expand Down Expand Up @@ -100,3 +106,52 @@ export const positioning = () => {
<DragExample label={ label } content={ content } noArrow={ noArrow } />
);
};

function DynamicHeightPopover() {
const [ height, setHeight ] = useState( 200 );
const increase = () => setHeight( height + 100 );
const decrease = () => setHeight( height - 100 );

return (
<div style={ { padding: '20px' } }>
<div>
<Button
isPrimary
onClick={ increase }
style={ {
marginRight: '20px',
} }
>
Increase Size
</Button>

<Button isPrimary onClick={ decrease }>
Decrease Size
</Button>
</div>

<p>
When the height of the popover exceeds the available space in
the canvas, a scrollbar inside the popover should appear.
</p>

<div>
<Popover>
<div
style={ {
height,
background: '#eee',
padding: '20px',
} }
>
Content with dynamic height
</div>
</Popover>
</div>
</div>
);
}

export const dynamicHeight = () => {
return <DynamicHeightPopover />;
};
5 changes: 0 additions & 5 deletions packages/components/src/popover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,6 @@ $arrow-size: 8px;
}
}

// The withFocusReturn div
.components-popover__content > div {
height: 100%;
}

.components-popover__header {
align-items: center;
background: $white;
Expand Down
12 changes: 10 additions & 2 deletions packages/components/src/popover/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ exports[`Popover should pass additional props to portaled element 1`] = `
class="components-popover__content"
tabindex="-1"
>
Hello
<div
style="position: relative;"
>
Hello
</div>
</div>
</div>
</div>
Expand All @@ -38,7 +42,11 @@ exports[`Popover should render content 1`] = `
class="components-popover__content"
tabindex="-1"
>
Hello
<div
style="position: relative;"
>
Hello
</div>
</div>
</div>
</div>
Expand Down