Files
koodiklinikka.fi/components/Fader.tsx
Olavi Haapala f4474523ad Chore/convert to typescript (#60)
* Convert to using TypeScript

Next.js v.9 allows converting an existing project to TS by simply renaming the files as TS files

* Fix type errors: Type 'string' is not assignable to type 'number'.

* Add mention about missing typings for javascript-time-ago

* Add GA_INITIALIZED to window

* Fix type error in feed transformers

* Model MembershipInfoForm state and props with TS

* Silence the typescript warning in MembershipInfoForm

* Add threshold to Fader props

* Fix Warning: Each child in a list should have a unique "key" prop.

Sponsors don't have ids – name is probably unique and can be used as a key

* Allow key as prop for SponsorLink

* Add type of the props for SponsorLink
2019-11-07 07:28:02 +02:00

47 lines
966 B
TypeScript

import React from "react";
type Props = {
threshold: number;
};
function clamp(min, max, value) {
return Math.min(Math.max(value, min), max);
}
export default class Fader extends React.Component<Props> {
static defaultProps = {
threshold: 100,
};
state = {
opacity: 0,
};
onScroll = () => {
const scrollableDistance = document.body.scrollHeight - window.innerHeight,
scrollTop = window.pageYOffset || document.documentElement.scrollTop,
distanceToBottom = scrollableDistance - scrollTop;
this.setState({
opacity: clamp(0, 1, distanceToBottom / this.props.threshold),
});
};
componentDidMount() {
window.addEventListener("scroll", this.onScroll);
this.onScroll();
}
componentWillUnmount() {
window.removeEventListener("scroll", this.onScroll);
}
render() {
const style = {
opacity: this.state.opacity,
};
return <div className="fader" style={style}></div>;
}
}