Skip to content

Instantly share code, notes, and snippets.

@kitze
Created October 1, 2019 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitze/36640f377ce9b3fe05aba005da35b56a to your computer and use it in GitHub Desktop.
Save kitze/36640f377ce9b3fe05aba005da35b56a to your computer and use it in GitHub Desktop.
Revealhttps://github.com/kitze/tortilla/blob/master/src/styles/reveal.tsx
import { VisibilityProperty } from 'csstype';
import React, { useEffect, useState } from 'react';
import { useInView } from 'react-intersection-observer';
import { reveals } from 'styles/reveal-animations';
import { CSSObject } from 'styled-components';
export enum RevealMode {
Clone,
Wrap
}
export const Reveal: React.FC<{
animation?: any;
delay?: number;
children?: any;
ratio?: number;
mode?: RevealMode;
debugName?: string;
style?: CSSObject;
onShowDone?: () => void;
}> = ({ children, onShowDone, mode = RevealMode.Wrap, animation = reveals.fadeInUp, delay = 0, debugName, style }) => {
const [ref, inView] = useInView({ triggerOnce: true });
const [show, setShow] = useState(false);
useEffect(() => {
if (debugName) {
console.log('debugging', debugName);
}
if (inView) {
if (debugName) {
console.log(`${debugName} is in view`);
}
setTimeout(() => {
setShow(true);
onShowDone && onShowDone();
if (debugName) {
console.log(`showing ${debugName}`);
}
}, delay);
}
}, [inView]);
let extraProps = {
className: show ? animation : children.props ? children.props.className : '',
style: { visibility: (show ? 'visible' : 'hidden') as VisibilityProperty, ...style },
ref
};
const cloned = React.cloneElement(children, extraProps);
return mode === RevealMode.Clone ? cloned : <div {...extraProps}>{children}</div>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment