Message Composer
The channel input bar: attachment control, text field with combobox hooks, and send.
The channel input bar: attachment control, text field with combobox hooks, and send. Compose it from the exported registry parts so local content and behaviour stay in the application while the visual contract stays token-driven.
newt-composer"use client"
import * as React from "react"
import { MessageComposer } from "@/registry/bases/newt/ui/message-composer"
export default function MessageComposerDemo() {
const [value, setValue] = React.useState("")
return (
<MessageComposer
channelName="general"
value={value}
onValueChange={setValue}
onSubmit={() => setValue("")}
className="w-full max-w-md"
/>
)
}<script setup lang="ts">
import { ref } from "vue"
import { MessageComposer } from "~~/registry/bases/newt/ui/message-composer"
const value = ref("")
</script>
<template>
<MessageComposer
v-model="value"
channel-name="general"
class="w-full max-w-md"
@submit="value = ''"
/>
</template>Installation
npx newtui@latest add message-composernpx shadcn@latest add @newtui/message-composernpx shadcn-vue@latest add @newtui/message-composerpnpm dlx newtui@latest add message-composerpnpm dlx shadcn@latest add @newtui/message-composerpnpm dlx shadcn-vue@latest add @newtui/message-composeryarn dlx newtui@latest add message-composeryarn dlx shadcn@latest add @newtui/message-composeryarn dlx shadcn-vue@latest add @newtui/message-composerbunx newtui@latest add message-composerbunx shadcn@latest add @newtui/message-composerbunx shadcn-vue@latest add @newtui/message-composerdeno run -A npm:newtui@latest add message-composerdeno run -A npm:shadcn@latest add @newtui/message-composerdeno run -A npm:shadcn-vue@latest add @newtui/message-composervlx newtui@latest add message-composervlx shadcn@latest add @newtui/message-composervlx shadcn-vue@latest add @newtui/message-composervp dlx newtui@latest add message-composervp dlx shadcn@latest add @newtui/message-composervp dlx shadcn-vue@latest add @newtui/message-composernubx newtui@latest add message-composernubx shadcn@latest add @newtui/message-composernubx shadcn-vue@latest add @newtui/message-composernlx newtui@latest add message-composernlx shadcn@latest add @newtui/message-composernlx shadcn-vue@latest add @newtui/message-composerupx newtui@latest add message-composerupx shadcn@latest add @newtui/message-composerupx shadcn-vue@latest add @newtui/message-composer- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install class-variance-authoritypnpm add class-variance-authorityyarn add class-variance-authoritybun add class-variance-authoritydeno add npm:class-variance-authorityvlt install class-variance-authorityvp add class-variance-authoritynub add class-variance-authorityni class-variance-authorityupm add class-variance-authority- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Copy the source into your project
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import * as React from "react"
import { cn } from "@/lib/utils"
/** The composer's own names. Pass `labels` to translate them. */
export interface MessageComposerLabels {
/** Accessible name of the whole bar, built from the channel name. */
group: (channelName: string) => string
/** Placeholder and name of the field, when `placeholder` is not set. */
placeholder: (channelName: string) => string
/** Accessible name of the add button. */
add: string
/** Accessible name of the send button. */
send: string
}
/** The English defaults. */
export const defaultMessageComposerLabels: MessageComposerLabels = {
group: (channelName) => `Message composer for #${channelName}`,
placeholder: (channelName) => `Message #${channelName}`,
add: "Add attachment",
send: "Send message",
}
const AddIcon = () => (
<svg viewBox="0 0 24 24" aria-hidden="true" className="h-5 w-5">
<path
fill="currentColor"
d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6z"
/>
</svg>
)
const SendIcon = () => (
<svg viewBox="0 0 24 24" aria-hidden="true" className="h-5 w-5">
<path fill="currentColor" d="M3 20.5v-6l8-2.5-8-2.5v-6l19 8.5z" />
</svg>
)
const messageComposerVariants = cva(
"flex min-w-0 items-center gap-2 text-newt-text-muted",
{
variants: {
layout: {
default:
"min-h-11 rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-input-elevated px-4",
/* A phone: no frame, a pill field, round buttons beside it. */
narrow: "min-h-10",
},
},
defaultVariants: { layout: "default" },
}
)
const fieldVariants = cva("flex min-w-0 flex-1 items-center gap-2", {
variants: {
layout: {
default: "",
narrow: "min-h-10 rounded-full bg-newt-bg-input-elevated pe-1 ps-4",
},
},
defaultVariants: { layout: "default" },
})
/**
* The style of the composer's icon buttons. Give it to a button you add, so
* it matches the built-in ones. In the narrow layout, use `layout: "narrow"`
* for a button beside the field and the default for one inside it.
*/
const messageComposerButtonVariants = cva(
"inline-flex shrink-0 cursor-pointer items-center justify-center border-0 p-0 text-inherit hover:bg-newt-bg-hover hover:text-newt-text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link disabled:cursor-not-allowed disabled:opacity-50",
{
variants: {
layout: {
default: "h-8 w-8 rounded-[var(--newt-radius-sm)] bg-transparent",
narrow: "h-10 w-10 rounded-full bg-newt-bg-input-elevated",
},
},
defaultVariants: { layout: "default" },
}
)
/* The send control only lights up once there is something to send. */
const sendReady = {
default: "text-newt-brand",
narrow:
"bg-newt-brand text-newt-on-accent hover:bg-newt-brand-hover hover:text-newt-on-accent",
}
type MessageComposerLayout = NonNullable<
VariantProps<typeof messageComposerVariants>["layout"]
>
/** Attributes for the focusable field. Spread them on it. */
export interface MessageComposerInputAttributes {
placeholder: string
"aria-label": string
role: "combobox" | undefined
autoComplete: "off" | undefined
"aria-controls": string | undefined
"aria-expanded": boolean | undefined
"aria-activedescendant": string | undefined
disabled: boolean
spellCheck: false
/** Enter submits, Escape and the arrow keys reach their callbacks. */
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void
}
/** What `renderInput` receives: the state of the built-in input. */
export interface MessageComposerInputProps {
value: string
/** Changes the value, as typing in the built-in input does. */
setValue: (value: string) => void
/** Sends the value, as Enter does. Does nothing while it is empty. */
submit: () => void
disabled: boolean
inputProps: MessageComposerInputAttributes
}
export interface MessageComposerProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onSubmit" | "onChange" | "children"
> {
/** Channel the message goes to; drives the default placeholder and labels. */
channelName: string
value?: string
defaultValue?: string
onValueChange?: (value: string) => void
placeholder?: string
/** Sent on Enter and on the send control. */
onSubmit?: (value: string) => void
onEscape?: () => void
/** Arrow keys, for driving a suggestion list above the composer. */
onNavigate?: (direction: "up" | "down") => void
/** Wire the input up as a combobox for slash-command autocomplete. */
autocomplete?: boolean
ariaControls?: string
ariaExpanded?: boolean
ariaActiveDescendant?: string
/** Turns off the input, the add button and the send button. */
disabled?: boolean
/** `"narrow"` is the phone layout: a pill field and round buttons. */
layout?: MessageComposerLayout
/** Shows the add button. */
showAdd?: boolean
onAdd?: () => void
/** Controls after the add button, before the field. */
leading?: React.ReactNode
/**
* Replaces the built-in input with your own field. It receives the value,
* a setter, the submit action and the attributes to spread on the field.
* The Vue port takes the same content through its `#input` slot.
*/
renderInput?: (props: MessageComposerInputProps) => React.ReactNode
/** Extra controls at the end of the field, before the send button. */
actions?: React.ReactNode
/** Shown in place of the send button while the field is empty. */
trailing?: React.ReactNode
/** Replaces the English names; omitted keys keep their default. */
labels?: Partial<MessageComposerLabels>
}
const MessageComposer = React.forwardRef<HTMLDivElement, MessageComposerProps>(
(
{
className,
channelName,
value,
defaultValue = "",
onValueChange,
placeholder,
onSubmit,
onEscape,
onNavigate,
autocomplete = false,
ariaControls,
ariaExpanded,
ariaActiveDescendant,
disabled = false,
layout = "default",
showAdd = true,
onAdd,
leading,
renderInput,
actions,
trailing,
labels: labelOverrides,
...props
},
ref
) => {
const text = { ...defaultMessageComposerLabels, ...labelOverrides }
const [uncontrolled, setUncontrolled] = React.useState(defaultValue)
const current = value ?? uncontrolled
const hasValue = current.trim().length > 0
const label = placeholder ?? text.placeholder(channelName)
const setValue = (next: string) => {
if (value === undefined) setUncontrolled(next)
onValueChange?.(next)
}
const submit = () => {
if (!hasValue || disabled) return
onSubmit?.(current)
}
const onKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
switch (event.key) {
case "Enter":
event.preventDefault()
submit()
return
case "Escape":
event.preventDefault()
onEscape?.()
return
case "ArrowUp":
case "ArrowDown":
if (!onNavigate) return
event.preventDefault()
onNavigate(event.key === "ArrowUp" ? "up" : "down")
}
}
const inputProps: MessageComposerInputAttributes = {
placeholder: label,
"aria-label": label,
role: autocomplete ? "combobox" : undefined,
autoComplete: autocomplete ? "off" : undefined,
"aria-controls": autocomplete ? ariaControls : undefined,
"aria-expanded": autocomplete ? ariaExpanded : undefined,
"aria-activedescendant": autocomplete ? ariaActiveDescendant : undefined,
disabled,
spellCheck: false,
onKeyDown,
}
return (
<div
ref={ref}
role="group"
aria-label={text.group(channelName)}
className={cn(messageComposerVariants({ layout }), className)}
{...props}
>
{showAdd && (
<button
type="button"
aria-label={text.add}
disabled={disabled}
className={messageComposerButtonVariants({ layout })}
onClick={onAdd}
>
<AddIcon />
</button>
)}
{leading}
<div className={fieldVariants({ layout })}>
{renderInput ? (
renderInput({
value: current,
setValue,
submit,
disabled,
inputProps,
})
) : (
<input
type="text"
value={current}
{...inputProps}
onChange={(event) => setValue(event.target.value)}
className="w-full min-w-0 border-0 bg-transparent font-[family-name:var(--newt-font-sans)] text-[15px] text-newt-text-primary outline-none placeholder:text-newt-text-muted disabled:cursor-not-allowed"
/>
)}
{actions}
</div>
{Boolean(trailing) && !hasValue ? (
trailing
) : (
<button
type="button"
aria-label={text.send}
disabled={disabled || !hasValue}
onClick={submit}
className={cn(
messageComposerButtonVariants({ layout }),
hasValue && !disabled && sendReady[layout]
)}
>
<SendIcon />
</button>
)}
</div>
)
}
)
MessageComposer.displayName = "MessageComposer"
export { MessageComposer, messageComposerButtonVariants }<script setup lang="ts">
import type { HTMLAttributes, VNode } from "vue"
import { computed } from "vue"
import { cn } from "@/lib/utils"
import {
defaultMessageComposerLabels,
type MessageComposerInputAttributes,
type MessageComposerInputProps,
type MessageComposerLabels,
type MessageComposerLayout,
messageComposerButtonVariants,
messageComposerFieldVariants,
messageComposerVariants,
} from "."
const props = withDefaults(
defineProps<{
class?: HTMLAttributes["class"]
/** Channel the message goes to; drives the placeholder and the labels. */
channelName: string
placeholder?: string
/** Wire the input up as a combobox for slash-command autocomplete. */
autocomplete?: boolean
ariaControls?: string
ariaExpanded?: boolean
ariaActivedescendant?: string
/** Turns off the input, the add button and the send button. */
disabled?: boolean
/** `"narrow"` is the phone layout: a pill field and round buttons. */
layout?: MessageComposerLayout
/** Shows the add button. */
showAdd?: boolean
/** Replaces the English names; omitted keys keep their default. */
labels?: Partial<MessageComposerLabels>
}>(),
{ autocomplete: false, disabled: false, layout: "default", showAdd: true }
)
const emit = defineEmits<{
submit: [value: string]
escape: []
/** Arrow keys, for driving a suggestion list above the composer. */
navigate: [direction: "up" | "down"]
add: []
}>()
const slots = defineSlots<{
/** Controls after the add button, before the field. */
leading?: () => VNode[]
/**
* Replaces the built-in input with your own field. It receives the value,
* a setter, the submit action and the attributes to bind on the field.
*/
input?: (props: MessageComposerInputProps) => VNode[]
/** Extra controls at the end of the field, before the send button. */
actions?: () => VNode[]
/** Shown in place of the send button while the field is empty. */
trailing?: () => VNode[]
}>()
const value = defineModel<string>({ default: "" })
const text = computed(() => ({
...defaultMessageComposerLabels,
...props.labels,
}))
const label = computed(
() => props.placeholder ?? text.value.placeholder(props.channelName)
)
const hasValue = computed(() => value.value.trim().length > 0)
/* The send control only lights up once there is something to send. */
const sendReady = {
default: "text-newt-brand",
narrow:
"bg-newt-brand text-newt-on-accent hover:bg-newt-brand-hover hover:text-newt-on-accent",
}
function setValue(next: string) {
value.value = next
}
function submit() {
if (!hasValue.value || props.disabled) return
emit("submit", value.value)
}
function onKeydown(event: KeyboardEvent) {
switch (event.key) {
case "Enter":
event.preventDefault()
submit()
return
case "Escape":
event.preventDefault()
emit("escape")
return
case "ArrowUp":
case "ArrowDown":
event.preventDefault()
emit("navigate", event.key === "ArrowUp" ? "up" : "down")
}
}
const inputProps = computed<MessageComposerInputAttributes>(() => ({
placeholder: label.value,
"aria-label": label.value,
role: props.autocomplete ? "combobox" : undefined,
autocomplete: props.autocomplete ? "off" : undefined,
"aria-controls": props.autocomplete ? props.ariaControls : undefined,
"aria-expanded": props.autocomplete ? props.ariaExpanded : undefined,
"aria-activedescendant": props.autocomplete
? props.ariaActivedescendant
: undefined,
disabled: props.disabled,
spellcheck: false,
onKeydown,
}))
</script>
<template>
<div
role="group"
:aria-label="text.group(props.channelName)"
:class="cn(messageComposerVariants({ layout: props.layout }), props.class)"
>
<button
v-if="props.showAdd"
type="button"
:aria-label="text.add"
:disabled="props.disabled"
:class="messageComposerButtonVariants({ layout: props.layout })"
@click="emit('add')"
>
<svg viewBox="0 0 24 24" aria-hidden="true" class="h-5 w-5">
<path
fill="currentColor"
d="M11 5a1 1 0 1 1 2 0v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6z"
/>
</svg>
</button>
<slot name="leading" />
<div :class="messageComposerFieldVariants({ layout: props.layout })">
<slot
name="input"
:value="value"
:setValue="setValue"
:submit="submit"
:disabled="props.disabled"
:inputProps="inputProps"
>
<input
v-model="value"
type="text"
v-bind="inputProps"
class="w-full min-w-0 border-0 bg-transparent font-[family-name:var(--newt-font-sans)] text-[15px] text-newt-text-primary outline-none placeholder:text-newt-text-muted disabled:cursor-not-allowed"
/>
</slot>
<slot name="actions" />
</div>
<slot v-if="slots.trailing && !hasValue" name="trailing" />
<button
v-else
type="button"
:aria-label="text.send"
:disabled="props.disabled || !hasValue"
:class="
cn(
messageComposerButtonVariants({ layout: props.layout }),
hasValue && !props.disabled && sendReady[props.layout]
)
"
@click="submit"
>
<!-- The send control only lights up once there is something to send. -->
<svg viewBox="0 0 24 24" aria-hidden="true" class="h-5 w-5">
<path fill="currentColor" d="M3 20.5v-6l8-2.5-8-2.5v-6l19 8.5z" />
</svg>
</button>
</div>
</template>import { cva, type VariantProps } from "class-variance-authority"
export { default as MessageComposer } from "./MessageComposer.vue"
export const messageComposerVariants = cva(
"flex min-w-0 items-center gap-2 text-newt-text-muted",
{
variants: {
layout: {
default:
"min-h-11 rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-input-elevated px-4",
/* A phone: no frame, a pill field, round buttons beside it. */
narrow: "min-h-10",
},
},
defaultVariants: { layout: "default" },
}
)
export const messageComposerFieldVariants = cva(
"flex min-w-0 flex-1 items-center gap-2",
{
variants: {
layout: {
default: "",
narrow: "min-h-10 rounded-full bg-newt-bg-input-elevated pe-1 ps-4",
},
},
defaultVariants: { layout: "default" },
}
)
/**
* The style of the composer's icon buttons. Give it to a button you add, so
* it matches the built-in ones. In the narrow layout, use `layout: "narrow"`
* for a button beside the field and the default for one inside it.
*/
export const messageComposerButtonVariants = cva(
"inline-flex shrink-0 cursor-pointer items-center justify-center border-0 p-0 text-inherit hover:bg-newt-bg-hover hover:text-newt-text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link disabled:cursor-not-allowed disabled:opacity-50",
{
variants: {
layout: {
default: "h-8 w-8 rounded-[var(--newt-radius-sm)] bg-transparent",
narrow: "h-10 w-10 rounded-full bg-newt-bg-input-elevated",
},
},
defaultVariants: { layout: "default" },
}
)
/** The composer's own names. Pass `labels` to translate them. */
export interface MessageComposerLabels {
/** Accessible name of the whole bar, built from the channel name. */
group: (channelName: string) => string
/** Placeholder and name of the field, when `placeholder` is not set. */
placeholder: (channelName: string) => string
/** Accessible name of the add button. */
add: string
/** Accessible name of the send button. */
send: string
}
/** The English defaults. */
export const defaultMessageComposerLabels: MessageComposerLabels = {
group: (channelName) => `Message composer for #${channelName}`,
placeholder: (channelName) => `Message #${channelName}`,
add: "Add attachment",
send: "Send message",
}
export type MessageComposerLayout = NonNullable<
VariantProps<typeof messageComposerVariants>["layout"]
>
/** Attributes for the focusable field. Bind them on it with `v-bind`. */
export interface MessageComposerInputAttributes {
placeholder: string
"aria-label": string
role: "combobox" | undefined
autocomplete: "off" | undefined
"aria-controls": string | undefined
"aria-expanded": boolean | undefined
"aria-activedescendant": string | undefined
disabled: boolean
spellcheck: false
/** Enter submits, Escape and the arrow keys reach their events. */
onKeydown: (event: KeyboardEvent) => void
}
/** What the `#input` slot receives: the state of the built-in input. */
export interface MessageComposerInputProps {
value: string
/** Changes the value, as typing in the built-in input does. */
setValue: (value: string) => void
/** Sends the value, as Enter does. Does nothing while it is empty. */
submit: () => void
disabled: boolean
inputProps: MessageComposerInputAttributes
}Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Message Composer to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Message Composer to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { MessageComposer } from "@/components/ui/message-composer"
export function Example() {
return <MessageComposer />
}<script setup lang="ts">
import { MessageComposer } from "@/components/ui/message-composer"
</script>
<template>
<MessageComposer />
</template>The bar holds, in order: the add button, the leading controls, the field and the send button. The field holds the input, then the actions. Set showAdd to false (:show-add="false" in Vue) to remove the add button.
Enter sends the value. The send button is off while the value is empty or holds only spaces. Set disabled to turn off the input, the add button and the send button.
Icon buttons
messageComposerButtonVariants gives the style of the built-in icon buttons to a button of your own. Use it for a button in leading, actions or trailing, so it matches the add and send buttons. In the HTML flavour, give the button the class newt-composer__button.
<MessageComposer
channelName="general"
actions={
<button
type="button"
aria-label="Emoji"
className={messageComposerButtonVariants()}
>
<EmojiIcon />
</button>
}
/>
Examples
Your own field
renderInput in React and the #input slot in Vue replace the built-in input. The composer keeps its value, its keyboard handling and the state of the send button. The function or the slot receives:
value: the current value.setValue: changes the value, as typing does.submit: sends the value, asEnterdoes.disabled: whether the composer is off.inputProps: the placeholder, the label, the combobox attributes,disabledand the keydown handler.
Spread inputProps on the element that takes focus: {...inputProps} in React, v-bind="inputProps" in Vue. The keydown handler is onKeyDown in React and onKeydown in Vue. The field here draws the command as a chip and keeps a plain input for the last option.
newt-composer"use client"
import * as React from "react"
import { MessageComposer } from "@/registry/bases/newt/ui/message-composer"
import { SlashCommand } from "@/registry/bases/newt/ui/slash-command"
/*
* A field of its own: the command as a chip, then an input for its last
* option. Enter still submits, and the send button still follows the value.
*/
export default function MessageComposerInputDemo() {
const [reason, setReason] = React.useState("")
return (
<MessageComposer
channelName="moderation"
placeholder="Reason for the ban"
value={reason}
onValueChange={setReason}
onSubmit={() => setReason("")}
className="w-full max-w-md"
renderInput={({ value, setValue, inputProps }) => (
<>
<SlashCommand
name="ban"
subcommand="member"
options={[{ name: "user", value: "@username" }]}
className="shrink-0"
/>
<input
type="text"
{...inputProps}
value={value}
onChange={(event) => setValue(event.target.value)}
className="w-full min-w-0 border-0 bg-transparent font-sans text-[15px] text-newt-text-primary outline-none placeholder:text-newt-text-muted"
/>
</>
)}
/>
)
}<script setup lang="ts">
import { ref } from "vue"
import { MessageComposer } from "~~/registry/bases/newt/ui/message-composer"
import { SlashCommand } from "~~/registry/bases/newt/ui/slash-command"
/*
* A field of its own: the command as a chip, then an input for its last
* option. Enter still submits, and the send button still follows the value.
*/
const reason = ref("")
const options = [{ name: "user", value: "@username" }]
</script>
<template>
<MessageComposer
v-model="reason"
channel-name="moderation"
placeholder="Reason for the ban"
class="w-full max-w-md"
@submit="reason = ''"
>
<template #input="{ value, setValue, inputProps }">
<SlashCommand
name="ban"
subcommand="member"
:options="options"
class="shrink-0"
/>
<input
type="text"
v-bind="inputProps"
:value="value"
class="w-full min-w-0 border-0 bg-transparent font-sans text-[15px] text-newt-text-primary outline-none placeholder:text-newt-text-muted"
@input="setValue(($event.target as HTMLInputElement).value)"
/>
</template>
</MessageComposer>
</template>Narrow layout
Set layout="narrow" on a phone. The bar loses its frame, the field becomes a pill, and the buttons beside the field become circles. The composer does not pick the layout from its width, so a composer in a narrow sidebar keeps the default bar. Choose the layout from the width of the screen in your app.
Give a button beside the field messageComposerButtonVariants({ layout: "narrow" }). A button in actions sits inside the pill, so it keeps the default style.
trailing takes the place of the send button while the value is empty. The example puts a microphone there, and the send button comes back once there is something to send. trailing works in both layouts.
In the HTML flavour, add newt-composer--narrow to the bar and newt-composer__button--narrow to each button beside the field.
newt-composer"use client"
import * as React from "react"
import {
MessageComposer,
messageComposerButtonVariants,
} from "@/registry/bases/newt/ui/message-composer"
const AppsIcon = () => (
<svg viewBox="0 0 24 24" aria-hidden="true" className="h-5 w-5">
<path
fill="currentColor"
d="M4 4h7v7H4zm9 0h7v7h-7zM4 13h7v7H4zm9 0h7v7h-7z"
/>
</svg>
)
const EmojiIcon = () => (
<svg viewBox="0 0 24 24" aria-hidden="true" className="h-5 w-5">
<path
fill="currentColor"
d="M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zM8.5 8a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3zm7 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3zM12 18a5.5 5.5 0 0 1-5-3.2h10A5.5 5.5 0 0 1 12 18z"
/>
</svg>
)
const MicrophoneIcon = () => (
<svg viewBox="0 0 24 24" aria-hidden="true" className="h-5 w-5">
<path
fill="currentColor"
d="M12 2a3 3 0 0 0-3 3v6a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3zm-7 9a1 1 0 1 1 2 0 5 5 0 0 0 10 0 1 1 0 1 1 2 0 7 7 0 0 1-6 6.93V21h3a1 1 0 1 1 0 2H8a1 1 0 1 1 0-2h3v-3.07A7 7 0 0 1 5 11z"
/>
</svg>
)
/* A composer 360px wide, the width it has on a phone. */
export default function MessageComposerNarrowDemo() {
const [value, setValue] = React.useState("")
return (
<div className="w-full max-w-[360px]">
<MessageComposer
channelName="general"
layout="narrow"
value={value}
onValueChange={setValue}
onSubmit={() => setValue("")}
leading={
<button
type="button"
aria-label="Apps"
className={messageComposerButtonVariants({ layout: "narrow" })}
>
<AppsIcon />
</button>
}
actions={
<button
type="button"
aria-label="Emoji"
className={messageComposerButtonVariants()}
>
<EmojiIcon />
</button>
}
trailing={
<button
type="button"
aria-label="Record a voice message"
className={messageComposerButtonVariants({ layout: "narrow" })}
>
<MicrophoneIcon />
</button>
}
/>
</div>
)
}<script setup lang="ts">
import { ref } from "vue"
import {
MessageComposer,
messageComposerButtonVariants,
} from "~~/registry/bases/newt/ui/message-composer"
const value = ref("")
</script>
<!-- A composer 360px wide, the width it has on a phone. -->
<template>
<div class="w-full max-w-[360px]">
<MessageComposer
v-model="value"
channel-name="general"
layout="narrow"
@submit="value = ''"
>
<template #leading>
<button
type="button"
aria-label="Apps"
:class="messageComposerButtonVariants({ layout: 'narrow' })"
>
<svg viewBox="0 0 24 24" aria-hidden="true" class="h-5 w-5">
<path
fill="currentColor"
d="M4 4h7v7H4zm9 0h7v7h-7zM4 13h7v7H4zm9 0h7v7h-7z"
/>
</svg>
</button>
</template>
<template #actions>
<button
type="button"
aria-label="Emoji"
:class="messageComposerButtonVariants()"
>
<svg viewBox="0 0 24 24" aria-hidden="true" class="h-5 w-5">
<path
fill="currentColor"
d="M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zM8.5 8a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3zm7 0a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3zM12 18a5.5 5.5 0 0 1-5-3.2h10A5.5 5.5 0 0 1 12 18z"
/>
</svg>
</button>
</template>
<template #trailing>
<button
type="button"
aria-label="Record a voice message"
:class="messageComposerButtonVariants({ layout: 'narrow' })"
>
<svg viewBox="0 0 24 24" aria-hidden="true" class="h-5 w-5">
<path
fill="currentColor"
d="M12 2a3 3 0 0 0-3 3v6a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3zm-7 9a1 1 0 1 1 2 0 5 5 0 0 0 10 0 1 1 0 1 1 2 0 7 7 0 0 1-6 6.93V21h3a1 1 0 1 1 0 2H8a1 1 0 1 1 0-2h3v-3.07A7 7 0 0 1 5 11z"
/>
</svg>
</button>
</template>
</MessageComposer>
</div>
</template>API reference
MessageComposer
channelName?string
Channel the message goes to; drives the default placeholder and labels.
stringvalue?string | undefined
The value, for a controlled composer. Vue uses `v-model` in its place.
string | undefineddefaultValue?string | undefined
The first value of an uncontrolled composer. React only.
string | undefined""onValueChange?(value: string) => void | undefined
Called with each new value. Vue uses `v-model` in its place.
(value: string) => void | undefinedplaceholder?string | undefined
Placeholder and accessible name of the field. Defaults to `Message #` and the channel name.
string | undefinedonSubmit / @submit?(value: string) => void | undefined
Sent on Enter and on the send control.
(value: string) => void | undefinedonEscape / @escape?() => void | undefined
Called when Escape is pressed in the field.
() => void | undefinedonNavigate / @navigate?(direction: "up" | "down") => void | undefined
Arrow keys, for driving a suggestion list above the composer.
(direction: "up" | "down") => void | undefinedautocomplete?boolean | undefined
Wire the input up as a combobox for slash-command autocomplete.
boolean | undefinedfalseariaControls?string | undefined
Id of the suggestion list, while `autocomplete` is set.
string | undefinedariaExpanded?boolean | undefined
Whether the suggestion list is open.
boolean | undefinedariaActiveDescendant?string | undefined
Id of the highlighted suggestion. Vue spells it `ariaActivedescendant`.
string | undefineddisabled?boolean | undefined
Turns off the input, the add button and the send button.
boolean | undefinedfalselayout?"default" | "narrow" | undefined
`narrow` is the phone layout: no frame, a pill field and round buttons.
"default" | "narrow" | undefined"default"showAdd?boolean | undefined
Shows the add button.
boolean | undefinedtrueonAdd / @add?() => void | undefined
Called when the add button is pressed.
() => void | undefinedleading / #leading?React.ReactNode / Vue slot
Controls after the add button, before the field.
React.ReactNode / Vue slotrenderInput / #input?(props: MessageComposerInputProps) => ReactNode / Vue slot
Replaces the built-in input. Receives `value`, `setValue`, `submit`, `disabled` and `inputProps`.
(props: MessageComposerInputProps) => ReactNode / Vue slotactions / #actions?React.ReactNode / Vue slot
Extra controls at the end of the field, before the send button.
React.ReactNode / Vue slottrailing / #trailing?React.ReactNode / Vue slot
Shown in place of the send button while the value is empty.
React.ReactNode / Vue slotlabels?Partial<MessageComposerLabels> | undefined
Replaces the English names: `group` and `placeholder` (functions of the channel name), `add` and `send`. Omitted keys keep their default.
Partial<MessageComposerLabels> | undefineddefaultMessageComposerLabelsmessageComposerButtonVariants
layout?"default" | "narrow" | undefined
`default` is a square 32px button. `narrow` is a filled 40px circle, for a button beside the field in the narrow layout.
"default" | "narrow" | undefined"default"The registry exports MessageComposer and messageComposerButtonVariants, and the types MessageComposerInputProps and MessageComposerInputAttributes. The root renders a <div> and accepts its native attributes. React uses className, while Vue uses class and fallthrough attributes.
Accessibility
The bar is a group named after the channel. The field takes its accessible name from the placeholder, so keep the channel in it.
The bar, the field, the add button and the send button take their names from labels. Translating labels lists every English default.
A field of your own must take focus and carry inputProps, so it keeps the label, the combobox attributes and the keyboard handling. If the field is not an <input>, set its aria-disabled from disabled.
Give every icon button you add an aria-label. A button in trailing replaces the send button while the value is empty, so name it after what it does.
Tokens
Message Composer reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-composer.
--newt-border--newt-radius-md--newt-bg-input-elevated--newt-text-muted--newt-text-primary--newt-font-sans--newt-radius-sm--newt-bg-hover--newt-text-link--newt-brand--newt-radius-full--newt-on-accent--newt-brand-hover