2023-01-16 12:13:21 +01:00
|
|
|
import { useEffect, useRef } from 'react'
|
2023-01-09 14:51:36 +01:00
|
|
|
import type { BubbleProps } from '@typebot.io/js'
|
2023-01-16 12:13:21 +01:00
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
type Props = BubbleProps
|
2023-01-09 14:51:36 +01:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
namespace JSX {
|
|
|
|
interface IntrinsicElements {
|
|
|
|
'typebot-bubble': 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-25 11:27:47 +01:00
|
|
|
type BubbleElement = HTMLElement & Props
|
|
|
|
|
|
|
|
export const Bubble = (props: Props) => {
|
|
|
|
const ref = useRef<BubbleElement | null>(null)
|
2023-01-16 12:13:21 +01:00
|
|
|
|
2023-01-09 14:51:36 +01:00
|
|
|
useEffect(() => {
|
|
|
|
;(async () => {
|
2023-01-25 11:27:47 +01:00
|
|
|
await import('@typebot.io/js/dist/web')
|
2023-01-09 14:51:36 +01:00
|
|
|
})()
|
2023-01-16 12:13:21 +01:00
|
|
|
}, [])
|
|
|
|
|
2023-01-25 11:27:47 +01:00
|
|
|
const updateProps = (element: BubbleElement | null, props: Props) => {
|
|
|
|
if (!element) return
|
|
|
|
Object.assign(element, props)
|
|
|
|
}
|
|
|
|
|
|
|
|
const initBubble = async () => {
|
|
|
|
const bubbleElement = document.createElement(
|
|
|
|
'typebot-bubble'
|
|
|
|
) as BubbleElement
|
|
|
|
ref.current = bubbleElement
|
|
|
|
document.body.append(bubbleElement)
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:13:21 +01:00
|
|
|
useEffect(() => {
|
2023-01-25 11:27:47 +01:00
|
|
|
;(async () => {
|
|
|
|
if (!ref.current) await initBubble()
|
|
|
|
updateProps(ref.current, props)
|
|
|
|
})()
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
ref.current?.remove()
|
|
|
|
}
|
|
|
|
}, [props])
|
|
|
|
|
|
|
|
return null
|
2023-01-09 14:51:36 +01:00
|
|
|
}
|