Add conversion example card

This commit is contained in:
Codex
2026-03-07 06:33:59 +00:00
parent fcbf803fe4
commit e20fcbb3df
2 changed files with 70 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
import type { CalculatorDef } from '$lib/data/calculators'; import type { CalculatorDef } from '$lib/data/calculators';
import { page } from '$app/stores'; import { page } from '$app/stores';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import QuickConversionExample from '$lib/components/QuickConversionExample.svelte';
import QuickConversionTable from '$lib/components/QuickConversionTable.svelte'; import QuickConversionTable from '$lib/components/QuickConversionTable.svelte';
export let config: CalculatorDef; export let config: CalculatorDef;
@@ -114,6 +115,7 @@
{/if} {/if}
</div> </div>
<QuickConversionTable {config} /> <QuickConversionTable {config} />
<QuickConversionExample {config} />
</div> </div>
<style> <style>

View File

@@ -0,0 +1,68 @@
<script lang="ts">
import { solve } from '$lib/engine';
import type { CalculatorDef } from '$lib/data/calculators';
export let config: CalculatorDef;
const exampleInput = 15;
const supportsExample =
config.type === 'standard' &&
typeof config.factor === 'number' &&
!!config.labels.in1 &&
!!config.labels.in2;
$: result = supportsExample
? solve(config, 1, exampleInput.toString(), '', '')
: null;
$: reverseResult = supportsExample
? solve(config, 2, '', '1', '')
: null;
$: offset = config.offset ?? 0;
$: formulaExpression = supportsExample
? `${exampleInput} × ${config.factor}${offset ? ` + ${offset}` : ''}`
: '';
</script>
{#if supportsExample && result}
<section class="example-card">
<h3>How to convert {config.labels.in1} to {config.labels.in2}</h3>
<p class="example-note">
1 {config.labels.in1} = {config.factor}{config.offset ? ` + ${config.offset}` : ''} {config.labels.in2}
</p>
<p class="example-note">
1 {config.labels.in2} = {reverseResult?.val1 ?? '—'} {config.labels.in1}
</p>
<p class="example">
Example: convert {exampleInput} {config.labels.in1} to {config.labels.in2}: {formulaParts} = {result.val2} {config.labels.in2}
</p>
</section>
{/if}
<style>
.example-card {
margin: 0 2rem 2rem;
padding: 1.25rem 1.5rem;
border-radius: 12px;
background: var(--section-bg);
border: 1px solid var(--border);
}
.example-card h3 {
margin: 0 0 0.25rem;
font-size: 1rem;
font-weight: 600;
}
.example-note {
font-size: 0.85rem;
margin: 0 0 0.35rem;
color: var(--text-muted);
}
.example {
margin: 0;
font-size: 0.95rem;
font-weight: 500;
}
</style>