CSS Variables
CSS custom properties are a platform feature and JAF does nothing special to them.
Define them where you like and use them anywhere:
source
<style global>
:root {
--primary: #3B82F6;
--secondary: #64748B;
--radius: 8px;
}
</style>
<style>
.btn {
background: var(--primary);
border-radius: var(--radius);
}
</style>
:root is one of the selectors scoping leaves alone, so a
<style global> block on :root defines
variables for the whole app.
Setting one from state
This is the part JAF adds.
The style: directive writes a custom property, so a variable can
come from component state and every rule using it follows:
source
<script>
let brand = '#22d3ee'
let radius = 10
</script>
<div class="preview" style:--brand={brand} style:--radius={radius + 'px'}>
<button class="chip">Buy now</button>
</div>
<div class="controls">
<button class="swatch cyan" onclick="brand = '#22d3ee'"></button>
<button class="swatch violet" onclick="brand = '#a78bfa'"></button>
<button class="swatch amber" onclick="brand = '#fbbf24'"></button>
<label>
Corner
<input type="range" bind:value={radius} min="0" max="24" />
</label>
</div>
<style>
.preview {
padding-bottom: 0.75rem;
}
.chip {
background: var(--brand);
color: #0a0a0b;
border: 0;
border-radius: var(--radius);
padding: 0.5rem 1.1rem;
font: inherit;
font-weight: 600;
}
.swatch {
width: 1.6rem;
height: 1.6rem;
margin-right: 0.4rem;
border: 0;
border-radius: 50%;
cursor: pointer;
}
.cyan { background: #22d3ee; }
.violet { background: #a78bfa; }
.amber { background: #fbbf24; }
.controls label {
display: block;
margin-top: 0.7rem;
font-size: 0.85rem;
}
.controls input[type="range"] {
width: 11rem;
padding: 0;
border: 0;
background: none;
accent-color: var(--accent);
vertical-align: middle;
}
</style>
output
style: writes the value, it does not add units.
style:--radius={10} sets --radius: 10, which the
CSS parser then throws away. Write
style:--radius={radius + 'px'}.
Variables as a component's dials
A component that reads its own sizes from variables gives its user somewhere to turn:
source
<!-- Card.html -->
<div class="card">
<slot />
</div>
<style>
.card {
--card-padding: 1.5rem;
--card-radius: 8px;
padding: var(--card-padding);
border-radius: var(--card-radius);
background: #1e293b;
}
</style>
A style attribute on the tag is merged onto the component's root
element, so the inline value beats the class rule:
source
<Card style="--card-padding: 2rem; --card-radius: 16px">
Roomier, rounder, same component.
</Card>
Fallbacks
var() takes a second argument, which is what the component uses
when nobody set the variable:
source
<style>
.card {
background: var(--card-bg, white);
color: var(--card-text, inherit);
padding: var(--card-padding, 1rem);
}
</style>
Related
- style: directive - setting any CSS property from state
-
Global Styles - where a
:rootblock belongs