Components
Loading preview...
useCharacterLimit hook
npx shadcn@latest add https://21st.dev/r/originui/use-character-limit"use client";
import { useCharacterLimit } from "@/components/hooks/use-character-limit";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useId } from "react";
function Component() {
const id = useId();
const maxLength = 50;
const {
value,
characterCount,
handleChange,
maxLength: limit,
} = useCharacterLimit({ maxLength });
return (
<div className="space-y-2 min-w-[300px]">
<Label htmlFor={id}>Input with character limit</Label>
<div className="relative">
<Input
id={id}
className="peer pe-14"
type="text"
value={value}
maxLength={maxLength}
onChange={handleChange}
aria-describedby={`${id}-description`}
/>
<div
id={`${id}-description`}
className="pointer-events-none absolute inset-y-0 end-0 flex items-center justify-center pe-3 text-xs tabular-nums text-muted-foreground peer-disabled:opacity-50"
aria-live="polite"
role="status"
>
{characterCount}/{limit}
</div>
</div>
</div>
);
}
export { Component };