Skip to content

Commit

Permalink
Components: remove __unstablePositionedAtSelection (#15035)
Browse files Browse the repository at this point in the history
* Components: remove __unstablePositionedAtSelection

* Address feedback

* Address feedback
  • Loading branch information
ellatrix committed May 1, 2019
1 parent 6552706 commit a9a3907
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 89 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/writing-flow/index.js
Expand Up @@ -284,7 +284,7 @@ class WritingFlow extends Component {
if ( ! isVertical ) {
this.verticalRect = null;
} else if ( ! this.verticalRect ) {
this.verticalRect = computeCaretRect( target );
this.verticalRect = computeCaretRect();
}

if ( isShift ) {
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/index.js
Expand Up @@ -43,7 +43,6 @@ export { default as PanelHeader } from './panel/header';
export { default as PanelRow } from './panel/row';
export { default as Placeholder } from './placeholder';
export { default as Popover } from './popover';
export { default as __unstablePositionedAtSelection } from './positioned-at-selection';
export { default as QueryControls } from './query-controls';
export { default as RadioControl } from './radio-control';
export { default as RangeControl } from './range-control';
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/popover/index.js
Expand Up @@ -282,6 +282,7 @@ class Popover extends Component {
getAnchorRect,
expandOnMobile,
animate = true,
anchorRect,
/* eslint-enable no-unused-vars */
...contentProps
} = this.props;
Expand Down
65 changes: 0 additions & 65 deletions packages/components/src/positioned-at-selection/index.js

This file was deleted.

4 changes: 0 additions & 4 deletions packages/dom/README.md
Expand Up @@ -18,10 +18,6 @@ npm install @wordpress/dom --save

Get the rectangle for the selection in a container.

_Parameters_

- _container_ `Element`: Editable container.

_Returns_

- `?DOMRect`: The rectangle.
Expand Down
8 changes: 1 addition & 7 deletions packages/dom/src/dom.js
Expand Up @@ -235,15 +235,9 @@ export function getRectangleFromRange( range ) {
/**
* Get the rectangle for the selection in a container.
*
* @param {Element} container Editable container.
*
* @return {?DOMRect} The rectangle.
*/
export function computeCaretRect( container ) {
if ( ! container.isContentEditable ) {
return;
}

export function computeCaretRect() {
const selection = window.getSelection();
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;

Expand Down
32 changes: 21 additions & 11 deletions packages/format-library/src/image/index.js
@@ -1,19 +1,31 @@
/**
* WordPress dependencies
*/
import { Path, SVG, TextControl, Popover, IconButton, __unstablePositionedAtSelection } from '@wordpress/components';
import { Path, SVG, TextControl, Popover, IconButton } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { Component, useMemo } from '@wordpress/element';
import { insertObject } from '@wordpress/rich-text';
import { MediaUpload, RichTextToolbarButton, MediaUploadCheck } from '@wordpress/block-editor';
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes';
import { computeCaretRect } from '@wordpress/dom';

const ALLOWED_MEDIA_TYPES = [ 'image' ];

const name = 'core/image';

const stopKeyPropagation = ( event ) => event.stopPropagation();

const PopoverAtImage = ( { dependencies, ...props } ) => {
return (
<Popover
position="bottom center"
focusOnMount={ false }
anchorRect={ useMemo( () => computeCaretRect(), dependencies ) }
{ ...props }
/>
);
};

export const image = {
name,
title: __( 'Image' ),
Expand Down Expand Up @@ -81,9 +93,6 @@ export const image = {
render() {
const { value, onChange, isObjectActive, activeObjectAttributes } = this.props;
const { style } = activeObjectAttributes;
// Rerender PositionedAtSelection when the selection changes or when
// the width changes.
const key = value.start + style;

return (
<MediaUploadCheck>
Expand Down Expand Up @@ -113,10 +122,11 @@ export const image = {
return null;
} }
/> }
{ isObjectActive && <__unstablePositionedAtSelection key={ key }>
<Popover
position="bottom center"
focusOnMount={ false }
{ isObjectActive &&
<PopoverAtImage
// Reposition Popover when the selection changes or
// when the width changes.
dependencies={ [ style, value.start ] }
>
{ // Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */ }
Expand Down Expand Up @@ -154,8 +164,8 @@ export const image = {
<IconButton icon="editor-break" label={ __( 'Apply' ) } type="submit" />
</form>
{ /* eslint-enable jsx-a11y/no-noninteractive-element-interactions */ }
</Popover>
</__unstablePositionedAtSelection> }
</PopoverAtImage>
}
</MediaUploadCheck>
);
}
Expand Down

0 comments on commit a9a3907

Please sign in to comment.