Skip to content

Commit 7caf87d

Browse files
Harden delete confirmation accessibility
1 parent faa37e6 commit 7caf87d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/components/DeleteButton/index.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function DeleteButton({ onConfirm, size, text }) {
1414
const [waitConfirm, setWaitConfirm] = useState(false)
1515
const confirmRef = useRef(null)
1616
const [confirming, setConfirming] = useState(false)
17+
const isMountedRef = useRef(true)
18+
19+
useEffect(() => {
20+
isMountedRef.current = true
21+
return () => {
22+
isMountedRef.current = false
23+
}
24+
}, [])
1725

1826
useEffect(() => {
1927
if (waitConfirm) confirmRef.current.focus()
@@ -31,12 +39,14 @@ function DeleteButton({ onConfirm, size, text }) {
3139
}}
3240
disabled={confirming}
3341
aria-busy={confirming ? 'true' : 'false'}
42+
aria-hidden={waitConfirm ? undefined : 'true'}
43+
tabIndex={waitConfirm ? 0 : -1}
3444
onMouseDown={(e) => {
3545
e.preventDefault()
3646
e.stopPropagation()
3747
}}
3848
onBlur={() => {
39-
if (!confirming) setWaitConfirm(false)
49+
if (!confirming && isMountedRef.current) setWaitConfirm(false)
4050
}}
4151
onClick={async (e) => {
4252
if (confirming) return
@@ -45,13 +55,13 @@ function DeleteButton({ onConfirm, size, text }) {
4555
setConfirming(true)
4656
try {
4757
await onConfirm()
48-
setWaitConfirm(false)
58+
if (isMountedRef.current) setWaitConfirm(false)
4959
} catch (err) {
5060
// Keep confirmation visible to allow retry; optionally log
5161
// eslint-disable-next-line no-console
5262
console.error(err)
5363
} finally {
54-
setConfirming(false)
64+
if (isMountedRef.current) setConfirming(false)
5565
}
5666
}}
5767
>

0 commit comments

Comments
 (0)