Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modal escape event #92

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions src/lib/components/Modal/Modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { type Snippet } from 'svelte';
import { tv } from 'tailwind-variants';

type Props = Dialog.RootProps & {
type Props = {
title: string;
size?: ModalSize;
class?: string;
Expand All @@ -26,11 +26,10 @@
let {
title,
size = 'medium',
open = $bindable(true),
open = true,
onClose,
class: className,
children,
...restProps
}: Props = $props();

const modalStyles = tv({
Expand All @@ -51,14 +50,14 @@
const bodyChildren = $derived(getChildSnippet(ChildKey.ModalBody));
const footerChildren = $derived(getChildSnippet(ChildKey.ModalFooter));

const handleClose = () => {
open = false;
onClose?.();
restProps?.onOpenChange?.(false);
const onChange = (value: boolean) => {
if (!value) {
onClose?.();
}
};
</script>

<Dialog.Root bind:open {...restProps}>
<Dialog.Root {open} onOpenChange={onChange}>
<Dialog.Portal>
<Dialog.Overlay class="absolute left-0 top-0 flex h-dvh w-screen backdrop-blur" />
<Dialog.Content
Expand All @@ -71,7 +70,9 @@
<CardHeader class="border-0 py-2">
<div class="flex items-center justify-between">
<CardTitle>{title}</CardTitle>
<CloseButton size="large" onclick={handleClose} />
<Dialog.Close>
<CloseButton size="large" onclick={() => onChange(false)} />
</Dialog.Close>
</div>
</CardHeader>

Expand Down
12 changes: 7 additions & 5 deletions src/routes/components/modal/BasicExample.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

<Button onclick={() => (open = true)}>Open</Button>

<Modal title="Modal Title" bind:open>
<ModalBody>
<Lorem />
</ModalBody>
</Modal>
{#if open}
<Modal title="Modal Title" onClose={() => (open = false)}>
<ModalBody>
<Lorem />
</ModalBody>
</Modal>
{/if}
38 changes: 20 additions & 18 deletions src/routes/components/modal/SizeExample.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
<Button onclick={() => handleOpen('full')}>Full</Button>
</HStack>

<Modal title="Modal Title" {size} bind:open>
<ModalBody>
<Stack gap={4}>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
</Stack>
</ModalBody>
<ModalFooter>
<div class="grid w-full grid-cols-1 gap-2 sm:grid-cols-2">
<Button onclick={handleClose} shape="round">Button 1</Button>
<Button onclick={handleClose} shape="round" color="secondary">Button 2</Button>
</div>
</ModalFooter>
</Modal>
{#if open}
<Modal title="Modal Title" {size} onClose={() => (open = false)}>
<ModalBody>
<Stack gap={4}>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
<Text><Lorem /></Text>
</Stack>
</ModalBody>
<ModalFooter>
<div class="grid w-full grid-cols-1 gap-2 sm:grid-cols-2">
<Button onclick={handleClose} shape="round">Button 1</Button>
<Button onclick={handleClose} shape="round" color="secondary">Button 2</Button>
</div>
</ModalFooter>
</Modal>
{/if}
Loading