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
Add safeguards in onTab #3364
Add safeguards in onTab #3364
Conversation
Checks for undefined before calling focus() on an element
Awesome! Thanks for the fix! Didn't notice the bug! |
src/fullpage.js
Outdated
return focusableElements[0].focus(); | ||
const firstFocusableElement = focusableElements[0]; | ||
if (firstFocusableElement) { | ||
firstFocusableElement.focus(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing the return statement here inside the condtion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I omitted the return statement inside the condition because regardless of whether the firstFocusableElement was defined or not, we MUST return. Adding the return statement inside the condition would make it have 2 return statements. Just FYI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now you are always returning undefined
.
But we need to return the focusable element if we don't want to break the code :)
See a few lines ahead this variable assign:
activeElement = preventAndFocusFirst(e);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I get that now. I'll make the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to keep a single return, probaby easier something like this:
return firstFocusableElement ? firstFocusableElement.focus() : null;
Return the focusedElement if defined in a single statement
made the change for a single return statement while returning the focused element if present. |
Thanks! Merged! |
Checks for undefined before calling focus() on an element
I'm facing a problem in my React app that uses fullpage.js. Whenever I press the Tab key, the keydown handler inside fullPage fails and prevents my own keydown handlers to work. In this pull request, I added a simple check that prevents calling
.focus()
onundefined
.