diff --git a/example/src/App.js b/example/src/App.js index 8135e97..30052b8 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -11,7 +11,8 @@ function App() { rightPane:{} }) const [configuration, setConfiguration] = useState({ - showClickableArea: false + showClickableArea: false, + loop: false }) const updateWebConsole = (...args) => { @@ -124,6 +125,22 @@ function App() { Show clickable areas +
+ setConfiguration(prev => { + return { + ...prev, + loop: !prev.loop + } + })} + defaultChecked={configuration.loop} + /> + +
{ if (typeof currentIndex === 'number') { if (currentIndex >= 0 && currentIndex < stories.length) { - setCurrentIdWrapper(() => currentIndex) + setCurrentId(currentIndex) } else { console.error('Index out of bounds. Current index was set to value more than the length of stories array.', currentIndex) } @@ -75,39 +75,35 @@ export default function () { setBufferAction(!!bufferAction) } - const setCurrentIdWrapper = (callback) => { + const setCurrentIdWrapper = (callback: (a: number) => {direction: string, prev: number, nextIdx: number}) => { const {direction, prev, nextIdx} = callback(currentId) setCurrentId(nextIdx); onSlideTransition && onSlideTransition(direction, prev, nextIdx === prev ? undefined : nextIdx) } const previous = () => { - setCurrentIdWrapper(prev => { + setCurrentIdWrapper((prev: number) => { const nextIdx = prev > 0 ? prev - 1 : prev return {direction: 'left', prev, nextIdx} }) } const next = () => { - const nextStoryIdForLoop = prev => { + const nextStoryIdForLoop = (prev: number) => { const nextIdx = (prev + 1) % stories.length return {direction: 'right', prev, nextIdx} } - const nextStoryId = prev => { + const nextStoryId = (prev: number) => { if (prev < stories.length - 1) { const nextIdx = prev + 1 return {direction: 'right', prev, nextIdx} } - return {direction: 'right', prev} + return {direction: 'right', prev, nextIdx: prev} } if (isMounted.current) { - if (loop) { - setCurrentIdWrapper(nextStoryIdForLoop) - } else { - setCurrentIdWrapper(nextStoryId) - } + setCurrentIdWrapper(loop? nextStoryIdForLoop : nextStoryId) } } diff --git a/src/interfaces.tsx b/src/interfaces.tsx index 1302973..e158a37 100644 --- a/src/interfaces.tsx +++ b/src/interfaces.tsx @@ -21,7 +21,7 @@ export interface ReactInstaStoriesProps { onStoryEnd?: Function; keyboardNavigation?: boolean; clickableAreaStyles?: Record; - onSlideTransition?: Function; + onSlideTransition?: (direction: string, prev: number, nextIdx: number) => any; } export interface GlobalCtx { @@ -44,7 +44,7 @@ export interface GlobalCtx { onStoryEnd?: Function; keyboardNavigation?: boolean; clickableAreaStyles?: Record; - onSlideTransition?: Function; + onSlideTransition?: (direction: string, prev: number, nextIdx: number) => any; } type NumberOrString = number | string;