Components and Reactivity
- Build a temperature converter using
<let>,<const>, and:=binding - Add conditional status messages and reactive CSS custom properties
- Extract reusable markup into a custom component in
tags/
In this tutorial, we will build an interactive app that converts temperatures between Fahrenheit and Celsius.
Introducing a Tag
First, let's gather a temperature from the user with a native HTML <input>:
<input type="number">input type="number"Adding State
To track the temperature, we declare mutable state with a tag variable using <let>:
<let/degF=80>
<input type="number" value=degF>
<div>It's ${degF}°F</div>let/degF=80
input type="number" value=degF
div -- It's ${degF}°FSyncing State
The input starts at 80, but typing into it does not yet update degF. Listening to the native input event is tempting:
// ⚠️ There's a better way to do this!
<let/degF=80>
<input
type="number"
value=degF
onInput(e, el) {
degF = parseFloat(el.value);
}
>
<div>It's ${degF}°F</div>// ⚠️ There's a better way to do this!
let/degF=80
input
,type="number"
,value=degF
,onInput(e, el) {
degF = parseFloat(el.value);
}
div -- It's ${degF}°FIn HTML, value= sets the default value, not the live value, so the input can fall out of sync with degF. Instead, we use Marko's controllable valueChange handler:
<let/degF=80>
<input
type="number"
value=degF
valueChange(value) {
degF = parseFloat(value);
}
>
<div>It's ${degF}°F</div>let/degF=80
input
,type="number"
,value=degF
,valueChange(value) {
degF = parseFloat(value);
}
div -- It's ${degF}°FBecause this pattern is so common, Marko provides the binding shorthand with a refining function:
<let/degF=80>
<input type="number" value:parseFloat:=degF>
<div>It's ${degF}°F</div>let/degF=80
input type="number" value:parseFloat:=degF
div -- It's ${degF}°FAdding Computed Values
Next, we derive Celsius from degF with <const>:
<let/degF=80>
<const/degC=((degF - 32) * 5) / 9>
<input type="number" value:parseFloat:=degF>
<div>${degF}°F ↔ ${degC.toFixed(1)}°C</div>let/degF=80
const/degC=((degF - 32) * 5) / 9
input type="number" value:parseFloat:=degF
div -- ${degF}°F ↔ ${degC.toFixed(1)}°CBecause degC is a tag variable derived from degF, it recomputes automatically whenever degF changes.
Using Conditionals
Let's add temperature feedback with <if> and <else>:
<let/degF=80>
<const/degC=((degF - 32) * 5) / 9>
<input type="number" value:parseFloat:=degF>
<div>${degF}°F ↔ ${degC.toFixed(1)}°C</div>
<if=(degF > 90)>It's <strong>hot</strong> 🥵</if>
<else if=(degF > 60)>Lovely day! 😎</else>
<else if=(degF < 32)>Brrrrr 🥶</else>let/degF=80
const/degC=((degF - 32) * 5) / 9
input type="number" value:parseFloat:=degF
div -- ${degF}°F ↔ ${degC.toFixed(1)}°C
if=(degF > 90)
-- It's${" "}
strong -- hot
-- ${" "}🥵
else if=(degF > 60) -- Lovely day! 😎
else if=(degF < 32) -- Brrrrr 🥶Adding Styles
Now let's visualize the reading with a CSS gauge whose needle rotates via a custom property:
<let/degF=80>
<const/degC=((degF - 32) * 5) / 9>
<input type="number" value:parseFloat:=degF>
<div>${degF}°F ↔ ${degC.toFixed(1)}°C</div>
<div class="gauge">
<div
class="needle"
style={ "--rotation": `${Math.min(180, Math.max(0, (degF * 180) / 100))}deg` }
/>
</div>
<if=(degF > 90)>It's <strong>hot</strong> 🥵</if>
<else if=(degF > 60)>Lovely day! 😎</else>
<else if=(degF < 32)>Brrrrr 🥶</else>
<style>
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
</style>let/degF=80
const/degC=((degF - 32) * 5) / 9
input type="number" value:parseFloat:=degF
div -- ${degF}°F ↔ ${degC.toFixed(1)}°C
div class="gauge"
div
,class="needle"
,style={ "--rotation": `${Math.min(180, Math.max(0, (degF * 180) / 100))}deg` }
if=(degF > 90)
-- It's${" "}
strong -- hot
-- ${" "}🥵
else if=(degF > 60) -- Lovely day! 😎
else if=(degF < 32) -- Brrrrr 🥶
style
---
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
---Reusable Components
Great! This template is growing, so let's extract the gauge, status message, and styles into a dedicated <gauge> component:
<let/degF=80>
<const/degC=((degF - 32) * 5) / 9>
<input type="number" value:parseFloat:=degF>
<div>${degF}°F ↔ ${degC.toFixed(1)}°C</div>
<gauge temperature=degF/>let/degF=80
const/degC=((degF - 32) * 5) / 9
input type="number" value:parseFloat:=degF
div -- ${degF}°F ↔ ${degC.toFixed(1)}°C
gauge temperature=degF<div class="gauge">
<div
class="needle"
style={
"--rotation": `${Math.min(180, Math.max(0, (input.temperature * 180) / 100))}deg`,
}
/>
</div>
<if=(input.temperature > 90)>It's <strong>hot</strong> 🥵</if>
<else if=(input.temperature > 60)>Lovely day! 😎</else>
<else if=input.temperature < 32>Brrrrr 🥶</else>
<style>
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
</style>export interface Input {
temperature: number;
}
<div class="gauge">
<div
class="needle"
style={
"--rotation": `${Math.min(180, Math.max(0, (input.temperature * 180) / 100))}deg`,
}
/>
</div>
<if=(input.temperature > 90)>It's <strong>hot</strong> 🥵</if>
<else if=(input.temperature > 60)>Lovely day! 😎</else>
<else if=(input.temperature < 32)>Brrrrr 🥶</else>
<style>
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
</style>div class="gauge"
div
,class="needle"
,style={
"--rotation": `${Math.min(180, Math.max(0, (input.temperature * 180) / 100))}deg`,
}
if=input.temperature > 90
-- It's${" "}
strong -- hot
-- ${" "}🥵
else if=input.temperature > 60 -- Lovely day! 😎
else if=input.temperature < 32 -- Brrrrr 🥶
style
---
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
---export interface Input {
temperature: number;
}
div class="gauge"
div
,class="needle"
,style={
"--rotation": `${Math.min(180, Math.max(0, (input.temperature * 180) / 100))}deg`,
}
if=(input.temperature > 90)
-- It's${" "}
strong -- hot
-- ${" "}🥵
else if=(input.temperature > 60) -- Lovely day! 😎
else if=(input.temperature < 32) -- Brrrrr 🥶
style
---
.gauge {
position: relative;
width: 8rem;
height: 4rem;
border-radius: 4rem 4rem 0 0;
background: conic-gradient(
from 270deg at 50% 100%,
lightblue,
blue,
green,
orange,
red 180deg
);
}
.needle {
position: absolute;
box-sizing: border-box;
width: 4rem;
height: 4px;
bottom: -2px;
background: black;
border: 1px solid white;
transform-origin: right;
transform: rotate(var(--rotation));
}
---Place gauge.marko inside a tags/ directory so Marko discovers it automatically.
Going Further
Here are a few ways to extend this app:
- Add another unit such as Kelvin or Delisle.
- Add a unit toggle so the input can start in Celsius or Fahrenheit.
- Calculate wind chill by adding a wind speed input.
- Generalize the converter to handle weights, volumes, or distances.
Next Steps
Contributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.