From d684ef6d23b2ab95076706f689910294abd0932e Mon Sep 17 00:00:00 2001 From: Andrey Dolgachev Date: Thu, 4 Dec 2025 20:33:33 +0900 Subject: [PATCH] feat(material/slide-toggle): Add option to completely hide label --- goldens/material/slide-toggle/index.api.md | 5 ++++- src/dev-app/slide-toggle/slide-toggle-demo.html | 4 ++++ src/material/slide-toggle/slide-toggle.html | 16 +++++++++------- src/material/slide-toggle/slide-toggle.spec.ts | 2 ++ src/material/slide-toggle/slide-toggle.ts | 3 +++ 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/goldens/material/slide-toggle/index.api.md b/goldens/material/slide-toggle/index.api.md index 4f42898e17b9..820554fd4f02 100644 --- a/goldens/material/slide-toggle/index.api.md +++ b/goldens/material/slide-toggle/index.api.md @@ -50,6 +50,7 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C _getAriaLabelledBy(): string | null; _handleClick(): void; hideIcon: boolean; + hideLabel: boolean; id: string; get inputId(): string; _labelId: string; @@ -66,6 +67,8 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C // (undocumented) static ngAcceptInputType_hideIcon: unknown; // (undocumented) + static ngAcceptInputType_hideLabel: unknown; + // (undocumented) static ngAcceptInputType_required: unknown; // (undocumented) static ngAcceptInputType_tabIndex: unknown; @@ -88,7 +91,7 @@ export class MatSlideToggle implements OnDestroy, AfterContentInit, OnChanges, C validate(control: AbstractControl): ValidationErrors | null; writeValue(value: any): void; // (undocumented) - static ɵcmp: i0.ɵɵComponentDeclaration; + static ɵcmp: i0.ɵɵComponentDeclaration; // (undocumented) static ɵfac: i0.ɵɵFactoryDeclaration; } diff --git a/src/dev-app/slide-toggle/slide-toggle-demo.html b/src/dev-app/slide-toggle/slide-toggle-demo.html index 59b711ac3e9f..837d1c47acce 100644 --- a/src/dev-app/slide-toggle/slide-toggle-demo.html +++ b/src/dev-app/slide-toggle/slide-toggle-demo.html @@ -12,6 +12,10 @@ Disable Bound No icon +

With no label.

+ + +

Example where the slide toggle is required inside of a form.

diff --git a/src/material/slide-toggle/slide-toggle.html b/src/material/slide-toggle/slide-toggle.html index 6e1c74ce2ad4..bc7b8e5f1ad6 100644 --- a/src/material/slide-toggle/slide-toggle.html +++ b/src/material/slide-toggle/slide-toggle.html @@ -53,11 +53,13 @@ - - + @if (!hideLabel) { + + + } diff --git a/src/material/slide-toggle/slide-toggle.spec.ts b/src/material/slide-toggle/slide-toggle.spec.ts index 6b6616830b32..ca91e65bef28 100644 --- a/src/material/slide-toggle/slide-toggle.spec.ts +++ b/src/material/slide-toggle/slide-toggle.spec.ts @@ -654,6 +654,8 @@ describe('MatSlideToggle with forms', () => { it('should update checked state on click if control is checked initially', fakeAsync(() => { fixture = TestBed.createComponent(SlideToggleWithModel); + fixture.detectChanges(); + slideToggle = fixture.debugElement.query(By.directive(MatSlideToggle))!.componentInstance; labelElement = fixture.debugElement.query(By.css('label'))!.nativeElement; diff --git a/src/material/slide-toggle/slide-toggle.ts b/src/material/slide-toggle/slide-toggle.ts index 2b0df39012c4..1f400f31b872 100644 --- a/src/material/slide-toggle/slide-toggle.ts +++ b/src/material/slide-toggle/slide-toggle.ts @@ -186,6 +186,9 @@ export class MatSlideToggle /** Whether to hide the icon inside of the slide toggle. */ @Input({transform: booleanAttribute}) hideIcon: boolean; + /** Whether to hide label for the slide toggle. */ + @Input({transform: booleanAttribute}) hideLabel: boolean; + /** Whether the slide toggle should remain interactive when it is disabled. */ @Input({transform: booleanAttribute}) disabledInteractive: boolean;