@@ -22,6 +22,7 @@ import {
2222 ViewEncapsulation ,
2323 DOCUMENT ,
2424 AfterViewInit ,
25+ InjectionToken ,
2526} from '@angular/core' ;
2627import { _animationsDisabled , ThemePalette } from '../core' ;
2728import { _CdkPrivateStyleLoader , _VisuallyHiddenLoader } from '@angular/cdk/private' ;
@@ -40,6 +41,24 @@ export type MatBadgePosition =
4041/** Allowed size options for matBadgeSize */
4142export type MatBadgeSize = 'small' | 'medium' | 'large' ;
4243
44+ /** Object that can be used to configure the default options for the badge component. */
45+ export interface MatBadgeConfig {
46+ /** Default position for badges. */
47+ position ?: MatBadgePosition ;
48+
49+ /** Default size for badges. */
50+ size ?: MatBadgeSize ;
51+
52+ /** Default color to apply to all badges. */
53+ color ?: ThemePalette ;
54+
55+ /** Whether badges should overlap by default. */
56+ overlap ?: boolean ;
57+ }
58+
59+ /** Injection token that can be used to configure the default options for the badge component. */
60+ export const MAT_BADGE_CONFIG = new InjectionToken < MatBadgeConfig > ( 'MAT_BADGE_CONFIG' ) ;
61+
4362const BADGE_CONTENT_CLASS = 'mat-badge-content' ;
4463
4564/**
@@ -93,10 +112,10 @@ export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
93112 this . _setColor ( value ) ;
94113 this . _color = value ;
95114 }
96- private _color : ThemePalette = 'primary' ;
115+ private _color : ThemePalette ;
97116
98117 /** Whether the badge should overlap its contents or not */
99- @Input ( { alias : 'matBadgeOverlap' , transform : booleanAttribute } ) overlap : boolean = true ;
118+ @Input ( { alias : 'matBadgeOverlap' , transform : booleanAttribute } ) overlap : boolean ;
100119
101120 /** Whether the badge is disabled. */
102121 @Input ( { alias : 'matBadgeDisabled' , transform : booleanAttribute } ) disabled : boolean = false ;
@@ -105,7 +124,7 @@ export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
105124 * Position the badge should reside.
106125 * Accepts any combination of 'above'|'below' and 'before'|'after'
107126 */
108- @Input ( 'matBadgePosition' ) position : MatBadgePosition = 'above after' ;
127+ @Input ( 'matBadgePosition' ) position : MatBadgePosition ;
109128
110129 /** The content for the badge */
111130 @Input ( 'matBadge' )
@@ -128,7 +147,7 @@ export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
128147 private _description ! : string ;
129148
130149 /** Size of the badge. Can be 'small', 'medium', or 'large'. */
131- @Input ( 'matBadgeSize' ) size : MatBadgeSize = 'medium' ;
150+ @Input ( 'matBadgeSize' ) size : MatBadgeSize ;
132151
133152 /** Whether the badge is hidden. */
134153 @Input ( { alias : 'matBadgeHidden' , transform : booleanAttribute } ) hidden : boolean = false ;
@@ -148,10 +167,16 @@ export class MatBadge implements OnInit, AfterViewInit, OnDestroy {
148167 private _document = inject ( DOCUMENT ) ;
149168
150169 constructor ( ) {
170+ const config = inject ( MAT_BADGE_CONFIG , { optional : true } ) ;
151171 const styleLoader = inject ( _CdkPrivateStyleLoader ) ;
152172 styleLoader . load ( _MatBadgeStyleLoader ) ;
153173 styleLoader . load ( _VisuallyHiddenLoader ) ;
154174
175+ this . _color = config ?. color || 'primary' ;
176+ this . overlap = config ?. overlap ?? true ;
177+ this . position = config ?. position || 'above after' ;
178+ this . size = config ?. size || 'medium' ;
179+
155180 if ( typeof ngDevMode === 'undefined' || ngDevMode ) {
156181 const nativeElement = this . _elementRef . nativeElement ;
157182
0 commit comments