CodeViewer with referenced height of parent div

This commit is contained in:
Delucse 2020-09-07 17:16:59 +02:00
parent c11ff8fc6c
commit dddd9432db

View File

@ -32,10 +32,10 @@ const AccordionSummary = withStyles((theme) => ({
root: {
backgroundColor: theme.palette.secondary.main,
borderBottom: `1px solid white`,
marginBottom: -1,
minHeight: 50,
marginBottom: '-1px',
minHeight: '50px',
'&$expanded': {
minHeight: 50,
minHeight: '50px',
},
},
content: {
@ -55,15 +55,24 @@ const AccordionDetails = withStyles((theme) => ({
class CodeViewer extends Component {
state = {
expanded: true
constructor(props){
super(props);
this.state = {
expanded: true,
componentHeight: null
};
this.myDiv = React.createRef();
}
componentDidMount() {
Prism.highlightAll();
this.setState({componentHeight: this.myDiv.current.offsetHeight+'px'});
}
componentDidUpdate() {
componentDidUpdate(props, state) {
if(this.myDiv.current && this.myDiv.current.offsetHeight+'px' !== this.state.componentHeight){
this.setState({componentHeight: this.myDiv.current.offsetHeight+'px'});
}
Prism.highlightAll();
}
@ -74,8 +83,9 @@ class CodeViewer extends Component {
render() {
var curlyBrackets = '{ }';
var unequal = '<>';
console.log('render', this.myDiv);
return (
<Card style={{height: '100%', maxHeight: '500px'}}>
<Card style={{height: '100%', maxHeight: '500px'}} ref={this.myDiv}>
<Accordion
square={true}
style={{margin: 0}}
@ -86,7 +96,7 @@ class CodeViewer extends Component {
<b style={{fontSize: '20px', marginRight: '5px', width: '35px'}}>{curlyBrackets}</b>
<div style={{margin: 'auto 5px 2px 0px'}}>Arduino Quellcode</div>
</AccordionSummary>
<AccordionDetails style={{padding: 0, height: 'calc(500px - 50px - 50px)', backgroundColor: 'white'}}>
<AccordionDetails style={{padding: 0, height: `calc(${this.state.componentHeight} - 50px - 50px)`, backgroundColor: 'white'}}>
<pre className="line-numbers" style={{paddingBottom: 0, width: '100%', overflow: 'auto', scrollbarWidth: 'thin', height: 'calc(100% - 30px)', margin: '15px 0', paddingTop: 0, whiteSpace: 'pre-wrap', backgroundColor: 'white'}}>
<code className="language-clike">
{this.props.arduino}
@ -104,7 +114,7 @@ class CodeViewer extends Component {
<b style={{fontSize: '20px', marginRight: '5px', width: '35px'}}>{unequal}</b>
<div style={{margin: 'auto 5px 2px 0px'}}>XML Blöcke</div>
</AccordionSummary>
<AccordionDetails style={{padding: 0, height: 'calc(500px - 50px - 50px)', backgroundColor: 'white'}}>
<AccordionDetails style={{padding: 0, height: `calc(${this.state.componentHeight} - 50px - 50px)`, backgroundColor: 'white'}}>
<pre className="line-numbers" style={{paddingBottom: 0, width: '100%', overflow: 'auto', scrollbarWidth: 'thin', height: 'calc(100% - 30px)', margin: '15px 0', paddingTop: 0, whiteSpace: 'pre-wrap', backgroundColor: 'white'}}>
<code className="language-xml">
{`${this.props.xml}`}