fn

useCycleList

v0.0.14testeddemo

Cycle through a list of items, with next/prev/shift/go controls. Supports a reactive list — the index is kept valid when the list changes.

Examples

ts
const { state, next, prev } = useCycleList(['a', 'b', 'c']);
next(); // state.value === 'b'
ts
const { index } = useCycleList(['a', 'b', 'c']);
index.value = 2; // jump directly to 'c'

Demo

Loading demo…

Signature

ts
export function useCycleList<T>(
  list: MaybeRefOrGetter<T[]>,
  options: UseCycleListOptions<T> ={ ... }

Type Parameters

T

Parameters

ParameterTypeDescription
listMaybeRefOrGetter<T[]>The list to cycle through (can be reactive)
options?UseCycleListOptions<T>Options

Returns

UseCycleListReturn<T>State and controls
PropertyTypeDescription
stateShallowRef<T>The currently selected item.
indexWritableComputedRef<number>The index of the currently selected item. Writable — assigning jumps to that index.
next(n?: number) => TMove forward by n items (wraps around). Defaults to 1.
prev(n?: number) => TMove backward by n items (wraps around). Defaults to 1.
shift(delta?: number) => TMove by a signed delta relative to the current item (wraps around). Defaults to 1.
go(i: number) => TJump to a specific index (wraps around out-of-range/negative indices).