kai-link-preview
Turns a bare URL into an image, a title, and a description without fetching anything itself.
- Shadow DOM
- Pure by default — never fetches
- Optional backend fetcher
- Graceful image fallback
- Card Contract events
Preview
Section titled “Preview”Set data in JavaScript (it’s an object, not an attribute):
<kai-link-preview id="lp"></kai-link-preview>
<script type="module"> import '@kitn.ai/ui/web-components';
await customElements.whenDefined('kai-link-preview');
const lp = document.getElementById('lp'); lp.cardId = 'card-link-1'; lp.data = { url: 'https://example.com/blog/generative-ui', title: 'Generative UI, explained', description: 'How agents render typed, themed cards in the chat.', image: 'https://example.com/og.png', favicon: 'https://example.com/favicon.ico', siteName: 'Example Blog', domain: 'example.com', };</script>data— every key excepturlis optional.cardId— set ascard-idattribute orcardIdproperty; correlates emitted events when multiple cards are in a<kai-cards>host.- Pure by default — the element never makes network calls. For bare
{ url }, callconfigureLinkPreview({ fetchMetadata })once at startup to point at your backend. - Click dispatches a bubbling
kai-cardCustomEvent with{ kind: 'open', url, target: 'tab' }. A missing or broken image hides the image region silently; an invalidurlrenders a non-clickable “Invalid link” chip.
Examples
Section titled “Examples”Full Preview
Section titled “Full Preview”No Image
Section titled “No Image”With no image, the card falls back to the text-and-domain layout.
Bare URL with Fetcher
Section titled “Bare URL with Fetcher”With only { url }, the card shows a skeleton until configureLinkPreview resolves metadata from your backend:
import { configureLinkPreview } from '@kitn.ai/ui';
configureLinkPreview({ fetchMetadata: (url) => fetch('/api/og?url=' + encodeURIComponent(url)).then((r) => r.json()),});Invalid Link
Section titled “Invalid Link”An invalid url renders a non-clickable “Invalid link” chip and emits one error event through the kai-card envelope.
| Property | Type | Default | Notes |
|---|---|---|---|
| theme | "light" | "dark" | "auto" | 'auto' | Color mode (`auto` follows prefers-color-scheme). |
| cardId | string | — | Stable card id correlating every emitted event. Set as an attribute or property. |
| data | — | The link payload (OG metadata). Set as a JS **property** (object). |
Composed from
Section titled “Composed from”This element wraps these SolidJS components — reach for them directly when you need finer control than the props expose.
LinkPreview