OTP Field
solid-ui/otp-fieldFreeComponent
An OTP field component
Live preview
live · rendered by the registry
Rendered by solid-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.solid-ui.com/r/otp-field.jsonSource
1import type { Component, ComponentProps, ValidComponent } from "solid-js"2import { Show, splitProps } from "solid-js"34import type { DynamicProps, RootProps } from "@corvu/otp-field"5import OtpField from "@corvu/otp-field"67import { cn } from "~/lib/utils"89export const REGEXP_ONLY_DIGITS = "^\\d*$"10export const REGEXP_ONLY_CHARS = "^[a-zA-Z]*$"11export const REGEXP_ONLY_DIGITS_AND_CHARS = "^[a-zA-Z0-9]*$"1213type OTPFieldProps<T extends ValidComponent = "div"> = RootProps<T> & { class?: string }1415const OTPField = <T extends ValidComponent = "div">(props: DynamicProps<T, OTPFieldProps<T>>) => {16 const [local, others] = splitProps(props as OTPFieldProps, ["class"])17 return (18 <OtpField19 class={cn(20 "flex items-center gap-2 disabled:cursor-not-allowed has-[:disabled]:opacity-50",21 local.class22 )}23 {...others}24 />25 )26}2728const OTPFieldInput = OtpField.Input2930const OTPFieldGroup: Component<ComponentProps<"div">> = (props) => {31 const [local, others] = splitProps(props, ["class"])32 return <div class={cn("flex items-center", local.class)} {...others} />33}3435const OTPFieldSlot: Component<ComponentProps<"div"> & { index: number }> = (props) => {36 const [local, others] = splitProps(props, ["class", "index"])37 const context = OtpField.useContext()38 const char = () => context.value()[local.index]39 const showFakeCaret = () => context.value().length === local.index && context.isInserting()4041 return (42 <div43 class={cn(44 "group relative flex size-10 items-center justify-center border-y border-r border-input text-sm first:rounded-l-md first:border-l last:rounded-r-md",45 local.class46 )}47 {...others}48 >49 <div50 class={cn(51 "absolute inset-0 z-10 transition-all group-first:rounded-l-md group-last:rounded-r-md",52 context.activeSlots().includes(local.index) && "ring-2 ring-ring ring-offset-background"53 )}54 />55 {char()}56 <Show when={showFakeCaret()}>57 <div class="pointer-events-none absolute inset-0 flex items-center justify-center">58 <div class="h-4 w-px animate-caret-blink bg-foreground duration-1000" />59 </div>60 </Show>61 </div>62 )63}6465const OTPFieldSeparator: Component<ComponentProps<"div">> = (props) => {66 return (67 <div {...props}>68 <svg69 xmlns="http://www.w3.org/2000/svg"70 viewBox="0 0 24 24"71 fill="none"72 stroke="currentColor"73 stroke-width="2"74// … truncated
What it pulls in
npm packages
Files it writes
More from solid-ui
All 55 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | solid-ui | Components | Free | 2 deps | |
| Alertalert | solid-ui | Components | Free | 3 deps | |
| Alert Dialogalert-dialog | solid-ui | Components | Free | 2 deps | |
| Aspect Ratioaspect-ratio | solid-ui | Components | Free | 1 dep | |
| Avataravatar | solid-ui | Components | Free | 2 deps | |
| Badgebadge | solid-ui | Components | Free | 2 deps | |
| Badge Deltabadge-delta | solid-ui | Components | Free | 2 deps | |
| Bar Listbar-list | solid-ui | Components | Free | 1 dep |
