Skip to content

Commit 56fdade

Browse files
committed
feat: Mention enhanecement #149
1 parent 19a50f0 commit 56fdade

17 files changed

+358
-111
lines changed
49 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Code examples",
3+
"position": 6
4+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
id: mention-actions
3+
sidebar_position: 1
4+
title: Mention actions
5+
---
6+
7+
import Screenshot from "../assets/mention-screenshot.png";
8+
9+
In this example, we will demonstrate how to create custom mention actions if a user clicks on or hovers over a mention in a message.
10+
11+
## Custom mention template
12+
13+
You can provide a custom message template to the `MessageList` component:
14+
15+
```html
16+
<div id="root">
17+
<stream-channel-list></stream-channel-list>
18+
<stream-channel>
19+
<stream-channel-header></stream-channel-header>
20+
<stream-message-list
21+
[mentionTemplate]="mentionTemplate"
22+
></stream-message-list>
23+
<stream-notification-list></stream-notification-list>
24+
<stream-message-input></stream-message-input>
25+
</stream-channel>
26+
</div>
27+
28+
<ng-template #mentionTemplate let-user="user">
29+
<b>@{{ user.name || user.id }}</b>
30+
</ng-template>
31+
```
32+
33+
The `MessageList` component will provide the mentioned user in a variable called `user`, the object has a [`UserResponse`](https://github.com/GetStream/stream-chat-js/blob/master/src/types.ts) type and you can use it to display information about the user.
34+
35+
This template looks and works like the default mention template, however you can add interactions to this element.
36+
37+
## Display user information on click
38+
39+
In this step we will create a popover with additional user information that will be displayed if the user clicks on a mention. We are using the [`ngx-popperjs`](https://www.npmjs.com/package/ngx-popperjs) library for the popover, but you can use a different library as well.
40+
41+
### Install `ngx-popperjs`
42+
43+
Let's install the necessary dependencies:
44+
45+
```bash
46+
npm install @popperjs/core ngx-popperjs --save
47+
```
48+
49+
### Import the `NgxPopperjsModule` module
50+
51+
```typescript
52+
import {NgxPopperjsModule} from 'ngx-popperjs';
53+
54+
@NgModule({
55+
// ...
56+
imports: [
57+
// ...
58+
NgxPopperjsModule
59+
]
60+
})
61+
```
62+
63+
### Import SCSS
64+
65+
Add this to your `styles.scss`:
66+
67+
```scss
68+
@import "~ngx-popperjs/scss/theme-white";
69+
```
70+
71+
[CSS is also supported.](https://www.npmjs.com/package/ngx-popperjs)
72+
73+
### Create the popover
74+
75+
```html
76+
<ng-template #mentionTemplate let-user="user">
77+
<span
78+
style="cursor: pointer"
79+
[popper]="popperContent"
80+
[popperPositionFixed]="true"
81+
>
82+
<b>@{{ user.name || user.id }}</b>
83+
</span>
84+
<popper-content #popperContent style="display: inline">
85+
<div style="display: flex; align-items: center">
86+
<stream-avatar
87+
[imageUrl]="user.image"
88+
[name]="user.name || user.id"
89+
></stream-avatar>
90+
<b>{{ user.name || user.id }}</b>
91+
<span>&nbsp;{{ user.online ? "online" : "offline" }}</span>
92+
</div>
93+
</popper-content>
94+
</ng-template>
95+
```
96+
97+
This is the popover that is displayed if we click on a mention in a message:
98+
99+
<img src={Screenshot} width="500" />
100+
101+
[`ngx-popperjs`](https://www.npmjs.com/package/ngx-popperjs) has a lot of other configuration options, feel free to explore those.
102+
103+
## Display user information on hover
104+
105+
Let's modify our solution and display the popover on hover instead of click:
106+
107+
We need to add a variable to our component class:
108+
109+
```typescript
110+
import { NgxPopperjsTriggers } from "ngx-popperjs";
111+
112+
popoverTrigger = NgxPopperjsTriggers.hover;
113+
```
114+
115+
And modify our template:
116+
117+
```html
118+
<ng-template #mentionTemplate let-user="user">
119+
<span
120+
style="cursor: pointer"
121+
[popper]="popperContent"
122+
[popperPositionFixed]="true"
123+
[popperTrigger]="popoverTrigger"
124+
>
125+
<b>@{{ user.name || user.id }}</b>
126+
</span>
127+
<popper-content #popperContent style="display: inline">
128+
<div style="display: flex; align-items: center">
129+
<stream-avatar
130+
[imageUrl]="user.image"
131+
[name]="user.name || user.id"
132+
></stream-avatar>
133+
<b>{{ user.name || user.id }}</b>
134+
<span>&nbsp;{{ user.online ? "online" : "offline" }}</span>
135+
</div>
136+
</popper-content>
137+
</ng-template>
138+
```

docusaurus/docs/Angular/components/message-list.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ The input used for message edit. By default, the [default message input componen
6767
| ----------- |
6868
| TemplateRef |
6969

70+
### mentionTemplate
71+
72+
The template used to display a mention in a message. It receives the mentioned user in a variable called `user` with the type [`UserResponse`](https://github.com/GetStream/stream-chat-js/blob/master/src/types.ts). You can provide your own template if you want to [add actions to mentions](../code-examples/mention-actions.mdx).
73+
74+
| Type |
75+
| ----------- |
76+
| TemplateRef |
77+
7078
### areReactionsEnabled
7179

7280
If true, the message reactions are displayed. Users can also react to messages if they have the necessary [channel capability](https://getstream.io/chat/docs/javascript/channel_capabilities/?language=javascript).

docusaurus/docs/Angular/components/message.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,11 @@ The input used for message edit. By default, the [default message input componen
157157
| Type |
158158
| ----------- |
159159
| TemplateRef |
160+
161+
### mentionTemplate
162+
163+
The template used to display a mention in a message. It receives the mentioned user in a variable called `user` with the type [`UserResponse`](https://github.com/GetStream/stream-chat-js/blob/master/src/types.ts). You can provide your own template if you want to [add actions to mentions](../code-examples/mention-actions.mdx).
164+
165+
| Type |
166+
| ----------- |
167+
| TemplateRef |

docusaurus/docs/Angular/pipes/_category_.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

docusaurus/docs/Angular/pipes/mentions-highlight.mdx

Lines changed: 0 additions & 51 deletions
This file was deleted.

projects/stream-chat-angular/src/lib/message-list/message-list.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
lastSentMessageId && message?.id === lastSentMessageId
2828
),
2929
canReceiveReadEvents: canReceiveReadEvents,
30-
messageInputTemplate: messageInputTemplate
30+
messageInputTemplate: messageInputTemplate,
31+
mentionTemplate: mentionTemplate
3132
}
3233
"
3334
></ng-container>
@@ -43,6 +44,7 @@
4344
[enabledMessageActions]="enabledMessageActions"
4445
[canReceiveReadEvents]="canReceiveReadEvents"
4546
[messageInputTemplate]="messageInputTemplate"
47+
[mentionTemplate]="mentionTemplate"
4648
></stream-message>
4749
</ng-template>
4850
</li>

projects/stream-chat-angular/src/lib/message-list/message-list.component.spec.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { TranslateModule } from '@ngx-translate/core';
1010
import { Channel } from 'stream-chat';
1111
import { ChannelService } from '../channel.service';
1212
import { ChatClientService } from '../chat-client.service';
13-
import { HighlightMentionsPipe } from '../message/highlight-mentions.pipe';
1413
import { MessageComponent } from '../message/message.component';
1514
import {
1615
MockChannelService,
@@ -35,11 +34,7 @@ describe('MessageListComponent', () => {
3534
channelServiceMock = mockChannelService();
3635
TestBed.configureTestingModule({
3736
imports: [TranslateModule.forRoot()],
38-
declarations: [
39-
MessageComponent,
40-
MessageListComponent,
41-
HighlightMentionsPipe,
42-
],
37+
declarations: [MessageComponent, MessageListComponent],
4338
providers: [
4439
{ provide: ChannelService, useValue: channelServiceMock },
4540
{

projects/stream-chat-angular/src/lib/message-list/message-list.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { MessageActions } from '../message-actions-box/message-actions-box.compo
2525
export class MessageListComponent implements AfterViewChecked, OnChanges {
2626
@Input() messageTemplate: TemplateRef<any> | undefined;
2727
@Input() messageInputTemplate: TemplateRef<any> | undefined;
28+
@Input() mentionTemplate: TemplateRef<any> | undefined;
2829
@Input() areReactionsEnabled = true;
2930
/* eslint-disable-next-line @angular-eslint/no-input-rename */
3031
@Input('enabledMessageActions') enabledMessageActionsInput: MessageActions[] =

0 commit comments

Comments
 (0)