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

Allow navigation between blocks with arrow keys #1161

Closed
wants to merge 2 commits into from
Closed

Conversation

ellatrix
Copy link
Member

This PR adds arrow key navigation between blocks in a very basic way, just like tabs would work. I took the approach of looking at focusable nodes instead of passing props, so block implementors don't have to worry about this as well.

@youknowriad
Copy link
Contributor

Nice! 👍 I'd suggest adding the same behavior to VisualEditorBlock if the focused element is the block container node, this would allow navigating from top to bottom without "stop".

Also, I noticed a "bug" in the button block where the focus moves to the link Editable. We could add tabindex="-1" maybe.

Regardless, this is great 👍

const moveDown = ( keyCode === DOWN || keyCode === RIGHT ) && this.isEndOfEditor();
const selectors = [
'*[contenteditable="true"]',
'*[tabindex]',
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 exclude tabindex=-1?

Copy link
Member

@aduth aduth Jun 13, 2017

Choose a reason for hiding this comment

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

Yeah, this won't account for all possibilities. Another being contenteditable without an explicitly true value (contenteditable, contenteditable=""). There's also select, button, object, area, a, though dunno if we want to test all of them in this behavior.

I'm forgetting the resource I used in creating it, but this implementation is close to spec for "focusable":

https://codepen.io/aduth/pen/wdRzxM

Maybe also worth moving out as a separate utility.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow, yeah, that's a lot. I'm all for using a library for this if that's better.

@ellatrix
Copy link
Member Author

@youknowriad Trying something slightly different at a block level... This approach looks at the range. If the range is the same, this means that nothing happened when pressing the arrow keys, which either means that the caret is at the start or end of text input, or that it's not text input. This seems to be good because it will handle more than just Editable.

@ellatrix
Copy link
Member Author

To do: use that library.

@youknowriad
Copy link
Contributor

This seems to work great for all our current blocks, but I wonder if it's not too aggressive. I mean, maybe a block with an input or something don't want to lose focus if we accidentally click on arrows. This question is I think hard to answer right now, so I'm ok with moving forward, I haven't seen any glitch (aside the link boundaries)

@ellatrix
Copy link
Member Author

ellatrix commented Jun 14, 2017

Not sure why the tests don't pass.

Yeah, the links boundaries are an issue with either approach.

@notnownikki
Copy link
Member

Purely from a user's perspective, this is so much better.

I'm undecided on how it handles the form fields... On one hand, it's not standard behaviour and yeah, we might lose focus accidentally. But on the other, it's great if I'm going from the bottom of the document to the top by holding down the 'up' arrow. I can completely navigate my post without leaving the arrow keys, and I only tab if I want to get into toolbars and other editing controls. However, if I was going up a document and ended up in an input field and arrow keys no longer took me up, that's expected behaviour and tabbing is the normal keyboard way of getting out of that, and would do what I expect.

Nice!

@ellatrix
Copy link
Member Author

Working on redoing this with props right now.

@ellatrix ellatrix force-pushed the add/arrow-nav branch 3 times, most recently from 6b2caf1 to 255d12a Compare June 22, 2017 12:45
@ellatrix
Copy link
Member Author

I think I'll leave table alone for now and do that in a separate PR. You can still navigate over it though.

@ellatrix ellatrix requested a review from mtias June 22, 2017 14:25
@mtias mtias added [Feature] Blocks Overall functionality of blocks General Interface Parts of the UI which don't fall neatly under other labels. [Type] Task Issues or PRs that have been broken down into an individual action to take labels Jun 22, 2017
@youknowriad
Copy link
Contributor

I like the flexibility of the current path because the block author has all the powers but I think I prefer the approach relying on "Tabbable" elements because it removes some burden from the block author. I hope I didn't push you to the wrong direction. Happy to follow the majority here.

I wonder if there's not a middleground where we can do tabbables but we can deactivate it for more control and rely on props. Anyway, I think we should start with tabbables only. What do you think?

@ellatrix
Copy link
Member Author

ellatrix commented Jun 23, 2017

Oh no, I really wish I saved the previous approach and didn't force push 🙈 Lesson learned

@ellatrix
Copy link
Member Author

@youknowriad So I really don't kind either approach, I can open a new PR with the previous approach. I'm not sure what the middle ground is you're suggesting. :) Maybe some more eyes could help here? @matias What do you think?

@mtias
Copy link
Member

mtias commented Jul 6, 2017

Closes #1723.

@ellatrix
Copy link
Member Author

ellatrix commented Jul 7, 2017

Follow up PR: #1778.

@nylen nylen mentioned this pull request Jul 7, 2017
20 tasks
@aduth
Copy link
Member

aduth commented Jul 8, 2017

Should this be closed as superseded by #1778 ?

@ellatrix
Copy link
Member Author

ellatrix commented Jul 8, 2017

Yep

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks General Interface Parts of the UI which don't fall neatly under other labels. [Type] Task Issues or PRs that have been broken down into an individual action to take
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants