Tip Calculator

import { useState, useEffect } from 'react'; import { DollarSign, Percent, Users, CreditCard, Smile, Frown, Zap } from 'lucide-react'; export default function UltimateTipCalculator() { const [billAmount, setBillAmount] = useState(''); const [tipPercentage, setTipPercentage] = useState(18); const [customTip, setCustomTip] = useState(''); const [numberOfPeople, setNumberOfPeople] = useState(1); const [tipAmount, setTipAmount] = useState(0); const [totalAmount, setTotalAmount] = useState(0); const [perPersonAmount, setPerPersonAmount] = useState(0); const [roundUp, setRoundUp] = useState(false); const [darkMode, setDarkMode] = useState(true); // Popular preset percentages based on research const tipPresets = [10, 15, 18, 20, 25, 30]; // Service quality suggestions const serviceQuality = [ { level: 'Poor', tip: 10, icon: }, { level: 'Good', tip: 15, icon: }, { level: 'Great', tip: 20, icon: }, { level: 'Excellent', tip: 25, icon: } ]; // Calculate results whenever inputs change useEffect(() => { if (billAmount) { const bill = parseFloat(billAmount); let tip = bill * (tipPercentage / 100); if (roundUp) { const total = bill + tip; tip = Math.ceil(total) - bill; } const total = bill + tip; setTipAmount(tip); setTotalAmount(total); setPerPersonAmount(total / Math.max(1, numberOfPeople)); } else { setTipAmount(0); setTotalAmount(0); setPerPersonAmount(0); } }, [billAmount, tipPercentage, numberOfPeople, roundUp]); // Handle custom tip input const handleCustomTip = (e) => { const value = e.target.value; setCustomTip(value); if (value && !isNaN(value)) { setTipPercentage(parseFloat(value)); } }; // Format currency for display const formatCurrency = (value) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(value); }; // Toggle color mode const toggleColorMode = () => { setDarkMode(!darkMode); }; return (
{/* Header with mode toggle */}

TIP HERO ®

{/* Main content */}
{/* Bill Amount Input */}
$ setBillAmount(e.target.value)} placeholder="0.00" className={`w-full ${darkMode ? 'bg-gray-800 text-white' : 'bg-gray-50 text-gray-800'} border ${darkMode ? 'border-gray-700' : 'border-gray-200'} rounded-md p-2 pl-8 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:border-transparent`} min="0" step="0.01" />
{/* Service Quality Suggestions */}
{serviceQuality.map((quality) => ( ))}
{/* Tip Percentage */}
{tipPresets.map((preset) => ( ))}
{ setTipPercentage(parseInt(e.target.value)); setCustomTip(''); }} className={`w-full h-2 rounded-lg appearance-none cursor-pointer ${darkMode ? 'accent-blue-400' : 'accent-blue-500'}`} />
%
{/* Number of People */}
setNumberOfPeople(Math.max(1, parseInt(e.target.value) || 1))} className={`w-16 p-2 text-center border-t border-b ${darkMode ? 'bg-gray-800 border-gray-700 text-white' : 'bg-gray-50 border-gray-200 text-gray-800'}`} />
{/* Round Up Option */}
{/* Results Section */}

Tip Amount

{formatCurrency(tipAmount)}

Total

{formatCurrency(totalAmount)}

Each Person Pays

{formatCurrency(perPersonAmount)}

{/* Footer */}

Tip Hero helps you calculate fair tips instantly

); }

Comments

Popular posts from this blog

Articles for Calculator.ist

Calc.ist: List of all Calculators: Total of 592.

HP-65 Calculator Simulator (1974)