Skip to content

Instantly share code, notes, and snippets.

@xamedow
Created August 18, 2019 21:10
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 xamedow/aff42ae9a8cbcf3dcd70f853247c17e5 to your computer and use it in GitHub Desktop.
Save xamedow/aff42ae9a8cbcf3dcd70f853247c17e5 to your computer and use it in GitHub Desktop.
Final form condition animated component with leave staet and translate animation
import React from "react";
import { Field } from "react-final-form";
import { useTransition, animated } from "react-spring";
import styled from "styled-components";
interface Props extends React.HTMLProps<HTMLElement> {
when: string;
is: any;
}
interface InnerProps {
input: {
value: any;
};
}
const Wrapper = styled.div`
overflow: hidden;
`;
const Inner = ({
value,
is,
children
}: {
value: any;
is: any;
children: React.ReactNode;
}) => {
const contentStatus = value === is;
const transitions = useTransition(contentStatus, null, {
from: {
transformOrigin: "top center",
opacity: 0,
transform: "translateY(-100%)"
},
enter: { opacity: 1, transform: "translateY(0)" },
leave: { opacity: 0, transform: "translateY(-100%)" }
});
return transitions.map(
({ item, key, props }) =>
item && (
<animated.div key={key} style={props}>
{children}
</animated.div>
)
);
};
const Condition = ({ when, is, children }: Props) => (
<Wrapper>
<Field name={when} subscription={{ value: true }}>
{({ input: { value } }: InnerProps) => (
// @ts-ignore
<Inner value={value} is={is}>
{children}
</Inner>
)}
</Field>
</Wrapper>
);
export default Condition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment