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

Add click outside functionality to the editor insert menu. #510

Closed
wants to merge 8 commits into from

Conversation

kopepasah
Copy link
Member

This fixes #376.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

This works great but the menu is not being closed when we hit the + button a second time. Do we want to address this issue @jasmussen?

@@ -16,7 +16,7 @@ class Inserter extends wp.element.Component {

toggle() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we rename this open?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's first see if this becomes a toggle after I make a few changes.

@@ -37,7 +37,7 @@ class Inserter extends wp.element.Component {
label={ wp.i18n.__( 'Insert block' ) }
onClick={ this.toggle }
className="editor-inserter__toggle" />
{ opened && <InserterMenu position={ position } onSelect={ this.close } /> }
{ opened && <InserterMenu position={ position } onSelect={ this.close } onClickOutside={ this.close } /> }
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we merge the onSelect and the onClickOutside prop into a single prop onClose that takes a nullable block parameter?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I created the second prop is in case there was an instance which we want a different actions for onSelect and onClickOutside.

I think we should keep these because I will probably use this to implement to toggle mentioned below.

@@ -18,6 +19,10 @@ class InserterMenu extends wp.element.Component {
this.filter = this.filter.bind( this );
}

handleClickOutside( e ) {
Copy link
Member

Choose a reason for hiding this comment

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

CI is unhappy about the unused variable here (e).

@aduth
Copy link
Member

aduth commented Apr 26, 2017

I feel the button should remain a toggle. I'd guess there might have been some difficulty with event propagation and the outside click detection considering the button itself "outside" the menu. Maybe we should move the handleClickOutside to the button and only react to it if the menu is currently toggled? Ideally we could have the event bindings only active while the menu is open, but it doesn't seem like react-click-outside exposes this option.

@kopepasah
Copy link
Member Author

I believe bringing back the toggle is doable and can see a use case where the toggle is needed. I will try this today and let you know.

@@ -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;
Copy link
Member Author

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).

const { position } = this.props;

return (
<div className="editor-inserter">
<div className="editor-inserter" ref={ ( inserter ) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Added a ref to determine if the current button clicked (when menu is open) is the same button which opened the menu.

toggle( e ) {
const toggle = this.inserter.getElementsByClassName( 'editor-inserter__toggle' );

if ( 'undefined' !== typeof e.currentTarget && toggle[ 0 ] === e.currentTarget.activeElement ) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

@kopepasah
Copy link
Member Author

I spent some time flushing this out today and believe it is now in working order. There are a few areas which seem a little tacky, but the functionality overall works as expected.

I tried several methods, but settled on the following because all the other methods were failing due to the handleClickOutside method running before a click on the toggle.

I added a few comments on some of the items.

@mtias mtias added Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Enhancement A suggestion for improvement. labels Apr 28, 2017
@aduth
Copy link
Member

aduth commented Apr 28, 2017

I still think there might be a simpler alternative where we simply bind the click outside logic to the parent Inserter component.

See: 85bc8a6

In my testing, this seems to work just as well. The main downside is that there will be two global click handlers always present for each of the inserters, but they won't do anything unless the menu is currently open.

Alternatively, maybe we could bind a click handler directly to the button and call event.stopPropagation (or stopImmediatePropagation, I forget which) to avoid it falling victim to the menu's default outside click behavior.

@kopepasah
Copy link
Member Author

@aduth yeah, I see what you mean now. I was trying to tie the handlers after the menu was opened, but your implementation looks much simpler.

@kopepasah kopepasah closed this May 1, 2017
@kopepasah
Copy link
Member Author

I have closed this in favor of #582.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Click outside to dismiss mode switcher, inserter overlays
4 participants