theming
A project theme in theme.yaml, light and dark.
Run it
cargo run -p duolith-cli -- dev examples/theming # live, reloads on save
cargo run -p duolith-cli -- build examples/theming # static output in examples/theming/dist
The folder on the web: examples/theming.
Features it shows
| Feature | Where | Screenshot |
|---|---|---|
look: platform, web-look: the kit as iOS and as Material | theming theme.yaml | theming-ios.png, theming-material.png |
duration, easing, shape, control tokens; parts: overrides of tokens, variants and slots | theming theme.yaml (Button, Card) | theming-light.png |
A font of your own: @font-face from public/fonts/ as the theme's font.family (Caladea, SIL Open Font License) | theming pages/index.duo, theme.yaml | theming-light.png |
Fonts of your own: @font-face from public/ shipped in the app and registered by the shells, the theme's font.family using it (E0450 to E0452) | theming pages/index.duo, theme.yaml, public/fonts/ | crates/emit-native/src/fonts.rs tests, native-output.test.ts |
Source
pages/index.duo
---
import { state } from "@duolith/client";
let quantity = state(1);
let note = state("");
function add() {
quantity = quantity + 1;
}
function remove() {
if (quantity > 1) quantity = quantity - 1;
}
---
<screen title="Order" class="p-4 gap-4 bg-canvas">
<view class="row items-center justify-between">
<text class="text-title text-ink">Order</text>
<text class="px-3 py-1 rounded-full bg-brand text-caption font-bold text-on-brand">New</text>
</view>
<view class="overflow-hidden rounded-lg bg-surface border shadow-md">
<view class="relative w-full aspect-video bg-primary">
<text class="absolute bottom-3 left-3 text-heading text-on-primary">Harbour blend</text>
</view>
<view class="gap-3 p-4">
<text class="line-clamp-2 text-body text-muted">
A slow roast with dark chocolate and a little orange peel, picked from the boats at the quay
each Thursday morning.
</text>
<view class="row wrap gap-2">
<text class="px-2 py-1 rounded-sm border border-primary text-caption text-primary">
Whole bean
</text>
<text class="px-2 py-1 rounded-sm border border-primary text-caption text-primary">
250 g
</text>
<text class="px-2 py-1 rounded-sm border border-brand text-caption text-brand">
Limited
</text>
</view>
</view>
</view>
<view class="row items-center justify-between p-4 rounded-lg bg-surface border shadow-sm">
<view class="gap-1">
<text class="text-label text-ink">Quantity</text>
<text class="text-caption text-muted">£9.50 each</text>
</view>
<view class="row items-center gap-3">
<button
class="w-10 h-10 rounded-md bg-raised text-heading text-ink pressed:bg-border hover:bg-border"
label="One fewer"
on:press={remove}
>
−
</button>
<text class="min-w-6 text-center text-heading text-ink">{quantity}</text>
<button
class="w-10 h-10 rounded-md bg-raised text-heading text-ink pressed:bg-border hover:bg-border"
label="One more"
on:press={add}
>
+
</button>
</view>
</view>
<input
class="px-3 py-3 rounded-md bg-surface border text-body text-ink focus:border-primary"
label="Note for the roaster"
placeholder="A note for the roaster"
value={note}
on:input={(event) => (note = event.value)}
/>
<button
class="py-3 rounded-md bg-primary text-label text-on-primary shadow-sm pressed:opacity-75 hover:shadow-md"
>
Add to basket
</button>
<text class="text-center text-caption text-muted">
Brand colours, radius, shadows and font all come from theme.yaml.
</text>
</screen>
<style>
/* The theme's font, shipped with the app: served from public/ on the web, in the app on iOS and Android. */
@font-face {
font-family: "Caladea";
font-weight: 400;
src: url("/fonts/Caladea-Regular.ttf") format("truetype");
}
@font-face {
font-family: "Caladea";
font-weight: 700;
src: url("/fonts/Caladea-Bold.ttf") format("truetype");
}
</style>theme.yaml
# A project theme. Every token here replaces the default one of the same name;
# everything left out keeps its default (spec/styling/default-theme.yaml).
# Checked against spec/schemas/theme.schema.json when the app is built.
# Follow the device setting. Use light or dark to always show one of them.
dark: system
color:
canvas: { light: "#f4f1ea", dark: "#0f1514" }
surface: { light: "#fffdf8", dark: "#172120" }
raised: { light: "#e9e3d6", dark: "#22302e" }
border: { light: "#d9d0bd", dark: "#2d3d3b" }
primary: { light: "#0f766e", dark: "#5eead4" }
on-primary: { light: "#f0fdfa", dark: "#042f2e" }
ink: { light: "#1f2a28", dark: "#e7f0ee" }
muted: { light: "#66706d", dark: "#93a3a0" }
# A new token: bg-brand, text-brand and border-brand now exist.
brand: { light: "#be185d", dark: "#f9a8d4" }
on-brand: { light: "#fff1f7", dark: "#3b0820" }
radius:
xs: 1px
sm: 2px
md: 4px
lg: 6px
shadow:
sm: { light: "0 1px 0 rgb(31 42 40 / 0.10)", dark: "0 1px 0 rgb(0 0 0 / 0.6)" }
md: { light: "0 3px 0 rgb(31 42 40 / 0.10)", dark: "0 3px 0 rgb(0 0 0 / 0.6)" }
text:
title: { size: 32px, line: 38px, weight: 800 }
heading: { size: 21px, line: 27px, weight: 700 }
# Caladea is the app's own font: pages/index.duo declares it with
# @font-face from public/fonts/ (SIL Open Font License, public/fonts/OFL.txt),
# so the web serves it and the iOS and Android builds ship it in the app.
font:
family: "Caladea, Georgia, 'Times New Roman', serif"
# Look like iOS on an iPhone and like Material on Android (decision 0103).
# The web follows the browser's platform; web-look: ios, material or
# default picks one instead.
look: platform
web-look: user-agent
# Motion and shape tokens: what the kit's parts and the motion presets read.
duration:
normal: 180ms
shape:
chip: "{radius.sm}"
# One part's own tokens, variants and slots, under every look.
parts:
Button:
tokens:
press-scale: "0.96"
variants:
primary:
background: "{color.brand}"
ink: "{color.on-brand}"
slots:
label:
weight: "700"
Card:
tokens:
radius: "{radius.lg}"