demarches-normaliennes/app/javascript/components/shared/FlashMessage.jsx

33 lines
818 B
React
Raw Normal View History

2021-03-11 12:09:27 +01:00
import React from 'react';
import { createPortal } from 'react-dom';
2021-08-31 18:14:04 +02:00
import PropTypes from 'prop-types';
2021-03-11 12:09:27 +01:00
export function FlashMessage({ message, level, sticky, fixed }) {
return createPortal(
<div className="flash_message center">
<div className={flashClassName(level, sticky, fixed)}>{message}</div>
</div>,
document.getElementById('flash_messages')
);
}
function flashClassName(level, sticky = false, fixed = false) {
const className =
level == 'notice' ? ['alert', 'alert-success'] : ['alert', 'alert-danger'];
if (sticky) {
className.push('sticky');
}
if (fixed) {
className.push('alert-fixed');
}
return className.join(' ');
}
2021-08-31 18:14:04 +02:00
FlashMessage.propTypes = {
message: PropTypes.string,
level: PropTypes.string,
sticky: PropTypes.bool,
fixed: PropTypes.bool
};