Skip to content

Commit

Permalink
isMobile inside componentDidMount #28
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Feb 19, 2018
1 parent a3198d2 commit 98f9331
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pages/public/read-chapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ class ReadChapter extends React.Component {
showChapters: false,
chapter,
htmlContent,
isMobile: false,
};
}

componentDidMount() {
this.mainContentElm.addEventListener('scroll', this.onScroll);

let isMobile = false;
if (window.innerWidth < 768) {
isMobile = true;
}

this.setState({ isMobile }); // eslint-disable-line
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -155,9 +163,18 @@ class ReadChapter extends React.Component {
});
};

closeTocWhenMobile = () => {
this.setState({ showChapters: !this.state.isMobile });
};

renderMainContent() {
const { user, showStripeModal } = this.props;
const { chapter, htmlContent } = this.state;
const {
chapter,
htmlContent,
isMobile,
showChapters,
} = this.state;

return (
<div
Expand Down Expand Up @@ -199,6 +216,7 @@ class ReadChapter extends React.Component {
color: activeSection && activeSection.hash === s.escapedText ? '#1565C0' : '#222',
}}
href={`#${s.escapedText}`}
onClick={this.closeTocWhenMobile}
>
{s.text}
</a>
Expand Down Expand Up @@ -239,6 +257,7 @@ class ReadChapter extends React.Component {
key={ch._id}
role="presentation"
style={{ listStyle: i === 0 ? 'none' : 'decimal', paddingBottom: '10px' }}
onClick={this.closeTocWhenMobile}
>
<Link
prefetch
Expand Down

0 comments on commit 98f9331

Please sign in to comment.