⚡ (audio) Implement more robust auto play
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/js",
|
"name": "@typebot.io/js",
|
||||||
"version": "0.0.79",
|
"version": "0.0.80",
|
||||||
"description": "Javascript library to display typebots on your website",
|
"description": "Javascript library to display typebots on your website",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
@ -13,22 +13,34 @@ const typingDuration = 500
|
|||||||
let typingTimeout: NodeJS.Timeout
|
let typingTimeout: NodeJS.Timeout
|
||||||
|
|
||||||
export const AudioBubble = (props: Props) => {
|
export const AudioBubble = (props: Props) => {
|
||||||
|
let isPlayed = false
|
||||||
let ref: HTMLDivElement | undefined
|
let ref: HTMLDivElement | undefined
|
||||||
let audioElement: HTMLAudioElement | undefined
|
let audioElement: HTMLAudioElement | undefined
|
||||||
const [isTyping, setIsTyping] = createSignal(true)
|
const [isTyping, setIsTyping] = createSignal(true)
|
||||||
|
|
||||||
|
const endTyping = () => {
|
||||||
|
if (isPlayed) return
|
||||||
|
isPlayed = true
|
||||||
|
setIsTyping(false)
|
||||||
|
setTimeout(
|
||||||
|
() => props.onTransitionEnd(ref?.offsetTop),
|
||||||
|
showAnimationDuration
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
typingTimeout = setTimeout(() => {
|
typingTimeout = setTimeout(endTyping, typingDuration)
|
||||||
setIsTyping(false)
|
audioElement?.addEventListener(
|
||||||
setTimeout(() => {
|
'canplay',
|
||||||
const audioElement = ref?.querySelector('audio')
|
() => {
|
||||||
if (audioElement)
|
clearTimeout(typingTimeout)
|
||||||
audioElement
|
audioElement
|
||||||
.play()
|
?.play()
|
||||||
.catch((e) => console.warn('Could not autoplay the audio:', e))
|
.catch((e) => console.warn("Couldn't autoplay audio", e))
|
||||||
props.onTransitionEnd(ref?.offsetTop)
|
endTyping()
|
||||||
}, showAnimationDuration)
|
},
|
||||||
}, typingDuration)
|
{ once: true }
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
onCleanup(() => {
|
onCleanup(() => {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/react",
|
"name": "@typebot.io/react",
|
||||||
"version": "0.0.79",
|
"version": "0.0.80",
|
||||||
"description": "React library to display typebots on your website",
|
"description": "React library to display typebots on your website",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
Reference in New Issue
Block a user