question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

optionally disable `getInitialFocusNode` on `firstTabbableNode`

See original GitHub issue

In some cases, especially in modals, it’s undesirable to autofocus the first tabbable node because the first tabbable node is a close button. That’s solvable by giving the modal container tabindex="-1" and setting initialFocus to a selector for that matches the modal container.

But what if that modal may have autofocusing children, like a text input in a registration form? And for some reason you can’t assign initialFocus to that input? In that case, the initialFocus option prevents the focus trap fallback behaviour of keeping an already focused element inside the focus trap contained focused.

a solution to this case would be adding a boolean option like autoFocusFirstTabbable, defaulted to true, that when set to false would prevent the fallback behavior of autofocusing the first tabbable element. Essentially, this change would make possible explicit consumer autofocus behavior instead of allowing the focus trap to search for a tabbable element and focus it.

this would be the crux of the change

function getInitialFocusNode() {
  var node;
  if (getNodeForOption("initialFocus") !== null) {
    node = getNodeForOption("initialFocus");
  } else if (container.contains(doc.activeElement)) {
    node = doc.activeElement;
  } else {
  // update
    node =
      (config.autofocusFirstTabbableNode ? state.firstTabbableNode : null) ||
      getNodeForOption("fallbackFocus");
  // end update
  }

  if (!node) {
    throw new Error(
      "Your focus-trap needs to have at least one focusable element"
    );
  }

  return node;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mruzekwcommented, Jun 24, 2021

@defnotrobbie @stefcameron Alright, that PR is up! #440

0reactions
stefcameroncommented, Apr 3, 2022

@CamilleHbp Yes! Thank you. Fixed with #440

Read more comments on GitHub >

github_iconTop Results From Across the Web

optionally disable `getInitialFocusNode` on `firstTabbableNode`
In some cases, especially in modals, it's undesirable to autofocus the first tabbable node because the first tabbable node is a close button....
Read more >
Is there a way to disable focus on div and its child nodes?
Firstly, there's a distinction between "not-focusable by tab key" and "never-focusable (programmatically, by click, or by tab)".
Read more >
Understanding Flutter's keyboard focus system
This means that if the focus is thereafter moved to the next node with FocusNode.nextFocus , it starts with the “first” focusable item...
Read more >
main.18b409e1.js.map - Google Git
node_modules/fbjs/lib/focusNode.js",". ... The optional arguments helps prevent the need\n * to bind in many cases.\n *\n * @param {function} method Member ...
Read more >
Index (JavaFX 16)
This enum describes the accessible role for a Node . ... This method will remove all cells from the VirtualFlow and remove them,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found