Zubra UI
Code for
GitHub

chat-message

One message: avatar, author, time, text (with @mentions and links picked out), the message it replies to, and reactions. me is the signed-in user's name: their messages, mentions of them and their reactions are marked.

Try it: events appear here (reacttoggle, reply).

Source: chat-message.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/chat-message.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/chat-message.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/chat-message.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/chat-message.json
One file, no dependencies (4.6 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/chat-message.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/chat-message.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/chat-message.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/chat-message.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/chat-message.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/chat-message.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/chat-message.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/chat-message.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/chat-message.js"></script>

The compiled install writes src/components/zubra/chat-message.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<chat-message me="Sam Rivera"></chat-message>

<script type="module">
  import './components/zubra/chat-message.js';

  const element = document.querySelector('chat-message');
  element.message = {
    id: 'm1',
    author: 'Maya Chen',
    text: 'Shipped the date picker. @sam can you check it on mobile? Notes: https://example.com/notes',
    time: 1794148200000,
    reactions: { "🎉": ['Sam Rivera', 'Jo Park'], "👀": ['Jo Park'] },
    replyTo: null,
  };
  element.addEventListener('reacttoggle', (event) => console.log('reacttoggle', event.detail));
  element.addEventListener('reply', (event) => console.log('reply', event.detail));
</script>
Example.jsx
import './components/zubra/chat-message.js';

const message = {
  id: 'm1',
  author: 'Maya Chen',
  text: 'Shipped the date picker. @sam can you check it on mobile? Notes: https://example.com/notes',
  time: 1794148200000,
  reactions: { "🎉": ['Sam Rivera', 'Jo Park'], "👀": ['Jo Park'] },
  replyTo: null,
};

export function ChatMessageExample() {
  return (
    <chat-message
      me="Sam Rivera"
      message={message}
      onreacttoggle={(event) => console.log('reacttoggle', event.detail)}
      onreply={(event) => console.log('reply', event.detail)}
    ></chat-message>
  );
}
Example.vue
<script setup>
import './components/zubra/chat-message.js';
const message = {
  id: 'm1',
  author: 'Maya Chen',
  text: 'Shipped the date picker. @sam can you check it on mobile? Notes: https://example.com/notes',
  time: 1794148200000,
  reactions: { "🎉": ['Sam Rivera', 'Jo Park'], "👀": ['Jo Park'] },
  replyTo: null,
};
const onReacttoggle = (event) => console.log('reacttoggle', event.detail);
const onReply = (event) => console.log('reply', event.detail);
</script>

<template>
  <chat-message
    me="Sam Rivera"
    :message.prop="message"
    @reacttoggle="onReacttoggle"
    @reply="onReply"
  ></chat-message>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/chat-message.js';
  const message = {
    id: 'm1',
    author: 'Maya Chen',
    text: 'Shipped the date picker. @sam can you check it on mobile? Notes: https://example.com/notes',
    time: 1794148200000,
    reactions: { "🎉": ['Sam Rivera', 'Jo Park'], "👀": ['Jo Park'] },
    replyTo: null,
  };
</script>

<chat-message
  me="Sam Rivera"
  {message}
  onreacttoggle={(event) => console.log('reacttoggle', event.detail)}
  onreply={(event) => console.log('reply', event.detail)}
></chat-message>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/chat-message.js';

@Component({
  selector: 'app-chat-message-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <chat-message
      me="Sam Rivera"
      [message]="message"
      (reacttoggle)="onReacttoggle($event)"
      (reply)="onReply($event)"
    ></chat-message>
  `,
})
export class ChatMessageExample {
  message = {
    id: 'm1',
    author: 'Maya Chen',
    text: 'Shipped the date picker. @sam can you check it on mobile? Notes: https://example.com/notes',
    time: 1794148200000,
    reactions: { "🎉": ['Sam Rivera', 'Jo Park'], "👀": ['Jo Park'] },
    replyTo: null,
  };

  onReacttoggle(event: Event) {
    console.log('reacttoggle', (event as CustomEvent).detail);
  }

  onReply(event: Event) {
    console.log('reply', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
mestring""
message
property only
{ id, author, text, time, reactions, replyTo }{"id":"","author":"","text":"","time":0,"reactions":{},"replyTo":null}
Events
EventWhen
reacttoggleA reaction was toggled; detail is the emoji.
replyReply was chosen.

Size

2.3 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 18.4 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour