Abortable reference metadata
Load optional metadata only while the preview is open. Closing or retrying cancels the previous request; recovery controls and the complete reference remain outside the popup.
Keyboard
- The reference link remains navigable while metadata loads or fails.
- Use the outside Simulate failure checkbox and Retry metadata button to inspect recovery.
- Escape closes the visual preview and cancels pending metadata.
States covered
- Loading
- Resolved metadata
- Recoverable failure
- Abort on close and retry
- Stale result protection
- Host-owned service boundary
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-preview-async-reference.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useEffect, useState } from 'react';
import { Button, Checkbox } from '@bld8/ui/core';
import { PreviewCard, PreviewCardTrigger, PreviewCardPopup } from '@bld8/ui/preview-card';
type Metadata = { title: string; updated: string; summary: string };
function loadMetadata(signal: AbortSignal, fail: boolean): Promise<Metadata> {
// Local timed adapter. Replace with fetch(url, { signal }) and validate the response.
return new Promise((resolve, reject) => {
const cancel = () => { clearTimeout(timer); reject(new DOMException('Aborted', 'AbortError')); };
const timer = setTimeout(() => {
signal.removeEventListener('abort', cancel);
if (fail) reject(new Error('Metadata could not be loaded. The reference link still works.'));
else resolve({ title: 'Interaction review', updated: 'Updated 12 November 2026', summary: 'Check focus entry and return, interrupted transitions and live reduced-motion settings.' });
}, 650);
if (signal.aborted) cancel(); else signal.addEventListener('abort', cancel, { once: true });
});
}
export function AsyncReferencePreviewExample() {
const [open, setOpen] = useState(false);
const [fail, setFail] = useState(false);
const [attempt, setAttempt] = useState(0);
const [result, setResult] = useState<{ status: 'idle' | 'loading' | 'ready' | 'error'; data?: Metadata; error?: string }>({ status: 'idle' });
useEffect(() => {
if (!open) return;
const controller = new AbortController();
setResult({ status: 'loading' });
loadMetadata(controller.signal, fail).then(data => {
if (!controller.signal.aborted) setResult({ status: 'ready', data });
}).catch(error => {
if (!controller.signal.aborted) setResult({ status: 'error', error: error instanceof Error ? error.message : 'Metadata unavailable.' });
});
return () => controller.abort();
}, [open, fail, attempt]);
return <div style={{ width: '100%', display: 'grid', gap: 20 }}>
<PreviewCard open={open} onOpenChange={setOpen}>
<p style={{ margin: 0 }}>Review the <PreviewCardTrigger href="#preview-async-reference-details" delay={300}>interaction checklist</PreviewCardTrigger> with the component change.</p>
<PreviewCardPopup className="async-reference-preview">
<p className="bui-preview-title">{result.data?.title ?? 'Interaction review'}</p>
{result.status === 'loading' && <p className="bui-preview-description">Loading optional metadata…</p>}
{result.status === 'error' && <p className="bui-preview-description">{result.error}</p>}
{result.status === 'ready' && <><p className="bui-preview-description">{result.data?.summary}</p><p className="bui-preview-meta">{result.data?.updated}</p></>}
</PreviewCardPopup>
</PreviewCard>
<Checkbox label="Simulate metadata failure" checked={fail} onCheckedChange={setFail} />
<Button variant="secondary" onClick={() => { setAttempt(value => value + 1); setOpen(true); }}>Retry metadata</Button>
<p className="bui-hint">Local timed sample, no network service. Preview metadata is {open ? result.status : 'closed'}.</p>
<section id="preview-async-reference-details"><strong>Interaction review</strong><p className="bui-hint">Updated 12 November 2026. Check focus entry and return, interrupted transitions and live reduced-motion settings. Confirm that the same information remains available when a visual preview is closed or unavailable.</p></section>
</div>;
}