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

feat: Focus title if title is empty #9608

Merged
merged 5 commits into from
Sep 7, 2018
Merged

Conversation

tofumatt
Copy link
Member

@tofumatt tofumatt commented Sep 4, 2018

Description

Focuses the title when empty. Fixes #3828.

How has this been tested?

Tested locally in Firefox and Chrome for Mac; IE 11 and Edge in Windows.

Added an E2E test as well.

Screenshots

2018-09-04 14 27 40

@tofumatt tofumatt requested a review from a team September 4, 2018 13:49
@@ -244,12 +244,12 @@ export const settings = {
} );
} );

// this checks for languages that do not typically have square brackets on their keyboards
// Check for languages that do not have square brackets on their keyboards.
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes are largely unrelated, but I forget why I found this file when working on this patch and just wanted to tidy things up. Sorry 😉

newPost,
} from '../support/utils';

describe( 'new post', () => {
Copy link
Member

Choose a reason for hiding this comment

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

For maintenance sake, what sorts of things would we expect to exist here? Would it make sense to consolidate this with what we already have in hello.test.js . I don't have a strong preference for the direction of consolidation, but there does appear to be overlap in the realm of "things we expect from the initial state of the editor".

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, fair enough. I'll propose renaming hello.test.js though, because that wasn't clear to me as the "new editor state" test suite.

Copy link
Member

Choose a reason for hiding this comment

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

I'll propose renaming hello.test.js though, because that wasn't clear to me as the "new editor state" test suite.

Yep, I'd agree on that one 😄

@@ -1,6 +1,7 @@
/**
* External dependencies
*/
import React from 'react';
Copy link
Member

Choose a reason for hiding this comment

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

We don't want to import from react. There's createRef available from @wordpress/element.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe worth considering ESLint rule error'ing on react import. It shouldn't be present anywhere (save for tests, I suppose).

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, right, sorry 'bout that.

@tofumatt tofumatt requested a review from aduth September 4, 2018 14:39
@tofumatt
Copy link
Member Author

tofumatt commented Sep 4, 2018

Changes made, good for another look!

// If there is a post title and it's empty, we should focus the title so
// the user can start typing the title right away.
if ( ! this.props.title || ! this.props.title.length ) {
this.textareaRef.current.textarea.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

This will have another maybe "unwanted" side effect? If you switch to code mode and you switch back to editor without touching the title, it will be autofocused?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah shoot, good call. I was thinking "we don't re-mount the title after we render the editor, right?" But I guess we do, I'll handle this more robustly. 👍

@tofumatt tofumatt self-assigned this Sep 5, 2018
@tofumatt tofumatt removed the request for review from aduth September 5, 2018 09:24
@aduth
Copy link
Member

aduth commented Sep 5, 2018

This will have another maybe "unwanted" side effect? If you switch to code mode and you switch back to editor without touching the title, it will be autofocused?

Shouldn't this be the same component, and thus not unmounted when switching modes?

@tofumatt
Copy link
Member Author

tofumatt commented Sep 5, 2018

It seems the layout of the editor is changed "above" the title component so things are re-rendered.

That's probably a legit bug but it might be beyond this one. 😄

// If there is a post title and it's empty, we should focus the title so
// the user can start typing the title right away.
if ( ! this.props.title || ! this.props.title.length ) {
this.textareaRef.current.textarea.focus();
Copy link
Member

Choose a reason for hiding this comment

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

Can Textarea receive an autoFocus prop to simplify the implementation?

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea#attr-autofocus

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if it supports passing through other props given it's not a stock <Textarea>, but I'll have a look.

Copy link
Member Author

Choose a reason for hiding this comment

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

It can and that actually worked well, but Puppeteer doesn't seem to like it. I'm just working on figuring out why and then this'll be good to go and less complicated.

@tofumatt
Copy link
Member Author

tofumatt commented Sep 6, 2018

Okay! Improvements made; this now uses autofocus and behaves a lot better. All set for another review.

@tofumatt tofumatt force-pushed the 3828/feat-focus-empty-title branch from a901273 to 9de29e3 Compare September 6, 2018 10:27
@@ -36,6 +36,7 @@ class PostTitle extends Component {
this.onUnselect = this.onUnselect.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.redirectHistory = this.redirectHistory.bind( this );
this.textareaRef = createRef();
Copy link
Member

Choose a reason for hiding this comment

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

Unused, should remove.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, good catch!

focus the title on new post so the author can start typing
right away, without needing to click anything.
*/
autofocus={ this.props.isPostEmpty ? 'autofocus' : undefined }
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't React warn about this usage, since we're assigning the attribute, not the DOM property autoFocus (capitalized "F")? With the latter, I'd also expect this to be passed as a boolean, i.e.

autoFocus={ this.props.isPostEmpty }

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't see a warning, also I meant to assign the attribute… will React do some magic and assign a value to a DOM attribute if passed like that?! 😱

Copy link
Member

@aduth aduth Sep 6, 2018

Choose a reason for hiding this comment

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

Related: https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html

Notably:

Note that you should still use the canonical React naming for known attributes:

// Yes, please
<div tabIndex="-1" />

// Warning: Invalid DOM property `tabindex`. Did you mean `tabIndex`?
<div tabindex="-1" />

Copy link
Member Author

Choose a reason for hiding this comment

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

I missed that one, cool!

@tofumatt tofumatt force-pushed the 3828/feat-focus-empty-title branch from 9de29e3 to 13b6d43 Compare September 7, 2018 20:28
@tofumatt tofumatt requested a review from aduth September 7, 2018 20:44
@tofumatt
Copy link
Member Author

tofumatt commented Sep 7, 2018

Set for another review, sorry 'bout the delay! Thanks for the info on DOM attributes!

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

LGTM 👍

I'm a little wary of the unintended remounts triggering focus grabs, but if anything it would be good for those to be surfaced (as disruptive as they might be) since the unintentionally surrounding this possibility should itself be considered a bug worth addressing.

A small thing, but it was a bit jarring for me to see the additional visual effect of the focused title (the border) when loading the editor. This is, of course, expected as a result of the title being auto-focused, though in the broader sense maybe we don't want to have such a heavy first impression. Worth iterating if it's sensed to be problematic.

image

@tofumatt
Copy link
Member Author

tofumatt commented Sep 7, 2018

Fair enough, I could see the focus style being tweaked is the post is in the same state that enables autoFocus (eg isCleanNewPost).

Unrelated, but that's some Chrome theme you have there. ✨

@tofumatt tofumatt merged commit b688d2e into master Sep 7, 2018
@tofumatt tofumatt deleted the 3828/feat-focus-empty-title branch September 7, 2018 21:24
@tofumatt tofumatt added this to the 3.9 milestone Sep 7, 2018
@designsimply
Copy link
Member

designsimply commented Sep 14, 2018

I tested and focus automatically goes to the title on new posts, but that does not happen for pre-existing published or draft posts with an empty title. Is this the intended behavior?

Tested with Firefox 62.0, Safari 11.1.2, and Chrome 69.0.3497.92 on macOS 10.13.6.

@tofumatt
Copy link
Member Author

That's the intended behaviour, yes. The idea is that any existing post that's been saved without a title was done so intentionally, so we don't focus the title in that case.

It would probably be nice if we eventually focused the last block on an existing page, but that's another issue 😄

For now nothing should be focused on an existing post.

@aduth
Copy link
Member

aduth commented Sep 14, 2018

Since I just encountered it in an unrelated fix, what should be the expected behavior for a new post with a non-empty title, either via the_title filter or some initial edits (see Demo page).

#9921

@designsimply
Copy link
Member

My take: since the demo post is a new post that has not yet been saved (bit of an edge case), so I think it's fine that auto-focus works for the title in that case.

@afercia
Copy link
Contributor

afercia commented Oct 14, 2018

I'd like to propose to reconsider this for the demo post, if no objections? I'd tend to think the demo post should serve to familiarize with the whole UI. Skipping all the UI that's before the post title doesn't help screen reader users in discovering the features in the top bar. Actually, I'm not even sure if the demo post is meant to stay after merge, but what about checking explicitly if the post is empty? Something like autoFocus={ isCleanNewPost && ! title }. /Cc @tofumatt

@tofumatt
Copy link
Member Author

Ah, that's a really good point @afercia. I'm not sure it's staying either but before I thought that edge case of focusing the title on the demo was fine because I wanted to err toward simple code. As it serves as an introduction to the editor, not focusing it seems good.

I mean, on the other hand, it means the intro to the editor won't focus the title like a new post would, but then a post like the demo page that had been saved wouldn't focus either. 🤷‍♂️

So yeah, I think that's a good point and we shouldn't focus it if a title is present. 👍

I've filed: #10601 to track it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants