Add cycle-modes middleware and next-mode state function.