|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import './CharacterCounter.css'; |
| 3 | + |
| 4 | +const CharacterCounter = () => { |
| 5 | + const [text, setText] = useState(''); |
| 6 | + |
| 7 | + const charCount = text.length; |
| 8 | + const wordCount = text.trim() === '' ? 0 : text.trim().split(/\s+/).filter(Boolean).length; |
| 9 | + const readingTime = Math.ceil(wordCount / 200); |
| 10 | + |
| 11 | + return ( |
| 12 | + <div className="character-counter-wrapper"> |
| 13 | + <div className="counter-card"> |
| 14 | + <h2 className="counter-title">Character & Word Counter</h2> |
| 15 | + |
| 16 | + <textarea |
| 17 | + className="counter-textarea" |
| 18 | + placeholder="Start typing or paste your text here..." |
| 19 | + value={text} |
| 20 | + onChange={(e) => setText(e.target.value)} |
| 21 | + /> |
| 22 | + |
| 23 | + <div className="stats-container"> |
| 24 | + <div className="stat-item"> |
| 25 | + <span className="stat-num">{charCount}</span> |
| 26 | + <span className="stat-label">Characters</span> |
| 27 | + </div> |
| 28 | + <div className="stat-item"> |
| 29 | + <span className="stat-num">{wordCount}</span> |
| 30 | + <span className="stat-label">Words</span> |
| 31 | + </div> |
| 32 | + <div className="stat-item"> |
| 33 | + <span className="stat-num">{readingTime} min</span> |
| 34 | + <span className="stat-label">Reading Time</span> |
| 35 | + </div> |
| 36 | + </div> |
| 37 | + |
| 38 | + <button className="reset-btn" onClick={() => setText('')}> |
| 39 | + Clear All |
| 40 | + </button> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + ); |
| 44 | +}; |
| 45 | + |
| 46 | +export default CharacterCounter; |
0 commit comments