Alert dialog example

Confirm leaving an unsaved note.

Keep editing preserves the draft. Discard restores the last local save and returns focus to an explicit Open editor action.

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

Interactive example

Confirm leaving an unsaved note

Keep editing preserves the draft. Discard restores the last local save and returns focus to an explicit Open editor action.

Keyboard

  • Leave editor opens only after the note changes; Keep editing receives focus.
  • Escape preserves the draft and returns to Leave editor.
  • Discard changes restores the saved text and focuses Open editor. This example does not intercept browser navigation.

States covered

  • dirty draft
  • safe cancellation
  • explicit discard
  • custom final focus
  • local save
  • host navigation boundary

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-alert-dialog-dirty-exit.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,Field,Textarea,UIProvider} from '@bld8/ui';
import {AlertDialog} from '@bld8/ui/alert-dialog';
export default function DirtyExitExample(){
  const [saved,setSaved]=useState('Review before publishing.'),[draft,setDraft]=useState('Review before publishing.'),[editing,setEditing]=useState(true),[status,setStatus]=useState('No unsaved changes');
  const reopen=useRef<HTMLButtonElement>(null),dirty=draft!==saved;
  return <UIProvider><div style={{display:'grid',gap:16,maxWidth:520}}>
    <Field label="Editor note"><Textarea value={draft} disabled={!editing} onChange={event=>{setDraft(event.target.value);setStatus('Draft changed');}}/></Field>
    <div style={{display:'flex',gap:8,flexWrap:'wrap'}}><Button disabled={!editing||!dirty} onClick={()=>{setSaved(draft);setStatus('Note saved locally');}}>Save note</Button>
      <AlertDialog trigger={<Button variant="secondary" disabled={!editing||!dirty}>Leave editor</Button>} title="Discard your unsaved changes?" description="Your current draft differs from the last local save. Keep editing preserves it; Discard changes restores the saved note." confirmLabel="Discard changes" cancelLabel="Keep editing" destructive finalFocus={()=>editing?true:reopen.current} onConfirm={()=>{setDraft(saved);setEditing(false);setStatus('Changes discarded; editor closed');}}/>
      <Button ref={reopen} variant="ghost" onClick={()=>{setEditing(true);setStatus('Editor open');}}>Open editor</Button></div>
    <p role="status">{status}</p><p className="bui-hint">The host decides when navigation needs confirmation. This local example does not install route or browser unload guards.</p>
  </div></UIProvider>;
}