Chat Viewer
    Preparing search index...

    Interface ChatViewerProps<M>

    Props for the ChatViewer component.

    interface ChatViewerProps<M extends IdentifiableMessage = IdentifiableMessage> {
        alignment?: ChatAlignment;
        bufferSize?: number;
        className?: string;
        getKey?: (message: M) => string | number;
        historyEndOffset?: number;
        keepMountedIds?: MessageId<M>[];
        keepMountedIndexes?: number[];
        messages: M[];
        onAtBottom?: ChatCallback<M>;
        onAtTop?: ChatCallback<M>;
        onContentResize?: ChatCallback<M>;
        onHistoryEndReached?: ChatCallback<M>;
        onKeyDown?: KeyboardEventHandler;
        onNewerMessages?: OnMessagesCallback<M>;
        onOlderMessages?: OnMessagesCallback<M>;
        onPrefixDisplay?: OnAffixCallback<M>;
        onScroll?: (offset: number) => void;
        onScrollEnd?: () => void;
        onSuffixDisplay?: OnAffixCallback<M>;
        onUnpin?: ChatCallback<M>;
        onWheel?: WheelEventHandler;
        overscan?: number;
        pinnedId?: M["id"];
        pinOpts?: PinOpts;
        prefix?: ReactNode;
        renderMessage: (
            message: M,
            index: number,
            messages: M[],
        ) => null | ReactElement<unknown, string | JSXElementConstructor<any>>;
        scrollerClassName?: string;
        scrollerRef?: Ref<HTMLDivElement>;
        scrollerStyle?: CSSProperties;
        ssrCount?: number;
        style?: CSSProperties;
        suffix?: ReactNode;
    }

    Type Parameters

    Index

    Properties

    alignment?: ChatAlignment

    Alignment of the chat list. Determines whether new messages appear at the top or bottom.

    'bottom'
    
    bufferSize?: number

    Extra item space in pixels to render beyond the visible area (for virtualization performance). Passed to Virtua's bufferSize prop.

    className?: string

    Class name for the root element of the chat viewer.

    getKey?: (message: M) => string | number

    Returns a stable key for a message. Use it when the id of a message can change, for example when an optimistic message gets its id from the server. A message that keeps its key keeps its rendered element and its state, and it is not reported as a new message to ChatViewerProps.onNewerMessages or ChatViewerProps.onOlderMessages.

    Type declaration

      • (message: M): string | number
      • Parameters

        • message: M

          The message to get the key for.

        Returns string | number

    The id of the message.

    historyEndOffset?: number

    Offset in pixels from the end of the history at which to trigger onHistoryEndReached.

    10
    
    keepMountedIds?: MessageId<M>[]

    MessageIds of messages to keep mounted even when out of view.

    keepMountedIndexes?: number[]

    Indexes of messages to keep mounted even when out of view. This allows you to preserve the state of messages that are not currently visible.

    messages: M[]

    The array of messages to display in the chat viewer.

    onAtBottom?: ChatCallback<M>

    Callback fired when the viewport reaches the bottom of the message list.

    ChatViewerHandle instance. Allows you to control the chat viewer.

    onAtTop?: ChatCallback<M>

    Callback fired when the viewport reaches the top of the message list.

    ChatViewerHandle instance. Allows you to control the chat viewer.

    onContentResize?: ChatCallback<M>

    Callback fired when the size of the rendered content changes, for example when a streamed message grows or an image loads. ChatViewerHandle.atBottom still has the value from before the change, so followAtBottom keeps the chat at the bottom while the content grows.

    <ChatViewer
    messages={messages}
    renderMessage={renderMessage}
    onContentResize={followAtBottom()}
    />

    ChatViewerHandle instance. Allows you to control the chat viewer.

    onHistoryEndReached?: ChatCallback<M>

    Callback fired when the user scrolls to the end of the history (top or bottom, depending on alignment).

    ChatViewerHandle instance. Allows you to control the chat viewer.

    onKeyDown?: KeyboardEventHandler

    Callback fired on key down events in the scrollable container.

    onNewerMessages?: OnMessagesCallback<M>

    Callback fired when newer messages are requested (e.g., user scrolls to the bottom).

    You might want to use this callback with utility functions like followEveryMessage, followMessagesAtBottom, or followMessagesBy to automatically scroll to automatically follow new messages.

    <ChatViewer
    messages={messages}
    renderMessage={renderMessage}
    onNewerMessages={followMessagesAtBottom()}
    />

    ChatViewerHandle instance. Allows you to control the chat viewer.

    The current messages.

    followMessagesAtBottom - scrolls to the bottom when viewport is at the bottom of the history.

    onOlderMessages?: OnMessagesCallback<M>

    Callback fired when older messages are requested (e.g., user scrolls to the top).

    ChatViewerHandle instance. Allows you to control the chat viewer.

    The current messages.

    onPrefixDisplay?: OnAffixCallback<M>

    Callback fired when the prefix node is displayed in the viewport.

    ChatViewerHandle instance. Allows you to control the chat viewer.

    Whether the prefix is displayed.

    onScroll?: (offset: number) => void

    Callback fired on scroll with the current scroll offset.

    Type declaration

      • (offset: number): void
      • Parameters

        • offset: number

          The current scroll offset.

        Returns void

    onScrollEnd?: () => void

    Callback fired when scrolling ends.

    onSuffixDisplay?: OnAffixCallback<M>

    Callback fired when the suffix node is displayed in the viewport.

    ChatViewerHandle instance. Allows you to control the chat viewer.

    Whether the suffix is displayed.

    followSuffixAtBottom - scrolls to the bottom when viewport when suffix is displayed at the bottom of the history.

    onUnpin?: ChatCallback<M>

    Callback fired when the pin lets go of the list: the user scrolled away, or ChatViewerHandle.unpin was called.

    ChatViewerHandle instance. Allows you to control the chat viewer.

    onWheel?: WheelEventHandler

    Callback fired on wheel events in the scrollable container.

    overscan?: number

    Extra item space in pixels to render beyond the visible area (for virtualization performance). Passed to Virtua's bufferSize prop when bufferSize is not set.

    Use bufferSize instead.

    pinnedId?: M["id"]

    id of a message to keep at the top of the viewport, with room under it. The content that comes after the message fills the room, so a streamed answer does not move the list. The pin holds until the user scrolls away, ChatViewerHandle.unpin is called, or this prop changes. While the pin holds, the follow strategies do not scroll the chat.

    To pin new messages by a condition, use pinMessagesBy instead.

    pinOpts?: PinOpts
    prefix?: ReactNode

    React node to render before the message list (e.g., a header or history loader). It can be conditionally displayed. When changed, it will trigger a onPrefixDisplay callback.

    null
    
    renderMessage: (
        message: M,
        index: number,
        messages: M[],
    ) => null | ReactElement<unknown, string | JSXElementConstructor<any>>

    Function to render a message.

    Type declaration

      • (
            message: M,
            index: number,
            messages: M[],
        ): null | ReactElement<unknown, string | JSXElementConstructor<any>>
      • Parameters

        • message: M

          The message to render.

        • index: number
        • messages: M[]

        Returns null | ReactElement<unknown, string | JSXElementConstructor<any>>

        A React element or null.

    scrollerClassName?: string

    Class name for the scrollable container.

    scrollerRef?: Ref<HTMLDivElement>

    Ref to the scrollable container element. Use it to measure the scroller or to listen to its DOM events.

    scrollerStyle?: CSSProperties

    Style for the scrollable container.

    ssrCount?: number

    Number of items to render on the server for SSR. Passed to Virtua's ssrCount prop.

    style?: CSSProperties

    Style for the root element of the chat viewer.

    suffix?: ReactNode

    React node to render after the message list (e.g., a footer or typing indicator). It can be conditionally displayed. When changed, it will trigger a onSuffixDisplay callback.

    null