Skip to content

Pin

An input for typing pin codes / one time codes.

Showcase

import {
Pin,
PinLabel,
PinInput,
PinGroup,
PinSlots,
PinSlot
} from '@klnjs/react-pin'
import classes from './pin.module.css'
export default () => (
<Pin className={classes.pin}>
<PinInput />
<PinLabel className={classes.label}>Pincode</PinLabel>
<PinGroup className={classes.group}>
<PinSlots>
{({ slot }) => (
<PinSlot key={slot} slot={slot} className={classes.slot} />
)}
</PinSlots>
</PinGroup>
</Pin>
)

Features

  • Controlled or uncontrolled.

  • Concealing the pin while typing.

  • Auto complete one time codes.

Installation

Terminal window
bun install @klnjs/react-pin

Definitions

Pin (Root)

Contains all the parts of a pin.

PropTypeDefault
asChild
boolean
false
PinType
-
PinConceal
-
PinDirection
-
PinSlotItem
-

Components

Contains all the parts of a pin. Use the conceal property to conceal the pin while typing, and the type to determine the allowed characters. Use value or defaultValue to control the input and onChange to listen to changes.

PropTypeDefault
asChild
boolean
false
conceal
PinConceal
false
defaultValue
string
-
disabled
boolean
false
length
number
4
type
PinType
alphanumeric
value
string
-
onChange
(value: string) => void
-

Input

Used to render a hidden input which is accessible by keyboard. The input can be shown by setting hidden to false. It will auto complete one time codes by default, and handle sanitising of input based on root property type.

PropTypeDefault
asChild
boolean
false
autoComplete
string
one-time-code
hidden
boolean
true

Label

Used to optionally render an input label, and attach it to the input so it is accessible. If omitted, you should add a label to the input with property aria-label, so the component stays accessible.

PropTypeDefault
asChild
boolean
false

Group

Used to group slot items.

PropTypeDefault
asChild
boolean
false

Slots

Given the dynamic length of the pin, this utility component provides means to render slots of the pin using a render function as child.

PropTypeDefault
children
(item: PinSlotItem) => ReactNode
-

Slot

Used to render individual slots, it should be used in conjunction with the slots component. A slot will conceal its content based on the root property conceal, this concealing only occurs when slot has an internal value.

Clicking a slot will focus the input, therefore focus events should be attached to the input. The slot is primarily visual, and it provides data-* attributes enabling styling of the different states.

PropTypeDefault
asChild
boolean
false
placeholder
string
-
Data attributePresent when
data-caretcaret is shown
data-concealedslot is concealed
data-disabledslot is disabled
data-endslot is last slot
data-focusedslot is focused
data-placeholderplaceholder is shown
data-startslot is first slot