다이얼로그 (Dialog)
기본 창이나 다른 다이얼로그 창 위에 겹쳐 표시되는 창으로, 아래 콘텐츠를 비활성(inert) 상태로 만들어요. 모달 형태로 사용자의 주의를 집중시킬 때 쓰여요.
출처: 문서
본문
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
import * as Field from "$lib/components/ui/field/index.js";
import { Button, buttonVariants } from "$lib/components/ui/button/index.js";
import { Input } from "$lib/components/ui/input/index.js";
import { Label } from "$lib/components/ui/label/index.js";
</script>
<Dialog.Root>
<form>
<Dialog.Trigger
type="button"
class={buttonVariants({ variant: "outline" })}
>
Open Dialog
</Dialog.Trigger>
<Dialog.Content class="sm:max-w-sm">
<Dialog.Header>
<Dialog.Title>Edit profile</Dialog.Title>
<Dialog.Description>
Make changes to your profile here. Click save when you're done.
</Dialog.Description>
</Dialog.Header>
<Field.Group>
<Field.Field>
<Label for="name-1">Name</Label>
<Input id="name-1" name="name" defaultValue="Pedro Duarte" />
</Field.Field>
<Field.Field>
<Label for="username-1">Username</Label>
<Input id="username-1" name="username" defaultValue="@peduarte" />
</Field.Field>
</Field.Group>
<Dialog.Footer>
<Dialog.Close
type="button"
class={buttonVariants({ variant: "outline" })}
>
Cancel
</Dialog.Close>
<Button type="submit">Save changes</Button>
</Dialog.Footer>
</Dialog.Content>
</form>
</Dialog.Root>
View Code
설치
pnpm dlx shadcn-svelte@latest add dialog
npx shadcn-svelte@latest add dialog
bun x shadcn-svelte@latest add dialog
사용법
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
</script>
<Dialog.Root>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Are you absolutely sure?</Dialog.Title>
<Dialog.Description>
This action cannot be undone. This will permanently delete your account
and remove your data from our servers.
</Dialog.Description>
</Dialog.Header>
</Dialog.Content>
</Dialog.Root>
커스텀 닫기 버튼
기본 닫기 컨트롤을 직접 만든 버튼으로 바꿔 사용할 수 있어요.
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
import { Input } from "$lib/components/ui/input/index.js";
import { Label } from "$lib/components/ui/label/index.js";
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: "outline" })}
>Share</Dialog.Trigger
>
<Dialog.Content class="sm:max-w-md">
<Dialog.Header>
<Dialog.Title>Share link</Dialog.Title>
<Dialog.Description
>Anyone who has this link will be able to view this.</Dialog.Description
>
</Dialog.Header>
<div class="flex items-center gap-2">
<div class="grid flex-1 gap-2">
<Label for="link" class="sr-only">Link</Label>
<Input
id="link"
defaultValue="https://shadcn-svelte.com/docs/installation"
readonly
/>
</div>
</div>
<Dialog.Footer class="sm:justify-start">
<Dialog.Close type="button" class={buttonVariants()}>Close</Dialog.Close>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
View Code
닫기 버튼 없애기
showCloseButton={false}를 사용하면 닫기 버튼을 숨길 수 있어요.
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: "outline" })}
>No Close Button</Dialog.Trigger
>
<Dialog.Content showCloseButton={false}>
<Dialog.Header>
<Dialog.Title>No Close Button</Dialog.Title>
<Dialog.Description>
This dialog doesn't have a close button in the top-right corner.
</Dialog.Description>
</Dialog.Header>
</Dialog.Content>
</Dialog.Root>
View Code
고정 푸터 (Sticky Footer)
콘텐츠가 스크롤되는 동안에도 액션 버튼을 계속 보이게 유지해요.
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: "outline" })}
>Sticky Footer</Dialog.Trigger
>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Sticky Footer</Dialog.Title>
<Dialog.Description>
This dialog has a sticky footer that stays visible while the content
scrolls.
</Dialog.Description>
</Dialog.Header>
<div class="-mx-4 no-scrollbar max-h-[50vh] overflow-y-auto px-4">
{#each { length: 10 } as _, i (i)}
<p class="mb-4 leading-normal">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
{/each}
</div>
<Dialog.Footer>
<Dialog.Close class={buttonVariants({ variant: "outline" })}
>Close</Dialog.Close
>
</Dialog.Footer>
</Dialog.Content>
</Dialog.Root>
View Code
스크롤되는 콘텐츠
긴 콘텐츠는 스크롤되고 헤더는 화면에 고정된 채로 남아 있어요.
<script lang="ts">
import * as Dialog from "$lib/components/ui/dialog/index.js";
import { buttonVariants } from "$lib/components/ui/button/index.js";
</script>
<Dialog.Root>
<Dialog.Trigger class={buttonVariants({ variant: "outline" })}
>Scrollable Content</Dialog.Trigger
>
<Dialog.Content>
<Dialog.Header>
<Dialog.Title>Scrollable Content</Dialog.Title>
<Dialog.Description
>This is a dialog with scrollable content.</Dialog.Description
>
</Dialog.Header>
<div class="-mx-4 no-scrollbar max-h-[50vh] overflow-y-auto px-4">
{#each { length: 10 } as _, i (i)}
<p class="mb-4 leading-normal">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
{/each}
</div>
</Dialog.Content>
</Dialog.Root>
View Code
더 알아보기 (Learn more)
원문 문서에서는 bits-ui의 Dialog 문서와 API 레퍼런스 링크를 통해 컴포넌트의 동작 원리와 전체 prop 목록을 더 자세히 살펴볼 수 있어요. 설치 방법이나 다른 컴포넌트와의 조합이 궁금하면 shadcn-svelte 공식 문서를 참고해 보세요.