Cycle value

The next value in a list, wrapping from the last to the first.

Reach for it for a cycle toggle. 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

import { cycleValue } from '@ecopages/radiant-ui/helpers';

Cycling

A missing or unknown current value gives the first entry, so stale or unset state recovers. An empty list throws a RangeError.

CallResult
cycleValue(['light', 'dark', 'system'], 'light')"dark"
cycleValue(['light', 'dark', 'system'], 'system')"light"
cycleValue(['light', 'dark', 'system'], undefined)"light"
cycleValue(['light', 'dark', 'system'], 'sepia')"light"