Alert dialog example

Pending work, cancellation and retry.

An abortable local callback fails its first attempt and succeeds on retry. Cancel, Escape, reset and unmount invalidate pending results; hosts own service cancellation and rollback.

Complete React client module · MIT · Next.js and Vite

Interactive example

Pending work, cancellation and retry

An abortable local callback fails its first attempt and succeeds on retry. Cancel, Escape, reset and unmount invalidate pending results; hosts own service cancellation and rollback.

Keyboard

  • Cancel stays available during pending work. Escape also cancels the request.
  • A failed attempt keeps the dialog open and moves focus to Retry export.
  • Reset request starts a new confirmation session and aborts its pending callback.

States covered

  • pending
  • duplicate prevention
  • rejected callback
  • retry
  • AbortSignal
  • resetKey
  • noncooperative stale result
  • local sample task

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-alert-dialog-pending-recovery.json
Complete source
React / TypeScript
/*!
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 {useRef,useState} from 'react';
import {Button,Checkbox,UIProvider} from '@bld8/ui';
import {AlertDialog} from '@bld8/ui/alert-dialog';
function pause(signal:AbortSignal,hold:boolean,ignoreAbort:boolean){
  return new Promise<void>((resolve,reject)=>{
    const finish=()=>{signal.removeEventListener('abort',abort);resolve();};
    const timer=hold?undefined:setTimeout(finish,650);
    const abort=()=>{if(ignoreAbort)return;if(timer)clearTimeout(timer);reject(new DOMException('Cancelled','AbortError'));};
    if(signal.aborted)abort();else signal.addEventListener('abort',abort,{once:true});
  });
}
export default function PendingConfirmationExample(){
  const [session,setSession]=useState(0),[hold,setHold]=useState(false),[ignoreAbort,setIgnoreAbort]=useState(false),[requests,setRequests]=useState(0),[cancelled,setCancelled]=useState(0),[result,setResult]=useState('No export prepared'),[open,setOpen]=useState(false);
  const failures=useRef(0);
  return <UIProvider><div style={{display:'grid',gap:16,maxWidth:520}}>
    <AlertDialog trigger={<Button>Prepare export</Button>} open={open} onOpenChange={setOpen} title="Prepare a local export?" description="The first completed sample request fails. Retry uses the same confirmation; Cancel keeps your current data." confirmLabel="Retry export" pendingLabel="Preparing export…" resetKey={session} errorMessage={error=>error instanceof Error?error.message:'Export failed. Try again.'} onConfirm={async({signal})=>{
      setRequests(count=>count+1);try{await pause(signal,hold,ignoreAbort);}catch(error){if(signal.aborted)setCancelled(count=>count+1);throw error;}
      if(signal.aborted)return;
      if(failures.current++===0)throw new Error('The sample export failed. Retry to recover.');
      setResult('Local export prepared');
    }}>{({pending})=><div style={{display:'grid',gap:12}}>
      <Checkbox label="Hold request until cancelled" checked={hold} onCheckedChange={setHold} disabled={pending}/>
      <Checkbox label="Let the sample finish after cancellation" checked={ignoreAbort} onCheckedChange={setIgnoreAbort} disabled={pending||hold}/>
      <Button variant="secondary" onClick={()=>setSession(value=>value+1)}>Reset request</Button>
      <p className="bui-hint">These are local sample tasks. An aborted signal prevents this example from changing its data; a real service must define its own cancellation and transaction rules.</p>
    </div>}</AlertDialog>
    <p role="status" aria-label="Export result">{result}</p><p aria-label="Export request count">Requests: {requests}</p><p aria-label="Export cancellation count">Aborted tasks: {cancelled}</p>
  </div></UIProvider>;
}