Skip to content

Commit 91edeb9

Browse files
authored
Merge pull request #159 from GetStream/mention-enhancement
Mention enhancement
2 parents 0dc4ea5 + ed23461 commit 91edeb9

19 files changed

+364
-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/channel-preview.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ The channel list uses the `ChannelPreview` component to display channels, if you
2727
</ng-template>
2828
```
2929

30+
Your custom template can receive the same inputs (with the same name) as the default `ChannelPreview` component.
31+
3032
:::note
3133

3234
If you want to build your own `ChannelPreview` component, you might find the following building blocks useful:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ If you want to create your own message input, here is how to use it:
3838

3939
The default chat UI uses the message input in two different places: at the bottom of the channel to send new message, and in the message list to edit a message.
4040

41+
Your custom template can receive the same inputs (with the same name) as the default `MessageInput` component.
42+
4143
:::note
4244
If you want to create your own message input, you can use the following building blocks:
4345

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ The message list uses the `Message` component to display messages, if you want t
5353
</ng-template>
5454
```
5555

56+
Your custom template can receive the same inputs (with the same name) as the default `Message` component.
57+
5658
:::note
5759

5860
If you want to build your own `Message` component, you might find the following building blocks useful:
@@ -157,3 +159,11 @@ The input used for message edit. By default, the [default message input componen
157159
| Type |
158160
| ----------- |
159161
| TemplateRef |
162+
163+
### mentionTemplate
164+
165+
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).
166+
167+
| Type |
168+
| ----------- |
169+
| 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>

0 commit comments

Comments
 (0)