logo

Imperative API

Bind the component instance to access these methods:

<script lang="ts">
    import SvelteVirtualChat from '@humanspeak/svelte-virtual-chat'
    let chat: ReturnType<typeof SvelteVirtualChat>
</script>

<SvelteVirtualChat bind:this={chat} ... />
<script lang="ts">
    import SvelteVirtualChat from '@humanspeak/svelte-virtual-chat'
    let chat: ReturnType<typeof SvelteVirtualChat>
</script>

<SvelteVirtualChat bind:this={chat} ... />

scrollToBottom(options?)

Scrolls the viewport to the bottom.

chat.scrollToBottom()                    // instant
chat.scrollToBottom({ smooth: true })    // smooth animation
chat.scrollToBottom()                    // instant
chat.scrollToBottom({ smooth: true })    // smooth animation
OptionTypeDefaultDescription
smoothbooleanfalseUse smooth scrolling animation

scrollToMessage(id, options?)

Scrolls to a specific message by its ID.

chat.scrollToMessage('msg-42')
chat.scrollToMessage('msg-42', { smooth: true })
chat.scrollToMessage('msg-42')
chat.scrollToMessage('msg-42', { smooth: true })
ParamTypeDescription
idstringThe message ID (as returned by getMessageId)
smoothbooleanUse smooth scrolling animation

If the message ID is not found, this is a no-op.

isAtBottom()

Returns whether the viewport is currently in follow-bottom state.

if (chat.isAtBottom()) {
    // Safe to assume user sees the latest message
}
if (chat.isAtBottom()) {
    // Safe to assume user sees the latest message
}

getDebugInfo()

Returns a snapshot of the current internal state.

const info = chat.getDebugInfo()
console.log(info.totalMessages)    // 1000
console.log(info.renderedCount)    // 18 (DOM nodes)
console.log(info.isFollowingBottom) // true
const info = chat.getDebugInfo()
console.log(info.totalMessages)    // 1000
console.log(info.renderedCount)    // 18 (DOM nodes)
console.log(info.isFollowingBottom) // true

SvelteVirtualChatDebugInfo

FieldTypeDescription
totalMessagesnumberTotal messages in the array
renderedCountnumberMessages currently in the DOM
measuredCountnumberMessages with measured heights
startIndexnumberFirst rendered index
endIndexnumberLast rendered index
totalHeightnumberCalculated total content height (px)
scrollTopnumberCurrent scroll position (px)
viewportHeightnumberViewport height (px)
isFollowingBottombooleanWhether viewport is pinned to bottom
averageHeightnumberAverage measured message height (px)