import * as React from 'react' import Cookie from 'js-cookie' export type ChildrenProps = Readonly<{ name: string }> export type Props = Readonly<{ name: string className?: string hasCloseButton: boolean children: (props: ChildrenProps) => React.ReactNode }> export type State = Readonly<{ bannerHasBeenClosed: boolean }> export default class Banner extends React.Component { readonly state: State constructor(props: Props) { super(props) this.state = { bannerHasBeenClosed: Cookie.get(`${props.name}--is-closed`), } } closeBanner = () => { Cookie.set(`${this.props.name}--is-closed`, true, { expires: 365 }) this.setState({ bannerHasBeenClosed: true }) } render() { const { name, className, hasCloseButton, children } = this.props return !this.state.bannerHasBeenClosed ? (
{children({ name })} {hasCloseButton && (
)}
) : null } }