|
|
@ -1,4 +1,10 @@ |
|
|
|
import React, { useState, FunctionComponent, KeyboardEvent } from 'react'; |
|
|
|
import React, { |
|
|
|
|
|
|
|
useState, |
|
|
|
|
|
|
|
FunctionComponent, |
|
|
|
|
|
|
|
KeyboardEvent, |
|
|
|
|
|
|
|
useEffect, |
|
|
|
|
|
|
|
useRef, |
|
|
|
|
|
|
|
} from 'react'; |
|
|
|
import { Arrow } from '../arrow'; |
|
|
|
import { Arrow } from '../arrow'; |
|
|
|
import { ItemProvider } from '../item'; |
|
|
|
import { ItemProvider } from '../item'; |
|
|
|
import { |
|
|
|
import { |
|
|
@ -13,6 +19,7 @@ import { |
|
|
|
import { SlideDirection, Item, ArrowKeys } from '../../types/carousel'; |
|
|
|
import { SlideDirection, Item, ArrowKeys } from '../../types/carousel'; |
|
|
|
import { defaultProps } from './defaultProps'; |
|
|
|
import { defaultProps } from './defaultProps'; |
|
|
|
import styles from '../../styles/styles.module.css'; |
|
|
|
import styles from '../../styles/styles.module.css'; |
|
|
|
|
|
|
|
import { usePrevious } from '../../hooks'; |
|
|
|
|
|
|
|
|
|
|
|
export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselProps) => { |
|
|
|
export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselProps) => { |
|
|
|
const props: Required<CarouselProps> = { ...defaultProps, ...userProps }; |
|
|
|
const props: Required<CarouselProps> = { ...defaultProps, ...userProps }; |
|
|
@ -29,20 +36,35 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
const [showArrow, setShowArrow] = useState( |
|
|
|
const [showArrow, setShowArrow] = useState( |
|
|
|
getShowArrow(props.children.length, props.show, props.infinite, current), |
|
|
|
getShowArrow(props.children.length, props.show, props.infinite, current), |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
const prevChildren = usePrevious<Item[]>(userProps.children); |
|
|
|
|
|
|
|
const [page, setPage] = useState<number>(0); |
|
|
|
|
|
|
|
const itemsRef = useRef(initItems(props.children, props.slide, props.infinite)); |
|
|
|
|
|
|
|
const isPaginating = useRef(false); |
|
|
|
|
|
|
|
|
|
|
|
if (props.dynamic) { |
|
|
|
if (props.dynamic) { |
|
|
|
React.useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
const newItems = updateNodes( |
|
|
|
const newItems = updateNodes( |
|
|
|
items, |
|
|
|
itemsRef.current, |
|
|
|
userProps.children, |
|
|
|
props.children, |
|
|
|
|
|
|
|
prevChildren, |
|
|
|
props.slide, |
|
|
|
props.slide, |
|
|
|
props.infinite, |
|
|
|
props.infinite, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
setItems(newItems); |
|
|
|
setItems(newItems); |
|
|
|
}, userProps.children); |
|
|
|
itemsRef.current = newItems; |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
page < props.pageCount && |
|
|
|
|
|
|
|
prevChildren && |
|
|
|
|
|
|
|
prevChildren?.length < props.children.length |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
slide(SlideDirection.Right); |
|
|
|
|
|
|
|
setPage(page + 1); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, [props.children]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const slide = (direction: SlideDirection, slide: number): void => { |
|
|
|
const slide = (direction: SlideDirection) => { |
|
|
|
if ( |
|
|
|
if ( |
|
|
|
animation.isSliding || |
|
|
|
animation.isSliding || |
|
|
|
(direction === SlideDirection.Right && !showArrow.right) || |
|
|
|
(direction === SlideDirection.Right && !showArrow.right) || |
|
|
@ -51,37 +73,55 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (props.beforeChange) props.beforeChange(direction); |
|
|
|
if ( |
|
|
|
|
|
|
|
props.paginationCallback && |
|
|
|
|
|
|
|
direction === SlideDirection.Right && |
|
|
|
|
|
|
|
page < props.pageCount - 1 && |
|
|
|
|
|
|
|
!isPaginating.current |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
isPaginating.current = true; |
|
|
|
|
|
|
|
props.paginationCallback(direction); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const elements = props.children; |
|
|
|
|
|
|
|
|
|
|
|
const next = getCurrent(current, slide, props.children.length, direction); |
|
|
|
const next = getCurrent(current, props.slide, elements.length, direction); |
|
|
|
const rotated = props.infinite |
|
|
|
const rotated = props.infinite |
|
|
|
? rotateItems(props.children, items, next, props.show, slide, direction) |
|
|
|
? rotateItems(elements, items, next, props.show, props.slide, direction) |
|
|
|
: items; |
|
|
|
: items; |
|
|
|
if (props.infinite && direction === SlideDirection.Right) { |
|
|
|
if (props.infinite && direction === SlideDirection.Right) { |
|
|
|
setItems(rotated); |
|
|
|
setItems(rotated); |
|
|
|
|
|
|
|
itemsRef.current = rotated; |
|
|
|
} |
|
|
|
} |
|
|
|
setAnimation({ |
|
|
|
setAnimation({ |
|
|
|
transform: animation.transform + getTransformAmount(width, slide, direction), |
|
|
|
transform: |
|
|
|
|
|
|
|
animation.transform + getTransformAmount(width, props.slide, direction), |
|
|
|
transition: props.transition, |
|
|
|
transition: props.transition, |
|
|
|
isSliding: true, |
|
|
|
isSliding: true, |
|
|
|
}); |
|
|
|
}); |
|
|
|
setCurrent(next); |
|
|
|
setCurrent(next); |
|
|
|
setShowArrow( |
|
|
|
setShowArrow(getShowArrow(elements.length, props.show, props.infinite, next)); |
|
|
|
getShowArrow(props.children.length, props.show, props.infinite, next), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
setTimeout(() => { |
|
|
|
if (props.infinite) { |
|
|
|
if (props.infinite) { |
|
|
|
setItems(cleanItems(rotated, slide, direction)); |
|
|
|
const cleanedItems = cleanItems( |
|
|
|
|
|
|
|
direction === SlideDirection.Right ? itemsRef.current : rotated, |
|
|
|
|
|
|
|
props.slide, |
|
|
|
|
|
|
|
direction, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
setItems(cleanedItems); |
|
|
|
|
|
|
|
itemsRef.current = cleanedItems; |
|
|
|
} |
|
|
|
} |
|
|
|
setAnimation({ |
|
|
|
setAnimation({ |
|
|
|
transform: props.infinite |
|
|
|
transform: props.infinite |
|
|
|
? getTransformAmount(width, slide, SlideDirection.Right) |
|
|
|
? getTransformAmount(width, props.slide, SlideDirection.Right) |
|
|
|
: animation.transform + getTransformAmount(width, slide, direction), |
|
|
|
: animation.transform + |
|
|
|
|
|
|
|
getTransformAmount(width, props.slide, direction), |
|
|
|
transition: 0, |
|
|
|
transition: 0, |
|
|
|
isSliding: false, |
|
|
|
isSliding: false, |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (props.afterChange) props.afterChange(direction); |
|
|
|
|
|
|
|
}, props.transition * 1_0_0_0); |
|
|
|
}, props.transition * 1_0_0_0); |
|
|
|
|
|
|
|
isPaginating.current = false; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const widthCallBack = (calculatedWidth: number) => { |
|
|
|
const widthCallBack = (calculatedWidth: number) => { |
|
|
@ -108,14 +148,14 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const slideCallback = (direction: SlideDirection) => { |
|
|
|
const slideCallback = (direction: SlideDirection) => { |
|
|
|
slide(direction, props.slide); |
|
|
|
slide(direction); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleOnKeyDown = (e: KeyboardEvent) => { |
|
|
|
const handleOnKeyDown = (e: KeyboardEvent) => { |
|
|
|
if (e.keyCode === ArrowKeys.Left) { |
|
|
|
if (e.keyCode === ArrowKeys.Left) { |
|
|
|
slide(SlideDirection.Left, props.slide); |
|
|
|
slide(SlideDirection.Left); |
|
|
|
} else if (e.keyCode === ArrowKeys.Right) { |
|
|
|
} else if (e.keyCode === ArrowKeys.Right) { |
|
|
|
slide(SlideDirection.Right, props.slide); |
|
|
|
slide(SlideDirection.Right); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -128,25 +168,19 @@ export const Carousel: FunctionComponent<CarouselProps> = (userProps: CarouselPr |
|
|
|
className={`${styles.carouselBase} ${props.className}`} |
|
|
|
className={`${styles.carouselBase} ${props.className}`} |
|
|
|
> |
|
|
|
> |
|
|
|
{showArrow.left && ( |
|
|
|
{showArrow.left && ( |
|
|
|
<Arrow |
|
|
|
<Arrow direction="left" onClick={() => slide(SlideDirection.Left)} /> |
|
|
|
direction="left" |
|
|
|
|
|
|
|
onClick={() => slide(SlideDirection.Left, props.slide)} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
)} |
|
|
|
)} |
|
|
|
<ItemProvider |
|
|
|
<ItemProvider |
|
|
|
{...props} |
|
|
|
{...props} |
|
|
|
transition={animation.transition} |
|
|
|
transition={animation.transition} |
|
|
|
items={items} |
|
|
|
items={itemsRef.current} |
|
|
|
transform={animation.transform} |
|
|
|
transform={animation.transform} |
|
|
|
slideCallback={slideCallback} |
|
|
|
slideCallback={slideCallback} |
|
|
|
dragCallback={dragCallback} |
|
|
|
dragCallback={dragCallback} |
|
|
|
widthCallBack={widthCallBack} |
|
|
|
widthCallBack={widthCallBack} |
|
|
|
/> |
|
|
|
/> |
|
|
|
{showArrow.right && ( |
|
|
|
{showArrow.right && ( |
|
|
|
<Arrow |
|
|
|
<Arrow direction="right" onClick={() => slide(SlideDirection.Right)} /> |
|
|
|
direction="right" |
|
|
|
|
|
|
|
onClick={() => slide(SlideDirection.Right, props.slide)} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
)} |
|
|
|
)} |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
); |
|
|
|
); |
|
|
@ -165,8 +199,8 @@ export interface CarouselProps { |
|
|
|
useArrowKeys?: boolean; |
|
|
|
useArrowKeys?: boolean; |
|
|
|
a11y?: { [key: string]: string }; |
|
|
|
a11y?: { [key: string]: string }; |
|
|
|
dynamic?: boolean; |
|
|
|
dynamic?: boolean; |
|
|
|
beforeChange?: ((direction: SlideDirection) => void) | null; |
|
|
|
paginationCallback?: ((direction: SlideDirection) => any) | null; |
|
|
|
afterChange?: ((direction: SlideDirection) => void) | null; |
|
|
|
pageCount?: number; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export interface CarouselState { |
|
|
|
export interface CarouselState { |
|
|
|