From 8f02dcab7080fb365a7897e8eb6fb7926670e5a2 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Mon, 9 Oct 2017 20:30:09 -0400 Subject: [PATCH 1/3] Title: Unselect title by blur event Title: Detect blur outside root container node Components: Enable title wrapper as focusable Firefox won't include relatedTarget on blur event for div unless focusable. Assign as negative tab index to avoid creating a tab stop. --- editor/post-title/index.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/editor/post-title/index.js b/editor/post-title/index.js index bdbb042f47ba3..4c01f36bd5e68 100644 --- a/editor/post-title/index.js +++ b/editor/post-title/index.js @@ -3,7 +3,6 @@ */ import { connect } from 'react-redux'; import Textarea from 'react-autosize-textarea'; -import clickOutside from 'react-click-outside'; import classnames from 'classnames'; /** @@ -32,12 +31,16 @@ class PostTitle extends Component { constructor() { super( ...arguments ); - this.bindTextarea = this.bindTextarea.bind( this ); + this.bindContainer = this.bindNode.bind( this, 'container' ); + this.bindTextarea = this.bindNode.bind( this, 'textarea' ); this.onChange = this.onChange.bind( this ); this.onSelect = this.onSelect.bind( this ); this.onUnselect = this.onUnselect.bind( this ); this.onSelectionChange = this.onSelectionChange.bind( this ); this.onKeyDown = this.onKeyDown.bind( this ); + this.blurIfOutside = this.blurIfOutside.bind( this ); + + this.nodes = {}; this.state = { isSelected: false, @@ -52,12 +55,12 @@ class PostTitle extends Component { document.removeEventListener( 'selectionchange', this.onSelectionChange ); } - bindTextarea( ref ) { - this.textareaContainer = ref; + bindNode( name, node ) { + this.nodes[ name ] = node; } onSelectionChange() { - const textarea = this.textareaContainer.textarea; + const textarea = this.nodes.textarea.textarea; if ( document.activeElement === textarea && textarea.selectionStart !== textarea.selectionEnd @@ -80,8 +83,10 @@ class PostTitle extends Component { this.setState( { isSelected: false } ); } - handleClickOutside() { - this.setState( { isSelected: false } ); + blurIfOutside( event ) { + if ( ! this.nodes.container.contains( event.relatedTarget ) ) { + this.onUnselect(); + } } onKeyDown( event ) { @@ -97,7 +102,13 @@ class PostTitle extends Component { const className = classnames( 'editor-post-title', { 'is-selected': isSelected } ); return ( -
+
{ isSelected && }