Skip to content

Commit

Permalink
feat: add animation for icon
Browse files Browse the repository at this point in the history
  • Loading branch information
nhanluongoe committed Jan 4, 2024
1 parent 51853a6 commit e5b820a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
66 changes: 66 additions & 0 deletions src/components/icons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import { styled } from 'goober';

const StyledCheck = styled('i')`
& {
box-sizing: border-box;
position: relative;
display: block;
width: 30px;
height: 30px;
border: 2px solid transparent;
border-radius: 100px;
color: #61d345;
}
&::after {
color: #61d345;
content: '';
display: block;
box-sizing: border-box;
position: absolute;
left: 4px;
top: -1px;
width: 9px;
height: 15px;
border-width: 0 3px 3px 0;
border-style: solid;
transform-origin: bottom left;
transform: rotate(45deg);
}
`;
export const Check: React.FC = () => {
return <StyledCheck />;
};

const StyledClose = styled('i')`
& {
box-sizing: border-box;
position: relative;
display: block;
width: 30px;
height: 30px;
border: 2px solid transparent;
border-radius: 40px;
color: #ff4b4b;
}
&::after,
&::before {
color: ##ff4b4b;
content: '';
display: block;
box-sizing: border-box;
position: absolute;
width: 20px;
height: 3px;
background: currentColor;
transform: rotate(45deg);
top: 10px;
left: 4px;
}
&::after {
transform: rotate(-45deg);
}
`;
export const Close: React.FC = () => {
return <StyledClose />;
};
32 changes: 28 additions & 4 deletions src/components/toast-icon.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import { keyframes, setup, styled } from 'goober';
import React from 'react';
import { Toast } from '../core/types';
import { Check, Close } from './icons';

setup(React.createElement);

interface ToastIconProps {
icon?: Toast['icon'];
type: Toast['type'];
}

const enter = keyframes`
from {
transform: scale(0.6);
opacity: 0.4;
}
to {
transform: scale(1);
opacity: 1;
}`;

export const AnimatedIconWrapper = styled('div')`
position: relative;
transform: scale(0.6);
opacity: 0.4;
min-width: 20px;
animation: ${enter} 0.3s 0.12s cubic-bezier(0.175, 0.885, 0.32, 1.275)
forwards;
`;

export default function ToastIcon(props: ToastIconProps) {
const { icon, type } = props;

if (icon !== undefined) {
return icon;
return <AnimatedIconWrapper>{icon}</AnimatedIconWrapper>;
}

const renderIcon = () => {
if (type === 'success') {
return '✅';
return <Check />;
}
if (type === 'error') {
return '❌';
return <Close />;
}
return null;
};

return <div>{renderIcon()}</div>;
return <AnimatedIconWrapper>{renderIcon()}</AnimatedIconWrapper>;
}

0 comments on commit e5b820a

Please sign in to comment.