mirror of
https://github.com/ascribe/onion.git
synced 2024-11-15 09:35:10 +01:00
add font rendering logic
This commit is contained in:
parent
c58227b443
commit
370a3b43dd
@ -25,8 +25,6 @@ let FlowType = React.createClass({
|
|||||||
children: React.PropTypes.element.isRequired // only supporting one child element at once right now
|
children: React.PropTypes.element.isRequired // only supporting one child element at once right now
|
||||||
},
|
},
|
||||||
|
|
||||||
// Establish default settings/variables
|
|
||||||
// ====================================
|
|
||||||
getDefaultProps() {
|
getDefaultProps() {
|
||||||
return {
|
return {
|
||||||
maximum: 9999,
|
maximum: 9999,
|
||||||
@ -39,18 +37,33 @@ let FlowType = React.createClass({
|
|||||||
|
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
return {
|
return {
|
||||||
fontSize: 1
|
// 32 because that's the default font display size
|
||||||
|
// doesn't really matter though
|
||||||
|
fontSize: 32
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
|
// Make changes upon resize, calculate changes and rerender
|
||||||
window.addEventListener('resize', this.handleResize);
|
window.addEventListener('resize', this.handleResize);
|
||||||
},
|
},
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
|
// stop listening to window once the component was unmounted
|
||||||
window.removeEventListener('resize', this.handleResize);
|
window.removeEventListener('resize', this.handleResize);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
handleResize() {
|
||||||
|
let elemWidth = this.refs.flowTypeElement.getDOMNode().offsetWidth;
|
||||||
|
let width = elemWidth > this.props.maximum ? this.props.maximum : elemWidth < this.props.minimum ? this.props.minimum : elemWidth;
|
||||||
|
let fontBase = width / this.props.fontRatio;
|
||||||
|
let fontSize = fontBase > this.props.maxFont ? this.props.maxFont : fontBase < this.props.minFont ? this.props.minFont : fontBase;
|
||||||
|
|
||||||
|
this.setState({ fontSize });
|
||||||
|
},
|
||||||
|
|
||||||
|
// The child the user passes to this component needs to have it's
|
||||||
|
// style.fontSize property to be updated
|
||||||
renderChildren() {
|
renderChildren() {
|
||||||
return ReactAddons.Children.map(this.props.children, (child) => {
|
return ReactAddons.Children.map(this.props.children, (child) => {
|
||||||
return ReactAddons.addons.cloneWithProps(child, {
|
return ReactAddons.addons.cloneWithProps(child, {
|
||||||
@ -63,7 +76,7 @@ let FlowType = React.createClass({
|
|||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<span>
|
<span ref="flowTypeElement">
|
||||||
{this.renderChildren()}
|
{this.renderChildren()}
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user