Controlled visual reference preview
Control the preview with an explicit trigger ID and observe its lifecycle. Outside show and hide actions demonstrate host state while the linked reference stays independently usable.
Keyboard
- Tab reaches the reference link and the separate show and hide actions.
- Enter on the link navigates; Enter or Space on a preview action changes visual preview state.
- Escape dismisses the card. Focus remains in the surrounding page.
States covered
- Controlled open state
- Explicit trigger association
- Open and close completion
- Actions outside the popup
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-preview-controlled.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 { useId, useState } from 'react';
import { Button, Checkbox } from '@bld8/ui/core';
import { PreviewCard, PreviewCardTrigger, PreviewCardPopup } from '@bld8/ui/preview-card';
export function ControlledReferencePreviewExample() {
const triggerId = useId();
const [open, setOpen] = useState(false);
const [complete, setComplete] = useState('No transition yet');
return <div style={{ width: '100%', display: 'grid', gap: 20 }}>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 12 }}><Button variant="secondary" disabled={open} onClick={() => setOpen(true)}>Show visual preview</Button><Button variant="secondary" disabled={!open} onClick={() => setOpen(false)}>Hide visual preview</Button></div>
<PreviewCard open={open} triggerId={triggerId} onOpenChange={setOpen} onOpenChangeComplete={value => setComplete(value ? 'Preview entered' : 'Preview exited')}>
<p style={{ margin: 0 }}>Read the <PreviewCardTrigger id={triggerId} href="#preview-controlled-reference" delay={400}>source update checklist</PreviewCardTrigger> before replacing customised files.</p>
<PreviewCardPopup className="controlled-preview" side="bottom" collisionAvoidance={{ side: 'none', align: 'shift' }}><p className="bui-preview-title">Source update checklist</p><p className="bui-preview-description" style={{ marginTop: 8 }}>Compare local changes, retain licence notices and verify the updated interaction in your application.</p><p className="bui-preview-meta">Reference · 3 review steps</p></PreviewCardPopup>
</PreviewCard>
<p role="status">{open ? 'Visual preview open' : 'Visual preview closed'} · {complete}</p>
<section id="preview-controlled-reference"><strong>Source update checklist</strong><ol className="bui-hint"><li>Compare upstream changes with your local customisations.</li><li>Retain copyright, permission and dependency notices.</li><li>Verify keyboard paths, failure recovery and responsive layout in your application.</li></ol></section>
</div>;
}