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

Add safeguards in onTab #3364

Merged
merged 2 commits into from Aug 12, 2018
Merged

Add safeguards in onTab #3364

merged 2 commits into from Aug 12, 2018

Conversation

ArsalanDotMe
Copy link
Contributor

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() on undefined.

Checks for undefined before calling focus() on an element
@alvarotrigo
Copy link
Owner

Awesome! Thanks for the fix! Didn't notice the bug!
Could you please add the return statement within the condition? Then I'll merge it!

src/fullpage.js Outdated
return focusableElements[0].focus();
const firstFocusableElement = focusableElements[0];
if (firstFocusableElement) {
firstFocusableElement.focus();
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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);

Copy link
Contributor Author

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.

Copy link
Owner

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
@ArsalanDotMe
Copy link
Contributor Author

made the change for a single return statement while returning the focused element if present.

@alvarotrigo alvarotrigo merged commit cc7337e into alvarotrigo:dev Aug 12, 2018
@alvarotrigo
Copy link
Owner

Thanks! Merged!

alvarotrigo added a commit that referenced this pull request Aug 12, 2018
@ArsalanDotMe ArsalanDotMe deleted the dev branch August 12, 2018 17:38
@alvarotrigo alvarotrigo mentioned this pull request Sep 26, 2018
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.

None yet

2 participants