Pin Input
For pin or verification codes with auto-focus transfer and masking options.
Usage
import { PinInput, type PinInputProps } from '~/components/ui'
export const Demo = (props: PinInputProps) => {
return (
<PinInput placeholder="0" onValueComplete={(e) => alert(e.valueAsString)} {...props}>
Pin Input
</PinInput>
)
}
Installation
npx @park-ui/cli components add pin-input
1
Styled Primitive
Copy the code snippet below into ~/components/ui/primitives/pin-input.tsx
'use client'
import type { Assign } from '@ark-ui/react'
import { PinInput } from '@ark-ui/react/pin-input'
import { type PinInputVariantProps, pinInput } from 'styled-system/recipes'
import type { ComponentProps, HTMLStyledProps } from 'styled-system/types'
import { createStyleContext } from '~/lib/create-style-context'
const { withProvider, withContext } = createStyleContext(pinInput)
export type RootProviderProps = ComponentProps<typeof RootProvider>
export const RootProvider = withProvider<
HTMLDivElement,
Assign<Assign<HTMLStyledProps<'div'>, PinInput.RootProviderBaseProps>, PinInputVariantProps>
>(PinInput.RootProvider, 'root')
export type RootProps = ComponentProps<typeof Root>
export const Root = withProvider<
HTMLDivElement,
Assign<Assign<HTMLStyledProps<'div'>, PinInput.RootBaseProps>, PinInputVariantProps>
>(PinInput.Root, 'root')
export const Control = withContext<
HTMLDivElement,
Assign<HTMLStyledProps<'div'>, PinInput.ControlBaseProps>
>(PinInput.Control, 'control')
export const Input = withContext<
HTMLInputElement,
Assign<HTMLStyledProps<'input'>, PinInput.InputBaseProps>
>(PinInput.Input, 'input')
export const Label = withContext<
HTMLLabelElement,
Assign<HTMLStyledProps<'label'>, PinInput.LabelBaseProps>
>(PinInput.Label, 'label')
export {
PinInputContext as Context,
PinInputHiddenInput as HiddenInput,
} from '@ark-ui/react/pin-input'
import { type Assign, PinInput } from '@ark-ui/solid'
import type { ComponentProps } from 'solid-js'
import { type PinInputVariantProps, pinInput } from 'styled-system/recipes'
import type { HTMLStyledProps } from 'styled-system/types'
import { createStyleContext } from '~/lib/create-style-context'
const { withProvider, withContext } = createStyleContext(pinInput)
export type RootProviderProps = ComponentProps<typeof RootProvider>
export const RootProvider = withProvider<
Assign<Assign<HTMLStyledProps<'div'>, PinInput.RootProviderBaseProps>, PinInputVariantProps>
>(PinInput.RootProvider, 'root')
export type RootProps = ComponentProps<typeof Root>
export const Root = withProvider<
Assign<Assign<HTMLStyledProps<'div'>, PinInput.RootBaseProps>, PinInputVariantProps>
>(PinInput.Root, 'root')
export const Control = withContext<Assign<HTMLStyledProps<'div'>, PinInput.ControlBaseProps>>(
PinInput.Control,
'control',
)
export const Input = withContext<Assign<HTMLStyledProps<'input'>, PinInput.InputBaseProps>>(
PinInput.Input,
'input',
)
export const Label = withContext<Assign<HTMLStyledProps<'label'>, PinInput.LabelBaseProps>>(
PinInput.Label,
'label',
)
export {
PinInputContext as Context,
PinInputHiddenInput as HiddenInput,
} from '@ark-ui/solid'
No snippet found
Extend ~/components/ui/primitives/index.ts
with the following line:
export * as PinInput from './pin-input'
2
Add Composition
Copy the code snippet below into ~/components/ui/pin-input.tsx
import { forwardRef } from 'react'
import { PinInput as ArkPinInput } from '~/components/ui/primitives'
import { Input } from '~/components/ui/primitives'
export interface PinInputProps extends ArkPinInput.RootProps {
/**
* The number of inputs to render.
* @default 4
*/
length?: number
}
export const PinInput = forwardRef<HTMLDivElement, PinInputProps>((props, ref) => {
const { children, length = 4, ...rootProps } = props
return (
<ArkPinInput.Root ref={ref} {...rootProps}>
{children && <ArkPinInput.Label>{children}</ArkPinInput.Label>}
<ArkPinInput.Control>
{Array.from({ length }, (_, index) => index).map((id, index) => (
<ArkPinInput.Input key={id} index={index} asChild>
<Input size={rootProps.size} />
</ArkPinInput.Input>
))}
</ArkPinInput.Control>
<ArkPinInput.HiddenInput />
</ArkPinInput.Root>
)
})
PinInput.displayName = 'PinInput'
import { Index, Show, children } from 'solid-js'
import { PinInput as ArkPinInput, Input } from '~/components/ui/primitives'
export interface PinInputProps extends ArkPinInput.RootProps {
/**
* The number of inputs to render.
* @default 4
*/
length?: number
}
export const PinInput = (props: PinInputProps) => {
const getChildren = children(() => props.children)
return (
<ArkPinInput.Root {...props}>
<Show when={getChildren()}>
<ArkPinInput.Label>{getChildren()}</ArkPinInput.Label>
</Show>
<ArkPinInput.Control>
<Index each={Array.from({ length: props.length ?? 4 }, (_, index) => index)}>
{(index) => (
<ArkPinInput.Input
index={index()}
asChild={(inputProps) => <Input {...inputProps()} size={props.size} />}
/>
)}
</Index>
</ArkPinInput.Control>
<ArkPinInput.HiddenInput />
</ArkPinInput.Root>
)
}
Extend ~/components/ui/index.ts
with the following line:
export * from './primitives'
export { PinInput, type PinInputProps } from './pin-input'
3
Integrate Recipe
If you're not using @park-ui/preset
, add the following recipe to yourpanda.config.ts
:
import { pinInputAnatomy } from '@ark-ui/anatomy'
import { defineSlotRecipe } from '@pandacss/dev'
export const pinInput = defineSlotRecipe({
className: 'pinInput',
slots: pinInputAnatomy.keys(),
base: {
root: {
display: 'flex',
flexDirection: 'column',
gap: '1.5',
},
control: {
display: 'flex',
gap: '2',
},
label: {
color: 'fg.default',
fontWeight: 'medium',
},
input: {
px: '0!',
textAlign: 'center',
},
},
defaultVariants: {
size: 'md',
},
variants: {
size: {
xs: {
label: {
textStyle: 'sm',
},
input: {
width: '8',
},
},
sm: {
label: {
textStyle: 'sm',
},
input: {
width: '9',
},
},
md: {
label: {
textStyle: 'sm',
},
input: {
width: '10',
},
},
lg: {
label: {
textStyle: 'sm',
},
input: {
width: '11',
},
},
xl: {
label: {
textStyle: 'md',
},
input: {
width: '12',
},
},
'2xl': {
label: {
textStyle: 'md',
},
input: {
width: '16',
},
},
},
},
})