Transfer List
Two lists side by side, with buttons that move rows from one to the other.
Installation
pnpm dlx @human-kit/design add transfer-listnpx @human-kit/design add transfer-listyarn dlx @human-kit/design add transfer-listbunx @human-kit/design add transfer-listThe command copies the files to src/lib/components/ui/transfer-list. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as TransferList from '$lib/components/ui/transfer-list';
const columns = [
{ id: 'name', label: 'Patient name' },
{ id: 'doctor', label: 'Doctor' },
{ id: 'clinic', label: 'Clinic' }
];
let shown = $state(['name']);
</script>
<TransferList.Root aria-label="Table columns" items={columns} bind:value={shown} /> Examples
A long list
With thousands of rows, set virtualizer={{ rowHeight: 24 }}: both lists draw only the rows near their viewport. The search, Shift ranges and "move all" still reach the rows that are not drawn, since they work on the array.
Guidelines
- Use a transfer list to choose several items out of a long list and see both sides at once: the columns of a table, the members of a team.
- For a few options, checkboxes are faster. For a choice the reader makes by typing, a multiple
Combobox.Roottakes less room. - Name both lists for what they mean, "Hidden" and "Shown", not "Left" and "Right".
- Turn on
reorderwhen the order of the right list is part of the answer, such as the order of columns. - Keep
searchableon for lists longer than a screen. With a search on, the "move all" buttons move only the rows found. - The value is the keys of the right list, in order. Give
getKeywhen the items have noid, andgetLabelwhen their text is not inlabelorname.
Accessibility
- Each list is a
role="listbox"with several choices, named by its label. With anaria-labelon the root, the whole is arole="group". - Each list is one tab stop. The arrow keys move through the rows,
SpaceorEnterchooses one or lets it go,Shiftwith an arrow chooses a range. CtrlorCmdwithEntersends the chosen rows to the other list; a double click sends one row.- The move buttons stay in the tab order when they have nothing to do, with
aria-disabled, so the keyboard always finds them. Each is named by where the rows go: "Move selected to Shown". - Each move is announced in a polite live region.
API reference
| Prop | Type | Default |
|---|---|---|
items | T[] Every item, in the order of the left list. Required. | — |
value | (string | number)[] The keys of the items in the right list, in their order. Bindable. | — |
defaultValue | (string | number)[] The keys in the right list at the start, when you give no | — |
onChange | (value, details) => void Called after a move, with the new value and the items that moved. | — |
getKey | (item: T) => string | number The identity of an item. | item.id |
getLabel | (item: T) => string The text of a row, also what the search reads. | item.label ?? item.name |
row | Snippet<[T]> Draws a row yourself, in place of its text. | — |
sourceLabel | string The name over the left list. | 'Available' |
targetLabel | string The name over the right list. | 'Selected' |
searchable | boolean A search field at the head of each list. Case and accents are ignored. | true |
reorder | boolean Buttons to move the chosen rows of the right list up and down. | false |
emptyText | string What an empty list says. | 'Nothing here' |
noMatchText | string What a search that finds nothing says. | 'No matches' |
virtualizer | { rowHeight?: number; overscan?: number } Draws only the rows near the viewport, in both lists. | — |
disabledKeys | Iterable<string | number> The keys of the items that stay where they are. | — |
name | string The name of the hidden inputs that carry the value, so a form submits it. | — |
moveShortcut | boolean Lets | true |
Plus every attribute of a <div>.