Skip to content

Commit 0ca9772

Browse files
committed
refactor(chat): header should be displayed only if it has content
1 parent b80c175 commit 0ca9772

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/components/chat/chat-state.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ export class ChatState {
322322
const [fileBaseType] = fileType.split('/');
323323
return this._acceptedTypesCache.wildcardTypes.has(fileBaseType);
324324
}
325+
326+
/**
327+
* Checks if a slot is empty.
328+
* @param name Slot name to check
329+
* @returns True if the slot has content, false otherwise
330+
*/
331+
public hasSlotContent(name: string): boolean {
332+
return (
333+
this._host.renderRoot.querySelector<HTMLSlotElement>(`slot[name=${name}]`)
334+
?.childNodes.length !== 0
335+
);
336+
}
325337
//#endregion
326338
}
327339

src/components/chat/chat.spec.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,6 @@ describe('Chat', () => {
187187

188188
expect(chat).shadowDom.to.equal(
189189
` <div part="chat-container">
190-
<div part="header" part="header">
191-
<div part="info">
192-
<slot name="prefix" part="prefix">
193-
</slot>
194-
<slot name="title" part="title">
195-
</slot>
196-
</div>
197-
<slot part="actions" name="actions">
198-
</slot>
199-
</div>
200190
<div part="empty-state">
201191
<slot name="empty-state">
202192
</slot>
@@ -401,13 +391,11 @@ describe('Chat', () => {
401391

402392
expect(headerArea).dom.to.equal(
403393
`<div part="header" part="header">
404-
<div part="info">
405-
<slot name="prefix" part="prefix">
406-
</slot>
407-
<slot name="title" part="title">
408-
Chat
409-
</slot>
410-
</div>
394+
<slot name="prefix" part="prefix">
395+
</slot>
396+
<slot name="title" part="title">
397+
Chat
398+
</slot>
411399
<slot part="actions" name="actions">
412400
</slot>
413401
</div>`

src/components/chat/chat.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ContextProvider } from '@lit/context';
22
import { html, LitElement, nothing, type TemplateResult } from 'lit';
3-
import { property, query } from 'lit/decorators.js';
3+
import { property, query, state } from 'lit/decorators.js';
44
import { addThemingController } from '../../theming/theming-controller.js';
55
import IgcButtonComponent from '../button/button.js';
66
import { chatContext } from '../common/context.js';
@@ -157,6 +157,9 @@ export default class IgcChatComponent extends EventEmitterMixin<
157157
initialValue: this._chatState,
158158
});
159159

160+
@state()
161+
private _hasContent = false;
162+
160163
@query(IgcChatInputComponent.tagName)
161164
private _chatInput!: IgcChatInputComponent;
162165

@@ -246,12 +249,10 @@ export default class IgcChatComponent extends EventEmitterMixin<
246249

247250
private renderHeader() {
248251
return html` <div part="header">
249-
<div part="info">
250-
<slot name="prefix" part="prefix"></slot>
251-
<slot name="title" part="title"
252-
>${this._chatState.options?.headerText}</slot
253-
>
254-
</div>
252+
<slot name="prefix" part="prefix"></slot>
253+
<slot name="title" part="title"
254+
>${this._chatState.options?.headerText}</slot
255+
>
255256
<slot name="actions" part="actions"></slot>
256257
</div>`;
257258
}
@@ -284,10 +285,23 @@ export default class IgcChatComponent extends EventEmitterMixin<
284285
this._context.setValue(this._chatState, true);
285286
}
286287

288+
protected override createRenderRoot(): HTMLElement | DocumentFragment {
289+
const root = super.createRenderRoot();
290+
root.addEventListener('slotchange', () => {
291+
this._hasContent =
292+
this._chatState?.hasSlotContent('prefix') ||
293+
this._chatState?.hasSlotContent('title') ||
294+
this._chatState?.hasSlotContent('actions');
295+
});
296+
return root;
297+
}
298+
287299
protected override render() {
288300
return html`
289301
<div part="chat-container">
290-
${this.renderHeader()}
302+
${this._hasContent || this._chatState.options?.headerText
303+
? this.renderHeader()
304+
: nothing}
291305
${this.messages.length === 0
292306
? html`<div part="empty-state">
293307
<slot name="empty-state"> </slot>

0 commit comments

Comments
 (0)