-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from abhinavkrin/new/toastbar-components
[New] toastbar components
- Loading branch information
Showing
24 changed files
with
296 additions
and
24 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
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
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,14 @@ | ||
import React from 'react'; | ||
|
||
const Check = (props) => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 32 32" | ||
fill="currentColor" | ||
{...props} | ||
> | ||
<path d="M26.7033 7.28911C27.0959 7.67753 27.0993 8.31069 26.7109 8.7033L11.871 23.7033C11.6831 23.8932 11.427 24.0001 11.1599 24C10.8927 23.9999 10.6367 23.8929 10.4489 23.7029L5.28872 18.4814C4.90052 18.0886 4.90426 17.4554 5.29709 17.0672C5.68991 16.679 6.32307 16.6827 6.71128 17.0756L11.1605 21.5777L25.2891 7.2967C25.6775 6.90408 26.3107 6.90069 26.7033 7.28911Z" /> | ||
</svg> | ||
); | ||
|
||
export default Check; |
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,14 @@ | ||
import React from 'react'; | ||
|
||
const ErrorCircle = (props) => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 32 32" | ||
fill="currentColor" | ||
{...props} | ||
> | ||
<path d="M29 16C29 23.1797 23.1797 29 16 29C8.8203 29 3 23.1797 3 16C3 8.8203 8.8203 3 16 3C23.1797 3 29 8.8203 29 16ZM20.7071 11.2929C20.3166 10.9024 19.6834 10.9024 19.2929 11.2929L16 14.5858L12.7071 11.2929C12.3166 10.9024 11.6834 10.9024 11.2929 11.2929C10.9024 11.6834 10.9024 12.3166 11.2929 12.7071L14.5858 16L11.2929 19.2929C10.9024 19.6834 10.9024 20.3166 11.2929 20.7071C11.6834 21.0976 12.3166 21.0976 12.7071 20.7071L16 17.4142L19.2929 20.7071C19.6834 21.0976 20.3166 21.0976 20.7071 20.7071C21.0976 20.3166 21.0976 19.6834 20.7071 19.2929L17.4142 16L20.7071 12.7071C21.0976 12.3166 21.0976 11.6834 20.7071 11.2929Z" /> | ||
</svg> | ||
); | ||
|
||
export default ErrorCircle; |
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
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,86 @@ | ||
/* eslint-disable no-shadow */ | ||
import React, { useMemo, useRef, useEffect } from 'react'; | ||
import { css, useTheme, keyframes } from '@emotion/react'; | ||
import alpha from 'color-alpha'; | ||
import { appendClassNames } from '../../lib/appendClassNames'; | ||
import useComponentOverrides from '../../theme/useComponentOverrides'; | ||
import { Box } from '../Box'; | ||
import { Icon } from '../Icon'; | ||
import { ActionButton } from '../ActionButton'; | ||
|
||
const ToastBar = ({ toast, onClose }) => { | ||
const { type, message, time = 2000 } = toast; | ||
const toastRef = useRef(); | ||
const theme = useTheme(); | ||
const { classNames, styleOverrides } = useComponentOverrides('ToastBar'); | ||
const { iconName, bgColor, color } = useMemo(() => { | ||
const color = theme.palette?.[type]?.main; | ||
const bgColor = alpha(color, 0.3); | ||
let iconName = 'success'; | ||
switch (type) { | ||
case 'info': | ||
iconName = 'info'; | ||
break; | ||
case 'warning': | ||
iconName = 'report'; | ||
break; | ||
case 'error': | ||
iconName = 'error-circle'; | ||
break; | ||
case 'success': | ||
default: | ||
iconName = 'check'; | ||
} | ||
return { iconName, color, bgColor }; | ||
}, [theme.palette, type]); | ||
|
||
const animation = keyframes` | ||
0% { | ||
opacity: 0; | ||
} | ||
20% { | ||
opacity: 1; | ||
} | ||
80% { | ||
opacity: 1; | ||
} | ||
100% { | ||
opacity: 0; | ||
} | ||
`; | ||
|
||
const ToastBarCSS = css` | ||
display: flex; | ||
flex-direction: row; | ||
gap: 1em; | ||
align-items: center; | ||
justify-content: space-between; | ||
width: fit-content; | ||
max-width: 20rem; | ||
color: ${color}; | ||
background-color: ${bgColor}; | ||
border-radius: 0.25em; | ||
padding: 0.75em 1em; | ||
z-index: ${theme.zIndex?.toastbar}; | ||
animation: ${animation} ${time}ms ease-in-out forwards; | ||
`; | ||
|
||
useEffect(() => { | ||
setTimeout(onClose, time); | ||
}, [onClose, time]); | ||
|
||
return ( | ||
<Box | ||
ref={toastRef} | ||
css={ToastBarCSS} | ||
className={appendClassNames('ec-toast-bar', classNames)} | ||
style={styleOverrides} | ||
> | ||
<Icon size="1em" name={iconName} /> | ||
{message} | ||
<ActionButton icon="cross" size="small" onClick={onClose} ghost /> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default ToastBar; |
76 changes: 76 additions & 0 deletions
76
packages/react/src/components/ToastBar/ToastBar.stories.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,76 @@ | ||
import React from 'react'; | ||
import { ThemeProvider } from '@emotion/react'; | ||
import { ToastBar, ToastBarProvider } from '.'; | ||
import DefaultTheme from '../../theme/DefaultTheme'; | ||
import { Button } from '../Button'; | ||
import { useToastBarDispatch } from '../../hooks/useToastBarDispatch'; | ||
|
||
export default { | ||
title: 'Components/ToastBar', | ||
component: ToastBar, | ||
}; | ||
|
||
const ToastBarContainer = () => { | ||
const dispatchToast = useToastBarDispatch(); | ||
return ( | ||
<> | ||
<Button | ||
onClick={() => | ||
dispatchToast({ | ||
message: 'Success Message', | ||
type: 'success', | ||
}) | ||
} | ||
color="success" | ||
> | ||
Show Success Toast | ||
</Button> | ||
<Button | ||
color="error" | ||
onClick={() => | ||
dispatchToast({ | ||
message: 'Error Message', | ||
type: 'error', | ||
}) | ||
} | ||
> | ||
Show Error Toast | ||
</Button> | ||
<Button | ||
color="info" | ||
onClick={() => | ||
dispatchToast({ | ||
message: 'Info Message', | ||
type: 'info', | ||
}) | ||
} | ||
> | ||
Show Info Toast | ||
</Button> | ||
<Button | ||
color="warning" | ||
onClick={() => | ||
dispatchToast({ | ||
message: 'Warning Message', | ||
type: 'warning', | ||
}) | ||
} | ||
> | ||
Show Warning Toast | ||
</Button> | ||
</> | ||
); | ||
}; | ||
|
||
const Template = (args) => ( | ||
<ThemeProvider theme={DefaultTheme}> | ||
<ToastBarProvider {...args}> | ||
<ToastBarContainer /> | ||
</ToastBarProvider> | ||
</ThemeProvider> | ||
); | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
position: 'bottom right', | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/react/src/components/ToastBar/ToastBarProvider.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,30 @@ | ||
import React, { useState, useCallback, useMemo } from 'react'; | ||
import ToastContext from '../../context/ToastContext'; | ||
import ToastContainer from './ToastContainer'; | ||
|
||
const ToastBarProvider = ({ position = 'bottom left', children }) => { | ||
const [toasts, setToasts] = useState([]); | ||
const dispatchToast = useCallback( | ||
(toast) => { | ||
setToasts((prevToasts) => [toast, ...prevToasts]); | ||
}, | ||
[setToasts] | ||
); | ||
const contextValue = useMemo( | ||
() => ({ | ||
toasts, | ||
dispatchToast, | ||
position, | ||
setToasts, | ||
}), | ||
[toasts, dispatchToast, position, setToasts] | ||
); | ||
return ( | ||
<ToastContext.Provider value={contextValue}> | ||
{children} | ||
<ToastContainer /> | ||
</ToastContext.Provider> | ||
); | ||
}; | ||
|
||
export default ToastBarProvider; |
Oops, something went wrong.