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