Skip to content

Commit

Permalink
feat: add enter and exit animation
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanluongoe committed Feb 7, 2024
1 parent 2ae7542 commit 6522ba8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/components/toast.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from '@emotion/styled';
import React, { CSSProperties, HTMLAttributes } from 'react';
import { ReactChildren } from '../core/types';
import { keyframes } from '@emotion/react';

interface ToastViewportProps {
position: 'left' | 'center' | 'right';
Expand Down Expand Up @@ -51,13 +52,22 @@ function calculatePosition(
}

interface ToastProps {
visible?: boolean;
collapsed?: string;
idx?: number;
style?: CSSProperties;
className?: string;
children: ReactChildren;
}

const fadeOut = keyframes`
0% { opacity: 1; }
100% { opacity: 0; }
`;
const fadeIn = keyframes`
0% { opacity: 0; }
100% { opacity: 1; }
`;
const StyledToast = styled('li')`
display: flex;
position: relative;
Expand All @@ -71,19 +81,29 @@ const StyledToast = styled('li')`
line-height: 1.3;
padding: 16px;
margin-bottom: 8px;
animation: ${(props: ToastProps) => (props.visible ? fadeIn : fadeOut)} 0.4s
forwards cubic-bezier(0.06, 0.71, 0.55, 1);
`;

const Toast: React.FC<ToastProps> = (props) => {
const { collapsed, idx, style = {}, children, className = '' } = props;
const {
collapsed,
idx,
style = {},
children,
className = '',
visible,
} = props;
const animationStyle = calculateAnimationStyle(idx!, collapsed!);

return (
<StyledToast
style={{
...style,
...animationStyle,
...style,
}}
className={className}
visible={visible}
>
{children}
</StyledToast>
Expand Down
6 changes: 5 additions & 1 deletion src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ export const Toaster: React.FC<ToasterProps> = (props) => {
className={viewportClassName}
>
{toasts.map(
({ id, title, description, type, icon, style, className }, index) => (
(
{ id, title, description, type, icon, visible, style, className },
index
) => (
<Toast
key={id}
idx={index}
collapsed={collapsed.toString()}
style={style}
visible={visible}
className={className}
>
<div
Expand Down

0 comments on commit 6522ba8

Please sign in to comment.