Skip to content

Commit

Permalink
Merge pull request #43 from shanedgarrity/true-ref-forwarding
Browse files Browse the repository at this point in the history
implement true ref forwarding, eliminate setTimeout/state/ref workaround
  • Loading branch information
shanedg committed Sep 10, 2018
2 parents d52e361 + 2b548b7 commit 6bb6611
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
24 changes: 13 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ class App extends Component {
}

componentDidMount() {
setTimeout(() => {
if (!this.state.consent.alreadyAsked) {
this.setState({
extraPadding: {
paddingBottom: this.toasterRef.current.state.height + 'px'
}
});
}
}, 0);
if (!this.state.consent.alreadyAsked) {
this.setState({
extraPadding: {
paddingBottom: this.toasterRef.current.clientHeight
}
});
}
}

updateConsent(isGranted) {
Expand Down Expand Up @@ -143,10 +141,14 @@ class App extends Component {
transitionLeaveTimeout={500}>
{this.state.consent &&
!this.state.consent.alreadyAsked &&
<ConsentToaster ref={this.toasterRef} key={'consent-toaster'} consentHandler={this.consentHandler} dismissHandler={this.dismissHandler} />
<ConsentToaster
key={'consent-toaster'}
ref={this.toasterRef}
consentHandler={this.consentHandler}
dismissHandler={this.dismissHandler}
/>
}
</ReactCSSTransitionGroup>

</div>
</Router>
);
Expand Down
24 changes: 7 additions & 17 deletions src/components/Utils/ConsentToaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@ import './ConsentToaster.css';

class ConsentToaster extends Component {

constructor(props) {
super(props);
this.toasterDOM = React.createRef();

this.state = {
height: 0
};
}

componentDidMount() {
this.setState(() => ({
height: this.toasterDOM.current.clientHeight
}));
}

render() {

return (
<div className="Consent-Toaster">
<button className="button dismiss-toaster" id="dismiss-consent-toaster" onClick={this.props.dismissHandler}>X</button>
<div className="toaster-body" ref={this.toasterDOM}>
<div className="toaster-body" ref={this.props.forwardedRef}>
<p>This site uses <a href="https://en.wikipedia.org/wiki/HTTP_cookie" target="_blank" rel="noopener noreferrer" title="read more about Cookies">Cookies</a>; do you consent to allowing this site to store Cookies on your device? You can always change your mind later <Link to="/preferences">here</Link>.</p>
<span className="span consent-choice">
<button className="button" id="cookie-consent-yes" onClick={this.props.consentHandler}>Yes</button>
Expand All @@ -39,4 +24,9 @@ class ConsentToaster extends Component {
}
}

export default ConsentToaster;
export default React.forwardRef((props, ref) => {
return <ConsentToaster
{...props}
forwardedRef={ref}
/>;
});

0 comments on commit 6bb6611

Please sign in to comment.