-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(focus-monitor): add documentation (#10547)
- Loading branch information
Showing
10 changed files
with
289 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/material-examples/focus-monitor-directives/focus-monitor-directives-example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.example-focus-monitor { | ||
padding: 20px; | ||
} | ||
|
||
.example-focus-monitor .cdk-mouse-focused { | ||
background: rgba(255, 0, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-keyboard-focused { | ||
background: rgba(0, 255, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-touch-focused { | ||
background: rgba(0, 0, 255, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-program-focused { | ||
background: rgba(255, 0, 255, 0.5); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/material-examples/focus-monitor-directives/focus-monitor-directives-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="example-focus-monitor"> | ||
<button cdkMonitorSubtreeFocus | ||
(cdkFocusChange)="elementOrigin = formatOrigin($event); markForCheck()"> | ||
Focus Monitored Element ({{elementOrigin}}) | ||
</button> | ||
</div> | ||
|
||
<div class="example-focus-monitor"> | ||
<div cdkMonitorSubtreeFocus | ||
(cdkFocusChange)="subtreeOrigin = formatOrigin($event); markForCheck()"> | ||
<p>Focus Monitored Subtree ({{subtreeOrigin}})</p> | ||
<button>Child Button 1</button> | ||
<button>Child Button 2</button> | ||
</div> | ||
</div> |
25 changes: 25 additions & 0 deletions
25
src/material-examples/focus-monitor-directives/focus-monitor-directives-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {FocusOrigin} from '@angular/cdk/a11y'; | ||
import {ChangeDetectorRef, Component, NgZone} from '@angular/core'; | ||
|
||
/** @title Monitoring focus with FocusMonitor */ | ||
@Component({ | ||
selector: 'focus-monitor-directives-example', | ||
templateUrl: 'focus-monitor-directives-example.html', | ||
styleUrls: ['focus-monitor-directives-example.css'] | ||
}) | ||
export class FocusMonitorDirectivesExample { | ||
elementOrigin: string = this.formatOrigin(null); | ||
subtreeOrigin: string = this.formatOrigin(null); | ||
|
||
constructor(private ngZone: NgZone, private cdr: ChangeDetectorRef) {} | ||
|
||
|
||
formatOrigin(origin: FocusOrigin): string { | ||
return origin ? origin + ' focused' : 'blurred'; | ||
} | ||
|
||
// Workaround for the fact that (cdkFocusChange) emits outside NgZone. | ||
markForCheck() { | ||
this.ngZone.run(() => this.cdr.markForCheck()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/material-examples/focus-monitor-focus-via/focus-monitor-focus-via-example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.example-focus-monitor { | ||
padding: 20px; | ||
} | ||
|
||
.example-focus-monitor .cdk-mouse-focused { | ||
background: rgba(255, 0, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-keyboard-focused { | ||
background: rgba(0, 255, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-touch-focused { | ||
background: rgba(0, 0, 255, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-program-focused { | ||
background: rgba(255, 0, 255, 0.5); | ||
} | ||
|
||
.example-focus-monitor button:focus { | ||
box-shadow: 0 0 30px cyan; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/material-examples/focus-monitor-focus-via/focus-monitor-focus-via-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="example-focus-monitor"> | ||
<button #monitored>1. Focus Monitored Element ({{origin}})</button> | ||
<button #unmonitored>2. Not Monitored</button> | ||
</div> | ||
|
||
<mat-form-field> | ||
<mat-label>Simulated focus origin</mat-label> | ||
<mat-select #simulatedOrigin value="mouse"> | ||
<mat-option value="mouse">Mouse</mat-option> | ||
<mat-option value="keyboard">Keyboard</mat-option> | ||
<mat-option value="touch">Touch</mat-option> | ||
<mat-option value="program">Programmatic</mat-option> | ||
</mat-select> | ||
</mat-form-field> | ||
|
||
<button (click)="focusMonitor.focusVia(monitored, simulatedOrigin.value)"> | ||
Focus button #1 | ||
</button> | ||
<button (click)="focusMonitor.focusVia(unmonitored, simulatedOrigin.value)"> | ||
Focus button #2 | ||
</button> |
42 changes: 42 additions & 0 deletions
42
src/material-examples/focus-monitor-focus-via/focus-monitor-focus-via-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y'; | ||
import { | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
NgZone, | ||
OnDestroy, | ||
OnInit, | ||
ViewChild | ||
} from '@angular/core'; | ||
|
||
/** @title Focusing with a specific FocusOrigin */ | ||
@Component({ | ||
selector: 'focus-monitor-focus-via-example', | ||
templateUrl: 'focus-monitor-focus-via-example.html', | ||
styleUrls: ['focus-monitor-focus-via-example.css'] | ||
}) | ||
export class FocusMonitorFocusViaExample implements OnDestroy, OnInit { | ||
@ViewChild('monitored') monitoredEl: ElementRef; | ||
|
||
origin: string = this.formatOrigin(null); | ||
|
||
constructor(public focusMonitor: FocusMonitor, | ||
private cdr: ChangeDetectorRef, | ||
private ngZone: NgZone) {} | ||
|
||
ngOnInit() { | ||
this.focusMonitor.monitor(this.monitoredEl.nativeElement) | ||
.subscribe(origin => this.ngZone.run(() => { | ||
this.origin = this.formatOrigin(origin); | ||
this.cdr.markForCheck(); | ||
})); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.focusMonitor.stopMonitoring(this.monitoredEl.nativeElement); | ||
} | ||
|
||
formatOrigin(origin: FocusOrigin): string { | ||
return origin ? origin + ' focused' : 'blurred'; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/material-examples/focus-monitor-overview/focus-monitor-overview-example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.example-focus-monitor { | ||
padding: 20px; | ||
} | ||
|
||
.example-focus-monitor .cdk-mouse-focused { | ||
background: rgba(255, 0, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-keyboard-focused { | ||
background: rgba(0, 255, 0, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-touch-focused { | ||
background: rgba(0, 0, 255, 0.5); | ||
} | ||
|
||
.example-focus-monitor .cdk-program-focused { | ||
background: rgba(255, 0, 255, 0.5); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/material-examples/focus-monitor-overview/focus-monitor-overview-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="example-focus-monitor"> | ||
<button #element>Focus Monitored Element ({{elementOrigin}})</button> | ||
</div> | ||
|
||
<div class="example-focus-monitor"> | ||
<div #subtree> | ||
<p>Focus Monitored Subtree ({{subtreeOrigin}})</p> | ||
<button>Child Button 1</button> | ||
<button>Child Button 2</button> | ||
</div> | ||
</div> |
50 changes: 50 additions & 0 deletions
50
src/material-examples/focus-monitor-overview/focus-monitor-overview-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {FocusMonitor, FocusOrigin} from '@angular/cdk/a11y'; | ||
import { | ||
ChangeDetectorRef, | ||
Component, | ||
ElementRef, | ||
NgZone, | ||
OnDestroy, | ||
OnInit, | ||
ViewChild | ||
} from '@angular/core'; | ||
|
||
/** @title Monitoring focus with FocusMonitor */ | ||
@Component({ | ||
selector: 'focus-monitor-overview-example', | ||
templateUrl: 'focus-monitor-overview-example.html', | ||
styleUrls: ['focus-monitor-overview-example.css'] | ||
}) | ||
export class FocusMonitorOverviewExample implements OnDestroy, OnInit { | ||
@ViewChild('element') element: ElementRef; | ||
@ViewChild('subtree') subtree: ElementRef; | ||
|
||
elementOrigin: string = this.formatOrigin(null); | ||
subtreeOrigin: string = this.formatOrigin(null); | ||
|
||
constructor(private focusMonitor: FocusMonitor, | ||
private cdr: ChangeDetectorRef, | ||
private ngZone: NgZone) {} | ||
|
||
ngOnInit() { | ||
this.focusMonitor.monitor(this.element.nativeElement) | ||
.subscribe(origin => this.ngZone.run(() => { | ||
this.elementOrigin = this.formatOrigin(origin); | ||
this.cdr.markForCheck(); | ||
})); | ||
this.focusMonitor.monitor(this.subtree.nativeElement, true) | ||
.subscribe(origin => this.ngZone.run(() => { | ||
this.subtreeOrigin = this.formatOrigin(origin); | ||
this.cdr.markForCheck(); | ||
})); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.focusMonitor.stopMonitoring(this.element.nativeElement); | ||
this.focusMonitor.stopMonitoring(this.subtree.nativeElement); | ||
} | ||
|
||
formatOrigin(origin: FocusOrigin): string { | ||
return origin ? origin + ' focused' : 'blurred'; | ||
} | ||
} |