Skip to content

Commit

Permalink
🧑‍💻 Better Typebot import in vanilla JS sites
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 21, 2023
1 parent 907cad8 commit ab43d80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.0.7",
"version": "0.0.8",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
9 changes: 7 additions & 2 deletions packages/js/src/web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { registerWebComponents } from './register'
import { injectTypebotInWindow } from './window'
import { parseTypebot, injectTypebotInWindow } from './window'

registerWebComponents()
injectTypebotInWindow()

const typebot = parseTypebot()

injectTypebotInWindow(typebot)

export default typebot
53 changes: 27 additions & 26 deletions packages/js/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,37 @@ export const initBubble = (props: BubbleProps) => {
document.body.appendChild(bubbleElement)
}

type Typebot = {
initStandard: typeof initStandard
initPopup: typeof initPopup
initBubble: typeof initBubble
close: typeof close
hidePreviewMessage: typeof hidePreviewMessage
open: typeof open
setPrefilledVariables: typeof setPrefilledVariables
showPreviewMessage: typeof showPreviewMessage
toggle: typeof toggle
}

declare const window:
| {
Typebot:
| {
initStandard: typeof initStandard
initPopup: typeof initPopup
initBubble: typeof initBubble
close: typeof close
hidePreviewMessage: typeof hidePreviewMessage
open: typeof open
setPrefilledVariables: typeof setPrefilledVariables
showPreviewMessage: typeof showPreviewMessage
toggle: typeof toggle
}
| undefined
Typebot: Typebot | undefined
}
| undefined

export const injectTypebotInWindow = () => {
if (typeof window === 'undefined') return
export const parseTypebot = () => ({
initStandard,
initPopup,
initBubble,
close,
hidePreviewMessage,
open,
setPrefilledVariables,
showPreviewMessage,
toggle,
})

window.Typebot = {
initStandard,
initPopup,
initBubble,
close,
hidePreviewMessage,
open,
setPrefilledVariables,
showPreviewMessage,
toggle,
}
export const injectTypebotInWindow = (typebot: Typebot) => {
if (typeof window === 'undefined') return
window.Typebot = { ...typebot }
}
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.0.7",
"version": "0.0.8",
"description": "React library to display typebots on your website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

2 comments on commit ab43d80

@vercel
Copy link

@vercel vercel bot commented on ab43d80 Feb 21, 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.vercel.app
docs.typebot.io
docs-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ab43d80 Feb 21, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.