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

feat(cdk/a11y): expose current focus state from cdkMonitorElementFocus & cdkMonitorSubtreeFocus #25423

Closed
EmmanuelRoux opened this issue Aug 9, 2022 · 1 comment · Fixed by #25427
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team

Comments

@EmmanuelRoux
Copy link
Contributor

Feature Description

It would be very helpful to be able to export cdkMonitorElementFocus & cdkMonitorSubtreeFocus directives to read current focus state from template.

This requires:

  • Add exportAs to CdkMonitorFocus directive
  • Memoize and expose the focus state, eg. add new readonly focusOrigin: FocusOrigin or focused: boolean field(s)

I would be pleased to suggest a PR.

Use Case

Use case is to be able to easily check the focus state of an element to adapt other parts of the UI (eg. showing/hiding other elements...).

Example:

<div cdkMonitorSubtreeFocus #focusMonitor="cdkMonitorElementFocus ">
  <input type="text" />
  <input type="text" />
  <button type="button">Button 1</button>

  <!-- Following button should be displayed only when any of above inputs or button is focused -->
  <button *ngIf="focusMonitor.focused" type="button" (click)="doSomething()">Button 2</button>
</div>

My current alternative is to declare a custom directive like below:

@Directive({
  selector: '[cdkMonitorElementFocus], [cdkMonitorSubtreeFocus]', // Match existing CDK directives, or add another selector
  exportAs: 'myFocusMonitor',
})
export class MonitorFocusDirective {
  focused = false;

  @HostListener('cdkFocusChange', ['$event'])
  onFocusChange(origin: FocusOrigin): void {
    this.focused = !!origin;
  }
}

Note: existing cdkMonitorElementFocus/cdkMonitorSubtreeFocus directive already apply some css classes, which can be used to hide the element. But a css-only solution is not suitable for all scenarios.

@EmmanuelRoux EmmanuelRoux added feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team labels Aug 9, 2022
@EmmanuelRoux EmmanuelRoux changed the title feat(FocusMonitor): expose current focus state from cdkMonitorElementFocus & cdkMonitorSubtreeFocus feat(cdk/a11y): expose current focus state from cdkMonitorElementFocus & cdkMonitorSubtreeFocus Aug 9, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature This issue represents a new feature or feature request rather than a bug or bug fix needs triage This issue needs to be triaged by the team
Projects
None yet
1 participant