---
title: Selection
description: 'Toggle a value by selection mode, extend a range from an anchor, and the state of a select-all checkbox.'
order: 14
---

import { HelperResults } from '@/components/primitive-example/helper-results';
import { extendSelection, selectAllState, toggleSelection } from '@ecopages/radiant-ui/helpers';

export const order = ['a', 'b', 'c', 'd'];
export const notC = (value) => value !== 'c';

# Selection

<p class="docs-lede">
	Toggle a value by selection mode, extend a range from an anchor, and the state of a select-all checkbox.
</p>

Reach for them in a collection with its own selection input; [SelectionController](/docs/controllers/selection) maps clicks and keys to them for you. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.

## Import

```ts
import {
	extendSelection,
	selectAllState,
	toggleSelection,
	type ExtendSelectionOptions,
	type SelectionMode,
	type SelectionOptions,
} from '@ecopages/radiant-ui/helpers';
```

Every helper takes `{ selectable }`, a function that returns `false` for a value the user cannot select or deselect, such as a disabled item. The arrays passed in are never changed: `toggleSelection` and `extendSelection` return a new one. The calls below use `order = ['a', 'b', 'c', 'd']` and `notC = (value) => value !== 'c'`.

## Toggle

`toggleSelection(values, value, mode, { selectable })` is the selection after the user toggles `value`. `SelectionMode` is `none`, `single` or `multiple`. With `single` the value replaces the selection, or clears it when it was the selected one; with `multiple` it is added or removed; with `none`, or for a value `selectable` refuses, nothing changes.

<HelperResults
	rows={[
		{ code: "toggleSelection(['a'], 'b', 'multiple')", result: toggleSelection(['a'], 'b', 'multiple') },
		{ code: "toggleSelection(['a', 'b'], 'a', 'multiple')", result: toggleSelection(['a', 'b'], 'a', 'multiple') },
		{ code: "toggleSelection(['a'], 'b', 'single')", result: toggleSelection(['a'], 'b', 'single') },
		{ code: "toggleSelection(['a'], 'a', 'single')", result: toggleSelection(['a'], 'a', 'single') },
		{ code: "toggleSelection(['a'], 'b', 'none')", result: toggleSelection(['a'], 'b', 'none') },
		{
			code: "toggleSelection(['a'], 'c', 'multiple', { selectable: notC })",
			result: toggleSelection(['a'], 'c', 'multiple', { selectable: notC }),
		},
	]}
/>

## Extend

`extendSelection(order, anchor, target, values, { additive, selectable })` is the selection after the user extends it from `anchor` to `target`, as Shift+click and Shift+arrow do. The range is every selectable value of `order` from one to the other, both included, in the order of `order`. A `null` anchor, or one that is not in `order`, starts the range at `target`; a `target` that is not in `order` changes nothing. Without `additive` the range replaces the selection, except for the selected values `selectable` refuses, which stay; with `additive: true` the range is added to it.

<HelperResults
	rows={[
		{ code: "extendSelection(order, 'b', 'd', ['a'])", result: extendSelection(order, 'b', 'd', ['a']) },
		{
			code: "extendSelection(order, 'b', 'd', ['a'], { additive: true })",
			result: extendSelection(order, 'b', 'd', ['a'], { additive: true }),
		},
		{ code: "extendSelection(order, 'd', 'b', [])", result: extendSelection(order, 'd', 'b', []) },
		{ code: "extendSelection(order, null, 'c', ['a'])", result: extendSelection(order, null, 'c', ['a']) },
		{
			code: "extendSelection(order, 'a', 'd', [], { selectable: notC })",
			result: extendSelection(order, 'a', 'd', [], { selectable: notC }),
		},
		{
			code: "extendSelection(order, 'a', 'b', ['c'], { selectable: notC })",
			result: extendSelection(order, 'a', 'b', ['c'], { selectable: notC }),
		},
	]}
/>

## Select all

`selectAllState(order, values, { selectable })` is the state a select-all checkbox shows: `true` when every selectable value of `order` is selected, `false` when none is (or none can be), otherwise `'mixed'`, which is the checkbox's `indeterminate`.

<HelperResults
	rows={[
		{ code: "selectAllState(order, ['a'])", result: selectAllState(order, ['a']) },
		{ code: 'selectAllState(order, order)', result: selectAllState(order, order) },
		{ code: 'selectAllState(order, [])', result: selectAllState(order, []) },
		{
			code: "selectAllState(order, ['a', 'b', 'd'], { selectable: notC })",
			result: selectAllState(order, ['a', 'b', 'd'], { selectable: notC }),
		},
	]}
/>

[SelectionController](/docs/controllers/selection) has a list with a select-all checkbox built on `selectAllState`.
