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.

NgbModalWindow always focus the first element that can be focus.

See original GitHub issue

at viewInit of NgbModalWindow it will focus the first element if the page do not have auto focus, it will cause the close button often get focus when page no auto focus. whether provide a way to control this behavior?

if (!nativeElement.contains(document.activeElement)) { const autoFocusable = nativeElement.querySelector([ngbAutofocus]`) as HTMLElement; const firstFocusable = getFocusableBoundaryElements(nativeElement)[0];

  const elementToFocus = autoFocusable || firstFocusable || nativeElement;
  elementToFocus.focus();
}`

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jnizetcommented, Sep 16, 2020

You can simply add the ngbAutofocus attribute on a non-focusable element of the template. Here’s a demo: https://stackblitz.com/edit/angular-8mhynd?file=src/app/modal-basic.html

0reactions
wassimoocommented, Sep 16, 2020

Hi, I find this to be a little bit tricky, so maybe we should add such use case to the docs. Thank you! 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular7 and NgbModal: how to remove default auto focus
By default the focus is on the first focusable element within modal, which in your case is the close button. You can add...
Read more >
Modal - Angular powered Bootstrap
You can pass an existing component as content of the modal window. ... First focusable element within the modal window will receive focus...
Read more >
Dialog / How To / Prevent the focus on the first element
By default, the dialog focuses on the first elements of the content area which can be active and focusable. You can prevent this...
Read more >
Using JavaScript to trap focus in an element | hidde.blog
When a user presses TAB , the next focusable element receives focus. If this element is outside our component, focus should be set...
Read more >
How to Set Focus on Input Field or Textarea Inside a Bootstrap ...
You can simply use the jQuery .focus() method to set the focus on the first input box or textarea inside the Bootstrap modal...
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