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

Focus route if active element is not changed by app #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
resolve,
match,
insertParams,
validateRedirect
validateRedirect,
getActiveElement
} from "./lib/utils";
import {
globalHistory,
Expand Down Expand Up @@ -308,17 +309,28 @@ class FocusHandlerImpl extends React.Component {
return;
}

let { requestFocus } = this.props;
Promise.resolve().then(() => {
if (
getActiveElement() !==
"LAST_ACTIVE_ELEMENT" /* not sure how best to track this */
) {
// if the app has already updated the active (focused) element, leave
// it as is and don't focus the route
return;
}

if (requestFocus) {
requestFocus(this.node);
} else {
if (initialRender) {
initialRender = false;
let { requestFocus } = this.props;

if (requestFocus) {
requestFocus(this.node);
} else {
this.node.focus();
if (initialRender) {
initialRender = false;
} else {
this.node.focus();
}
}
}
});
}

requestFocus = node => {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ let validateRedirect = (from, to) => {
return fromString === toString;
};

////////////////////////////////////////////////////////////////////////////////
// getActiveElement()
//
// Returns the current active element if in a browser, otherwise returns null
let getActiveElement = () => {
return typeof window !== "undefined" ? document.activeElement : null;
}

////////////////////////////////////////////////////////////////////////////////
// Junk
let paramRe = /^:(.+)/;
Expand Down Expand Up @@ -259,4 +267,4 @@ let addQuery = (pathname, query) => pathname + (query ? `?${query}` : "");
let reservedNames = ["uri", "path"];

////////////////////////////////////////////////////////////////////////////////
export { startsWith, pick, match, resolve, insertParams, validateRedirect };
export { startsWith, pick, match, resolve, insertParams, validateRedirect, getActiveElement };