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