Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from 'preact';
  2. import styles from './style';
  3. import classNames from 'classnames';
  4. import debounce from '../../utils/debounce';
  5.  
  6. import root from 'window-or-global';
  7.  
  8. export default class slider extends Component {
  9.   state = {
  10.     activeIndex: 0,
  11.     translate: 0,
  12.     touchStartX: undefined,
  13.     touchMoveX: undefined,
  14.     moveX: undefined
  15.   }
  16.  
  17.   SWIPE_WIDTH = 50;
  18.   SCREEN_LG = 1100;
  19.   SLIDE_WIDTH = 100;
  20.  
  21.   slideTo(index) {
  22.     this.setState({ activeIndex: index, translate: `${-this.SLIDE_WIDTH * index}` });
  23.   }
  24.  
  25.   onTouchStart = (e) => {
  26.     this.setState({ touchStartX: e.touches[0].pageX });
  27.   }
  28.  
  29.   onTouchMove = (e) => {
  30.     const { activeIndex, touchStartX } = this.state;
  31.  
  32.     let touchMoveX = e.touches[0].pageX;
  33.     let moveX = touchStartX - touchMoveX;
  34.  
  35.     if (Math.abs(moveX) > this.SWIPE_WIDTH) {
  36.       this.setState({
  37.         touchMoveX: e.touches[0].pageX,
  38.         translate: -((activeIndex * this.SLIDE_WIDTH) + (touchStartX - touchMoveX)),
  39.         moveX
  40.       });
  41.     }
  42.   }
  43.  
  44.   onTouchEnd = (e) => {
  45.     const { activeIndex, translate, moveX } = this.state;
  46.  
  47.     const currentTranslate = activeIndex * -this.SLIDE_WIDTH;
  48.     let index = activeIndex;
  49.  
  50.     if (Math.abs(moveX) > this.SWIPE_WIDTH) {
  51.       if (translate < currentTranslate && activeIndex < (this.props.children.length - 1)) {
  52.         index++;
  53.       }
  54.       else if (translate > currentTranslate && activeIndex > 0) {
  55.         index--;
  56.       }
  57.  
  58.       this.slideTo(index);
  59.     }
  60.   }
  61.  
  62.   wrapChildren(child) {
  63.     return <li class={styles.slider__item}>{child}</li>;
  64.   }
  65.  
  66.   generateSliderDods() {
  67.     const slides = this.props.children;
  68.     return slides.map((item, index) => <div class={this.generateDotClasses(index)} onClick={() => this.slideTo(index)} />);
  69.   }
  70.  
  71.   generateDotClasses(index) {
  72.     return classNames(styles.slider__dot, { [styles['slider__dot--active']]: index === this.state.activeIndex });
  73.   }
  74.  
  75.   handleResize = debounce(() => {
  76.     if (root.innerWidth > this.SCREEN_LG) {
  77.       this.setState({ activeIndex: 0, translate: 0 });
  78.     }
  79.   }, 200);
  80.  
  81.   componentDidMount() {
  82.     root.addEventListener('resize', this.handleResize);
  83.   }
  84.  
  85.   render() {
  86.     const listTransition = `transform: translate3d(${this.state.translate}%, 0px, 0px); transition-duration: 200ms`;
  87.  
  88.     return (
  89.       <div class={styles.slider}>
  90.         <div class={styles.slider__content}>
  91.           <div
  92.             onTouchStart={this.onTouchStart}
  93.             onTouchMove={this.onTouchMove}
  94.             onTouchEnd={this.onTouchEnd}
  95.           >
  96.             <ul
  97.               class={styles.slider__list}
  98.               style={listTransition}
  99.             >
  100.               {this.props.children.map(child => this.wrapChildren(child))}
  101.             </ul>
  102.           </div>
  103.         </div>
  104.         <ul class={styles.slider__dots}>{this.generateSliderDods()}</ul>
  105.       </div>
  106.     );
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement