2
0

(audio) Implement more robust auto play

This commit is contained in:
Baptiste Arnaud
2023-07-12 15:37:31 +02:00
parent d6c6434050
commit 0a85f33694
3 changed files with 25 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.0.79",
"version": "0.0.80",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",

View File

@ -13,22 +13,34 @@ const typingDuration = 500
let typingTimeout: NodeJS.Timeout
export const AudioBubble = (props: Props) => {
let isPlayed = false
let ref: HTMLDivElement | undefined
let audioElement: HTMLAudioElement | undefined
const [isTyping, setIsTyping] = createSignal(true)
onMount(() => {
typingTimeout = setTimeout(() => {
const endTyping = () => {
if (isPlayed) return
isPlayed = true
setIsTyping(false)
setTimeout(() => {
const audioElement = ref?.querySelector('audio')
if (audioElement)
setTimeout(
() => props.onTransitionEnd(ref?.offsetTop),
showAnimationDuration
)
}
onMount(() => {
typingTimeout = setTimeout(endTyping, typingDuration)
audioElement?.addEventListener(
'canplay',
() => {
clearTimeout(typingTimeout)
audioElement
.play()
.catch((e) => console.warn('Could not autoplay the audio:', e))
props.onTransitionEnd(ref?.offsetTop)
}, showAnimationDuration)
}, typingDuration)
?.play()
.catch((e) => console.warn("Couldn't autoplay audio", e))
endTyping()
},
{ once: true }
)
})
onCleanup(() => {

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.0.79",
"version": "0.0.80",
"description": "React library to display typebots on your website",
"main": "dist/index.js",
"types": "dist/index.d.ts",