2021-06-21 03:23:39 +03:00
import { h , Component } from '/js/web_modules/preact.js' ;
2021-03-16 01:32:52 +03:00
import htm from '/js/web_modules/htm.js' ;
2021-06-21 03:23:39 +03:00
import MicroModal from '/js/web_modules/micromodal/dist/micromodal.min.js' ;
2021-04-01 08:28:55 +03:00
2021-03-16 01:32:52 +03:00
const html = htm . bind ( h ) ;
2021-06-21 03:23:39 +03:00
export default class ExternalActionModal extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
iframeLoaded : false ,
} ;
2021-04-01 08:28:55 +03:00
2021-06-21 03:23:39 +03:00
this . setIframeLoaded = this . setIframeLoaded . bind ( this ) ;
}
componentDidMount ( ) {
// initalize and display Micromodal on mount
try {
MicroModal . init ( {
awaitCloseAnimation : false ,
awaitOpenAnimation : true , // if using css animations to open the modal. This allows it to wait for the animation to finish before focusing on an element inside the modal.
} ) ;
MicroModal . show ( 'external-actions-modal' , {
onClose : this . props . onClose ,
} ) ;
} catch ( e ) {
2021-07-20 05:22:29 +03:00
console . error ( 'modal error: ' , e ) ;
2021-06-21 03:23:39 +03:00
}
2021-04-01 08:28:55 +03:00
}
2021-06-21 03:23:39 +03:00
setIframeLoaded ( ) {
this . setState ( {
iframeLoaded : true ,
} ) ;
}
2021-03-16 01:32:52 +03:00
2021-06-21 03:23:39 +03:00
render ( ) {
2022-01-13 00:53:10 +03:00
const { action , useIframe = true , customContent = null } = this . props ;
2021-06-21 03:23:39 +03:00
const { url , title , description } = action ;
const { iframeLoaded } = this . state ;
2021-06-21 03:24:12 +03:00
const iframeStyle = iframeLoaded
? null
2022-01-13 00:53:10 +03:00
: { backgroundImage : 'url(/img/loading.gif)' } ;
2021-06-21 03:23:39 +03:00
return html `
2021-06-21 03:24:12 +03:00
< div
class = "modal micromodal-slide"
id = "external-actions-modal"
aria - hidden = "true"
>
2021-06-21 03:23:39 +03:00
< div class = "modal__overlay" tabindex = "-1" data - micromodal - close >
2021-06-21 03:24:12 +03:00
< div
id = "modal-container"
class = "modal__container rounded-md"
role = "dialog"
aria - modal = "true"
aria - labelledby = "modal-1-title"
>
< header
id = "modal-header"
class = "modal__header flex flex-row justify-between items-center bg-gray-300 p-3 rounded-t-md"
>
2022-01-13 00:53:10 +03:00
< h2
id = "external-action-modal-header"
class = "modal__title text-indigo-600 font-semibold"
>
2021-06-21 03:23:39 +03:00
$ { title || description }
< / h 2 >
2021-06-21 03:24:12 +03:00
< button
class = "modal__close"
aria - label = "Close modal"
data - micromodal - close
> < / b u t t o n >
2021-06-21 03:23:39 +03:00
< / h e a d e r >
2022-01-13 00:53:10 +03:00
< div
id = "modal-content-content"
class = "modal-content-content rounded-b-md"
>
$ { useIframe
? html `
< div
id = "modal-content"
class = "modal__content text-gray-600 overflow-y-auto overflow-x-hidden"
>
< iframe
id = "external-modal-iframe"
style = $ { iframeStyle }
class = "bg-gray-100 bg-center bg-no-repeat"
width = "100%"
allowpaymentrequest = "true"
allowfullscreen = "false"
sandbox = "allow-same-origin allow-scripts allow-popups allow-forms"
src = $ { url }
onload = $ { this . setIframeLoaded }
/ >
< / d i v >
`
: customContent }
2021-03-16 01:32:52 +03:00
< / d i v >
< / d i v >
< / d i v >
< / d i v >
2021-06-21 03:23:39 +03:00
` ;
}
2021-03-16 01:32:52 +03:00
}
2022-01-13 00:53:10 +03:00
export function ExternalActionButton ( { action , onClick , label = '' } ) {
const { title , icon , color = undefined , description } = action ;
2021-03-16 01:32:52 +03:00
const logo =
icon &&
html `
< span class = "external-action-icon" > < img src = $ { icon } alt = "" / > < / s p a n >
` ;
const bgcolor = color && { backgroundColor : ` ${ color } ` } ;
2021-06-21 03:23:39 +03:00
const handleClick = ( ) => onClick ( action ) ;
2021-03-16 01:32:52 +03:00
return html `
< button
2022-01-13 00:53:10 +03:00
class = "external-action-button rounded-sm flex flex-row justify-center items-center overflow-hidden m-1 px-3 py-1 text-base text-white bg-gray-800 rounded"
2021-03-16 01:32:52 +03:00
onClick = $ { handleClick }
style = $ { bgcolor }
2022-01-13 00:53:10 +03:00
aria - label = $ { description }
title = $ { description || title }
2021-03-16 01:32:52 +03:00
>
$ { logo }
2022-01-13 00:53:10 +03:00
< span class = "external-action-label" > $ { label || title } < / s p a n >
2021-03-16 01:32:52 +03:00
< / b u t t o n >
` ;
}