-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use sonner for toast instead of chakra (#538)
* feat: Use sonner for toast instead of chakra * fix: toast clear in demo modal interceptor * fix: language in documentation * fix: dark mode issue * fix: remove unused import
- Loading branch information
1 parent
ff723c0
commit 6b6f3b2
Showing
24 changed files
with
312 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import React from 'react'; | ||
|
||
import { Box, Button, Flex } from '@chakra-ui/react'; | ||
import { Meta } from '@storybook/react'; | ||
import { toast } from 'sonner'; | ||
|
||
import { toastCustom } from '@/components/Toast'; | ||
|
||
export default { | ||
title: 'Components/Toast', | ||
decorators: [ | ||
(Story) => ( | ||
<Box h="10rem"> | ||
<Story /> | ||
</Box> | ||
), | ||
], | ||
} satisfies Meta; | ||
|
||
export const Default = () => { | ||
const handleOpenToast = (props: { status: 'success' | 'error' | 'info' }) => { | ||
toastCustom({ | ||
status: props.status, | ||
title: 'This is a toast', | ||
}); | ||
}; | ||
|
||
return ( | ||
<Flex gap={4}> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'success' })}> | ||
Success toast | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'error' })}> | ||
Error toast | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'info' })}> | ||
Info toast | ||
</Button> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const WithDescription = () => { | ||
const handleOpenToast = (props: { status: 'success' | 'error' | 'info' }) => { | ||
toastCustom({ | ||
status: props.status, | ||
title: 'This is a toast', | ||
description: | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis id porta lacus. Nunc tellus ipsum, blandit commodo neque at, eleifend facilisis arcu. Phasellus nec pretium sapien.', | ||
}); | ||
}; | ||
return ( | ||
<Flex gap={4}> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'success' })}> | ||
Success toast with description | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'error' })}> | ||
Error toast with description | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'info' })}> | ||
Info toast with description | ||
</Button> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const WithActions = () => { | ||
const handleOpenToast = (props: { status: 'success' | 'error' | 'info' }) => { | ||
toastCustom({ | ||
status: props.status, | ||
title: 'This is a toast', | ||
actions: ( | ||
<Button onClick={() => toast.dismiss()}>Close all toasts</Button> | ||
), | ||
}); | ||
}; | ||
return ( | ||
<Flex gap={4}> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'success' })}> | ||
Success toast with actions | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'error' })}> | ||
Error toast with actions | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'info' })}> | ||
Info toast with with actions | ||
</Button> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export const HideIcon = () => { | ||
const handleOpenToast = (props: { status: 'success' | 'error' | 'info' }) => { | ||
toastCustom({ | ||
status: props.status, | ||
title: 'This is a toast', | ||
hideIcon: true, | ||
}); | ||
}; | ||
return ( | ||
<Flex gap={4}> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'success' })}> | ||
Success toast without icon | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'error' })}> | ||
Error toast without icon | ||
</Button> | ||
<Button size="md" onClick={() => handleOpenToast({ status: 'info' })}> | ||
Info toast without icon | ||
</Button> | ||
</Flex> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { ReactNode } from 'react'; | ||
|
||
import { | ||
Box, | ||
ButtonGroup, | ||
Card, | ||
CardBody, | ||
Flex, | ||
Heading, | ||
IconButton, | ||
} from '@chakra-ui/react'; | ||
import i18n from 'i18next'; | ||
import { LuCheckCircle2, LuInfo, LuX, LuXCircle } from 'react-icons/lu'; | ||
import { ExternalToast, toast } from 'sonner'; | ||
import { match } from 'ts-pattern'; | ||
|
||
import { Icon } from '@/components/Icons'; | ||
|
||
export const toastCustom = (params: { | ||
status?: 'info' | 'success' | 'error'; | ||
hideIcon?: boolean; | ||
title: ReactNode; | ||
description?: ReactNode; | ||
actions?: ReactNode; | ||
}) => { | ||
const status = params.status ?? 'info'; | ||
const icon = match(status) | ||
.with('info', () => LuInfo) | ||
.with('success', () => LuCheckCircle2) | ||
.with('error', () => LuXCircle) | ||
.exhaustive(); | ||
|
||
const options: ExternalToast = { | ||
duration: status === 'error' ? Infinity : 3000, | ||
}; | ||
|
||
toast.custom( | ||
(t) => ( | ||
<Flex> | ||
<IconButton | ||
zIndex={1} | ||
size="xs" | ||
aria-label={i18n.t('components:toast.closeToast')} | ||
icon={<LuX />} | ||
onClick={() => toast.dismiss(t)} | ||
position="absolute" | ||
top={-2.5} | ||
right={-2.5} | ||
borderRadius="full" | ||
/> | ||
<Card | ||
w="356px" | ||
position="relative" | ||
overflow="hidden" | ||
boxShadow="layout" | ||
> | ||
<Box | ||
position="absolute" | ||
top={0} | ||
left={0} | ||
bottom={0} | ||
w="3px" | ||
bg={`${status}.600`} | ||
/> | ||
<CardBody | ||
display="flex" | ||
flexDirection="column" | ||
gap={1.5} | ||
p={4} | ||
color="gray.800" | ||
_dark={{ | ||
color: 'white', | ||
}} | ||
> | ||
<Flex alignItems="center" gap={2}> | ||
<Heading size="xs" flex={1}> | ||
{!params.hideIcon && ( | ||
<Icon | ||
icon={icon} | ||
mr={2} | ||
fontSize="1.2em" | ||
color={`${status}.500`} | ||
/> | ||
)} | ||
{params.title} | ||
</Heading> | ||
{!!params.actions && ( | ||
<ButtonGroup size="xs">{params.actions}</ButtonGroup> | ||
)} | ||
</Flex> | ||
{!!params.description && ( | ||
<Flex | ||
direction="column" | ||
fontSize="xs" | ||
color="gray.600" | ||
_dark={{ color: 'gray.400' }} | ||
> | ||
{params.description} | ||
</Flex> | ||
)} | ||
</CardBody> | ||
</Card> | ||
</Flex> | ||
), | ||
options | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.