Download Trigger
Download Trigger (다운로드 트리거)
DownloadTrigger 컴포넌트는 웹 애플리케이션에서 파일 다운로드를 프로그래밍 방식으로 트리거할 수 있게 해주는 편리한 방법을 제공해요. URL, Blob 등 다양한 데이터 타입의 파일 다운로드 복잡성을 처리해 줘요.
출처: 문서
본문
동기 (Motivation)
DownloadTrigger 컴포넌트는 웹 애플리케이션에서 파일 다운로드를 프로그래밍 방식으로 트리거하는 편리한 방법을 제공해요. URL, Blob 또는 기타 데이터 타입이든 파일 다운로드의 복잡성을 처리해 줘요.
예제 (Examples)
기본 (Basic)
다운로드할 데이터를 data prop에 전달하고, 파일의 fileName과 mimeType을 지정해 주세요.
import { DownloadTrigger } from '@ark-ui/react/download-trigger'
import { DownloadIcon, FileIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/download-trigger.module.css'
const content = 'Hello, World! This is a sample text file.'
export const Basic = () => {
return (
<div className={styles.Root}>
<div className={styles.Preview}>
<FileIcon />
<span className={styles.PreviewText}>{content}</span>
</div>
<DownloadTrigger className={button.Root} data={content} fileName="hello.txt" mimeType="text/plain">
<DownloadIcon /> Download txt
</DownloadTrigger>
</div>
)
}
SVG 다운로드 (Download SVG)
SVG 파일을 다운로드하는 예시를 보여 드릴게요.
import { DownloadTrigger } from '@ark-ui/react/download-trigger'
import { DownloadIcon, FileIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/download-trigger.module.css'
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red"/>
</svg>`
export const Svg = () => {
return (
<div className={styles.Root}>
<div className={styles.Preview}>
<FileIcon />
<span className={styles.PreviewText}>circle.svg</span>
</div>
<DownloadTrigger className={button.Root} data={svgContent} fileName="circle.svg" mimeType="image/svg+xml">
<DownloadIcon /> Download SVG
</DownloadTrigger>
</div>
)
}
Promise
Blob, File 또는 string을 반환하는 promise에서 다운로드를 트리거할 수도 있어요.
import { DownloadTrigger } from '@ark-ui/react/download-trigger'
import { DownloadIcon, ImageIcon } from 'lucide-react'
import button from 'styles/button.module.css'
import styles from 'styles/download-trigger.module.css'
export const WithPromise = () => {
const fetchImage = async () => {
const response = await fetch('https://picsum.photos/200/300')
return response.blob()
}
return (
<div className={styles.Root}>
<div className={styles.Preview}>
<ImageIcon />
<span className={styles.PreviewText}>random-image.jpg (fetched on download)</span>
</div>
<DownloadTrigger className={button.Root} data={fetchImage} fileName="random-image.jpg" mimeType="image/jpeg">
<DownloadIcon /> Download Image
</DownloadTrigger>
</div>
)
}
API 레퍼런스 (API Reference)
DownloadTrigger
Props
data
Type: DownloadableData | (() => MaybePromise<DownloadableData>)
Required: true
Default Value: undefined
Description: The data to download
fileName
Type: string
Required: true
Default Value: undefined
Description: The name of the file to download
mimeType
Type: FileMimeType
Required: true
Default Value: undefined
Description: The MIME type of the data to download
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.