-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update
ToastManager
to support custom container and transitio…
…n components
- Loading branch information
Showing
3 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/react-docs/pages/components/toast-manager/custom-toast-container.js
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,74 @@ | ||
import { Box, Button, Toast, ToastContainer, ToastManager, useToastManager } from '@tonic-ui/react'; | ||
import React from 'react'; | ||
|
||
const MyContainerComponent = ({ | ||
placement, | ||
...rest | ||
}) => { | ||
const verticalMargin = '6x'; // 24px | ||
const horizontalMargin = '4x'; // 16px | ||
const styleProps = {}; | ||
|
||
if (placement.includes('top')) { | ||
styleProps.top = verticalMargin; | ||
} | ||
if (placement.includes('bottom')) { | ||
styleProps.bottom = verticalMargin; | ||
} | ||
if (placement.includes('left')) { | ||
styleProps.left = horizontalMargin; | ||
} | ||
if (placement.includes('right')) { | ||
styleProps.right = horizontalMargin; | ||
} | ||
|
||
return ( | ||
<ToastContainer | ||
placement={placement} | ||
{...styleProps} | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
|
||
const MyApp = () => { | ||
const toast = useToastManager(); | ||
|
||
return ( | ||
<Button | ||
onClick={() => { | ||
const render = ({ onClose, placement }) => ( | ||
<Box my="2x"> | ||
<Toast appearance="info" isClosable onClose={onClose} minWidth={320}> | ||
Hello world! | ||
</Toast> | ||
</Box> | ||
); | ||
|
||
const placement = 'bottom-right'; | ||
if (toast.state[placement].length >= 3) { | ||
toast.close(toast.state[placement][0].id, placement); | ||
} | ||
|
||
const options = { | ||
placement: placement, | ||
duration: 30 * 1000, | ||
}; | ||
toast(render, options); | ||
}} | ||
> | ||
Notify | ||
</Button> | ||
); | ||
}; | ||
|
||
const App = () => ( | ||
<ToastManager | ||
ContainerComponent={MyContainerComponent} | ||
placement="bottom-right" | ||
> | ||
<MyApp /> | ||
</ToastManager> | ||
); | ||
|
||
export default App; |
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