Add mortgage calculator page with interactive widget

- Two-column layout matching property details page pattern
- Real-time mortgage calculation with principal, interest, loan amount, and total interest
- Input formatting with currency/percentage display
- Down payment syncs between dollar amount and percentage
- Sidebar with quick tips, help contact widget, and related resources
- "How to Use" and "Keep in Mind" sections with practical guidance
- Widget titles use Times New Roman 18px bold to match existing styles

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Hanson.xyz Dev
2025-12-01 00:29:57 -06:00
parent cbeca7c6d8
commit 9963446486
7 changed files with 685 additions and 2 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,178 @@
<?php
/**
* Template Name: Mortgage Calculator
* Template for the Mortgage Calculator resource page
*
* @package HomeProz
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
get_header();
// Get hero content from ACF or defaults
$hero_title = get_field('hero_title') ?: 'Mortgage Calculator';
$hero_subtitle = get_field('hero_subtitle') ?: 'Estimate your monthly payment and see what you can afford';
$hero_bg = get_field('hero_background');
$has_bg_class = $hero_bg ? 'has-background' : '';
$bg_style = $hero_bg ? 'style="background-image: url(' . esc_url($hero_bg) . ');"' : '';
?>
<main id="primary" class="site-main mortgage-calculator-main">
<!-- Archive Hero -->
<section class="archive-hero <?php echo esc_attr($has_bg_class); ?>" <?php echo $bg_style; ?>>
<?php if ($hero_bg) : ?>
<div class="archive-hero-overlay"></div>
<?php endif; ?>
<div class="container">
<h1 class="archive-hero-title"><?php echo esc_html($hero_title); ?></h1>
<?php if ($hero_subtitle) : ?>
<p class="archive-hero-subtitle"><?php echo esc_html($hero_subtitle); ?></p>
<?php endif; ?>
</div>
</section>
<div class="container">
<div class="mortgage-calculator-layout">
<!-- Main Content -->
<div class="mortgage-calculator-content">
<!-- Calculator Widget -->
<div class="calculator-widget">
<form class="calculator-form" id="mortgage-calculator-form">
<div class="calculator-inputs">
<div class="input-group">
<label for="home-price">Home Price</label>
<div class="input-wrapper input-currency">
<span class="input-prefix">$</span>
<input type="text" id="home-price" name="home-price" value="250,000" inputmode="numeric">
</div>
</div>
<div class="input-group">
<label for="down-payment">Down Payment</label>
<div class="input-row">
<div class="input-wrapper input-currency">
<span class="input-prefix">$</span>
<input type="text" id="down-payment" name="down-payment" value="50,000" inputmode="numeric">
</div>
<div class="input-wrapper input-percent">
<input type="text" id="down-payment-percent" name="down-payment-percent" value="20" inputmode="decimal">
<span class="input-suffix">%</span>
</div>
</div>
</div>
<div class="input-group">
<label for="loan-term">Loan Term</label>
<div class="input-wrapper">
<select id="loan-term" name="loan-term">
<option value="30">30 years</option>
<option value="20">20 years</option>
<option value="15">15 years</option>
<option value="10">10 years</option>
</select>
</div>
</div>
<div class="input-group">
<label for="interest-rate">Interest Rate</label>
<div class="input-wrapper input-percent">
<input type="text" id="interest-rate" name="interest-rate" value="6.5" inputmode="decimal">
<span class="input-suffix">%</span>
</div>
</div>
</div>
<div class="calculator-results">
<div class="result-main">
<span class="result-label">Estimated Monthly Payment</span>
<span class="result-value" id="monthly-payment">$1,264</span>
</div>
<div class="result-breakdown">
<div class="breakdown-item">
<span class="breakdown-label">Principal & Interest</span>
<span class="breakdown-value" id="principal-interest">$1,264</span>
</div>
<div class="breakdown-item">
<span class="breakdown-label">Loan Amount</span>
<span class="breakdown-value" id="loan-amount">$200,000</span>
</div>
<div class="breakdown-item">
<span class="breakdown-label">Total Interest Paid</span>
<span class="breakdown-value" id="total-interest">$255,089</span>
</div>
</div>
</div>
</form>
</div>
<!-- How to Use Section -->
<section class="calculator-guide">
<h2 class="section-title">How to Use This Calculator</h2>
<div class="guide-content">
<p>This calculator gives you a quick estimate of what your monthly mortgage payment might look like. Here's what each field means:</p>
<ul class="guide-list">
<li><strong>Home Price</strong> - The purchase price of the home you're considering.</li>
<li><strong>Down Payment</strong> - The amount you'll pay upfront. A larger down payment means a smaller loan and lower monthly payments. Most conventional loans require at least 3-5% down, though 20% helps you avoid private mortgage insurance (PMI).</li>
<li><strong>Loan Term</strong> - How long you'll take to repay the loan. A 30-year term has lower monthly payments but more total interest. A 15-year term has higher payments but saves significantly on interest.</li>
<li><strong>Interest Rate</strong> - The annual rate charged by your lender. Rates vary based on your credit score, down payment, and market conditions. Check with local lenders for current rates.</li>
</ul>
</div>
</section>
<!-- Important Notes -->
<section class="calculator-notes">
<h2 class="section-title">Keep in Mind</h2>
<div class="notes-content">
<p>This calculator shows principal and interest only. Your actual monthly payment will likely include:</p>
<ul class="notes-list">
<li>Property taxes (varies by location)</li>
<li>Homeowner's insurance</li>
<li>Private mortgage insurance (PMI) if your down payment is less than 20%</li>
<li>HOA fees if applicable</li>
</ul>
<p>For a complete picture of what you can afford, we recommend talking with a mortgage lender who can provide a detailed estimate based on your specific situation.</p>
</div>
</section>
</div>
<!-- Sidebar -->
<aside class="mortgage-calculator-sidebar">
<!-- Quick Tips Widget -->
<div class="sidebar-widget tips-widget">
<h3 class="widget-title">Quick Tips</h3>
<ul class="tips-list">
<li>Aim for a monthly payment that's no more than 28% of your gross monthly income.</li>
<li>A higher credit score typically means a lower interest rate.</li>
<li>Get pre-approved before house hunting to know your budget.</li>
<li>Compare rates from multiple lenders to find the best deal.</li>
</ul>
</div>
<!-- Need Help Widget -->
<div class="sidebar-widget help-widget">
<h3 class="widget-title">Need Help?</h3>
<p class="help-text">Figuring out financing can feel overwhelming. We're happy to connect you with trusted local lenders or answer any questions about the home buying process.</p>
<a href="<?php echo esc_url(home_url('/contact/')); ?>" class="btn btn-primary btn-block">Get in Touch</a>
</div>
<!-- Resources Widget -->
<div class="sidebar-widget resources-widget">
<h3 class="widget-title">More Resources</h3>
<ul class="resource-links">
<li><a href="<?php echo esc_url(home_url('/resources/buyers-guide/')); ?>">Buyer's Guide</a></li>
<li><a href="<?php echo esc_url(home_url('/resources/moving-checklist/')); ?>">Moving Checklist</a></li>
<li><a href="<?php echo esc_url(home_url('/resources/faq/')); ?>">FAQ</a></li>
</ul>
</div>
</aside>
</div>
</div>
</main>
<?php
get_footer();
+1
View File
@@ -11,6 +11,7 @@ import '../template-parts/header/navigation.js';
import '../template-parts/components/hero-section.js';
import '../template-parts/property/property-filters.js';
import '../template-parts/property/property-gallery.js';
import '../template-parts/content/content-mortgage-calculator.js';
(function($) {
'use strict';
+1
View File
@@ -22,6 +22,7 @@
@import '../template-parts/content/content-templates.scss';
@import '../template-parts/content/content-communities.scss';
@import '../template-parts/content/content-resources.scss';
@import '../template-parts/content/content-mortgage-calculator.scss';
@import '../template-parts/property/property-card.scss';
@import '../template-parts/property/property-filters.scss';
@import '../template-parts/property/property-gallery.scss';
@@ -0,0 +1,148 @@
/**
* Mortgage Calculator JavaScript
*
* @package HomeProz
*/
(function($) {
'use strict';
if (!$('.mortgage-calculator-main').length) return;
var MortgageCalculator = {
init: function() {
this.$form = $('#mortgage-calculator-form');
this.$homePrice = $('#home-price');
this.$downPayment = $('#down-payment');
this.$downPaymentPercent = $('#down-payment-percent');
this.$loanTerm = $('#loan-term');
this.$interestRate = $('#interest-rate');
this.$monthlyPayment = $('#monthly-payment');
this.$principalInterest = $('#principal-interest');
this.$loanAmount = $('#loan-amount');
this.$totalInterest = $('#total-interest');
this.bindEvents();
this.calculate();
},
bindEvents: function() {
var self = this;
// Format inputs on blur
this.$homePrice.on('blur', function() {
self.formatCurrency($(this));
self.syncDownPaymentFromPercent();
self.calculate();
});
this.$downPayment.on('blur', function() {
self.formatCurrency($(this));
self.syncDownPaymentPercent();
self.calculate();
});
this.$downPaymentPercent.on('blur', function() {
self.syncDownPaymentFromPercent();
self.calculate();
});
this.$interestRate.on('blur', function() {
self.calculate();
});
this.$loanTerm.on('change', function() {
self.calculate();
});
// Calculate on input (debounced)
var debounceTimer;
this.$form.find('input').on('input', function() {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(function() {
self.calculate();
}, 300);
});
// Prevent form submission
this.$form.on('submit', function(e) {
e.preventDefault();
});
},
parseNumber: function(value) {
if (typeof value === 'number') return value;
return parseFloat(value.replace(/[^0-9.-]/g, '')) || 0;
},
formatCurrency: function($input) {
var value = this.parseNumber($input.val());
if (value > 0) {
$input.val(value.toLocaleString('en-US', { maximumFractionDigits: 0 }));
}
},
formatCurrencyDisplay: function(value) {
return '$' + value.toLocaleString('en-US', { maximumFractionDigits: 0 });
},
syncDownPaymentPercent: function() {
var homePrice = this.parseNumber(this.$homePrice.val());
var downPayment = this.parseNumber(this.$downPayment.val());
if (homePrice > 0) {
var percent = (downPayment / homePrice) * 100;
this.$downPaymentPercent.val(percent.toFixed(1));
}
},
syncDownPaymentFromPercent: function() {
var homePrice = this.parseNumber(this.$homePrice.val());
var percent = this.parseNumber(this.$downPaymentPercent.val());
if (homePrice > 0 && percent >= 0) {
var downPayment = (homePrice * percent) / 100;
this.$downPayment.val(downPayment.toLocaleString('en-US', { maximumFractionDigits: 0 }));
}
},
calculate: function() {
var homePrice = this.parseNumber(this.$homePrice.val());
var downPayment = this.parseNumber(this.$downPayment.val());
var loanTerm = parseInt(this.$loanTerm.val(), 10);
var annualRate = this.parseNumber(this.$interestRate.val());
// Calculate loan amount
var loanAmount = homePrice - downPayment;
if (loanAmount < 0) loanAmount = 0;
// Calculate monthly payment using amortization formula
var monthlyRate = (annualRate / 100) / 12;
var numPayments = loanTerm * 12;
var monthlyPayment = 0;
var totalInterest = 0;
if (loanAmount > 0 && monthlyRate > 0 && numPayments > 0) {
// M = P * [r(1+r)^n] / [(1+r)^n - 1]
var compoundedRate = Math.pow(1 + monthlyRate, numPayments);
monthlyPayment = loanAmount * (monthlyRate * compoundedRate) / (compoundedRate - 1);
totalInterest = (monthlyPayment * numPayments) - loanAmount;
} else if (loanAmount > 0 && monthlyRate === 0) {
// 0% interest rate
monthlyPayment = loanAmount / numPayments;
totalInterest = 0;
}
// Update display
this.$monthlyPayment.text(this.formatCurrencyDisplay(Math.round(monthlyPayment)));
this.$principalInterest.text(this.formatCurrencyDisplay(Math.round(monthlyPayment)));
this.$loanAmount.text(this.formatCurrencyDisplay(Math.round(loanAmount)));
this.$totalInterest.text(this.formatCurrencyDisplay(Math.round(totalInterest)));
}
};
$(document).ready(function() {
MortgageCalculator.init();
});
})(jQuery);
@@ -0,0 +1,355 @@
/**
* Mortgage Calculator Page Styles
*
* @package HomeProz
*/
// Main Layout
.mortgage-calculator-main {
padding-bottom: 4rem;
}
.mortgage-calculator-layout {
display: grid;
grid-template-columns: 1fr;
gap: 2rem;
padding-top: 2rem;
@media (min-width: 1024px) {
grid-template-columns: 1fr 350px;
gap: 3rem;
}
}
// Calculator Widget
.calculator-widget {
background-color: var(--color-bg-card);
border-radius: 0.5rem;
padding: 2rem;
margin-bottom: 2rem;
@media (max-width: 640px) {
padding: 1.5rem;
}
}
.calculator-form {
display: flex;
flex-direction: column;
gap: 2rem;
@media (min-width: 768px) {
flex-direction: row;
gap: 3rem;
}
}
.calculator-inputs {
flex: 1;
display: flex;
flex-direction: column;
gap: 1.25rem;
}
.input-group {
label {
display: block;
font-family: 'Times New Roman', Times, serif;
font-size: 0.875rem;
font-weight: 700;
color: var(--color-text);
margin-bottom: 0.5rem;
}
}
.input-row {
display: flex;
gap: 0.75rem;
.input-currency {
flex: 2;
}
.input-percent {
flex: 1;
min-width: 80px;
}
}
.input-wrapper {
position: relative;
display: flex;
align-items: center;
input,
select {
width: 100%;
padding: 0.75rem 1rem;
background-color: var(--color-bg-dark);
border: 1px solid var(--color-border);
border-radius: 0.25rem;
color: var(--color-text);
font-size: 1rem;
font-family: inherit;
&:focus {
outline: none;
border-color: var(--color-accent);
}
}
select {
appearance: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23B0B0B0' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 0.75rem center;
padding-right: 2.5rem;
cursor: pointer;
}
&.input-currency {
input {
padding-left: 1.75rem;
}
.input-prefix {
position: absolute;
left: 0.75rem;
color: var(--color-text-muted);
pointer-events: none;
}
}
&.input-percent {
input {
padding-right: 2rem;
}
.input-suffix {
position: absolute;
right: 0.75rem;
color: var(--color-text-muted);
pointer-events: none;
}
}
}
// Calculator Results
.calculator-results {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
@media (min-width: 768px) {
padding-left: 2rem;
border-left: 1px solid var(--color-border);
}
@media (max-width: 767px) {
padding-top: 1.5rem;
border-top: 1px solid var(--color-border);
}
}
.result-main {
text-align: center;
margin-bottom: 1.5rem;
.result-label {
display: block;
font-size: 0.875rem;
color: var(--color-text-muted);
margin-bottom: 0.5rem;
}
.result-value {
display: block;
font-family: var(--font-display);
font-size: 2.5rem;
color: var(--color-accent-light);
line-height: 1.2;
@media (max-width: 640px) {
font-size: 2rem;
}
}
}
.result-breakdown {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.breakdown-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.5rem 0;
border-bottom: 1px solid var(--color-border);
&:last-child {
border-bottom: none;
}
.breakdown-label {
font-size: 0.875rem;
color: var(--color-text-muted);
}
.breakdown-value {
font-size: 0.9375rem;
font-weight: 600;
color: var(--color-text);
}
}
// Section Title (reuse from property page pattern)
.calculator-guide,
.calculator-notes {
margin-bottom: 2rem;
.section-title {
font-size: 1.25rem;
margin-bottom: 1.25rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--color-border);
}
}
.guide-content,
.notes-content {
p {
color: var(--color-text-muted);
line-height: 1.7;
margin-bottom: 1rem;
}
}
.guide-list,
.notes-list {
list-style: none;
margin: 0;
padding: 0;
li {
position: relative;
padding-left: 1.5rem;
margin-bottom: 0.75rem;
color: var(--color-text-muted);
line-height: 1.6;
&::before {
content: '';
position: absolute;
left: 0;
top: 0.5rem;
width: 6px;
height: 6px;
background-color: var(--color-accent);
border-radius: 50%;
}
strong {
color: var(--color-text);
}
}
}
// Sidebar
.mortgage-calculator-sidebar {
display: flex;
flex-direction: column;
gap: 1.5rem;
@media (min-width: 1024px) {
align-self: start;
}
}
// Sidebar Widgets (reuse patterns from property page)
.mortgage-calculator-sidebar .sidebar-widget {
background-color: var(--color-bg-card);
border-radius: 0.5rem;
padding: 1.5rem;
}
.mortgage-calculator-sidebar .widget-title {
font-family: 'Times New Roman', Times, serif;
font-size: 18px;
font-weight: 700;
margin-bottom: 1rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--color-border);
}
// Tips Widget
.tips-list {
list-style: none;
margin: 0;
padding: 0;
li {
position: relative;
padding-left: 1.25rem;
margin-bottom: 0.875rem;
font-size: 0.9375rem;
color: var(--color-text-muted);
line-height: 1.5;
&::before {
content: '';
position: absolute;
left: 0;
top: 0.5rem;
width: 5px;
height: 5px;
background-color: var(--color-accent);
border-radius: 50%;
}
&:last-child {
margin-bottom: 0;
}
}
}
// Help Widget
.help-text {
font-size: 0.9375rem;
color: var(--color-text-muted);
line-height: 1.6;
margin-bottom: 1.25rem;
}
.btn-block {
display: block;
width: 100%;
text-align: center;
}
// Resources Widget
.resource-links {
list-style: none;
margin: 0;
padding: 0;
li {
margin-bottom: 0.625rem;
&:last-child {
margin-bottom: 0;
}
}
a {
color: var(--color-text-muted);
font-size: 0.9375rem;
text-decoration: none;
&:hover {
color: var(--color-accent-light);
}
}
}