blob: b700cf7a1cd7f5dd8958375f869f8a8aadd96155 [file] [log] [blame]
pineafan15b813d2022-02-13 10:06:09 +00001import react, { Component } from "react";
2import Styles from '../styles/Components/panels.module.css';
3import handleViewport from 'react-in-viewport';
4
5
6class PanelClass extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +01007 constructor(props) {
8 super(props);
PineaFana465f352023-02-05 16:45:01 +00009 this.panel = react.createRef();
pineafanaa9c4fd2022-06-10 19:58:10 +010010 }
pineafan15b813d2022-02-13 10:06:09 +000011
pineafanaa9c4fd2022-06-10 19:58:10 +010012 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 }
pineafan15b813d2022-02-13 10:06:09 +000018
pineafanaa9c4fd2022-06-10 19:58:10 +010019 render() {
PineaFana465f352023-02-05 16:45:01 +000020 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 }
pineafanaa9c4fd2022-06-10 19:58:10 +010034 </div>
35 }
pineafan15b813d2022-02-13 10:06:09 +000036}
37
38
39class AutoLayout extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +010040 constructor(props) {
41 super(props);
pineafanaa9c4fd2022-06-10 19:58:10 +010042 this.validity = []
43 this.calculateValidity()
44 }
pineafan15b813d2022-02-13 10:06:09 +000045
pineafanaa9c4fd2022-06-10 19:58:10 +010046 calculateValidity() {
PineaFana465f352023-02-05 16:45:01 +000047 react.Children.toArray(this.props.children).map((item, index) => {
pineafan9babd752022-10-21 21:47:52 +010048 if ( index > 0 && item.props.halfSize && this.validity[index - 1] === 'half1' ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010049 this.validity[index] = 'half2';
pineafan9babd752022-10-21 21:47:52 +010050 } else if ( index > 0 && !item.props.halfSize && this.validity[index - 1] === 'half1' ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010051 this.validity[index] = 'full';
52 this.validity[index - 1] = 'full';
pineafan9babd752022-10-21 21:47:52 +010053 } else if ( item.props.halfSize && !(index === this.props.children.length - 1) ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010054 this.validity[index] = 'half1';
pineafan9babd752022-10-21 21:47:52 +010055 } else if ( index === this.props.children.length - 1 && this.validity[index - 1] === 'half1' ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010056 this.validity[index] = 'full';
57 this.validity[index - 1] = 'full';
pineafan9babd752022-10-21 21:47:52 +010058 } else if ( index === this.props.children.length - 1 && this.validity[index - 1] === 'half2' ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010059 this.validity[index] = 'full';
60 } else {
61 this.validity[index] = 'full';
62 }
63 })
64 }
pineafan15b813d2022-02-13 10:06:09 +000065
pineafanaa9c4fd2022-06-10 19:58:10 +010066 render() {
PineaFana465f352023-02-05 16:45:01 +000067 const children = react.Children.toArray(this.props.children)
pineafanaa9c4fd2022-06-10 19:58:10 +010068 return (
PineaFana465f352023-02-05 16:45:01 +000069 <div className={Styles.container} ref={this.props.forwardedRef}>
pineafanaa9c4fd2022-06-10 19:58:10 +010070 {
PineaFana465f352023-02-05 16:45:01 +000071 children.map((item, index) => {
pineafan9babd752022-10-21 21:47:52 +010072 if ( this.validity[index] === 'full' ) {
PineaFana465f352023-02-05 16:45:01 +000073 return children[index]
pineafan9babd752022-10-21 21:47:52 +010074 } else if ( this.validity[index] === 'half1' ) {
pineafanaa9c4fd2022-06-10 19:58:10 +010075 return <div key={index} className={Styles.doublePanel}>
PineaFana465f352023-02-05 16:45:01 +000076 {children[index]}
77 {children[index + 1]}
pineafanaa9c4fd2022-06-10 19:58:10 +010078 </div>
79 }
80 })
81 }
82 </div>
pineafan15b813d2022-02-13 10:06:09 +000083 )
pineafanaa9c4fd2022-06-10 19:58:10 +010084 }
pineafan15b813d2022-02-13 10:06:09 +000085}
86
87
88class Title extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +010089 render() {
90 return <h1 className={Styles.title}>{this.props.children}</h1>
91 }
pineafan15b813d2022-02-13 10:06:09 +000092}
93
94class Subtitle extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +010095 render() {
96 return <h2 className={Styles.subtitle}>{this.props.children}</h2>
97 }
pineafan15b813d2022-02-13 10:06:09 +000098}
99
100class Text extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +0100101 render() {
102 return <p className={Styles.text}>{this.props.children}</p>
103 }
pineafan15b813d2022-02-13 10:06:09 +0000104}
105
106class Divider extends Component {
pineafanaa9c4fd2022-06-10 19:58:10 +0100107 constructor(props) {
108 super(props);
109 }
pineafanff3d4522022-05-06 19:51:02 +0100110
pineafanaa9c4fd2022-06-10 19:58:10 +0100111 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 }
pineafan15b813d2022-02-13 10:06:09 +0000117}
118
119const Panel = handleViewport(PanelClass, { rootMargin: '-1.0px' });
120
121export { AutoLayout, Panel, Title, Subtitle, Text, Divider };