mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 23:46:03 +01:00
refactor(web/notifications): improve notification transitions
This commit is contained in:
@@ -3,7 +3,7 @@ import { IconProp } from '@fortawesome/fontawesome-svg-core';
|
|||||||
import { toast, Toaster, ToastPosition } from 'react-hot-toast';
|
import { toast, Toaster, ToastPosition } from 'react-hot-toast';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import ReactMarkdown from 'react-markdown';
|
import ReactMarkdown from 'react-markdown';
|
||||||
import { Avatar, createStyles, Group, Stack, Box, Text } from '@mantine/core';
|
import { Avatar, createStyles, Group, Stack, Box, Text, keyframes } from '@mantine/core';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
export interface NotificationProps {
|
export interface NotificationProps {
|
||||||
@@ -49,6 +49,73 @@ const useStyles = createStyles((theme) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// I hate this
|
||||||
|
const enterAnimationTop = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateY(-30px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateY(0px)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const enterAnimationBottom = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateY(30px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateY(0px)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const exitAnimationTop = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateY(0px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateY(-100%)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const exitAnimationRight = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateX(0px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateX(100%)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const exitAnimationLeft = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateX(0px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateX(-100%)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const exitAnimationBottom = keyframes({
|
||||||
|
from: {
|
||||||
|
opacity: 1,
|
||||||
|
transform: 'translateY(0px)',
|
||||||
|
},
|
||||||
|
to: {
|
||||||
|
opacity: 0,
|
||||||
|
transform: 'translateY(100%)',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const Notifications: React.FC = () => {
|
const Notifications: React.FC = () => {
|
||||||
const { classes } = useStyles();
|
const { classes } = useStyles();
|
||||||
|
|
||||||
@@ -68,39 +135,56 @@ const Notifications: React.FC = () => {
|
|||||||
data.icon = data.type === 'error' ? 'xmark' : data.type === 'success' ? 'check' : 'info';
|
data.icon = data.type === 'error' ? 'xmark' : data.type === 'success' ? 'check' : 'info';
|
||||||
}
|
}
|
||||||
|
|
||||||
toast(
|
toast.custom(
|
||||||
<Box style={data.style} className={`${classes.container}`}>
|
(t) => (
|
||||||
<Group noWrap spacing={12}>
|
<Box
|
||||||
{data.icon && (
|
sx={{
|
||||||
<>
|
animation: t.visible
|
||||||
{!data.iconColor ? (
|
? `${position?.includes('bottom') ? enterAnimationBottom : enterAnimationTop} 0.2s ease-out forwards`
|
||||||
<Avatar
|
: `${
|
||||||
color={data.type === 'error' ? 'red' : data.type === 'success' ? 'teal' : 'blue'}
|
position?.includes('right')
|
||||||
radius="xl"
|
? exitAnimationRight
|
||||||
size={(data.title && !data.description) || (data.description && !data.title) ? 28 : undefined}
|
: position?.includes('left')
|
||||||
>
|
? exitAnimationLeft
|
||||||
<FontAwesomeIcon icon={data.icon} fixedWidth size="lg" />
|
: position === 'top-center'
|
||||||
</Avatar>
|
? exitAnimationTop
|
||||||
) : (
|
: position
|
||||||
<FontAwesomeIcon icon={data.icon} style={{ color: data.iconColor }} fixedWidth size="lg" />
|
? exitAnimationBottom
|
||||||
)}
|
: exitAnimationRight
|
||||||
</>
|
} 0.4s ease-in forwards`,
|
||||||
)}
|
}}
|
||||||
<Stack spacing={0}>
|
style={{
|
||||||
{data.title && <Text className={classes.title}>{data.title}</Text>}
|
...data.style,
|
||||||
{data.description && <ReactMarkdown className={classes.description}>{data.description}</ReactMarkdown>}
|
}}
|
||||||
</Stack>
|
className={`${classes.container}`}
|
||||||
</Group>
|
>
|
||||||
</Box>,
|
<Group noWrap spacing={12}>
|
||||||
|
{data.icon && (
|
||||||
|
<>
|
||||||
|
{!data.iconColor ? (
|
||||||
|
<Avatar
|
||||||
|
color={data.type === 'error' ? 'red' : data.type === 'success' ? 'teal' : 'blue'}
|
||||||
|
radius="xl"
|
||||||
|
size={(data.title && !data.description) || (data.description && !data.title) ? 28 : undefined}
|
||||||
|
>
|
||||||
|
<FontAwesomeIcon icon={data.icon} fixedWidth size="lg" />
|
||||||
|
</Avatar>
|
||||||
|
) : (
|
||||||
|
<FontAwesomeIcon icon={data.icon} style={{ color: data.iconColor }} fixedWidth size="lg" />
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
<Stack spacing={0}>
|
||||||
|
{data.title && <Text className={classes.title}>{data.title}</Text>}
|
||||||
|
{data.description && <ReactMarkdown className={classes.description}>{data.description}</ReactMarkdown>}
|
||||||
|
</Stack>
|
||||||
|
</Group>
|
||||||
|
</Box>
|
||||||
|
),
|
||||||
{
|
{
|
||||||
id: data.id?.toString(),
|
id: data.id?.toString(),
|
||||||
duration: data.duration || 3000,
|
duration: data.duration || 3000,
|
||||||
position: position || 'top-right',
|
position: position || 'top-right',
|
||||||
style: {
|
|
||||||
padding: 0,
|
|
||||||
boxShadow: 'none',
|
|
||||||
backgroundColor: 'transparent',
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user