-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (66 loc) · 1.85 KB
/
index.js
File metadata and controls
77 lines (66 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React, { Component } from 'react';
import {findDOMNode} from "react-dom";
import PropTypes from 'prop-types'
class ScaledComponent extends Component {
static propTypes = {
maxHeight: PropTypes.number.required,
maxWidth: PropTypes.number.required,
contentClass: PropTypes.string,
children: PropTypes.node.required,
};
static defaultProps = {
contentClass: '',
};
constructor(props) {
super(props);
this.state = {
contentSize: { width: 0, height: 0 },
scale: 1,
};
}
componentDidMount() {
this.setComponentSize()
}
/**
* Gets the size of the component inside the container and scales it using css to the maxWidth, maxHeight set
* Can be called externally as a callback using refs
*/
setComponentSize = () => {
const { maxHeight, maxWidth } = this.props;
const { content } = this.refs;
const actualContent = content.children[0];
const contentSize = findDOMNode(actualContent).getBoundingClientRect()
this.setState({
scale: Math.min((maxWidth / contentSize.width), (maxHeight / contentSize.height)),
contentSize: { width: (maxWidth / contentSize.width), height: (maxHeight / contentSize.height) },
});
}
render() {
const { scale } = this.state;
const { children, contentClass, maxWidth, maxHeight } = this.props;
return (
<div
style={{
height: maxHeight + 'px',
width: maxWidth + 'px',
display: 'flex',
justifyContent: 'center',
}}
>
<div
ref="content"
className={contentClass}
style={{
transform: 'scale(' + scale + ')',
transformOrigin: '0 0 0',
width: '100%',
height: '100%',
}}
>
{children}
</div>
</div>
);
}
}
export default ScaledComponent