|
1 | | -import React, { FC, ReactElement } from 'react'; |
2 | | -import { CarouselProvider, Slider } from 'pure-react-carousel'; |
| 1 | +import React, { FC, ReactElement, Children, cloneElement } from 'react'; |
3 | 2 | import { CarouselItemProps } from './CarouselItem'; |
4 | 3 |
|
5 | 4 | export type CarouselProps = { |
6 | | - /** Infinite loop */ |
7 | | - infinite?: boolean; |
8 | | - /** Amount of slides per view */ |
9 | | - perView?: number; |
10 | | - /** Amount of slides per move */ |
11 | | - perMove?: number; |
12 | | - /** Auto calculate slides' height */ |
13 | | - autoHeight?: boolean; |
14 | | - /** Ideal slide width */ |
15 | | - slideWidth?: number; |
16 | | - /** Ideal slide height */ |
17 | | - slideHeight?: number; |
18 | | - /** A list of carousel items */ |
| 5 | + /** |
| 6 | + * The only children of the Carousel are the carousel item. |
| 7 | + */ |
19 | 8 | children?: ReactElement<CarouselItemProps>[] | ReactElement<CarouselItemProps>; |
| 9 | + /** |
| 10 | + * Base slide width set on the `Carousel` component level, it can be overriden for each CarouselItem by setting |
| 11 | + * a `width` attribute on the `<item>` component. |
| 12 | + * |
| 13 | + * It can be set to either a percentage, e.g. `slideWidth="40%"` or to a pixel based value `slideWidth="200px"`. |
| 14 | + * |
| 15 | + * @default '120px' |
| 16 | + */ |
| 17 | + slideWidth?: string; |
| 18 | + /** |
| 19 | + * Additional carousel class name |
| 20 | + */ |
| 21 | + className?: string; |
20 | 22 | }; |
21 | 23 |
|
22 | 24 | /** |
23 | | - * A carousel is a nice mobile friendly way of letting a user select |
24 | | - * something |
| 25 | + * A carousel is a nice mobile friendly way of letting a user select something |
| 26 | + * |
| 27 | + * Super simple scroll based carousel slightly inspired by [react-scroll-snap-slider](https://github.com/lifarl/react-scroll-snap-slider) |
25 | 28 | */ |
26 | | -export const Carousel: FC<CarouselProps> = ({ |
27 | | - infinite = true, |
28 | | - perView = 2.5, |
29 | | - perMove = 1, |
30 | | - autoHeight = true, |
31 | | - slideWidth = 100, |
32 | | - slideHeight = 125, |
33 | | - children, |
34 | | -}) => { |
| 29 | +export const Carousel: FC<CarouselProps> = ({ children, slideWidth = '120px', className = '' }) => { |
35 | 30 | return ( |
36 | | - <div className="mml-carousel"> |
37 | | - <CarouselProvider |
38 | | - className="mml" |
39 | | - infinite={infinite} |
40 | | - visibleSlides={perView} |
41 | | - step={perMove} |
42 | | - isIntrinsicHeight={autoHeight} |
43 | | - naturalSlideWidth={slideWidth} |
44 | | - naturalSlideHeight={slideHeight} |
45 | | - totalSlides={React.Children.count(children)} |
46 | | - > |
47 | | - <Slider>{children}</Slider> |
48 | | - </CarouselProvider> |
| 31 | + <div className={`mml-carousel ${className}`}> |
| 32 | + <div className="mml-carousel__track"> |
| 33 | + <div className="mml-carousel__slides"> |
| 34 | + {Children.map(children as ReactElement, (child) => |
| 35 | + cloneElement(child, { className: 'mml-carousel__slide', slideWidth }), |
| 36 | + )} |
| 37 | + </div> |
| 38 | + </div> |
49 | 39 | </div> |
50 | 40 | ); |
51 | 41 | }; |
0 commit comments