import React, { Component } from "react" import cx from "classnames" import * as SC from "./styles" import { Container } from "../Container" interface IHeaderBareboneProps { above?: React.ReactNode title: string content?: React.ReactNode className?: string } interface IBoxedTitleProps { above?: React.ReactNode content?: React.ReactNode } type HeaderBareboneState = { scrollY: Number } const BoxedTitle: React.FC = (props) => { return ( {props.above} {props.children} {props.content} ) } const debounce = (fn) => { let frame return (...params) => { if (frame) { cancelAnimationFrame(frame) } frame = requestAnimationFrame(() => { fn(...params) }) } } export class HeaderBarebone extends Component { constructor(props) { super(props) this.tick = this.tick.bind(this) this.state = { scrollY: 0 } } tick() { if (typeof window !== 'undefined') { this.setState({ scrollY: window.scrollY }) } } componentWillMount() { this.tick() } componentDidMount() { document.addEventListener('scroll', debounce(this.tick), { passive: true }) } render() { const props = this.props; const isBoxed = props.above || props.content return ( {(isBoxed && this.state.scrollY < 30) && ( {props.title} )} {!isBoxed && {props.title}} ) } }