Skip to content

Commit 5c7e557

Browse files
authored
feat(material/slide-toggle): allow for default color to be configured globally (#22047)
Allows for the `color` of a slide toggle to be configured through a provider. Fixes #22012.
1 parent 8d6a07f commit 5c7e557

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

src/material-experimental/mdc-slide-toggle/slide-toggle-config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {InjectionToken} from '@angular/core';
9+
import {ThemePalette} from '@angular/material-experimental/mdc-core';
910

1011

1112
/** Default `mat-slide-toggle` options that can be overridden. */
1213
export interface MatSlideToggleDefaultOptions {
1314
/** Whether toggle action triggers value changes in slide toggle. */
1415
disableToggleValue?: boolean;
16+
17+
/** Default color for slide toggles. */
18+
color?: ThemePalette;
1519
}
1620

1721
/** Injection token to be used to override the default options for `mat-slide-toggle`. */
1822
export const MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS =
1923
new InjectionToken<MatSlideToggleDefaultOptions>('mat-slide-toggle-default-options', {
2024
providedIn: 'root',
21-
factory: () => ({disableToggleValue: false, disableDragValue: false})
25+
factory: () => ({disableToggleValue: false})
2226
});

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,22 @@ describe('MDC-based MatSlideToggle without forms', () => {
456456
expect(testComponent.dragTriggered).toBe(0);
457457
}));
458458

459+
it('should be able to change the default color', () => {
460+
TestBed
461+
.resetTestingModule()
462+
.configureTestingModule({
463+
imports: [MatSlideToggleModule],
464+
declarations: [SlideToggleWithForm],
465+
providers: [
466+
{provide: MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS, useValue: {color: 'warn'}},
467+
]
468+
});
469+
const fixture = TestBed.createComponent(SlideToggleWithForm);
470+
fixture.detectChanges();
471+
const slideToggle = fixture.nativeElement.querySelector('.mat-mdc-slide-toggle');
472+
expect(slideToggle.classList).toContain('mat-warn');
473+
});
474+
459475
it('should clear static aria attributes from the host node', () => {
460476
const fixture = TestBed.createComponent(SlideToggleWithStaticAriaAttributes);
461477
fixture.detectChanges();

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
114114
_rippleAnimation: RippleAnimationConfig = RIPPLE_ANIMATION_CONFIG;
115115

116116
/** The color palette for this slide toggle. */
117-
@Input() color: ThemePalette = 'accent';
117+
@Input() color: ThemePalette;
118118

119119
/** Name value will be applied to the input element if present. */
120120
@Input() name: string | null = null;
@@ -203,6 +203,7 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
203203
public defaults: MatSlideToggleDefaultOptions,
204204
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
205205
this.tabIndex = parseInt(tabIndex) || 0;
206+
this.color = defaults.color || 'accent';
206207
}
207208

208209
ngAfterViewInit() {

src/material/slide-toggle/slide-toggle-config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {InjectionToken} from '@angular/core';
9+
import {ThemePalette} from '@angular/material/core';
910

1011

1112
/** Default `mat-slide-toggle` options that can be overridden. */
1213
export interface MatSlideToggleDefaultOptions {
1314
/** Whether toggle action triggers value changes in slide toggle. */
1415
disableToggleValue?: boolean;
16+
17+
/** Default color for slide toggles. */
18+
color?: ThemePalette;
1519
}
1620

1721
/** Injection token to be used to override the default options for `mat-slide-toggle`. */

src/material/slide-toggle/slide-toggle.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ describe('MatSlideToggle without forms', () => {
407407
}));
408408
});
409409

410-
describe('custom action configuration', () => {
410+
describe('default options', () => {
411411
it('should not change value on click when click action is noop when using custom a ' +
412412
'action configuration', () => {
413413
TestBed
@@ -448,6 +448,22 @@ describe('MatSlideToggle without forms', () => {
448448
expect(testComponent.dragTriggered).toBe(0);
449449
});
450450

451+
it('should be able to change the default color', () => {
452+
TestBed
453+
.resetTestingModule()
454+
.configureTestingModule({
455+
imports: [MatSlideToggleModule],
456+
declarations: [SlideToggleWithForm],
457+
providers: [
458+
{provide: MAT_SLIDE_TOGGLE_DEFAULT_OPTIONS, useValue: {color: 'warn'}},
459+
]
460+
});
461+
const fixture = TestBed.createComponent(SlideToggleWithForm);
462+
fixture.detectChanges();
463+
const slideToggle = fixture.nativeElement.querySelector('.mat-slide-toggle');
464+
expect(slideToggle.classList).toContain('mat-warn');
465+
});
466+
451467
});
452468

453469
describe('without label', () => {

src/material/slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const _MatSlideToggleMixinBase:
7272
CanDisableRippleCtor &
7373
CanDisableCtor &
7474
typeof MatSlideToggleBase =
75-
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatSlideToggleBase)), 'accent'));
75+
mixinTabIndex(mixinColor(mixinDisableRipple(mixinDisabled(MatSlideToggleBase))));
7676

7777
/** Represents a slidable "switch" toggle that can be moved between on and off. */
7878
@Component({
@@ -168,6 +168,7 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
168168
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
169169
super(elementRef);
170170
this.tabIndex = parseInt(tabIndex) || 0;
171+
this.color = this.defaultColor = defaults.color || 'accent';
171172
}
172173

173174
ngAfterContentInit() {

tools/public_api_guard/material/slide-toggle.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export declare class MatSlideToggleChange {
5757
}
5858

5959
export interface MatSlideToggleDefaultOptions {
60+
color?: ThemePalette;
6061
disableToggleValue?: boolean;
6162
}
6263

0 commit comments

Comments
 (0)