Skip to content

Commit

Permalink
✨ (embed) Add new command setInputValue
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jul 15, 2023
1 parent 521cb50 commit be7be7b
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const NextjsLogo = (props: IconProps) => (
y1="116.5"
y2="160.5"
>
<stop stop-color="white"></stop>
<stop offset="1" stop-color="white" stop-opacity="0"></stop>
<stop stopColor="white"></stop>
<stop offset="1" stopColor="white" stopOpacity="0"></stop>
</linearGradient>
<linearGradient
gradientUnits="userSpaceOnUse"
Expand All @@ -54,8 +54,8 @@ export const NextjsLogo = (props: IconProps) => (
y1="54"
y2="106.875"
>
<stop stop-color="white"></stop>
<stop offset="1" stop-color="white" stop-opacity="0"></stop>
<stop stopColor="white"></stop>
<stop offset="1" stopColor="white" stopOpacity="0"></stop>
</linearGradient>
</defs>
</Icon>
Expand Down
1 change: 1 addition & 0 deletions apps/docs/docs/embed/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Here are the commands you can use to trigger your embedded typebot:
- `Typebot.showPreviewMessage()`: Show preview message from the bubble,
- `Typebot.hidePreviewMessage()`: Hide preview message from the bubble,
- `Typebot.setPrefilledVariables(...)`: Set prefilled variables.
- `Typebot.setInputValue(...)`: Set the value in the currently displayed input.

Example:

Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.1.0",
"version": "0.1.1",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { EmailInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'

type Props = {
block: EmailInputBlock
Expand All @@ -30,8 +31,19 @@ export const EmailInput = (props: Props) => {

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})

onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})

const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}

return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { NumberInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'

type NumberInputProps = {
block: NumberInputBlock
Expand All @@ -30,8 +31,19 @@ export const NumberInput = (props: NumberInputProps) => {

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})

onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})

const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}

return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { SendButton } from '@/components/SendButton'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { PhoneNumberInputOptions } from '@typebot.io/schemas'
import { createSignal, For, onMount } from 'solid-js'
import { createSignal, For, onCleanup, onMount } from 'solid-js'
import { isEmpty } from '@typebot.io/lib'
import { phoneCountries } from '@typebot.io/lib/phoneCountries'
import { CommandData } from '@/features/commands/types'

type PhoneInputProps = Pick<
PhoneNumberInputOptions,
Expand All @@ -33,7 +34,7 @@ export const PhoneInput = (props: PhoneInputProps) => {
const matchedCountry =
inputValue?.startsWith('+') &&
inputValue.length > 2 &&
phoneCountries.reduce<typeof phoneCountries[number] | null>(
phoneCountries.reduce<(typeof phoneCountries)[number] | null>(
(matchedCountry, country) => {
if (
!country?.dial_code ||
Expand Down Expand Up @@ -87,8 +88,19 @@ export const PhoneInput = (props: PhoneInputProps) => {

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})

onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})

const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}

return (
<div
class={'flex items-end justify-between pr-2 typebot-input'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Textarea, ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { TextInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'

type Props = {
block: TextInputBlock
Expand Down Expand Up @@ -36,8 +37,19 @@ export const TextInput = (props: Props) => {

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})

onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})

const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}

return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ShortTextInput } from '@/components'
import { SendButton } from '@/components/SendButton'
import { CommandData } from '@/features/commands/types'
import { InputSubmitContent } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import type { UrlInputBlock } from '@typebot.io/schemas'
import { createSignal, onMount } from 'solid-js'
import { createSignal, onCleanup, onMount } from 'solid-js'

type Props = {
block: UrlInputBlock
Expand Down Expand Up @@ -36,8 +37,19 @@ export const UrlInput = (props: Props) => {

onMount(() => {
if (!isMobile() && inputRef) inputRef.focus()
window.addEventListener('message', processIncomingEvent)
})

onCleanup(() => {
window.removeEventListener('message', processIncomingEvent)
})

const processIncomingEvent = (event: MessageEvent<CommandData>) => {
const { data } = event
if (!data.isFromTypebot) return
if (data.command === 'setInputValue') setInputValue(data.value)
}

return (
<div
class={'flex items-end justify-between pr-2 typebot-input w-full'}
Expand Down
6 changes: 6 additions & 0 deletions packages/embeds/js/src/features/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type CommandData = {
}
| ShowMessageCommandData
| SetPrefilledVariablesCommandData
| SetInputValueCommandData
)

export type ShowMessageCommandData = {
Expand All @@ -19,3 +20,8 @@ export type SetPrefilledVariablesCommandData = {
command: 'setPrefilledVariables'
variables: Record<string, string | number | boolean>
}

export type SetInputValueCommandData = {
command: 'setInputValue'
value: string
}
1 change: 1 addition & 0 deletions packages/embeds/js/src/features/commands/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './open'
export * from './setPrefilledVariables'
export * from './showPreviewMessage'
export * from './toggle'
export * from './setInputValue'
10 changes: 10 additions & 0 deletions packages/embeds/js/src/features/commands/utils/setInputValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CommandData } from '../types'

export const setInputValue = (value: string) => {
const message: CommandData = {
isFromTypebot: true,
command: 'setInputValue',
value,
}
window.postMessage(message)
}
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.1.0",
"version": "0.1.1",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.1.0",
"version": "0.1.1",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/embeds/react/src/stories/bubble.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
showPreviewMessage,
hidePreviewMessage,
setPrefilledVariables,
setInputValue,
} from '@typebot.io/js'
import { useState } from 'react'
import { leadGenerationTypebot } from './assets/leadGenerationTypebot'
Expand All @@ -23,6 +24,7 @@ export const Default = () => {
<button onClick={() => showPreviewMessage()}>
Show Preview Message
</button>
<button onClick={() => setInputValue('YOOOO!')}>Set input value</button>
<button onClick={hidePreviewMessage}>Close Preview Message</button>
<div>
<p>Predefined name:</p>
Expand Down

4 comments on commit be7be7b

@vercel
Copy link

@vercel vercel bot commented on be7be7b Jul 15, 2023

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.typebot.io
docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on be7be7b Jul 15, 2023

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 – ./apps/viewer

sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
poll.mosaicohairboutique.it
presente.empresarias.com.mx
register.algorithmpress.com
sell.sellthemotorhome.co.uk
teste.captacao.progenbr.com
alavancagem.horadelucrar.com
anamnese.odontopavani.com.br
austin.channelautomation.com
bot.marketingplusmindset.com
bot.seidibergamoseanchetu.it
desabafe.sergiolimajr.com.br
detective.chatvirtual.online
download.venturemarketing.in
mdb.gabinete.rp.progenbr.com
mdb.gabinete.sp.progenbr.com
open.campus.aalen.university
type.cookieacademyonline.com
upload.atlasoutfittersk9.com
atendimento.martinarod.online
bot.brigadeirosemdrama.com.br
mdb.eventoequipe.progenbr.com
mdb.qrcode.diego.progenbr.com
tuttirecepcao.fratucci.com.br
forms.escoladeautomacao.com.br
onboarding.libertydreamcare.ie
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
link.sobrancelhadesigner.com.br
viewer-v2-typebot-io.vercel.app
web.metodofelinoperfeito.com.br
yourfeedback.comebackreward.com
baleia.testeeventos.progenbr.com
bot.cabin-rentals-of-georgia.net
bot.confinanceconsultoria.com.br
chat.portaloficialautorizado.com
open.campus.bot.aalen.university
sondaggio.mosaicohairboutique.it
baleia.testegabinete.progenbr.com
chat.alfabetizacaobilingue.com.br
gerador.verificadordehospedes.com
mdb.assessoria.diego.progenbr.com
personal-trainer.barrettamario.it
sondaggio.mosaicohairboutique.com
mdb.assessoria.ademir.progenbr.com
mdb.assessoria.arthur.progenbr.com
mdb.assessoria.danilo.progenbr.com
mdb.assessoria.marcao.progenbr.com
mdb.assessoria.marcio.progenbr.com
mdb.gabinete.brasilia.progenbr.com
preagendamento.sergiolimajr.com.br
prenotazione.ristorantekintsugi.it
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
mdb.assessoria.aloisio.progenbr.com
mdb.assessoria.girotto.progenbr.com
mdb.assessoria.marinho.progenbr.com
register.thailandmicespecialist.com
mdb.assessoria.desideri.progenbr.com
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on be7be7b Jul 15, 2023

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 be7be7b Jul 15, 2023

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

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

Please sign in to comment.