Word Counter
Count words, characters, sentences, paragraphs, and estimate reading time from any text.
Text
100% Free
Runs in Browser
Preview
Words
0
Characters
0
Sentences
0
Paragraphs
0
Reading Time
1 min
Source Code
Toggle the controls below the code to live-edit the initial state values.
word-counter.tsx
import { useState } from "react";
import { Textarea } from "@/components/ui/textarea";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
export default function WordCounter() {
const [text, setText] = useState("");
const words = text.trim() ? text.trim().split(/\s+/).length : 0;
const characters = text.length;
const sentences = text.trim()
? text.split(/[.!?]+/).filter((s) => s.trim()).length
: 0;
const paragraphs = text.trim()
? text.split(/\n\s*\n/).filter((p) => p.trim()).length
: 0;
const readingTime = Math.max(1, Math.ceil(words / 200));
const stats = [
{ label: "Words", value: words },
{ label: "Characters", value: characters },
{ label: "Sentences", value: sentences },
{ label: "Paragraphs", value: paragraphs },
{ label: "Reading Time", value: `${readingTime} min` },
];
return (
<div className="space-y-4">
<Textarea
placeholder="Start typing or paste your text here..."
value={text}
onChange={(e) => setText(e.target.value)}
className="min-h-[200px] resize-y text-base"
/>
<div className="flex flex-wrap gap-3">
{stats.map((stat) => (
<div
key={stat.label}
className="flex items-center gap-2 rounded-lg border px-3 py-2"
>
<span className="text-sm text-muted-foreground">{stat.label}</span>
<Badge variant="secondary" className="font-mono text-sm">
{stat.value}
</Badge>
</div>
))}
</div>
</div>
);
}
Props Playground
1 prop
Required Libraries
Install the external dependencies used by this tool.
bun add react
Shadcn UI Setup
Add the required Shadcn UI primitives to your project.
bun x --bun shadcn@latest add badge card textarea
Shadcn UI Components
The following primitives are required in your
@/components/ui
directory.
| Component | Import Path |
|---|---|
| badge | @/components/ui/badge |
| card | @/components/ui/card |
| textarea | @/components/ui/textarea |
Imports
All import statements used in this tool.
| Source | Exports |
|---|---|
| react | useState |
| @/components/ui/textarea | Textarea |
| @/components/ui/card | Card, CardContent, CardHeader, CardTitle |
| @/components/ui/badge | Badge |
State Management
React state variables managed within this tool.
| Variable | Initial Value |
|---|---|
| text | "" |
Variables & Constants
Constants and computed values defined in this tool.
| Name | Value |
|---|---|
| words | text.trim() ? text.trim().split(/\s+/).length : 0 |
| characters | text.length |
| sentences | text.trim() |
| paragraphs | text.trim() |
| readingTime | Math.max(1, Math.ceil(words / 200)) |
| stats | [ |
External Resources
Documentation, tutorials, and package details for libraries used in this tool.