Skip to content

Commit 12a8bee

Browse files
authored
Add chat topic for blazor with snippets. (#318)
* Add chat topic for blazor with snippets. * Sync chat jp with Opus. * Add to jp toc. * Fix lint. * Remove typing indicator, since it's now a slot.
1 parent b95c65b commit 12a8bee

4 files changed

Lines changed: 477 additions & 10 deletions

File tree

docs/xplat/src/content/en/components/interactivity/chat.mdx

Lines changed: 235 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ npm install igniteui-react
4343

4444
</PlatformBlock>
4545

46+
<PlatformBlock for="Blazor">
47+
48+
```cmd
49+
Install-Package IgniteUI.Blazor
50+
```
51+
52+
Or via .NET CLI:
53+
54+
```cmd
55+
dotnet add package IgniteUI.Blazor
56+
```
57+
58+
</PlatformBlock>
59+
4660
Once installed, you can import the component in your project and register it so it becomes available as a custom element:
4761

4862
<PlatformBlock for="WebComponents">
@@ -68,6 +82,22 @@ import 'igniteui-webcomponents/themes/light/bootstrap.css';
6882

6983
</PlatformBlock>
7084

85+
<PlatformBlock for="Blazor">
86+
87+
```cs
88+
// in Program.cs file
89+
90+
builder.Services.AddIgniteUIBlazor(typeof(IgbChatModule));
91+
```
92+
93+
You will also need to link an additional CSS file to apply the styling to the <ApiLink pkg="core" type="Chat" /> component.
94+
95+
```razor
96+
<link href="_content/IgniteUI.Blazor/themes/light/bootstrap.css" rel="stylesheet" />
97+
```
98+
99+
</PlatformBlock>
100+
71101
The CSS file includes one of our default themes. You can replace it with a different theme or create a custom one if you want the <ApiLink type="Chat" /> to match your application’s branding.
72102

73103
## Usage
@@ -110,6 +140,24 @@ return (
110140

111141
</PlatformBlock>
112142

143+
<PlatformBlock for="Blazor">
144+
145+
146+
```cs
147+
public IgbChatOptions Options = new IgbChatOptions()
148+
{
149+
CurrentUserId = "user",
150+
HeaderText = "Support Chat"
151+
};
152+
```
153+
154+
```razor
155+
<IgbChat @ref="Chat" Options="Options">
156+
</IgbChat>
157+
```
158+
159+
</PlatformBlock>
160+
113161

114162
Here, the `currentUserId` property tells the component which messages are “outgoing” (sent by the current user) versus “incoming” (sent by others). The `headerText` provides a title for the chat window.
115163

@@ -165,6 +213,36 @@ const ChatExample = () => {
165213

166214
</PlatformBlock>
167215

216+
<PlatformBlock for="Blazor">
217+
218+
```razor
219+
<IgbChat @ref="Chat" Options="Options" Messages="Messages" MessageCreated="OnMessageCreated" Height="100%"></IgbChat>
220+
```
221+
222+
```cs
223+
public IgbChatMessage[] Messages = new IgbChatMessage[]
224+
{
225+
new IgbChatMessage()
226+
{
227+
Id = "1",
228+
Text = "Hi, I have a question about my recent order, #7890.",
229+
Sender = "user",
230+
Timestamp = (DateTime.Now - TimeSpan.FromMilliseconds(3500000)).ToString()
231+
}
232+
}
233+
```
234+
235+
You can then sync messages coming from the client, by hooking to the `messageCreated` event and adding the created messages to the collection:
236+
237+
```cs
238+
public void OnMessageCreated(IgbChatMessageEventArgs e)
239+
{
240+
Messages = Messages.Append(e.Detail).ToArray();
241+
}
242+
```
243+
244+
</PlatformBlock>
245+
168246
This approach makes it easy to plug the Chat into your own data source, such as a server endpoint, a chatbot engine, or a collaborative app backend.
169247

170248
### Properties
@@ -179,6 +257,8 @@ The <ApiLink type="Chat" /> component exposes several key properties that let yo
179257

180258
These properties make it straightforward to synchronize the Chat’s UI with your application’s state and backend.
181259

260+
<PlatformBlock for="WebComponents, React">
261+
182262
### Attachments
183263
Modern conversations are rarely limited to text alone. The Chat component includes built-in support for file attachments, allowing users to share images, documents, and other files.
184264
By default, the input area includes an attachment button. You can control which file types are allowed by setting the `acceptedFiles` property:
@@ -233,6 +313,8 @@ const options: IgrChatOptions = {
233313

234314
</PlatformBlock>
235315

316+
</PlatformBlock>
317+
236318
### Suggestions
237319
Quick reply suggestions provide users with pre-defined responses they can tap to reply instantly. This feature is particularly useful in chatbots, customer service flows, or when guiding users through a structured process.
238320
You can provide suggestions by binding an array of strings to the suggestions property. The `suggestions-position` attribute lets you control where they are displayed: either below the input area or below the messages list.
@@ -274,6 +356,19 @@ return (
274356

275357
</PlatformBlock>
276358

359+
<PlatformBlock for="Blazor">
360+
361+
```cs
362+
public IgbChatOptions Options = new IgbChatOptions
363+
{
364+
CurrentUserId = "me",
365+
Suggestions = new string[] { "Yes", "No", "Maybe later" },
366+
SuggestionsPosition = ChatSuggestionsPosition.BelowInput
367+
};
368+
```
369+
370+
</PlatformBlock>
371+
277372
This approach helps streamline user interactions by reducing the need to type repetitive answers and improves the overall experience in guided conversations.
278373

279374
### Typing Indicator
@@ -303,6 +398,17 @@ const options: IgrChatOptions = {
303398

304399
</PlatformBlock>
305400

401+
<PlatformBlock for="Blazor">
402+
403+
```cs
404+
public IgbChatOptions Options = new IgbChatOptions
405+
{
406+
IsTyping = true
407+
};
408+
```
409+
410+
</PlatformBlock>
411+
306412
This feature is typically toggled programmatically, for example when receiving a typing event from your backend service.
307413

308414
### Custom Renderers
@@ -312,10 +418,23 @@ The <ApiLink type="Chat" /> component addresses this need with a renderer system
312418
#### ChatTemplateRenderer
313419
Every renderer follows the same function signature:
314420

421+
<PlatformBlock for="WebComponents, React">
422+
315423
```ts
316424
export type ChatTemplateRenderer<T> = (ctx: T) => unknown;
317425
```
318426

427+
</PlatformBlock>
428+
429+
<PlatformBlock for="Blazor">
430+
431+
```js
432+
igRegisterScript("MyTemplate", (ctx) => {
433+
}, false);
434+
```
435+
436+
</PlatformBlock>
437+
319438
The ctx parameter provides different contextual data depending on what is being rendered.
320439

321440
#### Renderer Contexts
@@ -334,7 +453,6 @@ The following parts of the Chat can be customized:
334453
- Attachment-level: attachment, attachmentHeader, attachmentContent
335454
- Input-level: input, inputActions, inputActionsStart, inputActionsEnd, inputAttachments, fileUploadButton, sendButton
336455
- Suggestions: suggestionPrefix
337-
- Miscellaneous: typingIndicator
338456

339457
This level of granularity means you can tweak just one part (for example, how attachments look) without rewriting the entire chat layout.
340458

@@ -376,6 +494,28 @@ const options = {
376494

377495
</PlatformBlock>
378496

497+
<PlatformBlock for="Blazor">
498+
499+
```cs
500+
public IgbChatOptions Options = new IgbChatOptions
501+
{
502+
Renderers = new IgbChatRenderers()
503+
{
504+
MessageContentScript = "MessageContentScript"
505+
}
506+
};
507+
```
508+
509+
```js
510+
igRegisterScript("MessageContentScript", (ctx) => {
511+
var html = window.igTemplating.html;
512+
return html`<div className="bubble custom">${ctx.message.text}</div>`;
513+
}, false);
514+
515+
```
516+
517+
</PlatformBlock>
518+
379519
#### Example: Custom Input Area
380520
By default, the chat input is a text area. You can override it to provide a more tailored experience, such as adding a voice input button:
381521

@@ -416,12 +556,35 @@ const options = {
416556

417557
</PlatformBlock>
418558

559+
<PlatformBlock for="Blazor">
560+
561+
```cs
562+
public IgbChatOptions Options = new IgbChatOptions
563+
{
564+
Renderers = new IgbChatRenderers()
565+
{
566+
InputScript = "InputTemplate"
567+
}
568+
};
569+
```
570+
571+
572+
```js
573+
igRegisterScript("InputTemplate", (ctx) => {
574+
var html = window.igTemplating.html;
575+
return html`<textarea placeholder=${ctx.instance?.options?.inputPlaceholder || 'Type here...'}>${ctx.value}</textarea>
576+
<button @click=${() => alert('Voice input!')}>🎤</button>`;
577+
}, false);
578+
```
579+
580+
</PlatformBlock>
581+
419582
#### Example: Extending Input Actions
420583
The <ApiLink type="Chat" /> component provides two renderers which are useful when you want to keep the default actions (upload and send) but extend them with additional controls:
421584
- `inputActionsStart` – allows you to inject custom content after the built-in upload button.
422585
- `inputActionsEnd` – allows you to inject custom content after the built-in send button.
423586

424-
For example, you might want to add a voice recording button after the upload button, or a menu of extra options after the send button.
587+
For example, you might want to add a voice recording button before the other buttons, or a menu of extra options after the send button.
425588
In the following example, the default upload button is preserved, but we add a microphone button next to it. On the other end, we remove the default send button and replace it with a custom Ask button and a “more” menu:
426589

427590
<PlatformBlock for="WebComponents">
@@ -477,13 +640,55 @@ const options = {
477640

478641
</PlatformBlock>
479642

643+
<PlatformBlock for="Blazor">
644+
645+
```cs
646+
public IgbChatOptions Options = new IgbChatOptions
647+
{
648+
Renderers = new IgbChatRenderers()
649+
{
650+
InputActionsStartScript = "InputActionsStartTemplate",
651+
InputActionsEndScript = "InputActionsEndTemplate",
652+
SendButtonScript = "SendButtonTemplate"
653+
}
654+
};
655+
```
656+
657+
658+
```js
659+
igRegisterScript("SendButtonTemplate", (ctx) => {
660+
var html = window.igTemplating.html;
661+
return html``;
662+
}, false);
663+
664+
665+
igRegisterScript("InputActionsStartTemplate", (ctx) => {
666+
var html = window.igTemplating.html;
667+
return html`<igc-icon-button variant="flat">🎤</igc-icon-button>`;
668+
}, false);
669+
670+
igRegisterScript("InputActionsEndTemplate", (ctx) => {
671+
var html = window.igTemplating.html;
672+
return html`<div>
673+
<igc-button @click=${() => handleCustomSendClick(ctx.instance)}>Ask</igc-button>
674+
<igc-icon-button variant="flat" name="more_horiz"></igc-icon-button>
675+
</div>`;
676+
}, false);
677+
```
678+
679+
</PlatformBlock>
680+
480681
In this setup:
682+
<PlatformBlock for="WebComponents, React">
481683
- The upload button remains in place.
684+
</PlatformBlock>
482685
- A microphone button is added after it (inputActionsStart).
483686
- The default send button is removed and replaced with a custom Ask button and a “more” icon (inputActionsEnd).
484687

485688
This approach gives you full flexibility over the chat input bar, letting you add, remove, or reorder actions without rebuilding the input area from scratch.
486689

690+
<PlatformBlock for="WebComponents, React">
691+
487692
### Markdown Rendering
488693

489694
<PlatformBlock for="WebComponents">
@@ -572,6 +777,8 @@ This will enable highlighted code blocks for JavaScript, Python, and Go, styled
572777
| `theme` | An object specifying **Shiki** themes to apply. Supports separate values for `light` and `dark` mode (e.g., `{ light: 'github-light', dark: 'github-dark' }`). |
573778
| `sanitizer` | A custom function to sanitize the final HTML. Defaults to `DOMPurify.sanitize`. |
574779

780+
</PlatformBlock>
781+
575782
### Events
576783
To integrate with your application logic, the Chat component emits a set of events:
577784

@@ -608,6 +815,17 @@ To integrate with your application logic, the Chat component emits a set of even
608815

609816
</PlatformBlock>
610817

818+
819+
<PlatformBlock for="Blazor">
820+
821+
- MessageCreated – when a new message is created.
822+
- MessageReact – when a message is reacted to.
823+
- TypingChange – when typing status changes.
824+
- InputFocus / onInputBlur – input focus events.
825+
- InputChange – when the input value changes.
826+
827+
</PlatformBlock>
828+
611829
You can listen for these events and sync them with your backend:
612830

613831
<PlatformBlock for="WebComponents">
@@ -637,7 +855,22 @@ chatRef.current.addEventListener('onMessageCreated', (e) => {
637855

638856
</PlatformBlock>
639857

858+
<PlatformBlock for="Blazor">
640859

860+
```razor
861+
<IgbChat MessageCreated="OnMessageCreated"></IgbChat>
862+
```
863+
864+
```cs
865+
public void OnMessageCreated(IgbChatMessageEventArgs e)
866+
{
867+
Console.WriteLine($"Message: {e.Detail.Text}");
868+
}
869+
```
870+
871+
<Sample src="/interactions/chat/features" height={900} alt="{Platform} Chat Features" />
872+
873+
</PlatformBlock>
641874
## Styling
642875

643876
The <ApiLink type="Chat" /> component exposes both **CSS parts** and **slots** for fine-grained customization of its appearance and structure.

docs/xplat/src/content/en/toc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,12 +2607,12 @@
26072607
},
26082608
{
26092609
"exclude": [
2610-
"Angular",
2611-
"Blazor"
2610+
"Angular"
26122611
],
26132612
"name": "Chat",
26142613
"href": "interactivity/chat.md",
2615-
"new": true
2614+
"new": true,
2615+
"preview": true
26162616
},
26172617
{
26182618
"exclude": [

0 commit comments

Comments
 (0)