Composer
The message field: a growing textarea with attach, send and stop, and a drop zone for files.
Example
<focsd-composer id="composer-demo" placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer>
<script type="module">const composer = document.getElementById('composer-demo');composer.addEventListener('focsd-submit', (event) => console.log(event.detail.text, event.detail.files));</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/ai';</script>
<focsd-composer placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer>import { useEffect, useRef } from 'react';import '@focsd-dev/ai';
export function Example() { const ref = useRef(null); return <focsd-composer ref={ref} placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer>;}<script setup>import '@focsd-dev/ai';</script>
<template> <focsd-composer placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/ai';
// template<focsd-composer placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer><script> import '@focsd-dev/ai';</script>
<focsd-composer placeholder="Ask anything" accept="image/*,.pdf,.txt"></focsd-composer>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
value | value | string | — |
placeholder | placeholder | string | — |
label | label | string | — |
busy | busy | boolean | false |
disabled | disabled | boolean | false |
accept | accept | string | — |
max-files | maxFiles | number | 10 |
max-file-size | maxFileSize | number | 0 |
Methods
| Method | Description |
|---|---|
focus(options) | Focus the textarea (not the first button, as delegated focus would). |
addFiles(list) | Check and attach files. Rejected files are not added; their names and reasons are announced and shown. Does nothing while disabled. |
Events
| Event | Description |
|---|---|
input | The text changed while the person types (the textarea's native event). |
CSS parts
Style the inside of the component with ::part(), for example focsd-composer::part(field).
| Part | Description |
|---|---|
field | The whole composer; it accepts dropped files (`dragging` while files are dragged over it). |
dragging | Added to the field while files are dragged over it. |
drop-text | The "Drop files to attach" text shown while dragging. |
attachments | The list of attached files. |
error | Why the last files were not attached. |
textarea | The textarea. |
attach | The attach button. |
send | The send button. |
stop | The same button while `busy`. |
icon | The icons in the buttons. |
Accessibility
- A native
<textarea>named bylabel(default "Message"). It grows to eight lines. Enter sends; Shift+Enter adds a line. Nothing is sent while an input method (IME) composes, so choosing a candidate with Enter never sends. - While
busy, the send button becomes Stop in the same place and keeps focus, so Tab from the field reaches it. It firesfocsd-stop. Enter does nothing while busy. - Send, stop, attach and remove are at least 44 by 44 px. Send is
aria-disabledrather thandisabledwhen there is nothing to send, so it keeps focus. - Files can be attached with the button, pasted or dropped on the composer (an outline and "Drop files to attach" show while dragging). A rejected file (type, size or count) is announced assertively with the reason and also shown as text next to the field.
focsd-submitis cancelable: cancel it to keep the text and files. It sends the text as typed, withfilesasFileobjects.
Keyboard
| Key | Action |
|---|---|
| Enter | Send (not while composing text with an input method) |
| Shift + Enter | New line |
| Tab | Move to send or stop |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.