-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from dwhiffing/encryption-context
Encryption context
- Loading branch information
Showing
13 changed files
with
293 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
InputOTP, | ||
InputOTPGroup, | ||
InputOTPSlot, | ||
} from "@/components/ui/input-otp"; | ||
import { | ||
AlertDialog, | ||
AlertDialogAction, | ||
AlertDialogContent, | ||
AlertDialogFooter, | ||
AlertDialogHeader, | ||
AlertDialogTitle, | ||
} from "@/components/ui/alert-dialog"; | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { | ||
AlertDialogCancel, | ||
AlertDialogDescription, | ||
} from "@radix-ui/react-alert-dialog"; | ||
|
||
export function PinInput(props: { | ||
open: boolean; | ||
title: string; | ||
description: string; | ||
onSubmit?: (s: string) => void; | ||
onCancel?: () => void; | ||
pinLength?: number; | ||
}) { | ||
const [value, setValue] = useState(""); | ||
const length = props.pinLength ?? 4; | ||
|
||
useEffect(() => { | ||
if (props.open) setValue(""); | ||
}, [props.open]); | ||
|
||
return ( | ||
<AlertDialog open={props.open}> | ||
<AlertDialogContent className={props.description ? "w-[24rem]" : "w-60"}> | ||
<form | ||
className="flex flex-col gap-6" | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
if (value.length === length) props.onSubmit?.(value); | ||
}} | ||
> | ||
<AlertDialogHeader className="items-center gap-2"> | ||
<AlertDialogTitle>{props.title}</AlertDialogTitle> | ||
{props.description && ( | ||
<AlertDialogDescription className="!mt-0 text-center opacity-60"> | ||
{props.description} | ||
</AlertDialogDescription> | ||
)} | ||
<InputOTP | ||
autoFocus={props.open} | ||
maxLength={length} | ||
value={value} | ||
onChange={setValue} | ||
> | ||
<InputOTPGroup> | ||
{Array.from({ length }, (_, i) => ( | ||
<InputOTPSlot key={i} index={i} /> | ||
))} | ||
</InputOTPGroup> | ||
</InputOTP> | ||
</AlertDialogHeader> | ||
|
||
<AlertDialogFooter className="gap-4"> | ||
{props.onCancel && ( | ||
<AlertDialogCancel onClick={() => props.onCancel?.()}> | ||
Cancel | ||
</AlertDialogCancel> | ||
)} | ||
<AlertDialogAction disabled={value.length < length} type="submit"> | ||
Submit | ||
</AlertDialogAction> | ||
</AlertDialogFooter> | ||
</form> | ||
</AlertDialogContent> | ||
</AlertDialog> | ||
); | ||
} | ||
|
||
export const usePinInput = ({ | ||
title = "Enter PIN", | ||
description = "", | ||
allowCancel = true, | ||
} = {}) => { | ||
const [isPinOpen, setIsPinOpen] = useState(false); | ||
const pinPromiseRef = useRef<(s?: string) => void>(); | ||
const pinInput = ( | ||
<PinInput | ||
open={isPinOpen} | ||
title={title} | ||
description={description} | ||
onCancel={allowCancel ? pinPromiseRef.current : undefined} | ||
onSubmit={pinPromiseRef.current} | ||
/> | ||
); | ||
|
||
const getPin = useCallback(async () => { | ||
setIsPinOpen(true); | ||
const pin = await new Promise((resolve) => { | ||
pinPromiseRef.current = resolve; | ||
}); | ||
setIsPinOpen(false); | ||
return pin; | ||
}, []); | ||
|
||
return { pinInput, getPin }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use client" | ||
|
||
import * as React from "react" | ||
import { OTPInput, OTPInputContext } from "input-otp" | ||
import { Dot } from "lucide-react" | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
const InputOTP = React.forwardRef< | ||
React.ElementRef<typeof OTPInput>, | ||
React.ComponentPropsWithoutRef<typeof OTPInput> | ||
>(({ className, containerClassName, ...props }, ref) => ( | ||
<OTPInput | ||
ref={ref} | ||
containerClassName={cn( | ||
"flex items-center gap-2 has-[:disabled]:opacity-50", | ||
containerClassName | ||
)} | ||
className={cn("disabled:cursor-not-allowed", className)} | ||
{...props} | ||
/> | ||
)) | ||
InputOTP.displayName = "InputOTP" | ||
|
||
const InputOTPGroup = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> | ||
>(({ className, ...props }, ref) => ( | ||
<div ref={ref} className={cn("flex items-center", className)} {...props} /> | ||
)) | ||
InputOTPGroup.displayName = "InputOTPGroup" | ||
|
||
const InputOTPSlot = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> & { index: number } | ||
>(({ index, className, ...props }, ref) => { | ||
const inputOTPContext = React.useContext(OTPInputContext) | ||
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index] | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
className={cn( | ||
"relative flex h-10 w-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md", | ||
isActive && "z-10 ring-2 ring-ring ring-offset-background", | ||
className | ||
)} | ||
{...props} | ||
> | ||
{char} | ||
{hasFakeCaret && ( | ||
<div className="pointer-events-none absolute inset-0 flex items-center justify-center"> | ||
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" /> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
}) | ||
InputOTPSlot.displayName = "InputOTPSlot" | ||
|
||
const InputOTPSeparator = React.forwardRef< | ||
React.ElementRef<"div">, | ||
React.ComponentPropsWithoutRef<"div"> | ||
>(({ ...props }, ref) => ( | ||
<div ref={ref} role="separator" {...props}> | ||
<Dot /> | ||
</div> | ||
)) | ||
InputOTPSeparator.displayName = "InputOTPSeparator" | ||
|
||
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.