-
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
Add click outside functionality to the editor insert menu. #510
Changes from all commits
7e3b5c5
c16f4ad
3bdd444
50affb4
aa606db
10d0a08
e924dad
9052248
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,36 +8,55 @@ class Inserter extends wp.element.Component { | |
constructor() { | ||
super( ...arguments ); | ||
this.toggle = this.toggle.bind( this ); | ||
this.close = this.close.bind( this ); | ||
this.update = true; | ||
|
||
this.state = { | ||
opened: false | ||
toggle: false | ||
}; | ||
} | ||
|
||
toggle() { | ||
this.setState( { | ||
opened: ! this.state.opened | ||
} ); | ||
shouldComponentUpdate( nextProps, nextState ) { | ||
if ( nextState.toggle && ! this.update ) { | ||
this.update = true; | ||
|
||
this.setState( { | ||
toggle: false | ||
} ); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
close() { | ||
this.setState( { | ||
opened: false | ||
toggle( e ) { | ||
const toggle = this.inserter.getElementsByClassName( 'editor-inserter__toggle' ); | ||
|
||
if ( 'undefined' !== typeof e.currentTarget && toggle[ 0 ] === e.currentTarget.activeElement ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verifies that the toggle clicked is not a different toggle on the page. |
||
this.update = false; | ||
} | ||
|
||
this.setState( ( prevState ) => { | ||
return { | ||
toggle: ! prevState.toggle | ||
}; | ||
} ); | ||
} | ||
|
||
render() { | ||
const { opened } = this.state; | ||
const { toggle } = this.state; | ||
const { position } = this.props; | ||
|
||
return ( | ||
<div className="editor-inserter"> | ||
<div className="editor-inserter" ref={ ( inserter ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a |
||
this.inserter = inserter; | ||
} }> | ||
<IconButton | ||
icon="insert" | ||
label={ wp.i18n.__( 'Insert block' ) } | ||
onClick={ this.toggle } | ||
className="editor-inserter__toggle" /> | ||
{ opened && <InserterMenu position={ position } onSelect={ this.close } /> } | ||
{ toggle && <InserterMenu position={ position } onToggle={ this.toggle } /> } | ||
</div> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import clickOutside from 'react-click-outside'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -18,16 +19,20 @@ class InserterMenu extends wp.element.Component { | |
this.filter = this.filter.bind( this ); | ||
} | ||
|
||
handleClickOutside( e ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI is unhappy about the unused variable here ( |
||
this.props.onToggle( e ); | ||
} | ||
|
||
filter( event ) { | ||
this.setState( { | ||
filterValue: event.target.value | ||
} ); | ||
} | ||
|
||
selectBlock( slug ) { | ||
return () => { | ||
return ( e ) => { | ||
this.props.onInsertBlock( slug ); | ||
this.props.onSelect(); | ||
this.props.onToggle( e ); | ||
this.setState( { filterValue: '' } ); | ||
}; | ||
} | ||
|
@@ -93,4 +98,4 @@ export default connect( | |
} ); | ||
} | ||
} ) | ||
)( InserterMenu ); | ||
)( clickOutside( InserterMenu ) ); |
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.
Added to determine if we should run an update on the component (initial state is true).