Skip to content

Commit a73d838

Browse files
committed
feat: close <dialog> when clicking outside
1 parent 76534d1 commit a73d838

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/App.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ export default function App() {
1515

1616
return (
1717
<div>
18-
<dialog ref={dialogRef}>
18+
<dialog
19+
ref={dialogRef}
20+
onClick={(e) => {
21+
const rect = e.currentTarget.getBoundingClientRect()
22+
if (
23+
e.clientY < rect.top ||
24+
e.clientY > rect.bottom ||
25+
e.clientX < rect.left ||
26+
e.clientX > rect.right
27+
) {
28+
e.currentTarget.close()
29+
}
30+
}}
31+
>
1932
<div className='header'>
2033
<button onClick={() => dialogRef.current?.close()}>X</button>
2134
</div>

0 commit comments

Comments
 (0)