UNPKG

@payfit/unity-components

Version:

56 lines (55 loc) 1.7 kB
import { useCallback as e, useEffect as t, useRef as n, useState as r } from "react"; //#region src/components/carousel/hooks/useCarouselState.ts function i(i) { let [a, o] = r(0), [s, c] = r(0), [l, u] = r(() => /* @__PURE__ */ new Set()), [d, f] = r(null), p = n(!1), m = e(() => { if (!i) return { visible: /* @__PURE__ */ new Set(), focused: null }; let e = i.selectedSnap(), t = i.internalEngine(), n = (t.slidesToScroll.groupSlides(t.slideIndexes)[e] ?? [])[0] ?? null, r = i.slidesInView(), a = new Set(r); return n !== null && a.add(n), { visible: a, focused: n }; }, [i]), h = e(() => { if (!i) return; o(i.selectedSnap()); let e = m(); u(e.visible), p.current || f(e.focused); }, [i, m]), g = e(() => { i && (c(i.snapList().length), o(i.selectedSnap())); }, [i]); return t(() => { if (!i) return; c(i.snapList().length); let e = m(); return u(e.visible), f(e.focused), i.on("select", h), i.on("resize", g), () => { i.off("select", h), i.off("resize", g); }; }, [ i, h, g, m ]), { selectedSnap: a, snapCount: s, visibleSlideIndexes: l, focusedSlideIndex: d, goToPrev: e(() => i?.goToPrev(), [i]), goToNext: e(() => i?.goToNext(), [i]), goTo: e((e) => i?.goTo(e), [i]), canGoToPrev: e(() => i?.canGoToPrev(), [i]), canGoToNext: e(() => i?.canGoToNext(), [i]), goToSlide: e((e) => { if (!i) return; p.current = !0, f(e); let t = i.internalEngine(), n = t.slidesToScroll.groupSlides(t.slideIndexes).findIndex((t) => t.includes(e)), r = i.selectedSnap(); n !== -1 && n !== r && i.goTo(n), setTimeout(() => { p.current = !1; }, 0); }, [i]) }; } //#endregion export { i as useCarouselState };