2
0

🚸 Update onboarding bot

Closes #400
This commit is contained in:
Baptiste Arnaud
2023-07-25 09:12:53 +02:00
parent b17177f35f
commit 283c55c1a4
9 changed files with 55 additions and 50 deletions

View File

@@ -18,22 +18,29 @@ declare global {
type BubbleElement = HTMLElement & Props
export const Bubble = (props: Props) => {
const ref = useRef<BubbleElement | null>(null)
const bubbleElement = useRef<BubbleElement | null>(null)
const attachBubbleToDom = useCallback((props: Props) => {
const bubbleElement = document.createElement(
const newBubbleElement = document.createElement(
'typebot-bubble'
) as BubbleElement
ref.current = bubbleElement
injectPropsToElement(ref.current, props)
document.body.append(ref.current)
bubbleElement.current = newBubbleElement
injectPropsToElement(bubbleElement.current, props)
document.body.append(bubbleElement.current)
}, [])
useEffect(() => {
if (!ref.current) attachBubbleToDom(props)
injectPropsToElement(ref.current as BubbleElement, props)
if (!bubbleElement.current) attachBubbleToDom(props)
injectPropsToElement(bubbleElement.current as BubbleElement, props)
}, [attachBubbleToDom, props])
useEffect(() => {
return () => {
bubbleElement.current?.remove()
bubbleElement.current = null
}
}, [])
const injectPropsToElement = (element: BubbleElement, props: Props) => {
Object.assign(element, props)
}