Todo App: Markup and Styles
elements man recipes/todo-app-markupThe complete markup and stylesheet for the todo app (opens in a new tab). The recipe shows the page one section at a time. This page has the whole template and the whole stylesheet, so the page looks finished the first time it renders. Copy both files.
The stylesheet is mostly layout. The card, composer, tabs, badges, checkbox,
rename field, star, delete button, empty state and footer are @elements/ui
templates, each imported from its own path. Every color, space and duration
below is a token, so its value changes with dark mode and with a theme, and the
stylesheet needs no rules for either.
The template
The top of template.ehtml imports the names the functions in the recipe use,
and has one import line per @elements/ui template. The functions go between the imports and the markup.
app/pages/home/template.ehtmlimport "./style.css"; import { LifecycleEvent, LiveView, flush } from "@elements/app"; import { Badge } from "@elements/ui/badge"; import { Button } from "@elements/ui/button"; import { Card, CardFooter } from "@elements/ui/card"; import { Checkbox } from "@elements/ui/checkbox"; import { Empty, EmptyHeader, EmptyTitle } from "@elements/ui/empty"; import { Icon } from "@elements/ui/icon"; import { Input } from "@elements/ui/input"; import { Tabs, TabsList, TabsTrigger } from "@elements/ui/tabs"; import { Toggle } from "@elements/ui/toggle"; import { byPriority, Draft, Edit, Filter, matches, parseFilter, Todo, View } from "./models";
app/pages/home/template.ehtml<html class="page-todos" ( todos: LiveView<Todo>, view: View, private draft: Draft = { title: "" }, private edit: Edit = { id: "", title: "", }, ) oninit={() => window.addEventListener("popstate", () => onPopState(view))}> <head> <title>Todos</title> </head> <body> <main class="page-shell is-form todo-page"> <h1 class="todo-heading">Todos</h1> <Card class="todo-card"> <form class="todo-composer" onsubmit={() => onAdd(todos, draft)}> <Input class="todo-input" value={draft.title} placeholder="What needs doing?" attrs={{ "aria-label": "New todo" }} /> <Button variant="primary" type="submit">Add</Button> </form> <Tabs class="todo-tabs" value={view.filter} onValueChange={(filter) => onFilter(view, parseFilter(filter))}> <TabsList label="Filter"> <TabsTrigger value="all">All <Badge variant="secondary">{countOf(todos, "all")}</Badge></TabsTrigger> <TabsTrigger value="active">Active <Badge variant="secondary">{countOf(todos, "active")}</Badge></TabsTrigger> <TabsTrigger value="done">Done <Badge variant="secondary">{countOf(todos, "done")}</Badge></TabsTrigger> </TabsList> </Tabs> <ul class="todo-list" e:if={countOf(todos, view.filter) > 0}> <li e:for={todo of todos.filter((todo) => matches(todo, view.filter)).sort(byPriority)} class={["todo-item", todo.done && "is-done", todo.leaving && "is-leaving"]} onanimationend={() => onLeaveEnd(todos, todo)}> <Checkbox checked={todo.done} attrs={{ "aria-label": `Done: ${todo.title}` }} onCheckedChange={(done) => onToggle(todos, todo, done)} /> <Input class="todo-text" e:if={edit.id === todo.id} value={edit.title} attrs={{ "aria-label": "Edit todo" }} oninsert={(e: LifecycleEvent) => (e.target as HTMLInputElement).select()} onblur={() => onCommit(todos, edit, todo)} onkeydown={(e) => onEditKeyDown(e, edit)} /> <button class="todo-label" e:else type="button" aria-label={`Edit ${todo.title}`} onclick={() => onEdit(edit, todo)}> {todo.title} </button> <Toggle class="todo-star" size="sm" pressed={todo.starred} label={`Star ${todo.title}`} onPressedChange={(starred) => onStar(todos, todo, starred)}> <Icon name="star" /> </Toggle> <Button class="todo-remove" variant="ghost" size="sm" icon label={`Delete ${todo.title}`} onclick={() => onRemove(todos, todo)}> <Icon name="x" /> </Button> </li> </ul> <Empty e:else> <EmptyHeader> <EmptyTitle e:if={todos.length === 0}>Nothing on the list</EmptyTitle> <EmptyTitle e:elseif={view.filter === "active"}>All caught up</EmptyTitle> <EmptyTitle e:else>Nothing finished yet</EmptyTitle> </EmptyHeader> </Empty> <CardFooter e:if={countOf(todos, "done") > 0}> <Button variant="ghost" size="sm" onclick={() => onClearDone(todos)}>Clear done</Button> </CardFooter> </Card> </main> </body> </html>
Card is the container, and the composer, tabs, list, empty state and
CardFooter are its children in order. Checkbox and Toggle bind two-way
to the row's done and starred and call their handlers with the new value.
A Button is type="button" unless told otherwise, so the composer's says
type="submit"; the delete button is icon with a label, which is its
accessible name, and the star's label stays the same in both states because
aria-pressed carries the change. The todo's title is a <button>, not a
span, so it can take focus and be clicked to rename. The stylesheet removes its
border and background so it looks like text.
The stylesheet
Save the stylesheet before the template (start/patterns), so that the reload
that first shows the new markup also loads its style rules.
app/pages/home/style.css@import "#app/shared/styles/page.css"; .todo-page { padding-block: var(--space-10); } .todo-heading { margin-block: 0 var(--space-6); } /* The card's own parts would pad every row the same; the list wants its rows edge to edge, so the card is laid out here instead. */ .todo-composer { display: flex; gap: var(--space-2); padding: var(--space-4); } .todo-input { flex: 1; min-width: 0; } .todo-tabs { padding-inline: var(--space-4); } .todo-list { margin: 0; padding: 0; list-style: none; } .todo-item { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2) var(--space-4); border-bottom: 1px solid var(--rule-soft); } .todo-item:last-child { border-bottom: 0; } /* The label is a button so it can be focused and clicked to rename, but it reads as text: no box, left-aligned, wrapping. */ .todo-label { flex: 1; min-width: 0; height: auto; padding: var(--space-1) 0; border: 0; background: none; color: var(--ink); font: inherit; text-align: start; justify-content: flex-start; overflow-wrap: anywhere; } .todo-text { flex: 1; min-width: 0; } .todo-item.is-done .todo-label { color: var(--ink-dim); text-decoration: line-through; } .todo-star { color: var(--ink-dim); } .todo-star[data-state="on"] { color: var(--warning); } .todo-star[data-state="on"] .icon { fill: currentColor; } .todo-remove { color: var(--ink-dim); } .todo-remove:hover { color: var(--danger); } /* The delete waits on this animation's animationend; LEAVE_MS in the template matches --duration-normal as the backstop. */ .todo-item.is-leaving { animation: todo-leave var(--duration-normal) var(--ease-in) forwards; } @keyframes todo-leave { to { opacity: 0; transform: translateX(var(--space-4)); } } @media (prefers-reduced-motion: reduce) { .todo-item.is-leaving { animation-duration: 1ms; } }
The empty state's padding, the footer's strip, the tabs' underline and the
checkbox, toggle and button styles come with their templates, so no rule here
names them; .todo-star only colors the toggle, through the data-state the
template sets. A delete finishes when the
leave animation ends: onLeaveEnd runs on the animation's animationend and
deletes the row. LEAVE_MS in the template is the delay of a timer that
deletes the row if the event never fires, set to match --duration-normal. If
you change one, change the other.