feat: improve form usability with auto-focus, character limits, and unsaved changes warnings for better user experience

This commit is contained in:
Usman Baig
2026-02-22 20:02:50 +01:00
parent 5d234b30d6
commit da0366603e
6 changed files with 96 additions and 2 deletions

View File

@@ -0,0 +1,24 @@
'use client'
import { useEffect, useCallback } from 'react'
/**
* Warns users with a browser prompt when they try to navigate away
* or close the tab while there are unsaved form changes.
*
* @param isDirty - Whether the form has unsaved changes
*/
export function useUnsavedChanges(isDirty: boolean) {
const handleBeforeUnload = useCallback(
(e: BeforeUnloadEvent) => {
if (!isDirty) return
e.preventDefault()
},
[isDirty]
)
useEffect(() => {
window.addEventListener('beforeunload', handleBeforeUnload)
return () => window.removeEventListener('beforeunload', handleBeforeUnload)
}, [handleBeforeUnload])
}