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 */}
{/* Main content */}
{/* Footer */}
);
}
TIP HERO ®
{/* Bill Amount Input */}
{/* Service Quality Suggestions */}
{/* Tip Percentage */}
{/* Number of People */}
{/* Round Up Option */}
{/* Results Section */}
$
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"
/>
{serviceQuality.map((quality) => (
))}
{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'}`}
/>
%
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'}`}
/>
Tip Amount
{formatCurrency(tipAmount)}
Total
{formatCurrency(totalAmount)}
{formatCurrency(perPersonAmount)}
Tip Hero helps you calculate fair tips instantly
Comments
Post a Comment