Skip to content

Commit c32d22e

Browse files
authored
feat(badge): design enhancement (#16309)
1 parent ad2af00 commit c32d22e

File tree

13 files changed

+275
-46
lines changed

13 files changed

+275
-46
lines changed

projects/igniteui-angular/src/lib/badge/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# igx-badge
22

33
The **igx-badge** component is an absolutely positioned element that can be used in tandem with other components such as avatars, navigation menus, or anywhere else in an app where some active indication is required.
4-
With the igx-badge you can display active count or an icon in several different predefined styles.
4+
With the igx-badge you can display active count or an icon in several different predefined styles and sizes.
55
A walkthrough of how to get started can be found [here](https://www.infragistics.com/products/ignite-ui-angular/angular/components/badge.html)
66

77
# Usage
@@ -14,9 +14,12 @@ A walkthrough of how to get started can be found [here](https://www.infragistics
1414
|:----------|:-------------:|:------|
1515
| `id` | string | Unique identifier of the component. If not provided it will be automatically generated.|
1616
| `type` | string | Set the type of the badge to either `primary`, `info`, `success`, `warning`, or `error`. This will change the background color of the badge according to the values set in the default theme. |
17+
| `dot` | boolean | Set whether the badge is displayed as a minimal dot indicator without any content. Default is `false`. |
1718
| `position` | string | Set the position of the badge relative to its parent container to either `top-right`, `top-left`, `bottom-right`, or `bottom-left`. |
1819
| `value` | string | Set the value to be displayed inside the badge. |
1920
| `icon` | string | Set an icon for the badge from the material icons set. Will not be displayed if `value` for the badge is already set. |
21+
| `outlined` | boolean | Set whether the badge should have an outline. Default is `false`. |
22+
| `shape` | string | Set the shape of the badge to either `rounded` or `square`. Default is `rounded`. |
2023

2124
# Examples
2225

@@ -26,3 +29,18 @@ Using `igx-badge` with the `igx-avatar` component to show active status.
2629
<igx-badge type="info" value="8"></igx-badge>
2730
</igx-avatar>
2831
```
32+
33+
Using `igx-badge` as a dot indicator for notifications.
34+
```html
35+
<igx-badge dot type="success"></igx-badge>
36+
<igx-badge dot outlined type="error"></igx-badge>
37+
```
38+
39+
Using different badge types.
40+
```html
41+
<igx-badge type="primary" value="1"></igx-badge>
42+
<igx-badge type="info" value="2"></igx-badge>
43+
<igx-badge type="success" value="3"></igx-badge>
44+
<igx-badge type="warning" value="4"></igx-badge>
45+
<igx-badge type="error" value="5"></igx-badge>
46+
```

projects/igniteui-angular/src/lib/badge/badge.component.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Badge', () => {
1111
InitBadgeWithDefaultsComponent,
1212
InitBadgeWithIconComponent,
1313
IgxBadgeComponent,
14-
InitBadgeWithIconARIAComponent
14+
InitBadgeWithIconARIAComponent,
15+
InitBadgeWithDotComponent
1516
]
1617
}).compileComponents();
1718
}));
@@ -87,6 +88,26 @@ describe('Badge', () => {
8788
const container = fixture.nativeElement.querySelectorAll('.igx-badge')[0];
8889
expect(container.getAttribute('aria-roledescription')).toMatch(expectedDescription);
8990
});
91+
92+
it('Initializes badge with dot property', () => {
93+
const fixture = TestBed.createComponent(InitBadgeWithDotComponent);
94+
fixture.detectChanges();
95+
const badge = fixture.componentInstance.badge;
96+
97+
expect(badge.dot).toBeTruthy();
98+
expect(fixture.debugElement.query(By.css('.igx-badge--dot'))).toBeTruthy();
99+
});
100+
101+
it('Initializes success badge as dot', () => {
102+
const fixture = TestBed.createComponent(InitBadgeWithDotComponent);
103+
fixture.detectChanges();
104+
const badge = fixture.componentInstance.badge;
105+
106+
expect(badge.type).toBe(IgxBadgeType.SUCCESS);
107+
expect(badge.dot).toBeTruthy();
108+
expect(fixture.debugElement.query(By.css('.igx-badge--dot'))).toBeTruthy();
109+
expect(fixture.debugElement.query(By.css('.igx-badge--success'))).toBeTruthy();
110+
});
90111
});
91112

92113
@Component({
@@ -120,3 +141,11 @@ class InitBadgeWithIconComponent {
120141
class InitBadgeWithIconARIAComponent {
121142
@ViewChild(IgxBadgeComponent, { static: true }) public badge: IgxBadgeComponent;
122143
}
144+
145+
@Component({
146+
template: `<igx-badge dot type="success"></igx-badge>`,
147+
imports: [IgxBadgeComponent]
148+
})
149+
class InitBadgeWithDotComponent {
150+
@ViewChild(IgxBadgeComponent, { static: true }) public badge: IgxBadgeComponent;
151+
}

projects/igniteui-angular/src/lib/badge/badge.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ export class IgxBadgeComponent {
153153
/** @hidden @internal */
154154
@HostBinding('class.igx-badge--square')
155155
public get _squareShape(): boolean {
156-
return this.shape === 'square';
156+
if (!this.dot) {
157+
return this.shape === 'square';
158+
}
157159
}
158160

159161
/**
@@ -183,6 +185,20 @@ export class IgxBadgeComponent {
183185
@HostBinding('class.igx-badge--outlined')
184186
public outlined = false;
185187

188+
/**
189+
* Sets/gets whether the badge is displayed as a dot.
190+
* When true, the badge will be rendered as a minimal 8px indicator without any content.
191+
* Default value is `false`.
192+
*
193+
* @example
194+
* ```html
195+
* <igx-badge dot type="success"></igx-badge>
196+
* ```
197+
*/
198+
@Input({transform: booleanAttribute})
199+
@HostBinding('class.igx-badge--dot')
200+
public dot = false;
201+
186202
/**
187203
* Defines a human-readable, accessor, author-localized description for
188204
* the `type` and the `icon` or `value` of the element.

projects/igniteui-angular/src/lib/core/styles/components/badge/_badge-component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
@extend %igx-badge--error !optional;
3636
}
3737

38+
@include m(dot) {
39+
@extend %igx-badge--dot !optional;
40+
}
41+
3842
@include m(outlined) {
3943
@extend %igx-badge--outlined !optional;
4044
}

projects/igniteui-angular/src/lib/core/styles/components/badge/_badge-theme.scss

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
/// @param {Map} $theme - The theme used to style the component.
77
@mixin badge($theme) {
88
@include css-vars($theme);
9-
9+
$theme-variant: map.get($theme, '_meta', 'variant');
1010
$variant: map.get($theme, '_meta', 'theme');
1111

1212
%igx-badge-display {
13-
--size: #{rem(22px)};
14-
--_badge-size: var(--size);
13+
@include sizable();
14+
15+
--component-size: var(--ig-size, #{var-get($theme, 'default-size')});
16+
--badge-size: var(--component-size);
17+
--_badge-size: #{var-get($theme, 'size')};
1518

1619
display: inline-flex;
1720
justify-content: center;
@@ -25,18 +28,18 @@
2528
overflow: hidden;
2629

2730
igx-icon {
28-
--size: var(--igx-icon-size, calc(var(--_badge-size) / 2));
31+
--size: var(--igx-icon-size, #{sizable(rem(12px), rem(14px), rem(16px))});
32+
--component-size: var(--badge-size);
2933

3034
display: inline-flex;
3135
justify-content: center;
3236
align-items: center;
33-
font-weight: 400;
3437
color: var-get($theme, 'icon-color');
3538
}
3639

3740
@if $variant == 'indigo' {
3841
igx-icon {
39-
$icon-size: rem(12px);
42+
$icon-size: sizable(rem(8px), rem(10px), rem(12px));
4043

4144
--ig-icon-size: #{$icon-size};
4245
--igx-icon-size: #{$icon-size};
@@ -45,7 +48,7 @@
4548
}
4649

4750
%igx-badge--outlined {
48-
box-shadow: inset 0 0 0 rem(if($variant != 'bootstrap', 2px, 1px)) var-get($theme, 'border-color');
51+
box-shadow: 0 0 0 rem(2px) var-get($theme, 'border-color');
4952
}
5053

5154
%igx-badge--square {
@@ -54,23 +57,57 @@
5457

5558
%igx-badge-value {
5659
white-space: nowrap;
57-
padding-inline: pad-inline(rem(4px));
60+
padding-inline: pad-inline(rem(4px), rem(6px), if($variant == 'indigo', rem(6px), rem(8px)));
5861
}
5962

6063
%igx-badge--success {
61-
background: color($color: 'success');
64+
background: color($color: 'success', $variant: if($variant != 'material', if($variant == 'indigo', 700, 500), 900));
6265
}
6366

6467
%igx-badge--info {
65-
background: color($color: 'info');
68+
background: color($color: 'info', $variant: if($variant != 'material', if($variant == 'fluent', 700, 500), 800));
6669
}
6770

6871
%igx-badge--warn {
6972
background: color($color: 'warn');
73+
74+
@if $variant == 'indigo' and $theme-variant == 'light' {
75+
color: color($color: 'gray', $variant: 900);
76+
77+
igx-icon {
78+
color: color($color: 'gray', $variant: 900);
79+
}
80+
} @else if $variant == 'indigo' and $theme-variant == 'dark' {
81+
color: color($color: 'gray', $variant: 50);
82+
83+
igx-icon {
84+
color: color($color: 'gray', $variant: 50);
85+
}
86+
} @else {
87+
color: contrast-color($color: 'warn', $variant: 500);
88+
89+
igx-icon {
90+
color: contrast-color($color: 'warn', $variant: 500);
91+
}
92+
}
7093
}
7194

7295
%igx-badge--error {
73-
background: color($color: 'error');
96+
background: color($color: 'error', $variant: if($variant == 'material', 700, 500));
97+
color: contrast-color($color: 'error', $variant: if($variant == 'bootstrap', 100, 900));
98+
}
99+
100+
%igx-badge--dot {
101+
--_dot-size: #{var-get($theme, 'dot-size')};
102+
103+
min-width: var(--_dot-size);
104+
min-height: var(--_dot-size);
105+
padding: 0;
106+
107+
igx-icon,
108+
> * {
109+
display: none;
110+
}
74111
}
75112

76113
%igx-badge--hidden {
@@ -79,15 +116,28 @@
79116
}
80117

81118
/// Adds typography styles for the igx-badge component.
82-
/// Uses the 'caption' category from the typographic scale.
119+
/// Uses 'caption' and 'body-2' categories from the typographic scale.
83120
/// @group typography
84121
/// @param {Map} $categories [(text: 'caption')] - The categories from the typographic scale used for type styles.
85-
@mixin badge-typography($categories: (text: 'caption')) {
122+
@mixin badge-typography($categories: (text: null), $theme: null) {
86123
$text: map.get($categories, 'text');
87124

88125
%igx-badge-display {
89-
@include type-style($text) {
90-
margin: 0;
126+
@if $text {
127+
@include type-style($text);
128+
} @else {
129+
@if $theme == 'indigo' {
130+
@include type-style('button', false) {
131+
font-size: sizable(rem(9px), rem(10px), var(--ig-button-font-size));
132+
line-height: sizable(rem(12px), rem(14px), var(--ig-button-line-height));
133+
}
134+
} @else {
135+
font-size: sizable(var(--ig-caption-font-size), var(--ig-body-2-font-size), var(--ig-body-2-font-size));
136+
font-weight: sizable(var(--ig-caption-font-weight), var(--ig-body-2-font-weight), var(--ig-body-2-font-weight));
137+
line-height: sizable(var(--ig-caption-line-height), var(--ig-body-2-line-height), var(--ig-body-2-line-height));
138+
letter-spacing: sizable(var(--ig-caption-letter-spacing), var(--ig-body-2-letter-spacing), var(--ig-body-2-letter-spacing));
139+
text-transform: sizable(var(--ig-caption-text-transform), var(--ig-body-2-text-transform), var(--ig-body-2-text-transform));
140+
}
91141
}
92142
}
93143
}

projects/igniteui-angular/src/lib/core/styles/typography/_bootstrap.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
@use '../components/input/file-input-theme' as *;
3737

3838
@mixin typography($type-scale) {
39-
@include badge-typography();
39+
@include badge-typography($theme: 'bootstrap');
4040
@include banner-typography();
4141
@include bottom-nav-typography();
4242
@include button-typography();

projects/igniteui-angular/src/lib/core/styles/typography/_fluent.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
@use '../components/input/file-input-theme' as *;
3636

3737
@mixin typography() {
38-
@include badge-typography();
38+
@include badge-typography($theme: 'fluent');
3939
@include banner-typography($categories: (
4040
message: 'caption'
4141
));

projects/igniteui-angular/src/lib/core/styles/typography/_indigo.scss

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
@use '../components/input/file-input-theme' as *;
3636

3737
@mixin typography($type-scale) {
38-
@include badge-typography($categories: (
39-
text: 'button',
40-
));
38+
@include badge-typography($theme: 'indigo');
4139
@include banner-typography();
4240
@include bottom-nav-typography();
4341
@include button-typography();

projects/igniteui-angular/src/lib/core/styles/typography/_material.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
@use '../components/input/file-input-theme' as *;
3636

3737
@mixin typography() {
38-
@include badge-typography();
38+
@include badge-typography($theme: 'material');
3939
@include banner-typography();
4040
@include bottom-nav-typography();
4141
@include button-typography();

0 commit comments

Comments
 (0)