Signature Pad (서명 패드)
사용자가 서명 패드에 서명을 그릴 수 있게 해주는 컴포넌트예요. 마우스나 터치로 그린 서명을 이미지로 미리보기하거나 폼 값으로 제출할 수 있어요.
출처: 문서
본문
구조 (Anatomy)
서명 패드의 기본 구조는 다음과 같아요. Root 안에 Label, Control, 그리고 그리기 영역인 Segment와 지우기 트리거, 안내선을 배치해요.
<SignaturePad.Root>
<SignaturePad.Label />
<SignaturePad.Control>
<SignaturePad.Segment />
<SignaturePad.ClearTrigger />
<SignaturePad.Guide />
</SignaturePad.Control>
</SignaturePad.Root>
예제 (Examples)
기본 (Basic)
defaultValue로 선택될 항목을 지정해요. 각 SignaturePad.Item에는 고유한 value를 전달해요.
import { SignaturePad } from '@ark-ui/react/signature-pad'
import { RotateCcwIcon } from 'lucide-react'
import styles from 'styles/signature-pad.module.css'
export const Basic = () => (
<SignaturePad.Root className={styles.Root}>
<SignaturePad.Label className={styles.Label}>Sign below</SignaturePad.Label>
<SignaturePad.Control className={styles.Control}>
<SignaturePad.Segment className={styles.Segment} />
<SignaturePad.ClearTrigger className={styles.ClearTrigger}>
<RotateCcwIcon />
</SignaturePad.ClearTrigger>
<SignaturePad.Guide className={styles.Guide} />
</SignaturePad.Control>
</SignaturePad.Root>
)
제어된 방식 (Controlled)
paths prop을 onDraw(React/Solid), v-model:paths(Vue), bind:paths(Svelte)와 함께 사용해서 서명 패드를 외부에서 제어해요. 이 예제는 경로 개수를 추적하고 컴포넌트 밖에서 서명을 지울 수 있어요.
import { SignaturePad } from '@ark-ui/react/signature-pad'
import { RotateCcwIcon } from 'lucide-react'
import { useState } from 'react'
import button from 'styles/button.module.css'
import styles from 'styles/signature-pad.module.css'
export const Controlled = () => {
const [paths, setPaths] = useState<string[]>([])
return (
<div className="stack">
<output>paths: {paths.length}</output>
<SignaturePad.Root className={styles.Root} paths={paths} onDraw={(details) => setPaths(details.paths)}>
<SignaturePad.Label className={styles.Label}>Sign below</SignaturePad.Label>
<SignaturePad.Control className={styles.Control}>
<SignaturePad.Segment className={styles.Segment} />
<SignaturePad.ClearTrigger className={styles.ClearTrigger}>
<RotateCcwIcon />
</SignaturePad.ClearTrigger>
<SignaturePad.Guide className={styles.Guide} />
</SignaturePad.Control>
</SignaturePad.Root>
<button className={button.Root} onClick={() => setPaths([])}>
Clear
</button>
</div>
)
}
이미지 미리보기 (Image Preview)
사용자가 서명을 그린 후 서명을 이미지로 미리볼 수 있게 표시할 수 있어요. 저장하기 전에 사용자에게 서명 미리보기를 보여주고 싶을 때 유용해요.
import { SignaturePad } from '@ark-ui/react/signature-pad'
import { RotateCcwIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/signature-pad.module.css'
export const ImagePreview = () => {
const [imageUrl, setImageUrl] = useState('')
return (
<div className="stack">
<SignaturePad.Root
className={styles.Root}
onDrawEnd={(details) => details.getDataUrl('image/png').then((url) => setImageUrl(url))}
>
<SignaturePad.Label className={styles.Label}>Sign below</SignaturePad.Label>
<SignaturePad.Control className={styles.Control}>
<SignaturePad.Segment className={styles.Segment} />
<SignaturePad.ClearTrigger className={styles.ClearTrigger}>
<RotateCcwIcon />
</SignaturePad.ClearTrigger>
<SignaturePad.Guide className={styles.Guide} />
</SignaturePad.Control>
</SignaturePad.Root>
<div className="stack">
<span>Image Preview</span>
{imageUrl && <img src={imageUrl} alt="Signature" className={styles.Image} />}
</div>
</div>
)
}
Field 함께 사용하기 (Field)
Field 컴포넌트는 서명 패드의 폼 관련 상태와 접근성 속성을 관리할 수 있게 도와줘요. ARIA 라벨, 도우미 텍스트, 오류 텍스트 처리를 포함해서 올바른 접근성을 보장해요.
import { Field } from '@ark-ui/react/field'
import { SignaturePad } from '@ark-ui/react/signature-pad'
import { RotateCcwIcon } from 'lucide-react'
import field from 'styles/field.module.css'
import styles from 'styles/signature-pad.module.css'
export const WithField = () => (
<Field.Root className={field.Root}>
<SignaturePad.Root className={styles.Root}>
<SignaturePad.Label className={styles.Label}>Label</SignaturePad.Label>
<SignaturePad.Control className={styles.Control}>
<SignaturePad.Segment className={styles.Segment} />
<SignaturePad.ClearTrigger className={styles.ClearTrigger}>
<RotateCcwIcon />
</SignaturePad.ClearTrigger>
<SignaturePad.Guide className={styles.Guide} />
</SignaturePad.Control>
</SignaturePad.Root>
<Field.HelperText className={field.HelperText}>Additional Info</Field.HelperText>
<Field.ErrorText className={field.ErrorText}>Error Info</Field.ErrorText>
</Field.Root>
)
루트 프로바이더 (Root Provider)
서명 패드를 제어하는 또 다른 방법은 RootProvider 컴포넌트와 useSignaturePad 훅을 사용하는 거예요. 이렇게 하면 컴포넌트 밖에서 상태와 메서드에 접근할 수 있어요.
import { SignaturePad, useSignaturePad } from '@ark-ui/react/signature-pad'
import { RotateCcwIcon } from 'lucide-react'
import styles from 'styles/signature-pad.module.css'
export const RootProvider = () => {
const signaturePad = useSignaturePad()
return (
<div className="stack">
<output>no of paths: {signaturePad.paths.length}</output>
<SignaturePad.RootProvider className={styles.Root} value={signaturePad}>
<SignaturePad.Label className={styles.Label}>Sign below</SignaturePad.Label>
<SignaturePad.Control className={styles.Control}>
<SignaturePad.Segment className={styles.Segment} />
<SignaturePad.ClearTrigger className={styles.ClearTrigger}>
<RotateCcwIcon />
</SignaturePad.ClearTrigger>
<SignaturePad.Guide className={styles.Guide} />
</SignaturePad.Control>
</SignaturePad.RootProvider>
</div>
)
}
API 레퍼런스 (API Reference)
속성 (Props)
Root
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
defaultPaths
Type: string[]
Required: false
Default Value: undefined
Description: The default paths of the signature pad.
Use when you don't need to control the paths of the signature pad.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: Whether the signature pad is disabled.
drawing
Type: DrawingOptions
Required: false
Default Value: '{ size: 2, simulatePressure: true }'
Description: The drawing options.
ids
Type: Partial<{ root: string; control: string; hiddenInput: string; label: string }>
Required: false
Default Value: undefined
Description: The ids of the signature pad elements. Useful for composition.
name
Type: string
Required: false
Default Value: undefined
Description: The name of the signature pad. Useful for form submission.
onDraw
Type: (details: DrawDetails) => void
Required: false
Default Value: undefined
Description: Callback when the signature pad is drawing or the committed paths change.
paths contains only committed strokes; use currentPath for the in-progress stroke.
onDrawEnd
Type: (details: DrawEndDetails) => void
Required: false
Default Value: undefined
Description: Callback when the signature pad is done drawing.
paths
Type: string[]
Required: false
Default Value: undefined
Description: The controlled paths of the signature pad.
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: Whether the signature pad is read-only.
required
Type: boolean
Required: false
Default Value: undefined
Description: Whether the signature pad is required.
translations
Type: IntlTranslations
Required: false
Default Value: undefined
Description: The translations of the signature pad. Useful for internationalization.
데이터 속성 (Data Attributes)
data-scope: signature-pad
data-part: root
data-disabled: Present when disabled
ClearTrigger
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Control
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: signature-pad
data-part: control
data-disabled: Present when disabled
Guide
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: signature-pad
data-part: guide
data-disabled: Present when disabled
HiddenInput
Props
value
Type: string
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Label
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
데이터 속성 (Data Attributes)
data-scope: signature-pad
data-part: label
data-disabled: Present when disabled
data-required: Present when required
RootProvider
Props
value
Type: UseSignaturePadReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
Segment
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: Use the provided child element as the default rendered element, combining their props and behavior.
컨텍스트 (Context)
API:
| Property | Type | Description |
|---|---|---|
empty |
boolean |
Whether the signature pad is empty. |
drawing |
boolean |
Whether the user is currently drawing. |
currentPath |
`string | null` |
paths |
string[] |
The paths of the signature pad. |
getDataUrl |
(type: DataUrlType, quality?: number) => Promise<string> |
Returns the data URL of the signature pad. |
clear |
VoidFunction |
Clears the signature pad. |