-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyrylo Hudym-Levkovych
authored and
Kyrylo Hudym-Levkovych
committed
Jan 2, 2024
1 parent
4276858
commit 4bf88cf
Showing
16 changed files
with
240 additions
and
242 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React, { useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useIntl } from 'react-intl'; | ||
import classNames from 'classnames'; | ||
|
||
import Icon from '../Icon'; | ||
import IconButton from '../IconButton'; | ||
import Button from '../Button'; | ||
import { Close } from '../../icons'; | ||
|
||
function Toast({ | ||
id, message, onDismiss, actions, className, duration, ...rest | ||
}) { | ||
const intl = useIntl(); | ||
const intlCloseLabel = intl.formatMessage({ | ||
id: 'pgn.Toast.closeLabel', | ||
defaultMessage: 'Close', | ||
description: 'Close label for Toast component', | ||
}); | ||
|
||
const timerRef = useRef(); | ||
|
||
useEffect(() => { | ||
timerRef.current = setTimeout(() => onDismiss(id), duration); | ||
|
||
return () => clearTimeout(timerRef.current); | ||
}, [id, onDismiss, duration]); | ||
|
||
const clearTimer = () => { | ||
clearTimeout(timerRef.current); | ||
}; | ||
|
||
const startTimer = () => { | ||
clearTimer(); | ||
timerRef.current = setTimeout(() => onDismiss(id), duration); | ||
}; | ||
|
||
return ( | ||
<div | ||
className={classNames('pgn__toast', className)} | ||
onMouseOver={clearTimer} | ||
onMouseOut={startTimer} | ||
onFocus={clearTimer} | ||
onBlur={startTimer} | ||
{...rest} | ||
> | ||
<div className="pgn__toast__header small"> | ||
<p className="pgn__toast__message">{message}</p> | ||
|
||
<IconButton | ||
iconAs={Icon} | ||
alt={intlCloseLabel} | ||
className="pgn__toast__close-btn align-self-start" | ||
src={Close} | ||
onClick={() => onDismiss(id)} | ||
variant="primary" | ||
invertColors | ||
/> | ||
</div> | ||
{actions | ||
? ( | ||
<div className="pgn__toast__optional-actions"> | ||
{actions.map((action) => ( | ||
<Button | ||
as={action.href ? 'a' : 'button'} | ||
href={action.href} | ||
onClick={action.onClick} | ||
size="sm" | ||
variant="inverse-outline-primary" | ||
> | ||
{action.label} | ||
</Button> | ||
))} | ||
</div> | ||
) | ||
: null} | ||
</div> | ||
); | ||
} | ||
|
||
export default Toast; | ||
|
||
Toast.propTypes = { | ||
id: PropTypes.number.isRequired, | ||
message: PropTypes.string.isRequired, | ||
onDismiss: PropTypes.func, | ||
actions: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
label: PropTypes.string.isRequired, | ||
onClick: PropTypes.func, | ||
href: PropTypes.string, | ||
}), | ||
), | ||
className: PropTypes.string, | ||
duration: PropTypes.number, | ||
}; | ||
|
||
Toast.defaultProps = { | ||
onDismiss: () => {}, | ||
actions: null, | ||
className: '', | ||
duration: 5000, | ||
}; |
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 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,16 @@ | ||
export const TOAST_POSITIONS = ['top-left', 'top-right', 'bottom-left', 'bottom-right']; | ||
|
||
export const positionStyles = { | ||
'top-left': { | ||
top: '0', left: '0', right: 'auto', bottom: 'auto', | ||
}, | ||
'top-right': { | ||
top: '0', right: '0', left: 'auto', bottom: 'auto', | ||
}, | ||
'bottom-left': { | ||
bottom: '0', left: '0', right: 'auto', top: 'auto', | ||
}, | ||
'bottom-right': { | ||
bottom: '0', right: '0', left: 'auto', top: 'auto', | ||
}, | ||
}; |
Oops, something went wrong.