Angular Badge 구성 요소 개요
Angular Badge는 시각적 알림이 필요할 때 애플리케이션에서 아바타, 탐색 메뉴 또는 기타 구성 요소와 함께 사용되는 구성 요소입니다. 배지는 일반적으로 정보, 성공, 경고 또는 오류를 전달하기 위해 미리 정의된 스타일의 아이콘으로 디자인됩니다.
Angular Badge Example
Getting Started with Ignite UI for Angular Badge
Ignite UI for Angular Badge 구성 요소를 시작하려면 먼저 Ignite UI for Angular를 설치해야 합니다. 기존 Angular 응용 프로그램에서 다음 명령을 입력합니다.
ng add igniteui-angular
Ignite UI for Angular에 대한 전체 소개는 시작 항목을 참조하십시오.
다음 단계는 다음 단계를 가져오는 것입니다.IgxBadgeModule 당신의 app.module.ts 파일.
// app.module.ts
...
import { IgxBadgeModule } from 'igniteui-angular/badge';
// import { IgxBadgeModule } from '@infragistics/igniteui-angular'; for licensed package
@NgModule({
...
imports: [..., IgxBadgeModule],
...
})
export class AppModule {}
또는16.0.0 독립 실행형 의존성으로 가져올IgxBadgeComponent 수도 있습니다.
// home.component.ts
...
import { IgxBadgeComponent } from 'igniteui-angular/badge';
// import { IgxBadgeComponent } from '@infragistics/igniteui-angular'; for licensed package
@Component({
selector: 'app-home',
template: '<igx-badge icon="check" type="success" shape="square"></igx-badge>',
styleUrls: ['home.component.scss'],
standalone: true,
imports: [IgxBadgeComponent]
})
export class HomeComponent {}
Now that you have the Ignite UI for Angular Badge module or component imported, you can start with a basic configuration of the igx-badge component.
Using the Angular Badge Component
Let's see how the demo sample is done. It's a simple success badge on an avatar. To build that, we need to import the IgxAvatarModule, along with the IgxBadgeModule:
// app.module.ts
...
import { IgxBadgeModule } from 'igniteui-angular/badge';
import { IgxAvatarModule } from 'igniteui-angular/avatar';
// import { IgxBadgeModule, IgxAvatarModule } from '@infragistics/igniteui-angular'; for licensed package
@NgModule({
...
imports: [..., IgxBadgeModule, IgxAvatarModule],
...
})
export class AppModule {}
Alternatively, as of 16.0.0 you can import the IgxBadgeComponent and IgxAvatarComponent as standalone dependencies.
다음으로 해당 구성요소를 템플릿에 추가하겠습니다.
<div class="wrapper">
<igx-avatar icon="person" shape="circle" size="small"></igx-avatar>
<igx-badge icon="check" type="success"></igx-badge>
</div>
래퍼를 사용하여 아바타의 일부를 덮으면서 배지를 절대적으로 배치합니다.
.wrapper {
position: relative;
margin-top: 15px;
}
igx-badge {
position: absolute;
bottom: 0;
left: 28px;
}
Badge Shape
We can change the badge shape through the shape attribute setting its value to square. By default, the shape of the badge is rounded.
<igx-badge icon="check" type="success" shape="square"></igx-badge>
모든 작업이 올바르게 완료되면 위에 표시된 데모 샘플이 브라우저에 표시됩니다.
Badge Size
The size of the badge can be controlled using the --size variable. It will make sure that the badge sizes proportionally in both directions. Keep in mind, however, that badges containing text values use the caption typography style for its font-size and line-height. For that reason, when setting the --size of a badge containing text to values below 16px, you will also need to modify its typography.
본보기:
igx-badge {
--size: 12px;
font-size: calc(var(--size) / 2);
line-height: normal;
}
Badge Icon
In addition to material icons, the igx-badge component also supports usage of Material Icons Extended and any other custom icon set. To add an icon from the material icons extended set inside your badge component, first you have to register it:
export class BadgeIconComponent implements OnInit {
constructor (protected _iconService: IgxIconService) {}
public ngOnInit() {
this._iconService.addSvgIconFromText(heartMonitor.name, heartMonitor.value, 'imx-icons');
}
}
그런 다음 다음과 같이 아이콘 이름과 계열을 지정하십시오.
<igx-badge icon="heart-monitor" iconSet="imx-icons"></igx-badge>
Badge in List
Let's extend the previous sample and create a list with contacts, similar to those in chat clients. In addition to the contact name, we want to display an avatar and the current state of the contact (online, offline or away). To achieve this, we're using the igx-badge and igx-avatar components. For a container, igx-list is used.
계속하려면 필요한 모든 모듈을 포함하고 app.module.ts 파일로 가져오세요.
// app.module.ts
...
import { IgxListModule } from 'igniteui-angular/list';
import { IgxAvatarModule } from 'igniteui-angular/avatar';
import { IgxBadgeModule } from 'igniteui-angular/badge';
// import { IgxListModule, IgxAvatarModule, IgxBadgeModule } from '@infragistics/igniteui-angular'; for licensed package
@NgModule({
...
imports: [..., IgxListModule, IgxAvatarModule, IgxBadgeModule],
})
export class AppModule {}
Note
The igx-badge has icon and type inputs to configure the badge look. You can set the icon by providing its name from the official material icons set. The badge type can be set to either default, info, success, warning, or error. Depending on the type, a specific background color is applied.
In our sample, icon and type are bound to model properties named icon and type.
다음으로 템플릿에 연락처를 추가합니다.
<!-- contacts.component.html -->
<igx-list>
<igx-list-item isHeader="true">
Team Members (4)
</igx-list-item>
<igx-list-item *ngFor="let member of members">
<div class="wrapper">
<div>
<igx-avatar icon="person" shape="circle" size="small"></igx-avatar>
<igx-badge [icon]="member.icon" [type]="member.type" class="badge-style"></igx-badge>
</div>
<div class="contact-container">
<span class="contact-name">{{ member.name }}</span>
</div>
</div>
</igx-list-item>
</igx-list>
다음과 같이 TypeScript 파일에 멤버를 생성하겠습니다.
// contacts.component.ts
...
public members: Member[] = [
new Member('Terrance Orta', 'online'),
new Member('Donna Price', 'online'),
new Member('Lisa Landers', 'away'),
new Member('Dorothy H. Spencer', 'offline'),
];
...
class Member {
public name: string;
public status: string;
public type: string;
public icon: string;
constructor(name: string, status: string) {
this.name = name;
this.status = status;
switch (status) {
case 'online':
this.type = 'success';
this.icon = 'check';
break;
case 'away':
this.type = 'warning';
this.icon = 'schedule';
break;
case 'offline':
this.type = 'error';
this.icon = 'remove';
break;
}
}
}
상위 컨테이너에 배지를 배치합니다.
/* contacts.component.css */
.wrapper {
display: flex;
flex-direction: row;
}
.contact-name {
font-weight: 600;
}
.contact-container {
margin-left: 20px;
}
.badge-style {
position: absolute;
bottom: 2.5px;
left: 40px;
}
샘플이 올바르게 구성되면 구성원 목록이 표시되어야 하며 모든 구성원에는 현재 상태를 보여주는 아바타와 배지가 있습니다.
스타일링
Badge Theme Property Map
속성을 변경하면$background-color 다음 종속 속성들이 자동으로 업데이트됩니다:
| 기본 속성 | 종속 속성 | 설명 |
|---|---|---|
| $background색 | $icon색 | 배지에 아이콘이 사용된 색상입니다. |
| $text색 | 배지에 사용된 텍스트 색상입니다. |
To get started with styling the badges, we need to import the index file, where all the theme functions and component mixins live:
@use "igniteui-angular/theming" as *;
// IMPORTANT: Prior to Ignite UI for Angular version 13 use:
// @import '~igniteui-angular/lib/core/styles/themes/index';
Following the simplest approach, we create a new theme that extends the badge-theme and accepts some parameters that style the badge's items. When you set the $background-color, the $icon-color and $text-color are automatically assigned based on which offers better contrast—black or white. Note that the $border-radius property only takes effect when the badge's shape is set to square.
$custom-badge-theme: badge-theme(
$background-color: #57a5cd,
$border-radius: 4px
);
To include the new theme we use the css-vars mixin:
@include css-vars($custom-badge-theme);
Demo
Styling with Tailwind
저희 맞춤형 Tailwind 유틸리티 클래스를 사용해 스타일badge 링할 수 있습니다. 먼저 Tailwind를 꼭 설정 하세요.
전역 스타일시트의 순풍 가져오기와 함께 다음과 같이 원하는 테마 유틸리티를 적용할 수 있습니다.
@import "tailwindcss";
...
@use 'igniteui-theming/tailwind/utilities/material.css';
유틸리티 파일에는 테마 변형 두 가지light가dark 모두 포함되어 있습니다.
- 라이트 테마에는 클래스를 사용
light-*하세요. - 어두운 테마에는 클래스를 사용
dark-*하세요. - 접두사 뒤에 컴포넌트 이름을 덧붙이세요,
light-badgedark-badge예: .
이 클래스들이 적용되면 동적 테마 계산이 가능합니다. 그 다음에는 생성된 CSS 변수를 무arbitrary properties 시할 수 있습니다. 콜론 다음에는 유효한 CSS 색상 형식(HEX, CSS 변수, RGB 등)을 입력하세요.
전체 부동산 목록은 배지 테마에서 확인할 수 있습니다. 문법은 다음과 같습니다:
<igx-badge
class="!light-badge ![--background:#FF4E00] ![--border-radius:4px]">
</igx-badge>
Note
느낌표(!)는 유틸리티 클래스가 우선순위가 되도록 보장하기 위해 필요합니다. Tailwind는 레이어에 스타일을 적용하는데, 이 스타일을 중요하게 표시하지 않으면 컴포넌트의 기본 테마에 의해 덮어쓰여집니다.
마지막에 배지는 다음과 같이 보여야 합니다:
API References
Theming Dependencies
Additional Resources
우리 커뮤니티는 활동적이며 항상 새로운 아이디어를 환영합니다.