Composer

Create new conversations or reply to existing ones


When to use #

The Composer component renders a message composer to create new threads and to reply to existing ones.

This component pairs well with:


How to use #

import { Composer } from "@cord-sdk/react";

export const Example = () => {
  return (
    <Composer
      threadId={"<any string that is unique across your entire application>"}
      location={{ page: "index" }}
      onFocus={({ threadId }) =>
        console.log('Focussed <Composer /> threadId =', threadId)
      }
      onBlur={({ threadId }) =>
        console.log('Blurred <Composer /> threadId =', threadId)
      }
      onClose={({ threadId }) =>
        console.log('Closed <Composer /> threadId =', threadId)
      }
      onSend={({ threadId, messageId }) =>
      console.log('Sent a message from <Composer /> threadId =', threadId, 'messageId=', messageId)
    }
      style={{
        width: "300px" // Recommended so that the composer doesn't stretch horizontally based on its content
      }}
    />
  );
};
Copy

Properties #


threadId #

optional
string
An arbitrary string that uniquely identifies a thread. Messages sent will go to the provided thread ID. If the thread ID doesn't exist yet, it will be created. If no ID is passed, each message sent will create a new thread.
Warning! An important restriction of working with thread identifiers is that they must be unique across your entire application. You can't use the same thread identifier in two separate organizations. This is an intentional limitation imposed by Cord.

location #

optional
string
The location value set for the newly created threads.
If unset, this will default to the location provided to the useCordLocation hook if that was used. Otherwise, will default to the current URL for the page.

threadName #

optional
string
Sets the name of the thread. The thread name is used in a small number of places where a short name or header is useful to distinguish the thread; the default value is nearly always fine. A newly-created thread will have its title set to this value, and an existing thread will have its title updated to this value.
The default setting is the current page's title.

autofocus #

optional
boolean
If true, the composer input field will render with the autofocus HTML attribute set.
The default value is false.

showExpanded #

optional
boolean
If true, the composer will always appear expanded. This means that it will always show the button list (such as the mention button and emoji button) right below the editor.
If false, the composer will start from a single-line state, but will expand when a user clicks in the editor or starts typing. It will return to a single-line state when it loses focus and there is no input in the editor.
The default value is false.

showCloseButton #

optional
boolean
If true the composer will show a close button next to the send button that will trigger the onClose callback when clicked.
The default value is false.

size #

optional
"small" | "medium" | "large"
This customizes the size of the composer. You can set it to one of the three following values:
  1. small: The composer will start in a single-line state and expand to show the button list without a separator when a user clicks in the editor or starts typing. It will return to a single-line state when it loses focus and there is no input in the editor.
  2. medium: The composer will start in a single-line state and expand to show the button list with a separator when a user clicks in the editor or starts typing. It will return to a single-line state when it loses focus and there is no input in the editor.
  3. large: The composer will start with a size similar to the expanded state but without the button list and expand minimally to show the button list when a user clicks in the editor or starts typing. It will return to the state without the button list when it loses focus and there is no input in the editor.
The default setting is medium.

threadUrl #

optional
string
The URL of a thread is used to direct users to the correct place when clicking on a notification. The threadUrl defaults to window.location.href. Setting this property would override that default.
Note: The URL specified only applies to new threads and will not change the url of existing threads.
The default value is fine for almost all use cases.

messageMetadata #

optional
object
A JSON object that can be used to store metadata about a message on creation. Keys are strings, and values can be strings, numbers or booleans.

onFocus #

optional
function
Callback invoked when a user focuses the composer. The callback is passed an object containing the threadId linked to the composer.

onBlur #

optional
function
Callback invoked when the composer loses focus. The callback is passed an object containing the threadId linked to the composer.

onClose #

optional
function
Callback invoked when a user clicks on the close button in the composer. The callback is passed an object containing the threadId linked to the composer.

onSend #

optional
function
Callback invoked when a user sends a message from the composer. The callback is passed an object containing the threadId and messageId linked to the composer.

CSS customization #

If you want to customize this component, you can target the classes below in your app's CSS. These are guaranteed to be stable.

There are more classes that are best understood in context. We suggest inspecting the component with your browser's developer tools to view everything. You can target any classes starting with cord-.

Class nameDescription
.cord-composer
Applied to the container div. This class is always present.
.cord-expanded
Applied to the container div when the composer is expanded, usually on focus, or when some text is already there.
.cord-editor-container
Applied to the div containing the editor.
.cord-composer-menu
Applied to the div containing the primary and secondary buttons.
.cord-placeholder
Applied to the typing placeholder.
.cord-attachments
Applied to the div containing the attachments (files and images).

Ask Cordy