Kanban.csproj
Kanban.csproj
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>Kanban</AssemblyName>
<RootNamespace>Kanban</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.4" PrivateAssets="all" />
</ItemGroup>
</Project>
Program.cs
Program.cs
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Kanban;
using Kanban.Services;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
// <App> replaces <div id="app"> in wwwroot/index.html.
builder.RootComponents.Add<App>("#app");
// HeadOutlet lets <PageTitle> set the browser tab title.
builder.RootComponents.Add<HeadOutlet>("head::after");
// One board for the whole app. Components receive it with @inject.
// In Blazor WebAssembly a singleton lives as long as the browser tab.
builder.Services.AddSingleton<BoardState>();
await builder.Build().RunAsync();
App.razor
App.razor
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<h1>Not found</h1>
<p>There is no page at this address.</p>
</NotFound>
</Router>
_Imports.razor
_Imports.razor
@using System.Net.Http
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Kanban
@using Kanban.Components
@using Kanban.Services
Pages/Home.razor
Pages/Home.razor
@page "/"
@inject BoardState Board
<PageTitle>Kanban board</PageTitle>
<h1>Kanban board</h1>
<form class="add" @onsubmit="AddCard">
<input placeholder="New card" maxlength="60" aria-label="New card title" @bind="draft" @bind:event="oninput" />
<button class="primary" type="submit" disabled="@string.IsNullOrWhiteSpace(draft)">Add</button>
</form>
<p class="muted">@Board.Total cards, @(Board.CardsIn(Stage.Done).Count) done</p>
<div class="board">
@foreach (var stage in Enum.GetValues<Stage>())
{
@* Each column gets its stage and cards as parameters and reports button taps through
EventCallback parameters. Home handles them, so Home renders again afterwards. *@
<BoardColumn Stage="stage"
Cards="Board.CardsIn(stage)"
OnMoveBack="Board.MoveBack"
OnMoveForward="Board.MoveForward"
OnRemove="Board.Remove" />
}
</div>
@code {
private string draft = "";
private void AddCard()
{
Board.Add(draft);
draft = "";
}
}
Pages/Home.razor.css
Pages/Home.razor.css
/* Scoped CSS: these rules apply only to elements written in Home.razor. */
.add {
display: flex;
gap: 8px;
margin: 12px 0;
}
.add input {
flex: 1;
}
.board {
display: grid;
gap: 12px;
margin-top: 8px;
}
@media (min-width: 640px) {
.board {
grid-template-columns: repeat(3, 1fr);
align-items: start;
}
}
Components/BoardColumn.razor
Components/BoardColumn.razor
<section class="column @Stage.ToString().ToLowerInvariant()">
<header>
<h2>@BoardState.Label(Stage)</h2>
<span class="count">@Cards.Count</span>
</header>
@if (Cards.Count == 0)
{
<p class="empty">No cards</p>
}
else
{
<ul>
@foreach (var card in Cards)
{
@* @key ties each <li> to its card, so moving cards never mixes up elements. *@
<li @key="card.Id">
<span class="title">@card.Title</span>
<div class="actions">
@if (Stage != Stage.ToDo)
{
<button @onclick="() => OnMoveBack.InvokeAsync(card)">← @BoardState.Label(Stage - 1)</button>
}
@if (Stage != Stage.Done)
{
<button class="forward" @onclick="() => OnMoveForward.InvokeAsync(card)">@BoardState.Label(Stage + 1) →</button>
}
else
{
<button class="remove" @onclick="() => OnRemove.InvokeAsync(card)">Remove</button>
}
</div>
</li>
}
</ul>
}
</section>
@code {
[Parameter, EditorRequired] public Stage Stage { get; set; }
[Parameter, EditorRequired] public IReadOnlyList<KanbanCard> Cards { get; set; } = [];
// The column never changes the board itself. It tells its parent what the user asked for.
[Parameter] public EventCallback<KanbanCard> OnMoveBack { get; set; }
[Parameter] public EventCallback<KanbanCard> OnMoveForward { get; set; }
[Parameter] public EventCallback<KanbanCard> OnRemove { get; set; }
}
Components/BoardColumn.razor.css
Components/BoardColumn.razor.css
/* Scoped CSS: Blazor adds a b-xxxxxxxxxx attribute to this component's elements
and to these selectors, so .title or .empty here never leak into other components. */
.column {
border: 1px solid var(--line);
border-top: 4px solid #8b5cf6;
border-radius: 12px;
padding: 10px 12px;
background: rgba(139, 92, 246, 0.04);
}
.column.doing {
border-top-color: #f59e0b;
background: rgba(245, 158, 11, 0.05);
}
.column.done {
border-top-color: #10b981;
background: rgba(16, 185, 129, 0.05);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
}
h2 {
font-size: 1.05rem;
margin: 0;
}
.count {
min-width: 24px;
text-align: center;
border-radius: 999px;
padding: 0 8px;
background: var(--line);
color: #222;
font-size: 0.85rem;
}
ul {
list-style: none;
padding: 0;
margin: 10px 0 0;
display: grid;
gap: 8px;
}
li {
border: 1px solid var(--line);
border-radius: 10px;
padding: 10px;
background: Canvas;
}
.title {
display: block;
font-weight: 600;
margin-bottom: 8px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.actions button {
padding: 4px 10px;
font-size: 0.9rem;
}
.actions .forward {
margin-left: auto;
border-color: #8b5cf6;
color: #8b5cf6;
}
.actions .remove {
margin-left: auto;
color: #c2410c;
}
.empty {
color: var(--muted);
margin: 10px 0 0;
}
Services/BoardState.cs
Services/BoardState.cs
namespace Kanban.Services;
public enum Stage { ToDo, Doing, Done }
public sealed class KanbanCard
{
public required int Id { get; init; }
public required string Title { get; init; }
public Stage Stage { get; set; }
}
/// The board's data and the only code that changes it. Program.cs registers one instance,
/// and components get it with @inject, so every component sees the same cards.
public sealed class BoardState
{
private readonly List<KanbanCard> cards = [];
private int nextId = 1;
public BoardState()
{
Add("Sketch the three columns");
Add("Write BoardState");
Add("Style the cards");
Add("Try it on a phone");
// Start with one card in Done and one in Doing.
var sketch = cards[0];
var write = cards[1];
MoveForward(sketch);
MoveForward(sketch);
MoveForward(write);
}
public int Total => cards.Count;
public static string Label(Stage stage) => stage switch
{
Stage.ToDo => "To do",
Stage.Doing => "Doing",
_ => "Done",
};
public IReadOnlyList<KanbanCard> CardsIn(Stage stage) =>
cards.Where(card => card.Stage == stage).ToList();
public void Add(string title)
{
if (string.IsNullOrWhiteSpace(title)) return;
cards.Add(new KanbanCard { Id = nextId++, Title = title.Trim(), Stage = Stage.ToDo });
}
public void MoveForward(KanbanCard card)
{
if (card.Stage == Stage.Done) return;
MoveToEnd(card, card.Stage + 1);
}
public void MoveBack(KanbanCard card)
{
if (card.Stage == Stage.ToDo) return;
MoveToEnd(card, card.Stage - 1);
}
public void Remove(KanbanCard card) => cards.Remove(card);
// A moved card goes to the bottom of its new column.
private void MoveToEnd(KanbanCard card, Stage stage)
{
cards.Remove(card);
card.Stage = stage;
cards.Add(card);
}
}
wwwroot/index.html
wwwroot/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Kanban board</title>
<base href="/" />
<link rel="stylesheet" href="css/app.css" />
<link rel="stylesheet" href="Kanban.styles.css" />
</head>
<body>
<div id="app">
<p class="loading">Starting .NET…</p>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="." class="reload">Reload</a>
<span class="dismiss">✕</span>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
wwwroot/css/app.css
wwwroot/css/app.css
:root {
color-scheme: light dark;
--accent: #6b4eff;
--line: #d8d6e4;
--muted: #777;
}
body {
margin: 0;
font-family: -apple-system, system-ui, sans-serif;
line-height: 1.5;
}
#app {
max-width: 720px;
margin: 0 auto;
padding: 20px 16px 40px;
box-sizing: border-box;
}
a, button, input, label {
touch-action: manipulation;
}
h1 {
font-size: 1.8rem;
margin: 0 0 12px;
}
h1:focus {
outline: none;
}
h2 {
font-size: 1.15rem;
margin: 0 0 8px;
}
.loading {
padding: 32px 16px;
color: var(--muted);
}
.row {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 12px 0;
}
input:not([type=checkbox]), select {
font: inherit;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 8px;
min-width: 0;
background: transparent;
color: inherit;
}
button {
font: inherit;
padding: 8px 14px;
border-radius: 8px;
border: 1px solid var(--line);
background: transparent;
color: inherit;
}
button.primary {
background: var(--accent);
border-color: var(--accent);
color: #fff;
}
button:disabled {
opacity: 0.45;
}
button.link {
border: none;
color: var(--accent);
padding: 4px 6px;
}
.muted {
color: var(--muted);
}
.card {
border: 1px solid var(--line);
border-radius: 12px;
padding: 14px 16px;
margin: 16px 0;
}
.card > :last-child {
margin-bottom: 0;
}
code {
font-family: ui-monospace, Menlo, monospace;
font-size: 0.9em;
overflow-wrap: anywhere;
}
#blazor-error-ui {
display: none;
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding: 12px 16px;
background: #ffe8a3;
color: #222;
}