forked from openedx/paragon
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Dec 21, 2023
1 parent
0fa83e8
commit 1517acf
Showing
16 changed files
with
423 additions
and
515 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,22 @@ | ||
class EventEmitter { | ||
constructor() { | ||
this.events = {}; | ||
} | ||
|
||
subscribe(event, callback) { | ||
if (!this.events[event]) { | ||
this.events[event] = []; | ||
} | ||
this.events[event].push(callback); | ||
} | ||
|
||
emit(event, data) { | ||
const eventSubscribers = this.events[event]; | ||
if (eventSubscribers) { | ||
eventSubscribers.forEach(callback => callback(data)); | ||
} | ||
} | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const toastEmitter = new EventEmitter(); |
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 |
---|---|---|
@@ -1,40 +1,82 @@ | ||
import React from 'react'; | ||
/* eslint-disable react/prop-types */ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import PropTypes from 'prop-types'; | ||
|
||
class ToastContainer extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.toastRootName = 'toast-root'; | ||
if (typeof document === 'undefined') { | ||
this.rootElement = null; | ||
} else if (document.getElementById(this.toastRootName)) { | ||
this.rootElement = document.getElementById(this.toastRootName); | ||
} else { | ||
const rootElement = document.createElement('div'); | ||
rootElement.setAttribute('id', this.toastRootName); | ||
rootElement.setAttribute('class', 'toast-container'); | ||
rootElement.setAttribute('role', 'alert'); | ||
rootElement.setAttribute('aria-live', 'polite'); | ||
rootElement.setAttribute('aria-atomic', 'true'); | ||
this.rootElement = document.body.appendChild(rootElement); | ||
} | ||
import { toastEmitter } from './EventEmitter'; | ||
import Toast from '.'; | ||
|
||
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', | ||
}, | ||
}; | ||
|
||
function ToastContainer({ position, className }) { | ||
const [toasts, setToasts] = useState([]); | ||
const portalDivRef = useRef(null); | ||
const positionStyle = positionStyles[position] || positionStyles['bottom-left']; | ||
|
||
if (!portalDivRef.current) { | ||
portalDivRef.current = document.createElement('div'); | ||
portalDivRef.current.setAttribute('class', 'toast-portal'); | ||
portalDivRef.current.setAttribute('role', 'alert'); | ||
portalDivRef.current.setAttribute('aria-live', 'polite'); | ||
portalDivRef.current.setAttribute('aria-atomic', 'true'); | ||
document.body.appendChild(portalDivRef.current); | ||
} | ||
|
||
render() { | ||
if (this.rootElement) { | ||
return ReactDOM.createPortal( | ||
this.props.children, | ||
this.rootElement, | ||
const removeToast = (id) => { | ||
setToasts(currentToasts => currentToasts.filter(toast => toast.id !== id)); | ||
}; | ||
|
||
useEffect(() => { | ||
const handleShowToast = ({ message, duration, actions }) => { | ||
const id = Date.now(); | ||
setToasts(currentToasts => [...currentToasts, { | ||
id, message, duration, actions, | ||
}]); | ||
}; | ||
|
||
toastEmitter.subscribe('showToast', handleShowToast); | ||
|
||
return () => { | ||
toastEmitter.events.showToast = toastEmitter.events.showToast.filter( | ||
callback => callback !== handleShowToast, | ||
); | ||
} | ||
return null; | ||
} | ||
if (portalDivRef.current) { | ||
document.body.removeChild(portalDivRef.current); | ||
} | ||
}; | ||
}, []); | ||
|
||
return portalDivRef.current ? ReactDOM.createPortal( | ||
<div className="toast-container" style={{ ...positionStyle }}> | ||
{toasts.map(toast => ( | ||
<Toast key={toast.id} {...toast} onDismiss={() => removeToast(toast.id)} className={className} /> | ||
))} | ||
</div>, | ||
portalDivRef.current, | ||
) : null; | ||
} | ||
|
||
export default ToastContainer; | ||
|
||
ToastContainer.propTypes = { | ||
/** Specifies contents of the component. */ | ||
children: PropTypes.node.isRequired, | ||
position: PropTypes.oneOf(['top-left', 'top-right', 'bottom-left', 'bottom-right']), | ||
className: PropTypes.string, | ||
}; | ||
|
||
export default ToastContainer; | ||
ToastContainer.defaultProps = { | ||
position: 'bottom-left', | ||
className: '', | ||
}; |
Oops, something went wrong.