Introduction
A chat-style message history web component. Render conversations from plain text, with avatars, bubbles, and smooth animations.
Installation
Install the component from your command line.
npm install @bbki.ng/bb-msg-history
Basic Usage
Use plain text with author: message format. Each author gets a letter avatar by default.
<bb-msg-history>
bbki.ng: 谁呀
xwy: 谁谁谁,你猴爷爷!
</bb-msg-history>
Timestamps
Add optional timestamps using the [timestamp] author: text format. Timestamps appear on hover near the message bubble.
<bb-msg-history>
[09:00] alice: Good morning!
[09:02] bob: Hey, how are you?
[09:05] alice: Pretty good, thanks
alice: (no timestamp here - backward compatible)
[09:08] bob: Same here
</bb-msg-history>
Custom Avatars
Use setAuthor() to customize avatars (emoji, SVG, or image), side position, and bubble colors.
el.setAuthor('me', { avatar: '<svg>...</svg>', side: 'right', bubbleColor: '#f3f4f6' });
el.setAuthor('friend', { avatar: '<svg>...</svg>', side: 'left', bubbleColor: '#e0f2fe' });
Append Messages
Use appendMessage() to add messages programmatically with smooth scrolling.
el.appendMessage({ author: 'alice', text: 'Hello!' });
el.appendMessage({ author: 'bob', text: 'How are you?' });
Message Grouping
Consecutive messages from the same author are grouped together. The avatar is only shown on the first message.
Scrollable View
Long conversations automatically scroll. Use --bb-max-height CSS variable to customize the container height.
<bb-msg-history style="--bb-max-height: 200px;">...</bb-msg-history>
Multi-party Chat
Support for more than two participants in a conversation.
Loading State
Use the loading attribute or setLoading() method to show a loading animation while fetching messages. The loading overlay appears centered over the message area.
<!-- HTML attribute -->
<bb-msg-history loading></bb-msg-history>
<!-- JavaScript API -->
const el = document.querySelector('bb-msg-history');
// Show loading while fetching
el.setLoading(true);
fetchMessages().then(messages => {
el.setLoading(false);
messages.forEach(msg => el.appendMessage(msg));
});
Infinite Mode
Use the infinite attribute to remove height constraints. The container expands to fit all messages without scrolling. Useful when the parent handles scrolling or when displaying a full conversation.
<!-- No height limit, container expands to fit all messages -->
<bb-msg-history infinite>
alice: Hey there!
bob: No height limit here
alice: The container just expands
</bb-msg-history>
Custom Scroll Button
Use the hide-scroll-button attribute to hide the default scroll-to-bottom button. Listen for bb-scrollbuttonshow and bb-scrollbuttonhide events to implement your own button or trigger other actions.
<!-- Hide default button with attribute -->
<bb-msg-history hide-scroll-button>
alice: Hello
bob: Hi there
</bb-msg-history>
<!-- Listen to events for custom behavior -->
<script>
const el = document.querySelector('bb-msg-history');
el.addEventListener('bb-scrollbuttonshow', (e) => {
// Show custom button or trigger other actions
console.log('Scroll button would show');
});
el.addEventListener('bb-scrollbuttonhide', (e) => {
// Hide custom button
console.log('Scroll button would hide');
});
// Scroll to bottom programmatically
el.scrollToBottom();
</script>