-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Detect Inserter bounds collision by generic Popover component #1193
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { isEqual, includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
|
||
class Popover extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.bindNode = this.bindNode.bind( this ); | ||
|
||
this.state = { | ||
forcedPositions: {}, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.setForcedPositions(); | ||
} | ||
|
||
componentWillReceiveProps( nextProps ) { | ||
if ( this.props.position !== nextProps.position ) { | ||
this.setState( { forcedPositions: {} } ); | ||
} | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( this.props.position !== prevProps.position ) { | ||
this.setForcedPositions(); | ||
} | ||
} | ||
|
||
setForcedPositions() { | ||
const rect = this.node.getBoundingClientRect(); | ||
const { forcedPositions } = this.state; | ||
|
||
const nextForcedPositions = {}; | ||
|
||
// Check exceeding top or bottom of viewport | ||
if ( rect.top < 0 ) { | ||
nextForcedPositions.bottom = true; | ||
} else if ( rect.bottom > window.innerHeight ) { | ||
nextForcedPositions.top = true; | ||
} | ||
|
||
// Check exceeding left or right of viewport | ||
if ( rect.left < 0 ) { | ||
nextForcedPositions.right = true; | ||
} else if ( rect.right > window.innerWidth ) { | ||
nextForcedPositions.left = true; | ||
} | ||
|
||
if ( ! isEqual( nextForcedPositions, forcedPositions ) ) { | ||
this.setState( { | ||
forcedPositions: nextForcedPositions, | ||
} ); | ||
} | ||
} | ||
|
||
bindNode( node ) { | ||
this.node = node; | ||
} | ||
|
||
render() { | ||
const { position, children, className } = this.props; | ||
const positions = position.split( ' ' ); | ||
const { forcedPositions } = this.state; | ||
|
||
const classes = classnames( | ||
'components-popover', | ||
className, | ||
...[ [ 'top', 'bottom' ], [ 'center', 'left', 'right' ] ].map( ( directions ) => { | ||
// Consider first of directions set as the default | ||
const defaultDirection = directions[ 0 ]; | ||
|
||
// Prefer the forced direction, but allow direction from props | ||
// otherwise. Use default if neither forced nor prop value. | ||
const direction = directions.reduce( ( result, dir ) => ( | ||
forcedPositions[ dir ] || ( ! result && includes( positions, dir ) ) | ||
? dir | ||
: result | ||
), null ) || defaultDirection; | ||
|
||
return 'is-' + direction; | ||
} ) | ||
); | ||
|
||
return ( | ||
<div | ||
ref={ this.bindNode } | ||
className={ classes } | ||
tabIndex="0"> | ||
{ children } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Popover; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
.components-popover { | ||
box-shadow: $shadow-popover; | ||
border: 1px solid $light-gray-500; | ||
position: absolute; | ||
background: $white; | ||
z-index: z-index( ".components-popover" ); | ||
left: 0; | ||
right: 0; | ||
|
||
@include break-medium { | ||
width: 280px; | ||
left: -122px; | ||
right: auto; | ||
height: auto; | ||
} | ||
|
||
&:before { | ||
border: 10px dashed $light-gray-500; | ||
display: none; | ||
|
||
@include break-medium { | ||
display: block; | ||
} | ||
} | ||
|
||
&:after { | ||
border: 10px solid $white; | ||
} | ||
|
||
&:before, | ||
&:after { | ||
content: ""; | ||
position: absolute; | ||
left: 50%; | ||
margin-left: -10px; | ||
height: 0; | ||
width: 0; | ||
border-left-color: transparent; | ||
border-right-color: transparent; | ||
line-height: 0; | ||
} | ||
|
||
&.is-top { | ||
bottom: 42px; | ||
|
||
&:before { | ||
bottom: -10px; | ||
} | ||
|
||
&:after { | ||
bottom: -8px; | ||
} | ||
|
||
&:before, | ||
&:after { | ||
border-top-style: solid; | ||
border-bottom: none; | ||
} | ||
} | ||
|
||
&.is-bottom { | ||
top: $admin-bar-height-big + $item-spacing -1px; | ||
|
||
@include break-medium { | ||
top: $admin-bar-height + $item-spacing; | ||
} | ||
|
||
&:before { | ||
top: -10px; | ||
} | ||
|
||
&:after { | ||
top: -8px; | ||
} | ||
|
||
&:before, | ||
&:after { | ||
border-bottom-style: solid; | ||
border-top: none; | ||
} | ||
} | ||
|
||
&.is-right { | ||
left: -32px; | ||
|
||
&:before, | ||
&:after { | ||
left: 49px; | ||
} | ||
} | ||
|
||
&.is-left { | ||
left: auto; | ||
right: -42px; | ||
|
||
&:before, | ||
&:after { | ||
left: auto; | ||
right: 49px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we store forced positions like this
{ forceYAxis: 'top', forceXAxis: 'left' }
we could simplify the logic here and avoid some loops.