Skip to content

Commit 9a1d380

Browse files
committed
docs(composed-modal): add "Prevent default close behavior" example
1 parent 8e2b930 commit 9a1d380

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

docs/src/pages/components/ComposedModal.svx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Create a modal with a header, body, and footer. Each section can be customized i
1414

1515
<FileSource src="/framed/Modal/ComposedModal" />
1616

17+
## Prevent default close behavior
18+
19+
The modal dispatches a cancelable `close` event, allowing you to prevent the modal from closing using `e.preventDefault()`. The event includes a `trigger` property indicating what triggered the close attempt: `"escape-key"`, `"outside-click"`, or `"close-button"`.
20+
21+
<FileSource src="/framed/Modal/ComposedModalPreventClose" />
22+
1723
## Multiple secondary buttons
1824

1925
Set `secondaryButtons` in `ModalFooter` to create a 3-button modal. This prop replaces `secondaryButtonText` and takes a tuple of two button configurations.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script>
2+
import {
3+
Button,
4+
ComposedModal,
5+
ModalHeader,
6+
ModalBody,
7+
ModalFooter,
8+
InlineNotification,
9+
} from "carbon-components-svelte";
10+
11+
let open = false;
12+
let hasUnsavedChanges = true;
13+
let showWarning = false;
14+
</script>
15+
16+
<Button
17+
on:click={() => {
18+
open = true;
19+
hasUnsavedChanges = true;
20+
showWarning = false;
21+
}}>Open modal with unsaved changes</Button
22+
>
23+
24+
<ComposedModal
25+
bind:open
26+
on:close={(e) => {
27+
console.log("Close triggered by:", e.detail.trigger);
28+
if (hasUnsavedChanges) {
29+
e.preventDefault();
30+
showWarning = true;
31+
}
32+
}}
33+
on:submit={() => {
34+
hasUnsavedChanges = false;
35+
open = false;
36+
}}
37+
>
38+
<ModalHeader title="Edit profile" />
39+
<ModalBody>
40+
{#if showWarning}
41+
<InlineNotification
42+
kind="warning"
43+
title="Unsaved changes"
44+
subtitle="Please save or discard your changes before closing."
45+
hideCloseButton
46+
/>
47+
{/if}
48+
<p>You have unsaved changes. Click "Save" to apply them.</p>
49+
</ModalBody>
50+
<ModalFooter
51+
primaryButtonText="Save"
52+
secondaryButtonText="Discard changes"
53+
on:click:button--secondary={async () => {
54+
hasUnsavedChanges = false;
55+
showWarning = false;
56+
open = false;
57+
}}
58+
/>
59+
</ComposedModal>

0 commit comments

Comments
 (0)