<!-- @component A calculator that applies `mode` to `result` and the entered number, optionally rounding to the configured precision. --> <!-- @module Module docs via the HTML comment form, alongside the component comment above, which documents the component itself. See `Card.svelte` for the in-script JSDoc form of each. --> <script lang="ts"> import { add, multiply, roundTo, type MathConfig } from './math.js'; let { result = $bindable(0), config, mode = 'add', disabled = false, onresult }: { /** Current result (bindable). */ result?: number; /** Math configuration. */ config?: MathConfig; /** Operation mode. */ mode?: 'add' | 'multiply'; /** Disable the calculator. */ disabled?: boolean; /** Called with each new result. */ onresult?: (value: number) => void; } = $props(); let inputValue = $state(0); const calculate = () => { if (disabled) return; const op = mode === 'add' ? add : multiply; const value = op(result, inputValue); result = config?.round ? roundTo(value, config.precision) : value; onresult?.(result); }; </script> <div class="calculator"> <output>{result}</output> <input type="number" bind:value={inputValue} {disabled} /> <button onclick={calculate} {disabled}> {mode === 'add' ? 'Add' : 'Multiply'} </button> </div> <style> .calculator { display: flex; flex-direction: column; gap: 0.5rem; padding: 1rem; border: 1px solid #ccc; border-radius: 4px; } output { font-size: 2rem; font-weight: bold; } </style>
{ "path": "Calculator.svelte", "declarations": [ { "name": "Calculator", "kind": "component", "docComment": "A calculator that applies `mode` to `result` and the entered number,\noptionally rounding to the configured precision.", "sourceLine": 14, "props": [ { "name": "result", "type": "number", "optional": true, "description": "Current result (bindable).", "defaultValue": "0", "bindable": true }, { "name": "config", "type": "MathConfig", "optional": true, "description": "Math configuration." }, { "name": "mode", "type": "\"add\" | \"multiply\"", "typeInfo": { "kind": "union", "members": [ { "kind": "literal", "value": "add", "text": "\"add\"" }, { "kind": "literal", "value": "multiply", "text": "\"multiply\"" } ] }, "optional": true, "description": "Operation mode.", "defaultValue": "'add'" }, { "name": "disabled", "type": "boolean", "optional": true, "description": "Disable the calculator.", "defaultValue": "false" }, { "name": "onresult", "type": "(value: number) => void", "optional": true, "description": "Called with each new result.", "parameters": [ { "name": "value", "type": "number" } ], "returnType": "void" } ] } ], "moduleComment": "Module docs via the HTML comment form, alongside the component comment\nabove, which documents the component itself. See `Card.svelte` for the\nin-script JSDoc form of each.", "dependencies": [ "math.ts" ], "dependents": [ "index.ts" ] }