비밀번호 입력
비밀번호 입력 (Password Input)
사용자가 비밀번호나 API 키 같은 보안이 필요한 텍스트를 입력할 수 있게 해 주는 컴포넌트입니다. 입력의 표시/숨김을 전환해 주는 가시성 트리거와 함께 자동완성, 유효성 검사, 강도 측정기 등 다양한 시나리오를 지원합니다.
출처: 문서
본문
해부 구조 (Anatomy)
<PasswordInput.Root>
<PasswordInput.Label />
<PasswordInput.Control>
<PasswordInput.Input />
<PasswordInput.VisibilityTrigger>
<PasswordInput.Indicator />
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
예시 (Examples)
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import styles from 'styles/password-input.module.css'
export const Basic = () => (
<PasswordInput.Root className={styles.Root}>
<PasswordInput.Label className={styles.Label}>Password</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
)
자동완성 (Autocomplete)
입력에서 자동완성을 관리하려면 autoComplete prop을 사용하세요.
new-password— 사용자가 새 비밀번호를 만들고 있는 경우입니다.current-password— 사용자가 기존 비밀번호를 입력하고 있는 경우입니다.
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import styles from 'styles/password-input.module.css'
export const Autocomplete = () => (
<PasswordInput.Root className={styles.Root} autoComplete="new-password">
<PasswordInput.Label className={styles.Label}>Password</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
)
가시성 제어 (Controlled Visibility)
visible과 onVisibilityChange props를 사용해 비밀번호 입력의 가시성을 제어할 수 있어요.
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { useState } from 'react'
import styles from 'styles/password-input.module.css'
export const ControlledVisibility = () => {
const [visible, setVisible] = useState(false)
return (
<PasswordInput.Root className={styles.Root} visible={visible} onVisibilityChange={(e) => setVisible(e.visible)}>
<PasswordInput.Label className={styles.Label}>Password is {visible ? 'visible' : 'hidden'}</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
)
}
루트 프로바이더 (Root Provider)
비밀번호 입력을 제어하는 또 다른 방법은 RootProvider 컴포넌트와 usePasswordInput 훅을 사용하는 것입니다. 이렇게 하면 컴포넌트 외부에서 상태와 메서드에 접근할 수 있어요.
import { PasswordInput, usePasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import styles from 'styles/password-input.module.css'
export const RootProvider = () => {
const passwordInput = usePasswordInput()
return (
<div className="stack">
<output>password input is {passwordInput.visible ? 'visible' : 'hidden'}</output>
<PasswordInput.RootProvider className={styles.Root} value={passwordInput}>
<PasswordInput.Label className={styles.Label}>Password</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.RootProvider>
</div>
)
}
필드 (Field)
PasswordInput 컴포넌트를 Field 컴포넌트와 함께 사용하는 예시입니다.
import { Field } from '@ark-ui/react/field'
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import field from 'styles/field.module.css'
import styles from 'styles/password-input.module.css'
export const WithField = () => (
<Field.Root className={field.Root}>
<PasswordInput.Root className={styles.Root}>
<PasswordInput.Label className={styles.Label}>Password</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
<Field.HelperText className={field.HelperText}>Enter your password</Field.HelperText>
<Field.ErrorText className={field.ErrorText}>Password is required</Field.ErrorText>
</Field.Root>
)
비밀번호 관리자 (Password Managers)
ignorePasswordManager prop을 사용하면 1Password, LastPass 같은 비밀번호 관리자를 무시할 수 있게 해요. 로그인이 아닌 시나리오(예: "api keys", "secure notes", "temporary passwords")에서 유용합니다.
현재 이 기능은 1Password, LastPass, Bitwarden, Dashlane, Proton Pass에서만 동작합니다.
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import styles from 'styles/password-input.module.css'
export const IgnorePasswordManager = () => (
<PasswordInput.Root className={styles.Root} ignorePasswordManagers>
<PasswordInput.Label className={styles.Label}>API Key</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input className={styles.Input} defaultValue="spd_1234567890" />
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
</PasswordInput.Root>
)
강도 측정기 (Strength Meter)
PasswordInput을 비밀번호 강도 라이브러리와 결합해 비밀번호 강도에 대한 시각적 피드백을 보여 줄 수 있어요. 이 예시는 check-password-strength 패키지를 사용해 실시간 강도 검증을 제공합니다.
import { PasswordInput } from '@ark-ui/react/password-input'
import { passwordStrength, type Options } from 'check-password-strength'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { useMemo, useState } from 'react'
import styles from 'styles/password-input.module.css'
const strengthOptions: Options<string> = [
{ id: 0, value: 'weak', minDiversity: 0, minLength: 0 },
{ id: 1, value: 'medium', minDiversity: 2, minLength: 6 },
{ id: 2, value: 'strong', minDiversity: 4, minLength: 8 },
]
export const StrengthMeter = () => {
const [password, setPassword] = useState('asdfasdf')
const strength = useMemo(() => {
if (!password) return null
const { value } = passwordStrength(password, strengthOptions)
return value
}, [password])
return (
<PasswordInput.Root className={styles.Root}>
<PasswordInput.Label className={styles.Label}>Password</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input
className={styles.Input}
value={password}
onChange={(e) => setPassword(e.currentTarget.value)}
placeholder="Enter your password"
/>
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
{strength && (
<div className={styles.StrengthMeter}>
<div className={styles.StrengthBar}>
<div className={styles.StrengthFill} data-strength={strength} />
</div>
<div className={styles.StrengthLabel}>{strength} password</div>
</div>
)}
</PasswordInput.Root>
)
}
유효성 검사 (Validation)
커스텀 검증 로직과 결합해 실시간 피드백을 보여 줄 수 있어요. 검증 오류를 표시하려면 invalid prop을 사용하세요.
import { PasswordInput } from '@ark-ui/react/password-input'
import { EyeIcon, EyeOffIcon } from 'lucide-react'
import { useMemo, useState } from 'react'
import styles from 'styles/password-input.module.css'
export const WithValidation = () => {
const [password, setPassword] = useState('')
const isValid = useMemo(() => password.length >= 8, [password])
return (
<PasswordInput.Root className={styles.Root} invalid={!isValid && password.length > 0}>
<PasswordInput.Label className={styles.Label}>Password (min 8 characters)</PasswordInput.Label>
<PasswordInput.Control className={styles.Control}>
<PasswordInput.Input
className={styles.Input}
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Enter your password"
/>
<PasswordInput.VisibilityTrigger className={styles.VisibilityTrigger}>
<PasswordInput.Indicator className={styles.Indicator} fallback={<EyeOffIcon />}>
<EyeIcon />
</PasswordInput.Indicator>
</PasswordInput.VisibilityTrigger>
</PasswordInput.Control>
{password.length > 0 && !isValid && (
<p className={styles.ValidationMessage} data-valid="false">
Password must be at least 8 characters
</p>
)}
{isValid && password.length > 0 && (
<p className={styles.ValidationMessage} data-valid="true">
Password is valid
</p>
)}
</PasswordInput.Root>
)
}
API 참조 (API Reference)
Props
Root
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
autoComplete
Type: 'current-password' | 'new-password'
Required: false
Default Value: "current-password"
Description: 비밀번호 입력의 자동완성(autocomplete) 속성입니다.
defaultVisible
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력의 기본 가시성입니다.
disabled
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력이 비활성화되었는지 여부입니다.
id
Type: string
Required: false
Default Value: undefined
Description: 머신의 고유 식별자입니다.
ids
Type: Partial<{ input: string; visibilityTrigger: string }>
Required: false
Default Value: undefined
Description: 비밀번호 입력 부분들의 id입니다.
ignorePasswordManagers
Type: boolean
Required: false
Default Value: undefined
Description: true인 경우 입력이 비밀번호 관리자를 무시합니다.
다음 비밀번호 관리자에서만 동작합니다
- 1Password, LastPass, Bitwarden, Dashlane, Proton Pass
invalid
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력의 유효하지 않은 상태입니다.
name
Type: string
Required: false
Default Value: undefined
Description: 비밀번호 입력의 이름입니다.
onVisibilityChange
Type: (details: VisibilityChangeDetails) => void
Required: false
Default Value: undefined
Description: 가시성이 변경될 때 호출되는 함수입니다.
readOnly
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력이 읽기 전용인지 여부입니다.
required
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력이 필수인지 여부입니다.
translations
Type: Partial<{ visibilityTrigger: ((visible: boolean) => string) | undefined }>
Required: false
Default Value: undefined
Description: 사용할 지역화된 메시지입니다.
visible
Type: boolean
Required: false
Default Value: undefined
Description: 비밀번호 입력이 표시되는지 여부입니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: root
data-disabled: 비활성화되었을 때 표시됩니다.
data-invalid: 유효하지 않을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
Control
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: control
data-disabled: 비활성화되었을 때 표시됩니다.
data-invalid: 유효하지 않을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
Indicator
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
fallback
Type: string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>
Required: false
Default Value: undefined
Description: 비밀번호가 표시되지 않을 때 보여 줄 대체(fallback) 콘텐츠입니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: indicator
data-state: "visible" | "hidden"
data-disabled: 비활성화되었을 때 표시됩니다.
data-invalid: 유효하지 않을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
Input
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: input
data-state: "visible" | "hidden"
data-disabled: 비활성화되었을 때 표시됩니다.
data-invalid: 유효하지 않을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
Label
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: label
data-disabled: 비활성화되었을 때 표시됩니다.
data-invalid: 유효하지 않을 때 표시됩니다.
data-readonly: 읽기 전용일 때 표시됩니다.
data-required: 필수일 때 표시됩니다.
RootProvider
Props
value
Type: UsePasswordInputReturn
Required: true
Default Value: undefined
Description: undefined
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
VisibilityTrigger
Props
asChild
Type: boolean
Required: false
Default Value: undefined
Description: 제공된 자식 요소를 기본 렌더링 요소로 사용하며, 해당 요소의 props와 동작을 결합합니다.
데이터 속성 (Data Attributes)
data-scope: password-input
data-part: visibility-trigger
data-readonly: 읽기 전용일 때 표시됩니다.
data-disabled: 비활성화되었을 때 표시됩니다.
data-state: "visible" | "hidden"
Context
API:
| Property | Type | Description |
|---|---|---|
visible |
boolean |
비밀번호 입력이 표시되는지 여부입니다. |
disabled |
boolean |
비밀번호 입력이 비활성화되었는지 여부입니다. |
invalid |
boolean |
비밀번호 입력이 유효하지 않은지 여부입니다. |
focus |
VoidFunction |
비밀번호 입력에 포커스를 맞춥니다. |
setVisible |
(value: boolean) => void |
비밀번호 입력의 가시성을 설정합니다. |
toggleVisible |
VoidFunction |
비밀번호 입력의 가시성을 전환합니다. |