pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame^] | 1 | import react, { Component } from "react"; |
| 2 | import Styles from '../styles/Components/panels.module.css'; |
| 3 | import handleViewport from 'react-in-viewport'; |
| 4 | |
| 5 | |
| 6 | class PanelClass extends Component { |
| 7 | constructor(props) { |
| 8 | super(props); |
| 9 | } |
| 10 | |
| 11 | getStyle() { |
| 12 | const { inViewport, enterCount } = this.props; |
| 13 | if (inViewport && enterCount === 1) { return { WebkitTransition: 'all 0.75s ease-in-out', opacity: '1', transform: "translateY(0px)" }; |
| 14 | } else if (!inViewport && enterCount < 1) { return { WebkitTransition: 'none', opacity: '0', transform: "translateY(20px)" }; |
| 15 | } else { return {}; } |
| 16 | } |
| 17 | |
| 18 | render() { |
| 19 | return <div className={Styles.panel} style={this.getStyle()} id={this.props.id}> |
| 20 | { |
| 21 | react.Children.toArray(this.props.children).map((child, index) => { |
| 22 | return child; |
| 23 | }) |
| 24 | } |
| 25 | </div> |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | |
| 30 | class AutoLayout extends Component { |
| 31 | constructor(props) { |
| 32 | super(props); |
| 33 | this.children = react.Children.toArray(this.props.children) |
| 34 | this.validity = [] |
| 35 | this.calculateValidity() |
| 36 | } |
| 37 | |
| 38 | calculateValidity() { |
| 39 | this.children.map((item, index) => { |
| 40 | if ( index > 0 && item.props.halfSize && this.validity[index - 1] == 'half1' ) { |
| 41 | this.validity[index] = 'half2'; |
| 42 | } else if ( index > 0 && !item.props.halfSize && this.validity[index - 1] == 'half1' ) { |
| 43 | this.validity[index] = 'full'; |
| 44 | this.validity[index - 1] = 'full'; |
| 45 | } else if ( item.props.halfSize && !(index == this.props.children.length - 1) ) { |
| 46 | this.validity[index] = 'half1'; |
| 47 | } else if ( index == this.props.children.length - 1 && this.validity[index - 1] == 'half1' ) { |
| 48 | this.validity[index] = 'full'; |
| 49 | this.validity[index - 1] = 'full'; |
| 50 | } else if ( index == this.props.children.length - 1 && this.validity[index - 1] == 'half2' ) { |
| 51 | this.validity[index] = 'full'; |
| 52 | } else { |
| 53 | this.validity[index] = 'full'; |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | render() { |
| 59 | return ( |
| 60 | <div className={Styles.container}> |
| 61 | { |
| 62 | this.children.map((item, index) => { |
| 63 | if ( this.validity[index] == 'full' ) { |
| 64 | return this.children[index] |
| 65 | } else if ( this.validity[index] == 'half1' ) { |
| 66 | return <div key={index} className={Styles.doublePanel}> |
| 67 | {this.children[index]} |
| 68 | {this.children[index + 1]} |
| 69 | </div> |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | </div> |
| 74 | ) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | class Title extends Component { |
| 80 | render() { |
| 81 | return <h1 className={Styles.title}>{this.props.children}</h1> |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | class Subtitle extends Component { |
| 86 | render() { |
| 87 | return <h2 className={Styles.subtitle}>{this.props.children}</h2> |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | class Text extends Component { |
| 92 | render() { |
| 93 | return <p className={Styles.text}>{this.props.children}</p> |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | class Divider extends Component { |
| 98 | render() { |
| 99 | return <div className={Styles.divider}></div> |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | const Panel = handleViewport(PanelClass, { rootMargin: '-1.0px' }); |
| 104 | |
| 105 | export { AutoLayout, Panel, Title, Subtitle, Text, Divider }; |