Skip to content

Commit

Permalink
feat(core): add get function to retrieve active index
Browse files Browse the repository at this point in the history
  • Loading branch information
Grsmto committed Oct 19, 2022
1 parent b913dcf commit 5815ba8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/getActiveSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ export const getActiveSnap = ({
onChange?: (snapIndex: number) => void;
}) => {
let activeSnapObserver: IntersectionObserver;
let activeSnapIndex: number;
let activeSnapIndex = 0;
let timeout: number | null = null;
let isScrolling = false;

const children = root.children;

Expand All @@ -30,7 +29,6 @@ export const getActiveSnap = ({

const setSnapIndex = (snapIndex: number) => {
activeSnapIndex = snapIndex;

triggerChange(snapIndex);
};

Expand All @@ -42,11 +40,9 @@ export const getActiveSnap = ({
const onResizeWithDebounce = debounceHOF(onResize, 100);

const handleScrolling = () => {
isScrolling = true;
if (timeout) clearTimeout(timeout);

timeout = setTimeout(() => {
isScrolling = false;
if (root.scrollLeft === 0) {
setSnapIndex(0);
}
Expand Down Expand Up @@ -207,5 +203,8 @@ export const getActiveSnap = ({

return {
destroy,
getActiveIndex: () => {
return activeSnapIndex;
},
};
};
4 changes: 3 additions & 1 deletion packages/svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const carousel = (node: HTMLDivElement, options = {}) => {

if (_options.dragToScroll) dragToScroll = _dragToScroll({ root: node });

getActiveSnap({ root: node });
const activeSnap = getActiveSnap({ root: node });

return {
getActiveIndex: activeSnap.getActiveIndex,
destroy() {
if (_options.dragToScroll) dragToScroll.disable();
activeSnap.destroy();
},
};
};
Expand Down

0 comments on commit 5815ba8

Please sign in to comment.