Kanban board

Cards that keep their state (notes, running timers) as they move between columns.

App.kn
<script lang="ts">
  import { keyedReparentable } from '@inflector/keeno'
  import Card from './Card.kn'

  type Column = 'todo' | 'doing' | 'done'
  const COLUMNS: { id: Column; title: string; color: string }[] = [
    { id: 'todo', title: 'To do', color: 'var(--faint)' },
    { id: 'doing', title: 'Doing', color: 'var(--warning)' },
    { id: 'done', title: 'Done', color: 'var(--success)' },
  ]

  const cards = state([
    { id: 1, title: 'Design the API', column: 'done' as Column },
    { id: 2, title: 'Write the compiler', column: 'doing' as Column },
    { id: 3, title: 'Ship the docs', column: 'todo' as Column },
  ])

  // One live <Card> per id. Moving a card re-creates its <li>, but the Card inside moves with
  // its state (its timer and its notes) instead of being destroyed and rebuilt.
  const view = keyedReparentable((id: number) => <Card title={cards.peek().find((c) => c.id === id)!.title} />)

  const move = (id: number, column: Column) => cards.set(cards.map((c) => (c.id === id ? { ...c, column } : c)))
  const shift = (column: Column, by: number) => COLUMNS[COLUMNS.findIndex((c) => c.id === column) + by]?.id

  let dragged = 0
  function drop(e: DragEvent, column: Column) {
    e.preventDefault()
    if (dragged) move(dragged, column)
  }
</script>

<div class="board">
  {COLUMNS.map((col) => (
    <section key={col.id} style={`--c:${col.color}`} onDragover={(e: DragEvent) => e.preventDefault()} onDrop={(e: DragEvent) => drop(e, col.id)}>
      <h3><span class="dot"></span>{col.title} <small>{cards.filter((c) => c.column === col.id).length}</small></h3>
      <ul>
        {cards.filter((c) => c.column === col.id).map((c) => (
          <li key={c.id} draggable="true" onDragstart={() => (dragged = c.id)}>
            {view(c.id)}
            <div class="moves">
              <button disabled={!shift(c.column, -1)} aria-label="Move left" onClick={() => move(c.id, shift(c.column, -1)!)}>←</button>
              <button disabled={!shift(c.column, 1)} aria-label="Move right" onClick={() => move(c.id, shift(c.column, 1)!)}>→</button>
            </div>
          </li>
        ))}
      </ul>
    </section>
  ))}
</div>
<p class="hint">Start a timer or add a note, then move the card: its state comes along.</p>

<style>
  .board {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.5rem;
  }
  section {
    min-height: 160px;
    padding: 0.375rem;
    border-radius: 0.75rem;
    background: var(--surface);
  }
  h3 {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    margin: 0.25rem 0.375rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 500;
    letter-spacing: 0;
    color: var(--muted);
  }
  .dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--c);
  }
  small {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--faint);
  }
  ul {
    display: grid;
    gap: 0.375rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }
  li {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    padding: 0.5rem 0.375rem 0.375rem 0.625rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--bg);
    box-shadow: 0 1px 2px rgb(0 0 0 / 0.05);
    cursor: grab;
    transition: border-color 0.15s;
  }
  li:hover {
    border-color: var(--border-strong);
  }
  .moves {
    display: flex;
    justify-content: flex-end;
    gap: 2px;
  }
  .moves button {
    min-height: 1.25rem;
    padding: 0 0.375rem;
    border: none;
    background: none;
    box-shadow: none;
    color: var(--faint);
    font-size: 0.75rem;
  }
  .moves button:hover:not(:disabled) {
    background: var(--hover);
    color: var(--fg);
  }
  .moves button:disabled {
    opacity: 0.25;
  }
  .hint {
    margin: 0.75rem 0 0;
    color: var(--faint);
    font-size: 0.75rem;
    text-align: center;
  }
</style>