|
| 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> {{ 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> {{ user.online ? "online" : "offline" }}</span> |
| 135 | + </div> |
| 136 | + </popper-content> |
| 137 | +</ng-template> |
| 138 | +``` |
0 commit comments