Show code chatty-pattern-admin-list-thread.element.js Wrap code lines Copy import '@avada/chatty-ui/tokens/admin.css';
import {IconChevronLeft, EmailIcon} from '@avada/chatty-ui/elements/icon-glyphs.js';
import {html, render} from 'lit';
import {ref} from 'lit/directives/ref.js';
import {IconClose, IconMessage} from '@avada/chatty-ui/elements/icon-glyphs.js';
import '@avada/chatty-ui/elements/surface.js';
import '@avada/chatty-ui/elements/avatar.js';
import '@avada/chatty-ui/elements/badge.js';
import '@avada/chatty-ui/elements/icon-button.js';
import '@avada/chatty-ui/elements/launcher.js';
import '@avada/chatty-ui/elements/button.js';
import {sameGroup} from '@avada/chatty-ui/behaviors/messages.js';
// Reference viewport: 1280 × 900 desktop, 390 × 900 mobile.
const sizes = {
classic: {width: 400, height: 700},
'ai-mode': {width: 820, height: 657},
admin: {width: 527, height: 842}
};
const question = {
id: 'question',
senderType: 'customer',
text: 'Can you help me?',
createdAt: '2026-01-15T10:00:00Z'
};
/** Viewport-relative sizing survives the initial hidden iframe layout. */
function widgetSize(surface) {
const mode = new URLSearchParams(location.search).get('mobile');
const isMobile = mode === null ? innerWidth < 900 : mode === 'true';
const width = `min(${sizes[surface].width}px, max(0px, 100vw - 40px))`;
const height = isMobile
? `min(${surface === 'admin' ? 848 : 888}px, round(down, calc(${width} * 2.2), 1px))`
: `${sizes[surface].height}px`;
return {...sizes[surface], height, mobile: isMobile};
}
/** Local immutable message state with real subscriptions for live previews. */
function fixtureStore(initial) {
let state = {messages: initial};
const listeners = new Set();
return {
getState: () => state,
subscribe(listener) {
listeners.add(listener);
return () => listeners.delete(listener);
},
setMessages(messages) {
state = {messages};
listeners.forEach(listener => listener());
}
};
}
/** Appends the viewer message without replacing the existing conversation. */
function appendViewer(store, surface, text) {
const message = {
id: `viewer-${store.getState().messages.length}`,
senderType: surface === 'admin' ? 'agent' : 'customer',
senderMeta: {name: surface === 'admin' ? 'Alex' : 'You'},
text,
createdAt: new Date().toISOString()
};
store.setMessages([...store.getState().messages, message]);
}
/**
* One shell owns surface sizing, header, thread and composer for every pattern.
* @param {string} surface
* @param {Object} store
* @param {Object} strings
* @param {Object} content
* @param {Function} renderMessage
* @param {Function} onSend
* @param {Object} registry
* @param {boolean} composerHidden
* @param {string} title
* @param {Object} headerStart
* @param {Object} headerActions
* @param {boolean} sheet
* @param {string | null} status
* @returns {Object}
*/
function patternShell({
surface,
store,
strings = {header: 'Store assistant', message: 'Message', send: 'Send'},
content,
renderMessage,
onSend,
registry,
title,
headerStart,
headerActions,
status = 'online',
sheet = false,
composerHidden = false
}) {
const size = widgetSize(surface);
const mobileSheet = sheet && size.mobile;
if (mobileSheet) {
size.width = 'none';
size.height = 'calc(100dvh - 12px)';
}
const toggle = (event, closed) => {
const frame = event.currentTarget.closest('[data-pattern-frame]');
const panel = frame.querySelector('chatty-surface');
const launcher = frame.querySelector('[data-pattern-launcher]');
panel.hidden = closed;
frame.dataset.closed = String(closed);
const background = frame.querySelector('[data-sheet-page]');
if (background) background.inert = !closed && surface === 'ai-mode';
launcher.hidden = !closed;
if (closed) {
launcher.style.insetBlockEnd = `${Math.max(surface === 'ai-mode' ? 40 : 16, frame.getBoundingClientRect().bottom - innerHeight + 20)}px`;
launcher.focus({preventScroll: true});
} else {
const composer = panel.querySelector('chatty-composer');
const input = !composer.hidden
? composer.renderRoot.querySelector('textarea')
: (panel
.querySelector('chatty-contact-form:not([hidden])')
?.renderRoot.querySelector('chatty-text-field') ??
panel.querySelector('form chatty-text-field') ??
panel.querySelector('[data-welcome-invitation]:not([hidden]) chatty-button') ??
panel
.querySelector('chatty-pre-chat:not([hidden])')
?.renderRoot.querySelector('chatty-button'));
input?.focus({preventScroll: true});
}
};
const sender = message => {
const content = renderMessage?.(message) ?? {};
const messages = store.getState().messages;
const previous = messages[messages.indexOf(message) - 1];
if (message.senderType !== 'bot' || sameGroup(message, previous)) return content;
return {
...content,
sender: html`<span style="display:inline-flex;align-items:center;gap:6px"
><chatty-avatar size="sm" label="Assistant">A</chatty-avatar
>${content.sender ?? message.senderName ?? message.senderMeta?.name ?? 'Assistant'}</span
>`
};
};
return html`<style>
[data-pattern-frame] {
display: flex;
position: relative;
justify-content: center;
inline-size: 100%;
min-inline-size: 0;
}
[data-pattern-frame='classic'] {
justify-content: flex-end;
}
[data-pattern-frame] chatty-surface::part(header) {
padding: 0;
}
[data-pattern-frame='classic'] chatty-surface::part(header),
[data-pattern-frame='ai-mode'] chatty-surface::part(header) {
border-bottom-width: 0;
}
[data-pattern-frame] [hidden] {
display: none !important;
}
[data-pattern-header] {
display: flex;
align-items: center;
gap: 10px;
box-sizing: border-box;
min-inline-size: 0;
block-size: 52px;
padding: 0 16px;
}
[data-pattern-frame='classic'] [data-pattern-header] {
block-size: 56px;
background: var(--chatty-color-accent);
color: var(--chatty-color-on-accent);
}
[data-pattern-frame='classic'] [data-pattern-header] chatty-icon-button {
--chatty-color-border-focus: var(--chatty-color-on-accent);
}
[data-pattern-frame='admin'] [data-pattern-header] {
block-size: 68px;
padding: 8px 12px;
}
[data-pattern-header] chatty-avatar {
--chatty-avatar-size: 32px;
}
[data-pattern-frame='classic'] [data-pattern-header] chatty-avatar {
--chatty-avatar-background: #fff;
}
[data-pattern-frame='classic'] [data-pattern-header] chatty-avatar::part(base) {
color: #303030;
border: 1px solid #767676;
}
[data-pattern-actions] {
display: flex;
align-items: center;
gap: 8px;
}
[data-pattern-title] {
flex: 1;
min-inline-size: 0;
display: flex;
flex-direction: column;
}
[data-pattern-frame='admin'] [data-pattern-title] {
gap: 4px;
}
[data-pattern-frame='admin'] [data-pattern-title] strong {
display: flex;
align-items: center;
min-block-size: 28px;
font: var(--p-font-weight-semibold, 650) var(--p-text-heading-md-font-size, 14px) /
var(--p-text-heading-md-font-line-height, 20px) var(--p-font-family-sans, system-ui);
}
[data-pattern-title] strong {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
[data-pattern-header] chatty-icon-button {
--chatty-icon-button-size: 32px;
--chatty-icon-button-color: currentColor;
}
@media (max-width: 899px), (pointer: coarse) {
[data-pattern-header] chatty-icon-button {
--chatty-icon-button-size: 44px;
}
}
[data-pattern-launcher] {
position: absolute;
inset-inline-end: 0;
inset-block-end: 0;
}
[data-pattern-frame='ai-mode'] [data-pattern-launcher] {
inset-inline-end: auto;
inset-inline-start: 50%;
transform: translateX(-50%);
}
[data-pattern-frame='classic'] [data-pattern-launcher] {
--chatty-icon-button-size: 56px;
border-radius: 50%;
background: var(--chatty-color-accent);
--chatty-icon-button-color: var(--chatty-color-on-accent);
}
[data-sheet-mobile='true'] {
position: fixed;
inset: 0;
inline-size: 100vw;
}
[data-sheet-mobile='true'] chatty-surface::part(base) {
border-radius: 16px 16px 0 0;
}
[data-sheet-mobile='true'] chatty-surface {
position: relative;
z-index: 2;
margin-block-start: 12px;
}
[data-sheet-page],
[data-sheet-scrim] {
position: absolute;
inset: 0;
box-sizing: border-box;
}
[data-sheet-page] {
padding: 20px;
background: var(--chatty-color-bg-surface);
color: var(--chatty-color-text);
}
[data-sheet-scrim] {
z-index: 1;
border: 0;
background: rgb(0 0 0 / 40%);
animation: sheet-scrim 400ms ease;
}
[data-sheet-mobile='true'][data-closed='true'] [data-sheet-scrim] {
display: none;
}
[data-sheet-mobile='true'] [data-pattern-launcher] {
z-index: 3;
}
@keyframes sheet-scrim {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@media (prefers-reduced-motion: reduce) {
[data-sheet-scrim] {
animation: none;
}
}
</style>
<div
data-pattern-frame=${surface}
data-sheet-mobile=${String(mobileSheet)}
data-closed="false"
style=${`block-size:calc(${size.height} + ${mobileSheet ? 12 : 0}px)`}
>
${
mobileSheet
? html`<div data-sheet-page .inert=${surface === 'ai-mode'}>
<p>Example storefront</p>
<chatty-button
variant="secondary"
type="button"
@click=${event => {
event.currentTarget.textContent = 'Products opened';
}}
>
Browse products
</chatty-button>
</div>
${surface === 'ai-mode' ? html`<button type="button" data-sheet-scrim tabindex="-1" aria-label="Close chat background" @click=${event => toggle(event, true)}></button>` : ''}`
: ''
}
<chatty-surface
data-chatty-surface=${surface}
data-chatty-theme=${surface === 'admin' ? 'light' : (document.documentElement.dataset.chattyTheme ?? document.documentElement.dataset.theme ?? 'light')}
style=${`inline-size:100%;max-inline-size:${mobileSheet ? 'none' : `${size.width}px`};--chatty-surface-height:${size.height}`}
.store=${store}
.registry=${registry ?? null}
viewer-type=${surface === 'admin' ? 'agent' : 'customer'}
.selfTypes=${surface === 'admin' ? ['agent', 'bot'] : null}
.renderMessage=${sender}
>
<div slot="header" data-pattern-header>
${headerStart ?? ''}
${surface === 'admin' ? '' : html`<chatty-avatar label="Store" .status=${status}>S</chatty-avatar>`}
<span data-pattern-title
><strong
>${title ?? (surface === 'admin' ? 'Customer conversation' : strings.header)}</strong
>${surface === 'admin' ? html`<chatty-badge size="sm" variant="secondary" style="align-self:flex-start">Customer</chatty-badge>` : status == null ? '' : html`<small data-pattern-status>${status === 'online' ? 'Open' : 'Closed'}</small>`}</span
>
<span data-pattern-actions>${headerActions ?? ''}</span>
${
surface === 'admin'
? ''
: html`<chatty-icon-button label="Close panel" @click=${event => toggle(event, true)}
>${IconClose()}</chatty-icon-button
>`
}
</div>
<chatty-thread .strings=${{bot: 'Assistant', ...strings}}></chatty-thread>${content ?? ''}
<chatty-composer
slot="footer"
?hidden=${composerHidden}
.label=${strings.message}
.sendLabel=${strings.send}
@chatty-send=${event => {
appendViewer(store, surface, event.detail.text);
if (onSend) onSend(event.detail.text, event.currentTarget);
else if (surface !== 'admin')
store.setMessages([
...store.getState().messages,
{
id: `fixture-${store.getState().messages.length}`,
senderType: 'bot',
text: `You asked: “${event.detail.text}”. I can help with that.`,
hasEventTime: false
}
]);
}}
></chatty-composer> </chatty-surface
>${
surface === 'admin'
? ''
: surface === 'ai-mode'
? html`<chatty-launcher
data-pattern-launcher
hidden
label="Open chat"
@chatty-open=${event => toggle(event, false)}
>${IconMessage({size: 20, slot: 'icon'})}Ask a question</chatty-launcher
>`
: html`<chatty-icon-button
data-pattern-launcher
hidden
size="lg"
label="Open chat"
@click=${event => toggle(event, false)}
>${IconMessage({size: 24})}</chatty-icon-button
>`
}
</div>`;
}
import '@avada/chatty-ui/elements/skeleton.js';
import '@avada/chatty-ui/elements/icon.js';
export function Standard() {
const store = fixtureStore([question]);
const mode = new URLSearchParams(location.search).get('mobile');
const mobile = mode === null ? innerWidth < 768 : mode === 'true';
let rows = Array.from({length: 24}, (_, i) => ({
id: i + 1,
name: i ? `Customer ${i + 1}` : 'Alex Morgan',
unread: i > 0 && i % 3 === 0,
ai: i % 4 === 0
}));
let selected = 1;
let owner;
let request = 0;
let listAnchor = null;
const conversations = new Map();
const recordAnchor = () => {
const top = owner.querySelector('[data-conversation-list]').scrollTop;
if (top <= 0.5) return null;
return {id: rows[Math.floor(top / 64)]?.id, delta: top % 64};
};
const restoreAnchor = anchor => {
const index = anchor ? rows.findIndex(row => row.id === anchor.id) : 0;
owner.querySelector('[data-conversation-list]').scrollTop =
Math.max(0, index) * 64 + (anchor?.delta ?? 0);
};
const drawList = () =>
render(
html`${rows.map(
row =>
html` <button
type="button"
data-conversation=${row.id}
aria-label=${row.name}
aria-description=${[row.unread ? 'Unread' : 'Read', row.ai ? 'AI processing' : ''].filter(Boolean).join(', ')}
aria-current=${String(selected === row.id)}
@click=${() => open(row)}
>
<span data-row-avatar
><chatty-avatar label=${row.name}>${row.name[0]}</chatty-avatar>
<span data-channel aria-hidden="true">${EmailIcon({size: 12})}</span>
${
row.ai
? html`<chatty-icon size="sm" label="AI processing"
><svg
width="20"
height="20"
fill="currentColor"
aria-hidden="true"
viewBox="0 0 20 20"
>
<path
d="M5.702 4.253a.625.625 0 0 1 1.096 0l.196.358c.207.378.517.688.895.895l.358.196a.625.625 0 0 1 0 1.097l-.358.196a2.25 2.25 0 0 0-.895.894l-.196.359a.625.625 0 0 1-1.096 0l-.196-.359a2.25 2.25 0 0 0-.895-.894l-.358-.196a.625.625 0 0 1 0-1.097l.358-.196a2.25 2.25 0 0 0 .895-.895l.196-.358Z"
/>
<path
fill-rule="evenodd"
d="M12.948 7.89c-.18-1.167-1.852-1.19-2.064-.029l-.03.164a3.756 3.756 0 0 1-3.088 3.031c-1.15.189-1.173 1.833-.03 2.054l.105.02a3.824 3.824 0 0 1 3.029 3.029l.032.165c.233 1.208 1.963 1.208 2.196 0l.025-.129a3.836 3.836 0 0 1 3.077-3.045c1.184-.216 1.12-1.928-.071-2.107a3.789 3.789 0 0 1-3.18-3.154Zm-.944 6.887a5.34 5.34 0 0 1 2.542-2.647 5.305 5.305 0 0 1-2.628-2.548 5.262 5.262 0 0 1-2.488 2.508 5.329 5.329 0 0 1 2.574 2.687Z"
/></svg
></chatty-icon>`
: ''
}
</span>
<span data-row-text><strong>${row.name}</strong><span>Where is my order?</span></span>
<span data-row-time
>10:42<span data-unread ?hidden=${!row.unread} aria-label="Unread"></span
></span>
</button>`
)}`,
owner.querySelector('[data-conversation-list]')
);
const open = row => {
const surface = owner.querySelector('chatty-surface');
const composer = surface.querySelector('chatty-composer');
if (!surface.inert)
conversations.set(selected, {messages: store.getState().messages, draft: composer.value});
if (!mobile || owner.dataset.threadOpen !== 'true') listAnchor = recordAnchor();
const conversation = conversations.get(row.id) ?? {
messages: [{...question, text: `Hello, I am ${row.name}. Where is my order?`}],
draft: ''
};
composer.value = conversation.draft;
selected = row.id;
rows = rows.map(item => (item.id === row.id ? {...item, unread: false} : item));
drawList();
owner.dataset.threadOpen = 'true';
owner.querySelector('[data-pattern-title] strong').textContent = row.name;
surface.setAttribute('aria-busy', 'true');
surface.inert = true;
store.setMessages([]);
const current = ++request;
setTimeout(() => {
if (!owner.isConnected || current !== request) return;
store.setMessages(conversation.messages);
surface.removeAttribute('aria-busy');
surface.inert = false;
if (mobile) owner.querySelector('[data-admin-back]').focus({preventScroll: true});
}, 450);
};
return html`<style>
[data-admin-list] {
display: flex;
gap: 6px;
max-inline-size: 793px;
inline-size: 100%;
margin: auto;
min-inline-size: 0;
}
[data-admin-list] [hidden] {
display: none !important;
}
[data-conversation-list] {
box-sizing: border-box;
flex: 0 0 260px;
height: 842px;
overflow: auto;
overflow-anchor: none;
padding: 4px;
background: #fff;
border: 1px solid #e3e3e3;
scrollbar-gutter: stable;
}
[data-conversation-list] button {
box-sizing: border-box;
display: flex;
align-items: center;
gap: 8px;
width: 100%;
height: 64px;
padding: 8px;
border: 0;
border-radius: 6px;
background: #fff;
text-align: left;
color: #303030;
font: 13px/20px system-ui;
cursor: pointer;
}
[data-conversation-list] button[aria-current='true'] {
background: #ebebeb;
}
[data-conversation-list] button:focus-visible {
outline: 2px solid #005bd3;
outline-offset: -2px;
}
[data-row-avatar] {
position: relative;
flex-shrink: 0;
}
[data-row-avatar] chatty-icon {
position: absolute;
left: -2px;
top: -2px;
background: white;
border-radius: 50%;
color: #616161;
}
[data-channel] {
position: absolute;
right: -2px;
bottom: -2px;
background: white;
border-radius: 50%;
font-size: 12px;
line-height: 16px;
}
[data-row-text] {
min-width: 0;
display: grid;
flex: 1;
}
[data-row-text] strong,
[data-row-text] > span {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
[data-row-text] > span,
[data-row-time] {
color: #616161;
font-size: 12px;
}
[data-row-time] {
align-self: flex-start;
display: grid;
justify-items: end;
gap: 8px;
}
[data-unread] {
height: 7px;
width: 7px;
border-radius: 50%;
background: #005bd3;
}
[data-admin-list] [data-admin-thread] {
flex: 1;
min-width: 0;
}
[data-admin-list][data-mobile='true'] [data-conversation-list] {
flex: 1;
height: 770px;
}
[data-admin-list][data-mobile='true'][data-thread-open='false'] [data-admin-thread],
[data-admin-list][data-mobile='true'][data-thread-open='true'] [data-conversation-list] {
display: none;
}
</style>
<div
data-admin-list
@new-conversation=${() => {
const hiddenList = mobile && owner.dataset.threadOpen === 'true';
const anchor = hiddenList ? listAnchor : recordAnchor();
rows = [
{id: rows.length + 1, name: `New customer ${rows.length + 1}`, unread: true, ai: false},
...rows
];
drawList();
listAnchor = anchor;
if (!hiddenList) restoreAnchor(anchor);
}}
data-mobile=${String(mobile)}
data-thread-open="false"
${ref(node => {
if (node) {
owner = node;
queueMicrotask(drawList);
}
})}
>
<nav data-conversation-list aria-label="Conversations"></nav>
<div data-admin-thread>
${patternShell({
surface: 'admin',
store,
title: 'Alex Morgan',
headerStart: mobile
? html`<chatty-icon-button
data-admin-back
label="Back"
@click=${() => {
owner.dataset.threadOpen = 'false';
restoreAnchor(listAnchor);
owner
.querySelector(`[data-conversation="${selected}"]`)
.focus({preventScroll: true});
}}
>${IconChevronLeft()}</chatty-icon-button
>`
: '',
content: html`<div
slot="empty"
role="status"
aria-label="Loading messages"
style="display:grid;gap:16px;padding:16px"
>
<chatty-skeleton></chatty-skeleton><chatty-skeleton></chatty-skeleton
><chatty-skeleton></chatty-skeleton>
</div>`
})}
</div>
</div>`;
}