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

Fix element blurring (removing focus) #116

Merged
merged 3 commits into from
Jan 29, 2018
Merged

Fix element blurring (removing focus) #116

merged 3 commits into from
Jan 29, 2018

Conversation

BehindTheMath
Copy link
Collaborator

  • Only blur element if it's contained by one of the selectors

    Previously, Pjax would blur (remove focus) from the active element regardless of where it was on the page. This commit restricts that to happen only if the active element is contained by one of the elements represented by options.selectors, because only those are affected by Pjax.

    Fixes Add blur on active element when page is changed #4

  • Remove a redundant call to .blur()

Previously, Pjax would blur (remove focus) from the active element
regardless of where it was on the page. This restricts that to
happen only if the active element is contained by one of the
elements represented by options.selectors, because only those are
affected by Pjax.

Fixes #4
index.js Outdated
@@ -129,7 +130,7 @@ Pjax.prototype = {

// Clear out any focused controls before inserting new page contents.
// we clear focus on non form elements
if (document.activeElement && !document.activeElement.value) {
if (document.activeElement && !document.activeElement.value && contains(this.options.selectors, document.activeElement)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to not clear the focus on form elements? jquery-pjax doesn't do this: https://github.com/defunkt/jquery-pjax/blob/c9acf5e7e9e16fdd34cb2de882d627f97364a952/jquery.pjax.js#L296

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess the question is, what are we trying to accomplish.

From what I understand, jquery-pjax just wanted that the focus shouldn't be on an element that doesn't exist anymore, so they only blurred elements inside the containers that were being replaced. If we're trying to do the same, I think the PR as it is now accomplishes that.

After rereading #4, it sounds like the goal of that issue is to remove focus from the link (or form) that was clicked, in order to get rid of things like residual tooltips, while not removing focus from elements like search boxes etc.
If those links are inside the containers being replaced, I can understand why the user wouldn't want those tooltips, and that would still be covered after this PR.
If those links are elsewhere on the page, why would you want to remove focus from them?

Copy link
Collaborator

@robinnorth robinnorth Jan 29, 2018

Choose a reason for hiding this comment

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

Yes, I agree that the aim should be to clear the focus on elements that aren't going to exist after the switch, which is why I asked if there was a particular reason not to do this when the active element has a value attribute (is a form element). I think that check could be eliminated, simplifying the test for elements to blur to

if (document.activeElement && contains(this.options.selectors, document.activeElement)) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think you're right. I assume that check was only there since the original code was clearing focus no matter where on the page it was.

@@ -6,6 +6,7 @@ var forEachEls = require("./lib/foreach-els.js")
var newUid = require("./lib/uniqueid.js")

var noop = require("./lib/util/noop")
var contains = require("./lib/util/contains.js")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these vars were originally alphabetically ordered, but grouping modules by path is also something that makes sense -- another thing to standardise on for #97?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea. I'll mention that there.

@BehindTheMath BehindTheMath changed the title Fix/blur Fix element blurring (removing focus) Jan 29, 2018
@robinnorth
Copy link
Collaborator

LGTM! 🎉

@robinnorth robinnorth merged commit 20e9879 into master Jan 29, 2018
@robinnorth robinnorth deleted the fix/blur branch January 29, 2018 20:03
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.

2 participants