beam-cli
Version:
A beautifully simple CLI for running Lighthouse audits on a statically generated (SSG) website
24 lines (23 loc) • 800 B
JavaScript
import { useState } from 'react';
export function useListIndex(list, options) {
const { loops } = { loops: false, ...options };
const [index, setIndex] = useState(0);
const previous = () => {
let newIndex = index - 1;
if (newIndex < 0)
newIndex = loops ? list.length - 1 : 0;
setIndex(newIndex);
};
const next = () => {
let newIndex = index + 1;
if (newIndex >= list.length)
newIndex = loops ? 0 : list.length - 1;
setIndex(newIndex);
};
return [index, previous, next];
}
export function useListItem(list, fallback, options) {
const options_ = { loops: false, ...options };
const [index, previous, next] = useListIndex(list, options_);
return [list[index] ?? fallback, previous, next];
}