Add custom Modal component

This commit is contained in:
Gabe Kangas
2022-05-09 15:34:02 -07:00
parent 8c7c11cb72
commit 92a1da4df6
6 changed files with 131 additions and 57 deletions

View File

@@ -1,4 +1,6 @@
import { Button } from 'antd';
import { useState } from 'react';
import Modal from '../ui/Modal/Modal';
import { ExternalAction } from '../interfaces/external-action.interface';
import s from './ActionButton.module.scss';
@@ -7,13 +9,35 @@ interface Props {
}
export default function ActionButton(props: Props) {
const [showModal, setShowModal] = useState(false);
const { action } = props;
const { url, title, description, icon, color, openExternally } = action;
const buttonClicked = () => {
if (openExternally) {
window.open(url, '_blank');
} else {
setShowModal(true);
}
};
return (
<Button type="primary" className={`${s.button}`} style={{ backgroundColor: color }}>
<img src={icon} className={`${s.icon}`} alt={description} />
{title}
</Button>
<>
<Button
type="primary"
className={`${s.button}`}
onClick={buttonClicked}
style={{ backgroundColor: color }}
>
<img src={icon} className={`${s.icon}`} alt={description} />
{title}
</Button>
<Modal
title={description || title}
url={url}
visible={showModal}
handleCancel={() => setShowModal(false)}
/>
</>
);
}

View File

@@ -49,7 +49,7 @@ export default function ContentComponent() {
description: 'Example button description',
icon: 'https://owncast.online/images/logo.svg',
color: '#5232c8',
openExternally: true,
openExternally: false,
},
];

View File

@@ -0,0 +1,11 @@
.spinner {
position: absolute;
top: 50%;
left: 50%;
}
.content {
display: block;
height: 100%;
padding: 2vw;
}

View File

@@ -0,0 +1,74 @@
import { Spin, Skeleton, Modal as AntModal } from 'antd';
import React, { ReactNode, useState } from 'react';
import s from './Modal.module.scss';
interface Props {
title: string;
url?: string;
visible: boolean;
handleOk?: () => void;
handleCancel?: () => void;
afterClose?: () => void;
children?: ReactNode;
}
export default function Modal(props: Props) {
const { title, url, visible, handleOk, handleCancel, afterClose, children } = props;
const [loading, setLoading] = useState(!!url);
const modalStyle = {
padding: '0px',
height: '80vh',
};
console.log(url);
const iframe = url && (
<iframe
title={title}
src={url}
width="100%"
height="100%"
sandbox="allow-same-origin allow-scripts allow-popups allow-forms"
allowpaymentrequest="true"
frameBorder="0"
allowFullScreen
onLoad={() => setLoading(false)}
/>
);
const iframeDisplayStyle = loading ? 'none' : 'inline';
return (
<AntModal
title={title}
visible={visible}
onOk={handleOk}
onCancel={handleCancel}
afterClose={afterClose}
bodyStyle={modalStyle}
width="70%"
zIndex={9999}
footer={null}
centered
destroyOnClose
>
<>
{loading && (
<Skeleton active={loading} style={{ padding: '10px' }} paragraph={{ rows: 10 }} />
)}
{iframe && <div style={{ display: iframeDisplayStyle }}>{iframe}</div>}
{children && <div className={s.content}>{children}</div>}
{loading && <Spin className={s.spinner} spinning={loading} size="large" />}
</>
</AntModal>
);
}
Modal.defaultProps = {
url: undefined,
children: undefined,
handleOk: undefined,
handleCancel: undefined,
afterClose: undefined,
};