Skip to content

Commit

Permalink
🧑‍💻 (typebot-js) Implement easier commands: open / close / toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 15, 2022
1 parent 963072f commit 087d24e
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 103 deletions.
36 changes: 23 additions & 13 deletions apps/docs/docs/embed/html-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Here is an example:
```html
<script src="https://unpkg.com/typebot-js@2.2"></script>
<script>
var typebotCommands = Typebot.initPopup({
Typebot.initPopup({
url: 'https://viewer.typebot.io/my-typebot',
delay: 3000,
})
Expand All @@ -45,17 +45,21 @@ This code will automatically trigger the popup window after 3 seconds.
You can use these commands:

```js
Typebot.getPopupActions().open()
Typebot.open()
```

```js
Typebot.close()
```

```js
Typebot.getPopupActions().close()
Typebot.toggle()
```

You can bind these commands on a button element, for example:

```html
<button onclick="Typebot.getPopupActions().open()">Open the popup</button>
<button onclick="Typebot.open()">Contact us</button>
```

## Bubble
Expand All @@ -76,38 +80,44 @@ Here is an example:

This code will automatically trigger the popup window after 3 seconds.

### Open or close the proactive message
### Open or close the preview message

You can use this command:
You can use these commands:

```js
Typebot.getBubbleActions().openProactiveMessage()
Typebot.showMessage()
```

```js
Typebot.hideMessage()
```

You can bind this command on a button element, for example:

```html
<button onclick="Typebot.getBubbleActions().openProactiveMessage()">
Open proactive message
</button>
<button onclick="Typebot.showMessage()">Open message</button>
```

### Open or close the typebot

You can use these commands:

```js
Typebot.getBubbleActions().open()
Typebot.open()
```

```js
Typebot.close()
```

```js
Typebot.getBubbleActions().close()
Typebot.toggle()
```

You can bind these commands on a button element, for example:

```html
<button onclick="Typebot.getBubbleActions().open()">Open the chat</button>
<button onclick="Typebot.open()">Contact us</button>
```

## Additional configuration
Expand Down
2 changes: 1 addition & 1 deletion packages/typebot-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typebot-js",
"version": "2.2.13",
"version": "2.2.14",
"main": "dist/index.js",
"unpkg": "dist/index.global.js",
"license": "AGPL-3.0-or-later",
Expand Down
9 changes: 9 additions & 0 deletions packages/typebot-js/src/commands/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { closeIframe } from '../embedTypes/chat/iframe'
import { closePopup } from '../embedTypes/popup'

export const close = () => {
const existingPopup = document.querySelector('#typebot-popup')
if (existingPopup) closePopup(existingPopup)
const existingBubble = document.querySelector('#typebot-bubble')
if (existingBubble) closeIframe(existingBubble)
}
6 changes: 6 additions & 0 deletions packages/typebot-js/src/commands/hideMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { closeProactiveMessage } from '../embedTypes/chat/proactiveMessage'

export const hideMessage = () => {
const existingBubble = document.querySelector('#typebot-bubble')
if (existingBubble) closeProactiveMessage(existingBubble)
}
5 changes: 5 additions & 0 deletions packages/typebot-js/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './close'
export * from './hideMessage'
export * from './open'
export * from './showMessage'
export * from './toggle'
9 changes: 9 additions & 0 deletions packages/typebot-js/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { openIframe } from '../embedTypes/chat/iframe'
import { openPopup } from '../embedTypes/popup'

export const open = () => {
const existingPopup = document.querySelector('#typebot-popup')
if (existingPopup) openPopup(existingPopup)
const existingBubble = document.querySelector('#typebot-bubble')
if (existingBubble) openIframe(existingBubble)
}
6 changes: 6 additions & 0 deletions packages/typebot-js/src/commands/showMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { openProactiveMessage } from '../embedTypes/chat/proactiveMessage'

export const showMessage = () => {
const existingBubble = document.querySelector('#typebot-bubble')
if (existingBubble) openProactiveMessage(existingBubble)
}
20 changes: 20 additions & 0 deletions packages/typebot-js/src/commands/toggle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
closeIframe,
isIframeOpened,
openIframe,
} from '../embedTypes/chat/iframe'
import { closePopup, isPopupOpened, openPopup } from '../embedTypes/popup'

export const toggle = () => {
const existingPopup = document.querySelector('#typebot-popup')
if (existingPopup)
isPopupOpened(existingPopup)
? closePopup(existingPopup)
: openPopup(existingPopup)
const existingBubble = document.querySelector('#typebot-bubble')
console.log(existingBubble)
if (existingBubble)
isIframeOpened(existingBubble)
? closeIframe(existingBubble)
: openIframe(existingBubble)
}
15 changes: 7 additions & 8 deletions packages/typebot-js/src/embedTypes/chat/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ export const createIframeContainer = (
return iframe
}

export const openIframe = (
bubble: HTMLDivElement,
iframe: HTMLIFrameElement
): void => {
export const openIframe = (bubble: Element): void => {
const iframe = bubble.querySelector('.typebot-iframe') as HTMLIFrameElement
loadTypebotIfFirstOpen(iframe)
iframe.style.display = 'flex'
setTimeout(() => bubble.classList.add('iframe-opened'), 50)
bubble.classList.remove('message-opened')
}

export const closeIframe = (
bubble: HTMLDivElement,
iframe: HTMLIFrameElement
): void => {
export const closeIframe = (bubble: Element): void => {
const iframe = bubble.querySelector('.typebot-iframe') as HTMLIFrameElement
bubble.classList.remove('iframe-opened')
setTimeout(() => (iframe.style.display = 'none'), 550)
}
Expand All @@ -32,3 +28,6 @@ export const loadTypebotIfFirstOpen = (iframe: HTMLIFrameElement): void => {
iframe.src = iframe.dataset.src
iframe.removeAttribute('data-src')
}

export const isIframeOpened = (bubble: Element): boolean =>
bubble.classList.contains('iframe-opened')
24 changes: 7 additions & 17 deletions packages/typebot-js/src/embedTypes/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import {
ProactiveMessageParams,
} from '../../types'
import { createButton } from './button'
import {
closeIframe,
createIframeContainer,
loadTypebotIfFirstOpen,
openIframe,
} from './iframe'
import { closeIframe, createIframeContainer, openIframe } from './iframe'
import {
createProactiveMessage,
openProactiveMessage,
Expand All @@ -33,10 +28,7 @@ export const initBubble = (params: BubbleParams): BubbleActions => {
!hasBeenClosed()
) {
setRememberCloseInStorage()
setTimeout(
() => openIframe(bubbleElement, iframeElement),
params.autoOpenDelay
)
setTimeout(() => openIframe(bubbleElement), params.autoOpenDelay)
}
!document.body
? (window.onload = () => document.body.appendChild(bubbleElement))
Expand All @@ -62,8 +54,8 @@ const createBubble = (
const iframeElement = createIframeContainer(params)
buttonElement.addEventListener('click', () => {
iframeElement.style.display === 'none'
? openIframe(bubbleElement, iframeElement)
: closeIframe(bubbleElement, iframeElement)
? openIframe(bubbleElement)
: closeIframe(bubbleElement)
})
if (proactiveMessageElement)
proactiveMessageElement.addEventListener('click', () =>
Expand All @@ -77,9 +69,7 @@ const onProactiveMessageClick = (
bubble: HTMLDivElement,
iframe: HTMLIFrameElement
): void => {
iframe.style.display === 'none'
? openIframe(bubble, iframe)
: closeIframe(bubble, iframe)
iframe.style.display === 'none' ? openIframe(bubble) : closeIframe(bubble)
bubble.classList.remove('message-opened')
}

Expand Down Expand Up @@ -107,10 +97,10 @@ export const getBubbleActions = (
}
: undefined,
open: () => {
openIframe(existingBubbleElement, existingIframeElement)
openIframe(existingBubbleElement)
},
close: () => {
closeIframe(existingBubbleElement, existingIframeElement)
closeIframe(existingBubbleElement)
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/typebot-js/src/embedTypes/chat/proactiveMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const createCloseButton = (bubble: HTMLDivElement): HTMLButtonElement => {
return button
}

const openProactiveMessage = (bubble: HTMLDivElement): void => {
const openProactiveMessage = (bubble: Element): void => {
bubble.classList.add('message-opened')
}

Expand All @@ -56,7 +56,7 @@ const onCloseButtonClick = (
closeProactiveMessage(proactiveMessageElement)
}

const closeProactiveMessage = (bubble: HTMLDivElement): void => {
const closeProactiveMessage = (bubble: Element): void => {
setRememberCloseInStorage()
bubble.classList.remove('message-opened')
}
Expand Down
4 changes: 4 additions & 0 deletions packages/typebot-js/src/embedTypes/chat/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
filter: brightness(0.75);
}

#typebot-bubble > button:focus {
outline: none;
}

#typebot-bubble > button > .icon {
transition: opacity 500ms ease-out 0s, transform 500ms ease-out 0s;
}
Expand Down
Loading

5 comments on commit 087d24e

@vercel
Copy link

@vercel vercel bot commented on 087d24e Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs.typebot.io
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 087d24e Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 087d24e Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2-alpha – ./apps/viewer

ns8.vn
yobot.me
247987.com
8jours.top
bot.aws.bj
bot.bbc.bj
finplex.be
sat.cr8.ai
bot.aipr.kr
minipost.uk
bt.id8rs.com
bot.krdfy.com
vhpage.cr8.ai
am.nigerias.io
an.nigerias.io
ar.nigerias.io
bot.enreso.org
bot.lalmon.com
ticketfute.com
apo.nigerias.io
apr.nigerias.io
aso.nigerias.io
bot.ageenda.com
bot.artiweb.app
bot.devitus.com
bot.tc-mail.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
typebot.aloe.do
bot.piccinato.co
bot.upfunnel.art
botc.ceox.com.br
clo.closeer.work
faqs.nigerias.io
feedback.ofx.one
form.syncwin.com
kw.wpwakanda.com
myrentalhost.com
stan.vselise.com
start.taxtree.io
typebot.aloe.bot
voicehelp.cr8.ai
app.chatforms.net
bot.agfunnel.tech
bot.hostnation.de
bot.maitempah.com
bot.phuonghub.com
bot.reviewzer.com
cares.urlabout.me
fmm.wpwakanda.com
gentleman-shop.fr
k1.kandabrand.com
lb.ticketfute.com
ov1.wpwakanda.com
ov2.wpwakanda.com
ov3.wpwakanda.com
1988.bouclidom.com
andreimayer.com.br
bot.megafox.com.br
bot.neferlopez.com
bots.robomotion.io
cadu.uninta.edu.br
dicanatural.online
goalsettingbot.com
positivobra.com.br
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
bot.digitalbled.com
bot.eventhub.com.au
bot.jepierre.com.br
carsalesenquiry.com
demo.botscientis.us
forms.webisharp.com
kbsub.wpwakanda.com
live.botscientis.us
mentoria.omelhor.vc
nutrisamirbayde.com
order.maitempah.com
quest.wpwakanda.com
test.botscientis.us
typebot.stillio.com
bium.gratirabbit.com
bot.ansuraniphone.my
bot.cotemeuplano.com
chat.hayurihijab.com
chatbee.agfunnel.com
click.sevenoways.com
connect.growthguy.in
get.freebotoffer.xyz
link.cascadigital.com.br
onboarding.growthside.io
reward.onlinebotdemo.xyz
type.opaulovieira.com.br
aibot.angrybranding.co.uk
bot.aidigitalmarketing.kr
bot.arraesecenteno.com.br
bot.blackboxsports.com.br

@vercel
Copy link

@vercel vercel bot commented on 087d24e Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 087d24e Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app

Please sign in to comment.