refactor Modal, use snippet for actions

This commit is contained in:
Benjamin Palko 2025-03-14 23:47:55 -04:00
parent bf38f566c0
commit 727e2c1e4c
6 changed files with 27 additions and 44 deletions

View file

@ -1,10 +1,8 @@
<script module lang="ts"> <script module lang="ts">
import { defineMeta } from '@storybook/addon-svelte-csf'; import { defineMeta } from '@storybook/addon-svelte-csf';
import type { ComponentProps } from 'svelte'; import type { ComponentProps } from 'svelte';
import Button from './Button.svelte';
import Modal from './Modal.svelte'; import Modal from './Modal.svelte';
import ModalBody from './ModalBody.svelte';
import ModalActions from './ModalActions.svelte';
import Button from '../Button.svelte';
const { Story } = defineMeta({ const { Story } = defineMeta({
title: 'Actions/Modal', title: 'Actions/Modal',
@ -23,13 +21,11 @@
{#snippet template({ children: _, ...props }: Partial<ComponentProps<typeof Modal>>)} {#snippet template({ children: _, ...props }: Partial<ComponentProps<typeof Modal>>)}
<Button onclick={() => dialog?.showModal()}>Open</Button> <Button onclick={() => dialog?.showModal()}>Open</Button>
<Modal {...props} backdrop bind:dialog> <Modal {...props} backdrop bind:dialog>
<ModalBody> <h3 class="text-lg font-bold">Hello!</h3>
<h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">Press ESC key or click the button below to close</p>
<p class="py-4">Press ESC key or click the button below to close</p> {#snippet actions()}
<ModalActions> <Button onclick={() => dialog?.close()}>Close</Button>
<Button onclick={() => dialog?.close()}>Close</Button> {/snippet}
</ModalActions>
</ModalBody>
</Modal> </Modal>
{/snippet} {/snippet}

View file

@ -1,18 +1,34 @@
<script lang="ts"> <script lang="ts">
import clsx from 'clsx'; import clsx from 'clsx';
import type { Snippet } from 'svelte';
import type { SvelteHTMLElements } from 'svelte/elements'; import type { SvelteHTMLElements } from 'svelte/elements';
import { twMerge } from 'tailwind-merge'; import { twMerge } from 'tailwind-merge';
type Props = SvelteHTMLElements['dialog'] & { type Props = SvelteHTMLElements['dialog'] & {
actions?: Snippet;
backdrop?: boolean; backdrop?: boolean;
dialog?: HTMLDialogElement; dialog?: HTMLDialogElement;
}; };
let { backdrop, dialog = $bindable(), children, class: className, ...props }: Props = $props(); let {
actions,
backdrop,
dialog = $bindable(),
children,
class: className,
...props
}: Props = $props();
</script> </script>
<dialog {...props} class={twMerge('modal', clsx(className))} bind:this={dialog}> <dialog {...props} class={twMerge('modal', clsx(className))} bind:this={dialog}>
{@render children?.()} <div class="modal-box">
{@render children?.()}
{#if actions}
<div class="modal-action">
{@render actions()}
</div>
{/if}
</div>
{#if backdrop} {#if backdrop}
<form method="dialog" class="modal-backdrop"> <form method="dialog" class="modal-backdrop">
<button>close</button> <button>close</button>

View file

@ -1,13 +0,0 @@
<script lang="ts">
import clsx from 'clsx';
import type { SvelteHTMLElements } from 'svelte/elements';
import { twMerge } from 'tailwind-merge';
type Props = SvelteHTMLElements['div'];
let { children, class: className, ...props }: Props = $props();
</script>
<div {...props} class={twMerge('modal-action', clsx(className))}>
{@render children?.()}
</div>

View file

@ -1,13 +0,0 @@
<script lang="ts">
import clsx from 'clsx';
import type { SvelteHTMLElements } from 'svelte/elements';
import { twMerge } from 'tailwind-merge';
type Props = SvelteHTMLElements['div'];
let { children, class: className, ...props }: Props = $props();
</script>
<div {...props} class={twMerge('modal-box', clsx(className))}>
{@render children?.()}
</div>

View file

@ -1,3 +0,0 @@
export { default as Modal } from './Modal.svelte';
export { default as ModalActions } from './ModalActions.svelte';
export { default as ModalBody } from './ModalBody.svelte';

View file

@ -1,2 +1,2 @@
export { default as Button } from './Button.svelte'; export { default as Button } from './Button.svelte';
export * from './Modal/'; export { default as Modal } from './Modal.svelte';