@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
33 lines (29 loc) • 1.02 kB
JavaScript
var react = require('react');
var clamp = require('./clamp.cjs');
var wrap = require('./wrap.cjs');
const defaultOptions = {
wrap: true
};
function useIndex(initial, length, options) {
const { wrap: shouldWrap } = react.useMemo(() => {
return {
...defaultOptions,
...options
};
}, [options]);
const transform = react.useMemo(() => shouldWrap ? wrap.wrap : clamp.clamp, [shouldWrap]);
const [index, setIndex] = react.useState(initial);
react.useEffect(() => {
setIndex((index2) => clamp.clamp(index2, 0, Math.max(length - 1, 0)));
}, [length]);
const previousIndex = react.useCallback(() => {
setIndex((index2) => transform(index2 - 1, 0, Math.max(length, 0)));
}, [length, transform]);
const nextIndex = react.useCallback(() => {
setIndex((index2) => transform(index2 + 1, 0, Math.max(length, 0)));
}, [length, transform]);
return [index, previousIndex, nextIndex, setIndex];
}
exports.useIndex = useIndex;
//# sourceMappingURL=use-index.cjs.map
;