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.

Improve modal focus handling with NgbAutoFocus directive and first element focus

See original GitHub issue
  • Allow users to set the initial focus inside the modal with NgbAutoFocus directive.
<!-- will be focused after the modal opens -->
<input type="text" ngbAutoFocus />
  • If there is no ngbAutoFocus in the modal, focus first focusable element by default. Allow user to cancel this behaviour and focus nothing by doing
modal.open(content, {focusFirst: false})
  • add a demo for focus handling in a separate PR

Not sure if this is a breaking change and not sure if we need ngbAutoFocus as a directive or a simple attribute would do.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
kedar9444commented, Dec 18, 2018

@maxokorokov : modal.open(content, {focusFirst: false}) is this feature removed from V4, because i am not able to see focusFirst property in NgbModalOptions. if yes how to achieve a situation where i don’t want to set any default focus but should be able to close the modal with ESC key.

3reactions
peterblazejewiczcommented, Sep 21, 2018

hi folks, just wondering why the custom (and new) directive instead of using native autofocus? (I know that part, it does not work in first place, dynamic content behaviour, etc). But in the guts of the pending PR plain selector is being using:

const elementToFocus = this._elRef.nativeElement.querySelector(`[ngbAutofocus]`) as HTMLElement;

https://github.com/ng-bootstrap/ng-bootstrap/pull/2737/files#diff-8fb1cd1cc4cf9a5207c30f1474b6c8b9R67

and I think this does not differ much from:

const elementToFocus = this._elRef.nativeElement.querySelector(`[autofocus]`) as HTMLElement;

Thanks!

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 >
Adding autofocus to an input field in an Angular 5 Bootstrap 4 ...
The issue was that input field was auto focussed only the first time I ... start typing without having to manually focus the...
Read more >
Modal - Angular powered Bootstrap
First focusable element within the modal window will receive focus upon opening. ... to focus any other element by adding an ngbAutofocus attribute...
Read more >
How to set focus on input in modal? - MDBootstrap
If I use <input autofocus> or attempt to set focus dynamically on an input inside modal body, it would not work. How do...
Read more >
Angular7 And Ngbmodal: How To Remove Default Auto Focus
If there is no ngbAutoFocus in the modal, focus first focusable If there is ... for run focus trap event handlers outside Angular...
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