Chat
A conversation — support chats, comments on an order, a team thread. Message bubbles grouped by person and day, a reply box that grows as you type, "typing…" dots, read receipts, and a "New messages" button when you have scrolled up. On phones the reply box sits right on the keyboard.
Type a message and press Enter (Shift+Enter for a new line). Ana answers.
<div class="chat" data-moey-chat>
<div class="chat-log">
<div class="chat-day">Yesterday</div>
<div class="chat-msg">
<span class="avatar avatar-sm">AK</span>
<div class="chat-bubbles">
<div class="chat-bubble">Hi! I ordered the linen tote yesterday.</div>
<div class="chat-bubble">Could you send it to my office instead?</div>
<div class="chat-meta">Ana · 6:12 PM</div>
</div>
</div>
<div class="chat-msg chat-msg-out">
<div class="chat-bubbles">
<div class="chat-bubble">Of course — I've changed the address.</div>
<div class="chat-meta">6:20 PM · Read</div>
</div>
</div>
</div>
<form class="chat-composer">
<textarea class="form-control" rows="1" placeholder="Message" aria-label="Message"></textarea>
<button class="btn btn-default btn-icon" type="submit" aria-label="Send"><i class="mi mi-send"></i></button>
</form>
</div>
Build one, step by step
- The box:
<div class="chat" data-moey-chat>— 32rem tall by default (--mo-chat-height, or a height utility likeh-fullinside your app). Add a.chat-headeron top if you like. - The messages: a
.chat-logwith your history from the server. Each.chat-msgis one person in a row — an avatar, then.chat-bubbleswith one or more.chat-bubbles and a.chat-metaline. Your own messages get.chat-msg-out(right side, in the brand color). Separate days with.chat-day. - The reply box: a
<form class="chat-composer">with a textarea and a send button. Moey makes it grow, sends on Enter and puts your message in the log. - Send it: listen to
moey:send, poste.detail.textto your server and update the status withe.detail.status('Read'). - Receive: when a message comes in (websocket, polling), call
Moey.chat(el).add({ text, name, avatar }). It joins the person's last group or starts a new one, adds the day line when the date changes, and scrolls into view — unless the reader has scrolled up, who gets a "New message" button instead.
const chat = Moey.chat('#support');
chat.el.addEventListener('moey:send', async (e) => {
e.detail.status('Sending…');
try {
await fetch('/api/messages', { method: 'POST', body: JSON.stringify({ text: e.detail.text }) });
e.detail.status('Sent');
} catch {
e.detail.status('Not sent'); // shown in red
}
});
socket.on('message', (m) => chat.add({ text: m.text, name: m.author, avatar: m.initials, time: m.sentAt }));
socket.on('typing', (on) => chat.typing(on, 'Ana'));
A team thread, with names
With several people, add data-moey-names so each group shows who wrote it (.chat-name). Good for comments on an order or a task.
<div class="chat" data-moey-chat data-moey-names>
…
<div class="chat-msg">
<span class="avatar avatar-sm">MR</span>
<div class="chat-bubbles">
<div class="chat-name">Marco</div>
<div class="chat-bubble">The new tote photos are in the shared folder.</div>
<div class="chat-meta">9:02 AM</div>
</div>
</div>
</div>
<script>
Moey.chat('#thread').add({ text: 'Looks great!', name: 'Priya', avatar: 'PN' });
</script>
Full screen on a phone
.chat-full fills the screen. When the keyboard opens, the chat shrinks to the part you can still see, so the reply box sits right on the keyboard — like a messaging app. Try it:
<div class="chat chat-full" data-moey-chat>
<header class="chat-header">
<a class="btn btn-gray btn-ghost btn-icon" href="/inbox" aria-label="Back"><i class="mi mi-chevron-left rtl-flip"></i></a>
<div class="chat-header-title">Ana Kim<small>Order #10421</small></div>
</header>
<div class="chat-log">…</div>
<form class="chat-composer">…</form>
</div>
Files and pictures
Send a PDF, a spreadsheet, photos or a video the way people expect from a messaging app. A file is a card with an icon for its type, its name (the extension stays visible when a long name is cut), its size and a download mark. Pictures and videos sit in a neat grid and open in the lightbox. Try it: press the paperclip, paste a screenshot into the box, or drop files on the chat.
Files you send here aren't uploaded anywhere — the demo pretends, so you can see the progress ring (press it to cancel). Every third one "fails", to show Retry.
<!-- an <input type="file"> in the composer = the attach button, paste and drop -->
<div class="chat" data-moey-chat data-moey-max-size="25MB">
…
<form class="chat-composer">
<input type="file" multiple accept="image/*,video/*,.pdf,.csv,.xlsx,.docx,.zip" aria-label="Attach files">
<textarea class="form-control" rows="1" placeholder="Message" aria-label="Message"></textarea>
<button class="btn btn-default btn-icon" type="submit" aria-label="Send"><i class="mi mi-send"></i></button>
</form>
</div>
Step by step
- Add an
<input type="file" multiple>to the composer. It becomes a paperclip button;acceptlimits the kinds of file,data-moey-max-size="25MB"(on the chat) the size,data-moey-max-fileshow many at once (10). Files that don't fit are named in a short note — nothing is dropped silently. - Picked files wait in a tray above the message box — a thumbnail for pictures, a card for the rest, each with a × to remove it. Pasting (a screenshot) and dropping files on the chat add to it too.
- On send, your message appears at once with the files, and
moey:sendgetse.detail.files: for each, thefile, itsname,size,type, an abortsignal,progress(0–1)andurl(href)(the link once it's on your server). - Upload inside
e.detail.waitUntil(promise). Each file shows a ring that fills as you callprogress(); pressing it cancels (the signal aborts). If the promise fails the message says Not sent · Retry. NowaitUntil: the files count as sent right away. - Files from others: write the bubbles in HTML, or
Moey.chat(el).add({ files: [...] }).
chat.addEventListener('moey:send', (e) => {
const { text, files } = e.detail;
if (!files.length) return save(text); // just text
// the files show a ring until this is done; it can be cancelled, and "Retry" appears if it fails
e.detail.waitUntil(Promise.all(files.map((f) => new Promise((done, fail) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', '/upload');
xhr.upload.onprogress = (p) => f.progress(p.loaded / p.total); // the ring fills
xhr.onload = () => (xhr.status < 300 ? (f.url(JSON.parse(xhr.response).url), done()) : fail());
xhr.onerror = fail;
f.signal.addEventListener('abort', () => { xhr.abort(); fail(new DOMException('', 'AbortError')); });
const body = new FormData(); body.append('file', f.file); body.append('text', text);
xhr.send(body);
}))));
});
// received files, from your server or a websocket
Moey.chat(chat).add({
name: 'Ana', avatar: 'AK', text: 'Here you go',
files: [
{ name: 'Receipt.pdf', size: 48213, type: 'application/pdf', url: '/files/receipt.pdf' },
{ name: 'Strap.jpg', type: 'image/jpeg', url: '/photos/strap-big.jpg', thumb: '/photos/strap.jpg' },
],
});
A file
Only the link and the name are needed: the icon and its color follow the type (PDF, Word, spreadsheet, slides, zip, picture, video, audio, code, text), and the meta line becomes "48 KB · PDF" from data-size (bytes).
<!-- a file: the icon, the size and the type are added for you -->
<div class="chat-bubble">
<a class="chat-file" href="/files/delivery-note.pdf" download data-size="48213">
<span class="chat-file-name">Delivery note #10421.pdf</span>
</a>
</div>
Pictures and videos
One picture shows whole; two make a row; three, one wide and two below; four, a square; more, "+2" on the fourth — all of them still open in the lightbox, with swipe and zoom. A video link gets a ▶ and its length (data-duration, or found by itself when the link is a <video>).
<!-- pictures and videos: 1 to 4 in a grid, "+2" on the fourth when there are more -->
<div class="chat-bubble chat-media">
<a href="/photos/strap-big.jpg"><img src="/photos/strap.jpg" alt="Strap, front"></a>
<a href="/photos/side-big.jpg"><img src="/photos/side.jpg" alt="Strap, side"></a>
<a href="/videos/unboxing.mp4" data-duration="0:14"><img src="/videos/unboxing.jpg" alt="Unboxing video"></a>
</div>
On a phone
Accessible
- The log is a live region (
role="log"): screen readers read new messages as they arrive. - "Ana is typing" is announced, and the status ("Read", "Not sent") is text, not just a color.
- Messages are added as text — a name or message can't inject markup (use
html:only for content you trust). - Files: the card is one link ("Delivery note.pdf, 48 KB · PDF"), the attach button, each × and each progress ring have names ("Remove report.pdf", "Cancel sending report.pdf (40%)"), and a refused file is announced. Pictures keep their
alt; "+2" is read as "and 2 more". - Enter sends on computers; on touch screens Enter makes a new line and the button sends — the way phone keyboards expect.
Reference
| Class / attribute | What it does |
|---|---|
.chat[data-moey-chat] · .chat-full | The conversation (32rem tall; --mo-chat-height) · full screen, keyboard-aware |
.chat-header · .chat-header-title | An optional top bar (avatar, name with a small line, actions) |
.chat-log · .chat-day | The scrolling messages · a day line (sticks to the top while you scroll) |
.chat-msg · .chat-msg-out | One person's messages in a row · yours (right, brand color) |
.chat-bubbles · .chat-bubble · .chat-name · .chat-meta | The column of bubbles · a message · who (with data-moey-names) · time and status |
.chat-composer | The form: a growing textarea + a send button |
.chat-file (+ .chat-file-name, data-size) | A file card in a bubble: icon, name, size and type, download mark (added by moey.js) |
.chat-bubble.chat-media · data-duration | Pictures / videos in a grid (1–4, "+N"), opening in the lightbox · a video's length |
input[type=file] in .chat-composer · data-moey-max-size · data-moey-max-files | The attach button, tray, paste and drop · the largest file ("25MB") · how many at once (10) |
data-moey-status="Sending…" | The first status of your own messages ("Sent") |
Event moey:send | Cancelable (the message is taken back): detail: { text, files, message, messages, status(text), waitUntil(promise) } — each file: { file, name, size, type, signal, progress(0–1), url(href) }. Also moey:upload-cancel. |
Moey.chat(el) | { add({ text | html, files: [{ name, size, type, url, thumb, duration }], from: 'me' | 'them', name, avatar, time, status }), attach(files), typing(on, name), scrollToEnd() } |