Advanced
Hooks, keyboard shortcuts, and performance optimization. These utilities handle the finer details of building a production chat interface — scroll behavior, auto-resizing textareas, typing indicators, and more.
useAutoScroll
Keeps the message list scrolled to the bottom when new messages arrive, but only if the user hasn't scrolled up. Returns a containerRef to attach to the scroll container, scrollToBottom to imperatively jump down, isAtBottom for UI logic, and unseenCount to show a badge.
useAutoScroll(messages)useAutoResize
Automatically grows and shrinks a <textarea> to fit its content, up to a configurable maxRows limit. Returns a textareaRef and a resize function you call after each content change.
useTypingIndicator
Tracks whether the local user is actively typing and debounces the signal. Returns isTyping (current state), handleKeyDown (attach to the textarea), stopTyping (force stop), and accepts a debounceMs config (default 1500ms).
Keyboard Shortcuts
The composer and message list respond to these keyboard shortcuts by default:
| Shortcut | Action |
|---|---|
Enter | Send the current message |
Shift+Enter | Insert a newline without sending |
Escape | Cancel the active reply |
↑ in empty composer | Edit your last sent message |
Virtual Scrolling
For conversations with 10,000+ messages, rendering every DOM node will degrade performance. We recommend @tanstack/react-virtual for windowed rendering. It only mounts the messages visible in the viewport plus a configurable overscan buffer.
@tanstack/react-virtualSecurity Utilities
chatcn includes a handful of security helpers for sanitizing user-generated content:
| Utility | Description |
|---|---|
sanitizeUrl | Strips javascript: and data URIs from user-submitted links, returning a safe href or an empty string. |
validateFile | Checks a File object against allowed MIME types and a max size limit. Returns an error string or null. |
isValidEmoji | Validates that a string is a single Unicode emoji sequence, preventing injection of arbitrary text in reaction pickers. |
stripBidiOverrides | Removes Unicode bidirectional override characters (U+202A–U+202E, U+2066–U+2069) that can be used for text spoofing attacks. |