Skip to content

feat(FocusTrap): focus methods should accept focus options #21767

Closed
@lagatchell

Description

@lagatchell

Feature Description

FocusTrap methods should accept focus parameters.

Methods to change:

  • focusFirstTabbableElement()
  • focusFirstTabbableElementWhenReady()
  • focusInitialElement()
  • focusInitialElementWhenReady()
  • focusLastTabbableElement()
  • focusLastTabbableElementWhenReady()

Example:

// Would like to be able to do something like the following
constructor(
  private _elementRef: ElementRef,
  private _focusTrapFactory: ConfigurableFocusTrapFactory,
) {
  const focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
  const focusOptions = { preventScroll: true }; 
  focusTrap.focusFirstTabbableElementWhenReady(focusOptions);
}

Use Case

Using a FocusTrap to focus an element that starts out of view can cause parent elements to shift as the browser tries to bring the element into view. Allowing focus parameters to be configured, will enable consumers to set the preventScroll option and prevent this behavior (in supporting browsers).

Stackblitz demoing a scenario where the ability to specify focus parameters would be helpful.

Maybe be related to #17863

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: cdk/a11yfeatureThis issue represents a new feature or feature request rather than a bug or bug fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions