Skip to content

Fix bad scale on load #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ There are two optional props that can be passed.
* **minFontSize** - the minimum font size to scale down to (_floor_) - default `Number.NEGATIVE_INFINITY`
* **maxFontSize** - the maximum font size to scale up to (_ceiling_) - default `Number.POSITIVE_INFINITY`
* **widthOnly** - will scale the element based on the width of it's container only, not the height - default `false`
* **fitParent** - will calculcate width based on the parent div width and use fit-content when font don't scaled - default `false`
* **parentDiff** - diff for width when used **fitParent**
35 changes: 30 additions & 5 deletions docs/react-scale-text.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ScaleText extends Component {
if (this.shouldResize()) {
this.resize();
window.addEventListener('resize', this._handleResize);
window.addEventListener('load', this._handleResize);
}
}

Expand All @@ -54,6 +55,7 @@ class ScaleText extends Component {
componentWillUnmount() {
if (!this.shouldResize()) {
window.removeEventListener('resize', this._handleResize);
window.removeEventListener('load', this._handleResize);
}
}

Expand Down Expand Up @@ -93,11 +95,21 @@ class ScaleText extends Component {
// Create copy of wrapper for sizing
this.ruler = this._wrapper.cloneNode(true);
this.ruler.id = shortId();
let nodeWidth = '';
if (this.props.fitParent) {
nodeWidth = getStyle(this._wrapper.parentNode, 'width');
if (this.props.parentDiff !== undefined) {
nodeWidth = `calc(${nodeWidth} - ${this.props.parentDiff})`;
}
}
else {
nodeWidth = getStyle(this._wrapper, 'width');
}
css(this.ruler, {
position: 'absolute',
top: '0px',
left: 'calc(100vw * 2)',
width: getStyle(this._wrapper, 'width'),
width: nodeWidth,
height: getStyle(this._wrapper, 'height')
});
document.body.appendChild(this.ruler);
Expand All @@ -112,7 +124,7 @@ class ScaleText extends Component {

render() {
const { size: fontSize } = this.state;
const { children, widthOnly } = this.props;
const { children, widthOnly, maxFontSize, fitParent } = this.props;

const overflowStyle = widthOnly ?
{ overflowY: 'visible', overflowX: 'hidden', height: 'auto' } :
Expand All @@ -122,9 +134,14 @@ class ScaleText extends Component {
React.Children.only(children) :
(<span>{children}</span>);

let nodeWidth = '100%';
if (fitParent && fontSize !== null && (maxFontSize - fontSize) < Number.EPSILON) {
nodeWidth = 'fit-content';
}

const style = {
fontSize: fontSize ? `${fontSize.toFixed(2)}px` : 'inherit',
width: '100%',
width: nodeWidth,
height: '100%',
...overflowStyle
// overflow: 'hidden'
Expand Down Expand Up @@ -154,13 +171,17 @@ ScaleText.propTypes = {
children: PropTypes.node.isRequired,
minFontSize: PropTypes.number.isRequired,
maxFontSize: PropTypes.number.isRequired,
widthOnly: PropTypes.bool
widthOnly: PropTypes.bool,
fitParent: PropTypes.bool,
parentDiff: PropTypes.string
};

ScaleText.defaultProps = {
minFontSize: Number.NEGATIVE_INFINITY,
maxFontSize: Number.POSITIVE_INFINITY,
widthOnly: false
widthOnly: false,
fitParent: false,
parentDiff: undefined
};

// export default ScaleText;
Expand Down