-
Notifications
You must be signed in to change notification settings - Fork 1
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 #142 from kodiak-packages/toaster-component
Create toaster component
- Loading branch information
Showing
16 changed files
with
937 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,55 @@ | ||
.alert { | ||
padding: 14.5px 18px; | ||
background-color: var(--color-error-light); | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
border-radius: var(--border-radius-small); | ||
} | ||
|
||
.alert.alertError { | ||
background-color: var(--color-error-light); | ||
} | ||
|
||
.alert.alertSuccess { | ||
background-color: var(--color-success-light); | ||
} | ||
|
||
.alert .contentContainer { | ||
padding: 14.5px 18px; | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.alert .contentContainer.closable { | ||
padding-right: 18px; | ||
} | ||
|
||
.alert .closeContainer { | ||
padding: 13.5px 17px 13.5px 13.5px; | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.alert .closeAlert { | ||
color: var(--color-neutral-5); | ||
cursor: pointer; | ||
height: 18px; | ||
width: 18px; | ||
} | ||
|
||
.message { | ||
display: inline-block; | ||
} | ||
|
||
svg.icon { | ||
margin-right: 10px; | ||
color: var(--color-error); | ||
height: 16px; | ||
width: 16px; | ||
} | ||
|
||
svg.icon.iconSuccess { | ||
color: var(--color-success); | ||
} | ||
|
||
svg.icon.iconError { | ||
color: var(--color-error); | ||
} |
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,44 @@ | ||
.toastContainer { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
height: 0; | ||
transition: all 240ms cubic-bezier(0.0, 0.0, 0.2, 1); | ||
pointer-events: all; | ||
} | ||
|
||
.toastContainer[data-state="entering"], .toastContainer[data-state="entered"] { | ||
animation: openToast 240ms cubic-bezier(0.175, 0.885, 0.320, 1.175) both; | ||
} | ||
|
||
.toastContainer[data-state="exiting"] { | ||
animation: closeToast 120ms cubic-bezier(0.4, 0.0, 1, 1) both; | ||
} | ||
|
||
.toastPadding { | ||
padding: 0 0 8px 0; | ||
} | ||
|
||
@keyframes openToast { | ||
from { | ||
opacity: 0; | ||
transform: translateY(-120%); | ||
} | ||
|
||
to { | ||
transform: translateY(0%); | ||
} | ||
} | ||
|
||
@keyframes closeToast { | ||
from { | ||
transform: scale(1); | ||
opacity: 1; | ||
} | ||
|
||
to { | ||
transform: scale(0.9); | ||
opacity: 0; | ||
} | ||
} |
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,105 @@ | ||
import React, { ComponentProps, useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { Transition } from 'react-transition-group'; | ||
|
||
import Alert, { AlertIntent } from '../../Alert/Alert'; | ||
|
||
import styles from './Toast.module.css'; | ||
|
||
interface Props extends Pick<ComponentProps<typeof Alert>, 'intent' | 'onClose' | 'message'> { | ||
durationInSeconds: number; | ||
isShown: boolean; | ||
intent: AlertIntent; | ||
isClosable: boolean; | ||
} | ||
|
||
const Toast: React.FC<Props> = ({ | ||
durationInSeconds, | ||
onClose, | ||
isShown: isShownProp, | ||
intent, | ||
message, | ||
isClosable, | ||
}: Props) => { | ||
const [isShown, setIsShown] = useState(true); | ||
const [height, setHeight] = useState(0); | ||
const closeTimer = useRef<number | null>(null); | ||
|
||
const clearCloseTimer = useCallback(() => { | ||
if (closeTimer.current) { | ||
clearTimeout(closeTimer.current); | ||
closeTimer.current = null; | ||
} | ||
}, [closeTimer]); | ||
|
||
const close = useCallback(() => { | ||
clearCloseTimer(); | ||
setIsShown(false); | ||
}, [clearCloseTimer]); | ||
|
||
const startCloseTimer = useCallback(() => { | ||
if (durationInSeconds) { | ||
clearCloseTimer(); | ||
closeTimer.current = window.setTimeout(() => { | ||
close(); | ||
}, durationInSeconds * 1000); | ||
} | ||
}, [durationInSeconds, clearCloseTimer, closeTimer, close]); | ||
|
||
useEffect(() => { | ||
startCloseTimer(); | ||
|
||
return () => { | ||
clearCloseTimer(); | ||
}; | ||
}, [clearCloseTimer, startCloseTimer]); | ||
|
||
useEffect(() => { | ||
if (isShownProp !== isShown && typeof isShownProp === 'boolean') { | ||
setIsShown(isShownProp); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isShownProp]); | ||
|
||
const handleMouseEnter = useCallback(() => clearCloseTimer(), [clearCloseTimer]); | ||
const handleMouseLeave = useCallback(() => startCloseTimer(), [startCloseTimer]); | ||
|
||
const onRef = useCallback( | ||
(ref) => { | ||
if (ref === null) return; | ||
|
||
const { height: toastHeight } = ref.getBoundingClientRect(); | ||
setHeight(toastHeight); | ||
}, | ||
[setHeight], | ||
); | ||
|
||
const dynamicStyles = useMemo( | ||
() => ({ | ||
height, | ||
marginBottom: isShown ? 0 : -height, | ||
}), | ||
[isShown, height], | ||
); | ||
|
||
return ( | ||
<Transition appear unmountOnExit timeout={240} in={isShown} onExited={onClose}> | ||
{(state) => { | ||
return ( | ||
<div | ||
data-state={state} | ||
className={styles.toastContainer} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
style={dynamicStyles} | ||
> | ||
<div ref={onRef} className={styles.toastPadding}> | ||
<Alert intent={intent} message={message} onClose={isClosable ? close : undefined} /> | ||
</div> | ||
</div> | ||
); | ||
}} | ||
</Transition> | ||
); | ||
}; | ||
|
||
export default Toast; |
10 changes: 10 additions & 0 deletions
10
src/components/Toaster/ToastManager/ToastManager.module.css
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,10 @@ | ||
.toastManagerContainer { | ||
max-width: 560px; | ||
margin: 0 auto; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
position: fixed; | ||
z-index: 110; | ||
pointer-events: none; | ||
} |
Oops, something went wrong.