The code powering m.abunchtell.com https://m.abunchtell.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

82 rader
1.9 KiB

  1. import ColumnHeader from './column_header';
  2. import PropTypes from 'prop-types';
  3. const easingOutQuint = (x, t, b, c, d) => c*((t=t/d-1)*t*t*t*t + 1) + b;
  4. const scrollTop = (node) => {
  5. const startTime = Date.now();
  6. const offset = node.scrollTop;
  7. const targetY = -offset;
  8. const duration = 1000;
  9. let interrupt = false;
  10. const step = () => {
  11. const elapsed = Date.now() - startTime;
  12. const percentage = elapsed / duration;
  13. if (percentage > 1 || interrupt) {
  14. return;
  15. }
  16. node.scrollTop = easingOutQuint(0, elapsed, offset, targetY, duration);
  17. requestAnimationFrame(step);
  18. };
  19. step();
  20. return () => {
  21. interrupt = true;
  22. };
  23. };
  24. class Column extends React.PureComponent {
  25. constructor (props, context) {
  26. super(props, context);
  27. this.handleHeaderClick = this.handleHeaderClick.bind(this);
  28. this.handleWheel = this.handleWheel.bind(this);
  29. }
  30. handleHeaderClick () {
  31. const scrollable = ReactDOM.findDOMNode(this).querySelector('.scrollable');
  32. if (!scrollable) {
  33. return;
  34. }
  35. this._interruptScrollAnimation = scrollTop(scrollable);
  36. }
  37. handleWheel () {
  38. if (typeof this._interruptScrollAnimation !== 'undefined') {
  39. this._interruptScrollAnimation();
  40. }
  41. }
  42. render () {
  43. const { heading, icon, children, active, hideHeadingOnMobile } = this.props;
  44. let header = '';
  45. if (heading) {
  46. header = <ColumnHeader icon={icon} active={active} type={heading} onClick={this.handleHeaderClick} hideOnMobile={hideHeadingOnMobile} />;
  47. }
  48. return (
  49. <div role='section' aria-label={heading} className='column' onWheel={this.handleWheel}>
  50. {header}
  51. {children}
  52. </div>
  53. );
  54. }
  55. }
  56. Column.propTypes = {
  57. heading: PropTypes.string,
  58. icon: PropTypes.string,
  59. children: PropTypes.node,
  60. active: PropTypes.bool,
  61. hideHeadingOnMobile: PropTypes.bool
  62. };
  63. export default Column;