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 { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 7 | constructor(props) { |
| 8 | super(props); |
| 9 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 10 | |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 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 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 17 | |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 18 | render() { |
| 19 | return <div className={Styles.panel} style={this.getStyle()} id={this.props.id} tabIndex={0}> |
| 20 | { |
| 21 | react.Children.toArray(this.props.children).map((child, index) => { |
| 22 | return child; |
| 23 | }) |
| 24 | } |
| 25 | </div> |
| 26 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | |
| 30 | class AutoLayout extends Component { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 31 | constructor(props) { |
| 32 | super(props); |
| 33 | this.children = react.Children.toArray(this.props.children) |
| 34 | this.validity = [] |
| 35 | this.calculateValidity() |
| 36 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 37 | |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 38 | calculateValidity() { |
| 39 | this.children.map((item, index) => { |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 40 | if ( index > 0 && item.props.halfSize && this.validity[index - 1] === 'half1' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 41 | this.validity[index] = 'half2'; |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 42 | } else if ( index > 0 && !item.props.halfSize && this.validity[index - 1] === 'half1' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 43 | this.validity[index] = 'full'; |
| 44 | this.validity[index - 1] = 'full'; |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 45 | } else if ( item.props.halfSize && !(index === this.props.children.length - 1) ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 46 | this.validity[index] = 'half1'; |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 47 | } else if ( index === this.props.children.length - 1 && this.validity[index - 1] === 'half1' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 48 | this.validity[index] = 'full'; |
| 49 | this.validity[index - 1] = 'full'; |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 50 | } else if ( index === this.props.children.length - 1 && this.validity[index - 1] === 'half2' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 51 | this.validity[index] = 'full'; |
| 52 | } else { |
| 53 | this.validity[index] = 'full'; |
| 54 | } |
| 55 | }) |
| 56 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 57 | |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 58 | render() { |
| 59 | return ( |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 60 | <div className={Styles.container}> |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 61 | { |
| 62 | this.children.map((item, index) => { |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 63 | if ( this.validity[index] === 'full' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 64 | return this.children[index] |
pineafan | 9babd75 | 2022-10-21 21:47:52 +0100 | [diff] [blame^] | 65 | } else if ( this.validity[index] === 'half1' ) { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 66 | return <div key={index} className={Styles.doublePanel}> |
| 67 | {this.children[index]} |
| 68 | {this.children[index + 1]} |
| 69 | </div> |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | </div> |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 74 | ) |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 75 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | class Title extends Component { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 80 | render() { |
| 81 | return <h1 className={Styles.title}>{this.props.children}</h1> |
| 82 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | class Subtitle extends Component { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 86 | render() { |
| 87 | return <h2 className={Styles.subtitle}>{this.props.children}</h2> |
| 88 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | class Text extends Component { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 92 | render() { |
| 93 | return <p className={Styles.text}>{this.props.children}</p> |
| 94 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | class Divider extends Component { |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 98 | constructor(props) { |
| 99 | super(props); |
| 100 | } |
pineafan | ff3d452 | 2022-05-06 19:51:02 +0100 | [diff] [blame] | 101 | |
pineafan | aa9c4fd | 2022-06-10 19:58:10 +0100 | [diff] [blame] | 102 | render() { |
| 103 | let highlighted = this.props.toHighlight === this.props.name; |
| 104 | return <div className={Styles.divider} style={{ |
| 105 | backgroundColor: (highlighted && this.props.highlightColour) ? ("#" + this.props.highlightColour) : "var(--theme-ui-colors-hint)" |
| 106 | }}>{this.props.forceHighlight}</div> |
| 107 | } |
pineafan | 15b813d | 2022-02-13 10:06:09 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | const Panel = handleViewport(PanelClass, { rootMargin: '-1.0px' }); |
| 111 | |
| 112 | export { AutoLayout, Panel, Title, Subtitle, Text, Divider }; |