Skip to content

Commit 7879817

Browse files
widget (#433)
1 parent a81a97f commit 7879817

File tree

9 files changed

+2868
-0
lines changed

9 files changed

+2868
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
**/.definition
22
**/.preview/**
3+
node_modules/
4+
dist/
35
.env
46
.DS_Store

fern/assets/widget.js

Lines changed: 372 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fern/custom.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const WIDGET_TAG = 'vapi-voice-agent-widget';
2+
const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
3+
const WIDGET_SCRIPT_URL = isLocalhost
4+
? 'http://localhost:9001/widget.js'
5+
: 'https://docs-widget.vercel.app/widget.js';
6+
7+
function injectVapiWidget() {
8+
console.log('[custom.js] injectVapiWidget called');
9+
if (document.querySelector(WIDGET_TAG)) {
10+
console.log('[custom.js] Widget already present in DOM');
11+
return;
12+
}
13+
14+
const script = document.createElement('script');
15+
script.src = WIDGET_SCRIPT_URL;
16+
script.async = true;
17+
script.onload = () => {
18+
console.log('[custom.js] Widget script loaded');
19+
// Create the web component after the script loads
20+
const widget = document.createElement(WIDGET_TAG);
21+
const apiKey = '6d46661c-2dce-4032-b62d-64c151a14e0d';
22+
widget.setAttribute('apiKey', apiKey);
23+
widget.style.position = 'fixed';
24+
widget.style.bottom = '0';
25+
widget.style.right = '0';
26+
widget.style.zIndex = '9999';
27+
document.body.appendChild(widget);
28+
console.log('[custom.js] Widget element appended to DOM');
29+
};
30+
document.body.appendChild(script);
31+
console.log('[custom.js] Widget script appended to DOM');
32+
}
33+
34+
if (document.readyState === 'loading') {
35+
console.log('[custom.js] Waiting for DOMContentLoaded');
36+
document.addEventListener('DOMContentLoaded', injectVapiWidget);
37+
} else {
38+
injectVapiWidget();
39+
}

fern/docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ experimental:
3232
css: assets/styles.css
3333
js:
3434
- path: ./assets/close-playground.js
35+
- path: ./custom.js
3536
navbar-links:
3637
- type: minimal
3738
text: Website

fern/widget/index.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import reactToWebComponent from "react-to-webcomponent";
4+
import VoiceAgentWidget from "./voice-widget";
5+
6+
const tagName = "vapi-voice-agent-widget";
7+
console.log('[widget-webcomponent] Checking for custom element', tagName);
8+
if (!customElements.get(tagName)) {
9+
const WebComponent = reactToWebComponent(VoiceAgentWidget, React, ReactDOM, {
10+
props: ["apiKey", "apikey"]
11+
});
12+
customElements.define(tagName, WebComponent);
13+
console.log('[widget-webcomponent] Custom element defined:', tagName);
14+
} else {
15+
console.log('[widget-webcomponent] Custom element already defined:', tagName);
16+
}

0 commit comments

Comments
 (0)