Add conversion example card
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import type { CalculatorDef } from '$lib/data/calculators';
|
||||
import { page } from '$app/stores';
|
||||
import { onMount } from 'svelte';
|
||||
import QuickConversionExample from '$lib/components/QuickConversionExample.svelte';
|
||||
import QuickConversionTable from '$lib/components/QuickConversionTable.svelte';
|
||||
|
||||
export let config: CalculatorDef;
|
||||
@@ -114,6 +115,7 @@
|
||||
{/if}
|
||||
</div>
|
||||
<QuickConversionTable {config} />
|
||||
<QuickConversionExample {config} />
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
||||
68
hdyc-svelte/src/lib/components/QuickConversionExample.svelte
Normal file
68
hdyc-svelte/src/lib/components/QuickConversionExample.svelte
Normal 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>
|
||||
Reference in New Issue
Block a user