Skip to content

Commit 2d592a5

Browse files
julianobrasiljelbourn
authored andcommitted
feat(slider): add focus and blur methods do MatSlider (#9373)
1 parent b3f8a42 commit 2d592a5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/demo-app/slider/slider-demo.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ <h1>Inverted vertical slider</h1>
5353
aria-label="Inverted vertical slider">
5454
</mat-slider>
5555

56+
<h1>Set/lost focus to show thumblabel programmatically</h1>
57+
<mat-slider #demoSlider="matSlider" thumbLabel aria-label="Slider with thumb label"></mat-slider>
58+
<button (click)="demoSlider.focus()">Focus Slider</button>
59+
<button (click)="demoSlider.blur()">Blur Slider</button>
60+
5661
<mat-tab-group>
5762
<mat-tab label="One">
5863
<mat-slider min="1" max="5" value="3" aria-label="Slider in a tab"></mat-slider>

src/lib/slider/slider.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ export class MatSlider extends _MatSliderMixinBase
267267
return this.value || 0;
268268
}
269269

270+
/** set focus to the host element */
271+
focus() {
272+
this._focusHostElement();
273+
}
274+
275+
/** blur the host element */
276+
blur() {
277+
this._blurHostElement();
278+
}
279+
270280
/** onTouch function registered via registerOnTouch (ControlValueAccessor). */
271281
onTouched: () => any = () => {};
272282

@@ -691,6 +701,11 @@ export class MatSlider extends _MatSliderMixinBase
691701
this._elementRef.nativeElement.focus();
692702
}
693703

704+
/** Blurs the native element. */
705+
private _blurHostElement() {
706+
this._elementRef.nativeElement.blur();
707+
}
708+
694709
/**
695710
* Sets the model value. Implemented as part of ControlValueAccessor.
696711
* @param value

0 commit comments

Comments
 (0)