Skip to content

Commit 2f866b3

Browse files
authored
feat(module:message): add support for custom styles and classes (#9427)
1 parent 58b9460 commit 2f866b3

8 files changed

Lines changed: 145 additions & 14 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 7
3+
title:
4+
zh-CN: 自定义样式
5+
en-US: Customized style
6+
---
7+
8+
## zh-CN
9+
10+
使用 `nzStyle` 和 `nzClass` 来定义样式。
11+
12+
## en-US
13+
14+
The `nzStyle` and `nzClass` are available to customize Message.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Component, inject } from '@angular/core';
2+
3+
import { NzButtonModule } from 'ng-zorro-antd/button';
4+
import { NzMessageService } from 'ng-zorro-antd/message';
5+
6+
@Component({
7+
selector: 'nz-demo-message-custom-style',
8+
imports: [NzButtonModule],
9+
template: `<button nz-button nzType="primary" (click)="createNotification()">Open the notification box</button>`
10+
})
11+
export class NzDemoMessageCustomStyleComponent {
12+
readonly #messageService = inject(NzMessageService);
13+
14+
createNotification(): void {
15+
this.#messageService.success('This is the content of the notification', {
16+
nzStyle: {
17+
'margin-top': '20vh'
18+
},
19+
nzClass: 'custom-class'
20+
});
21+
}
22+
}

‎components/message/doc/index.en-US.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ This components provides some service methods, with usage and arguments as follo
3131

3232
The parameters that are set by the `options` support are as follows:
3333

34-
| Argument | Description | Type |
35-
| -------------- | ---------------------------------------------------------------------- | ----------- |
36-
| nzDuration | Duration (milliseconds), does not disappear when set to 0 | `number` |
37-
| nzPauseOnHover | Do not remove automatically when mouse is over while setting to `true` | `boolean` |
38-
| nzAnimate | Whether to turn on animation | `boolean` |
39-
| nzData | Data to pass to custom template | `NzSafeAny` |
34+
| Argument | Description | Type |
35+
| -------------- | ---------------------------------------------------------------------- | ---------------------------- |
36+
| nzDuration | Duration (milliseconds), does not disappear when set to 0 | `number` |
37+
| nzPauseOnHover | Do not remove automatically when mouse is over while setting to `true` | `boolean` |
38+
| nzAnimate | Whether to turn on animation | `boolean` |
39+
| nzData | Data to pass to custom template | `NzSafeAny` |
40+
| nzStyle | Customized inline style | `NgStyleInterface \| string` |
41+
| nzClass | Customized CSS class | `NgClassInterface \| string` |
4042

4143
Methods for destruction are also provided:
4244

‎components/message/doc/index.zh-CN.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ description: 全局展示操作反馈信息。
3232

3333
`options` 支持设置的参数如下:
3434

35-
| 参数 | 说明 | 类型 |
36-
| -------------- | --------------------------------- | ----------- |
37-
| nzDuration | 持续时间(毫秒),当设置为0时不消失 | `number` |
38-
| nzPauseOnHover | 鼠标移上时禁止自动移除 | `boolean` |
39-
| nzAnimate | 开关动画效果 | `boolean` |
40-
| nzData | 传递给自定义模板的数据 | `NzSafeAny` |
35+
| 参数 | 说明 | 类型 |
36+
| -------------- | --------------------------------- | ---------------------------- |
37+
| nzDuration | 持续时间(毫秒),当设置为0时不消失 | `number` |
38+
| nzPauseOnHover | 鼠标移上时禁止自动移除 | `boolean` |
39+
| nzAnimate | 开关动画效果 | `boolean` |
40+
| nzData | 传递给自定义模板的数据 | `NzSafeAny` |
41+
| nzStyle | 自定义内联样式 | `NgStyleInterface \| string` |
42+
| nzClassName | 自定义 CSS class | `NgClassInterface \| string` |
4143

4244
还提供了全局销毁方法:
4345

‎components/message/message-container.component.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const NZ_MESSAGE_DEFAULT_CONFIG: Required<MessageConfig> = {
3131
template: `
3232
<div class="ant-message" [class.ant-message-rtl]="dir === 'rtl'" [style.top]="top">
3333
@for (instance of instances; track instance) {
34-
<nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)"></nz-message>
34+
<nz-message [instance]="instance" (destroyed)="remove($event.id, $event.userAction)" />
3535
}
3636
</div>
3737
`,

‎components/message/message.component.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { NzMessageData } from './typings';
2929
template: `
3030
<div
3131
class="ant-message-notice"
32+
[class]="instance.options?.nzClass"
33+
[style]="instance.options?.nzStyle"
3234
[@moveUpMotion]="instance.state"
3335
(@moveUpMotion.done)="animationStateChanged.next($event)"
3436
(mouseenter)="onEnter()"

‎components/message/message.spec.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,93 @@ describe('message', () => {
198198
expect(messageContainerElement.style.top).toBe('24px');
199199
}));
200200

201+
describe('custom styling', () => {
202+
it('should apply custom class', () => {
203+
messageService.success('SUCCESS', { nzClass: 'custom-message-class' });
204+
fixture.detectChanges();
205+
overlayContainerElement = overlayContainer.getContainerElement();
206+
207+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
208+
expect(messageElement).not.toBeNull();
209+
expect(messageElement.classList.contains('custom-message-class')).toBe(true);
210+
expect(messageElement.classList.contains('ant-message-notice')).toBe(true);
211+
});
212+
213+
it('should apply space-separated custom class', () => {
214+
messageService.info('INFO', { nzClass: 'class1 class2 class3' });
215+
fixture.detectChanges();
216+
overlayContainerElement = overlayContainer.getContainerElement();
217+
218+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
219+
220+
expect(messageElement.classList.contains('ant-message-notice')).toBe(true);
221+
expect(messageElement.classList.contains('class1')).toBe(true);
222+
expect(messageElement.classList.contains('class2')).toBe(true);
223+
expect(messageElement.classList.contains('class3')).toBe(true);
224+
});
225+
226+
it('should apply custom styles', () => {
227+
messageService.error('ERROR', {
228+
nzStyle: {
229+
'background-color': 'rgb(255, 0, 0)',
230+
color: 'white',
231+
border: '2px solid black'
232+
}
233+
});
234+
fixture.detectChanges();
235+
overlayContainerElement = overlayContainer.getContainerElement();
236+
237+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
238+
expect(messageElement.style.backgroundColor).toBe('rgb(255, 0, 0)');
239+
expect(messageElement.style.color).toBe('white');
240+
expect(messageElement.style.border).toBe('2px solid black');
241+
});
242+
243+
it('should work without custom class or style', () => {
244+
messageService.warning('WARNING');
245+
fixture.detectChanges();
246+
overlayContainerElement = overlayContainer.getContainerElement();
247+
248+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
249+
expect(messageElement).not.toBeNull();
250+
expect(messageElement.classList.contains('ant-message-notice')).toBe(true);
251+
});
252+
253+
it('should combine custom class and style', () => {
254+
messageService.loading('LOADING', {
255+
nzClass: 'custom-loading-class',
256+
nzStyle: { 'font-weight': 'bold', opacity: '0.8' }
257+
});
258+
fixture.detectChanges();
259+
overlayContainerElement = overlayContainer.getContainerElement();
260+
261+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
262+
expect(messageElement.classList.contains('custom-loading-class')).toBe(true);
263+
expect(messageElement.classList.contains('ant-message-notice')).toBe(true);
264+
expect(messageElement.style.fontWeight).toBe('bold');
265+
expect(messageElement.style.opacity).toBe('0.8');
266+
});
267+
268+
it('should handle empty class gracefully', () => {
269+
messageService.success('SUCCESS', { nzClass: '' });
270+
fixture.detectChanges();
271+
overlayContainerElement = overlayContainer.getContainerElement();
272+
273+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
274+
expect(messageElement).not.toBeNull();
275+
expect(messageElement.classList.contains('ant-message-notice')).toBe(true);
276+
});
277+
278+
it('should handle empty style object gracefully', () => {
279+
messageService.info('INFO', { nzStyle: {} });
280+
fixture.detectChanges();
281+
overlayContainerElement = overlayContainer.getContainerElement();
282+
283+
const messageElement = overlayContainerElement.querySelector('.ant-message-notice') as HTMLElement;
284+
expect(messageElement).not.toBeNull();
285+
});
286+
});
287+
201288
describe('RTL', () => {
202289
it('should apply classname', () => {
203290
configService.set('message', { nzDirection: 'rtl' });

‎components/message/typings.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { TemplateRef } from '@angular/core';
77
import { Subject } from 'rxjs';
88

9-
import { NzSafeAny } from 'ng-zorro-antd/core/types';
9+
import { NzSafeAny, type NgClassInterface, type NgStyleInterface } from 'ng-zorro-antd/core/types';
1010

1111
import { NzMNComponent } from './base';
1212

@@ -19,6 +19,8 @@ export interface NzMessageDataOptions {
1919
nzAnimate?: boolean;
2020
nzPauseOnHover?: boolean;
2121
nzData?: NzSafeAny;
22+
nzStyle?: NgStyleInterface | string;
23+
nzClass?: NgClassInterface | string;
2224
}
2325

2426
export interface NzMessageData {

0 commit comments

Comments
 (0)