Quick start
This guide builds a small but complete chat interface with message history, a composer, safe text rendering, and a simulated assistant response.
Try the finished result
Section titled “Try the finished result”The preview uses no backend. It handles loquix-submit, appends the user message, briefly enters streaming mode, and adds a local response.
Build it
Section titled “Build it”-
Install Loquix.
Terminal window npm install @loquix/core lit -
Register the components and tokens.
Add these imports to your browser entry point:
main.js import '@loquix/core/tokens/variables.css';import '@loquix/core/define/define-chat-container';import '@loquix/core/define/define-chat-header';import '@loquix/core/define/define-message-list';import '@loquix/core/define/define-message-item';import '@loquix/core/define/define-message-content';import '@loquix/core/define/define-chat-composer'; -
Compose the chat surface.
index.html <loquix-chat-container id="chat" layout="full" mode="chat"><loquix-chat-headerslot="header"agent-name="My Assistant"></loquix-chat-header><loquix-message-listid="messages"slot="messages"auto-scrollshow-scroll-anchor><loquix-message-item sender="assistant" status="complete"><loquix-message-content>Hello! How can I help?</loquix-message-content></loquix-message-item></loquix-message-list><loquix-chat-composerid="composer"slot="composer"placeholder="Ask anything…"max-length="4000"></loquix-chat-composer></loquix-chat-container> -
Handle submitted messages.
Append untrusted text with
textContent, notinnerHTML:main.js const chat = document.querySelector('#chat');const messages = document.querySelector('#messages');const composer = document.querySelector('#composer');function appendMessage(sender, text) {const message = document.createElement('loquix-message-item');message.setAttribute('sender', sender);message.setAttribute('status', 'complete');const content = document.createElement('loquix-message-content');content.textContent = text;message.append(content);messages.append(message);}chat.addEventListener('loquix-submit', event => {const { content } = event.detail;appendMessage('user', content);composer.streaming = true;window.setTimeout(() => {appendMessage('assistant', `You sent: ${content}`);composer.streaming = false;}, 500);});
Connect your backend
Section titled “Connect your backend”Replace the simulated timeout with your application request:
chat.addEventListener('loquix-submit', async event => { const { content } = event.detail; appendMessage('user', content); composer.streaming = true;
try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ content }), });
if (!response.ok) throw new Error('Request failed'); const result = await response.json(); appendMessage('assistant', result.content); } catch { appendMessage('assistant', 'Something went wrong. Please try again.'); } finally { composer.streaming = false; }});Where to go next
Section titled “Where to go next”- Use Events and state to connect streaming and application state.
- Grow the
fetchcall above into something that streams, aborts, and tracks conversation history for you: the Integration section covers the agent provider and controller built for exactly that. - Apply your brand in Theming.
- Review Accessibility before shipping.
- Browse the component catalog.