Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

Board

Columns of cards you move from one column to another, with the card following the pointer and a placeholder marking where it lands.

Goal

A task board, a kanban or a triage view: each column is a list, and a card moves within it or to another column. The columns scroll, and the card must stay visible when it leaves one, so it is lifted out of its column while it moves.

Primitives

  • SortableController with preview: true lifts the card into a layer on document.body through PortalController, keeps a placeholder in its slot and moves the placeholder to the target index as the pointer or the keys move. It handles the pointer drag, the keyboard path (Space lifts, the arrows move within the column, PageDown and PageUp move to the next and previous column, Space drops, Escape cancels) and the announcements.
  • SortableGroup makes the columns containers that exchange cards. There is one controller per column, all sharing the group.

Left to the recipe: where the cards live (here, the DOM: onChange inserts the card into toContainer and focuses it), the look of the lifted card ([data-sortable-state='lifted']) and the placeholder ([data-sortable-placeholder]). The lifted card is no longer inside the example, so its styles select it by its own attribute, not by an ancestor.

Example

Drag a card to another column, or focus one, press Space, press PageDown to move it to the next column, use the arrows to choose its place and press Space to drop it. The first column scrolls.

Pick an authoring pattern below; they all behave the same, and the one you pick is remembered on the other recipes.

The same behavior as a RadiantElement. The markup is authored inside the element and the element finds its parts with @query; there is nothing to mount, clean up or run again.

To do

  • Write the brief
  • Sketch the layout
  • Pick the colors
  • Draft the copy
  • Review accessibility
  • Plan the launch

Doing

  • Build the header
  • Wire the form

Done

  • Set up the repo

Focus a card, press Space, PageDown for the next column, Space to drop

import { RadiantElement, customElement, query, queryAll } from '@ecopages/radiant';
import { SortableGroup, createSortableController } from '@ecopages/radiant-ui/controllers';

@customElement('board-element')
export class BoardElement extends RadiantElement {
	@queryAll('ul') private lists!: HTMLElement[];
	@query('[data-status]') private status!: HTMLElement;

	private readonly group = new SortableGroup();

	/**
	 * One sortable per column.
	 *
	 * @remarks
	 * A controller binds to the host when the class is constructed, before the markup can be queried, so the count is
	 * written here and has to match the number of `ul` in the markup; the component form takes it from its data.
	 */
	private readonly sortables = Array.from({ length: 3 }, (_, column) =>
		createSortableController(this, {
			group: this.group,
			preview: true,
			createPlaceholder: () => document.createElement('li'),
			getContainer: () => this.lists[column] ?? null,
			getItems: () => [...this.lists[column].children] as HTMLElement[],
			announce: (message) => (this.status.textContent = message),
			onChange: ({ item, to, toContainer }) => {
				const others = [...toContainer.children].filter((child) => child !== item);
				toContainer.insertBefore(item, others[to] ?? null);
				item.focus();
			},
		}),
	);
}

Each column gets its own createSortableController(this, ...), all sharing a SortableGroup kept in a field. The controllers are created in the element, one for each of the three columns, and look their column up by index when they attach, so @queryAll('ul') is read lazily and the markup stays plain. They attach when the element connects and are destroyed when it disconnects.

What the Radiant form removes: the mount function and its registration, the cleanup that destroys each controller, and the re-run after a client-side navigation.

Styles

The same attributes style the lifted card, the placeholder and the column a card is over; touch-action: none on the cards lets a touch drag move them instead of scrolling. The stylesheet is the board.css tab in the files above.