2023-01-16 12:13:21 +01:00
|
|
|
import { useEffect, useRef } from 'react'
|
2023-01-09 14:51:36 +01:00
|
|
|
import type { PopupProps } from '@typebot.io/js'
|
2023-01-16 12:13:21 +01:00
|
|
|
import { defaultPopupProps } from './constants'
|
|
|
|
|
|
|
|
|
|
type Props = PopupProps & { style?: React.CSSProperties; className?: string }
|
2023-01-09 14:51:36 +01:00
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
|
namespace JSX {
|
|
|
|
|
interface IntrinsicElements {
|
|
|
|
|
'typebot-popup': React.DetailedHTMLProps<
|
|
|
|
|
React.HTMLAttributes<HTMLElement>,
|
|
|
|
|
HTMLElement
|
2023-01-16 12:13:21 +01:00
|
|
|
> & { class?: string }
|
2023-01-09 14:51:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 12:13:21 +01:00
|
|
|
export const Popup = ({ style, className, ...props }: Props) => {
|
|
|
|
|
const ref = useRef<(HTMLDivElement & Props) | null>(null)
|
|
|
|
|
|
2023-01-09 14:51:36 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
;(async () => {
|
|
|
|
|
const { registerPopupComponent } = await import('@typebot.io/js')
|
2023-01-16 12:13:21 +01:00
|
|
|
registerPopupComponent(defaultPopupProps)
|
2023-01-09 14:51:36 +01:00
|
|
|
})()
|
2023-01-16 12:13:21 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!ref.current) return
|
|
|
|
|
ref.current.typebot = props.typebot
|
|
|
|
|
ref.current.prefilledVariables = props.prefilledVariables
|
|
|
|
|
ref.current.onClose = props.onClose
|
|
|
|
|
ref.current.onOpen = props.onOpen
|
|
|
|
|
ref.current.onNewInputBlock = props.onNewInputBlock
|
|
|
|
|
ref.current.onAnswer = props.onAnswer
|
|
|
|
|
ref.current.onEnd = props.onEnd
|
|
|
|
|
ref.current.onInit = props.onInit
|
|
|
|
|
}, [
|
|
|
|
|
props.onAnswer,
|
|
|
|
|
props.onClose,
|
|
|
|
|
props.onEnd,
|
|
|
|
|
props.onNewInputBlock,
|
|
|
|
|
props.onOpen,
|
|
|
|
|
props.onInit,
|
|
|
|
|
props.prefilledVariables,
|
|
|
|
|
props.typebot,
|
|
|
|
|
])
|
2023-01-09 14:51:36 +01:00
|
|
|
|
2023-01-16 12:13:21 +01:00
|
|
|
return (
|
|
|
|
|
<typebot-popup
|
|
|
|
|
ref={ref}
|
|
|
|
|
api-host={props.apiHost}
|
|
|
|
|
start-group-id={props.startGroupId}
|
|
|
|
|
result-id={props.resultId}
|
|
|
|
|
is-preview={props.isPreview}
|
|
|
|
|
is-open={props.isOpen}
|
|
|
|
|
default-open={props.defaultOpen}
|
|
|
|
|
class={className}
|
|
|
|
|
style={style}
|
|
|
|
|
/>
|
|
|
|
|
)
|
2023-01-09 14:51:36 +01:00
|
|
|
}
|