UI libraries
Mask is not a UI package. The same pattern works with shadcn/ui, Ant Design, Chakra and plain inputs.
Spread the returned object on the input. The component must pass ref to the real <input> (as a prop in React 19, with forwardRef inReact 18). Don't pass value; the input is uncontrolled.
Shared pattern
const { card } = useViraMask(form, { card: "card" })
// on screen: "4111 1111 1111 1111"
// form.watch("card"): "4111111111111111"
return <input {...card} placeholder="0000 0000 0000 0000" />// Need your own ref too? Call both
const inputRef = useRef<HTMLInputElement>(null)
<Input
{...card}
ref={(el) => {
card.ref(el)
inputRef.current = el
}}
/>shadcn/ui
The live demos on this site use the shadcn/ui Input too:
Form value: -
Display: -
Ant Design
import { useForm } from "react-hook-form"
import { useViraMask } from "@virastack/mask"
import { Input } from "antd"
function Example() {
const form = useForm<{ card: string }>()
const { card } = useViraMask(form, { card: "card" })
return <Input {...card} placeholder="0000 0000 0000 0000" />
}Chakra UI
import { useForm } from "react-hook-form"
import { useViraMask } from "@virastack/mask"
import { Input } from "@chakra-ui/react"
function Example() {
const form = useForm<{ card: string }>()
const { card } = useViraMask(form, { card: "card" })
return <Input {...card} placeholder="0000 0000 0000 0000" />
}