Hi I have a project in NextJs, where I use app router. In my public folder I have a file called chatbutton.js, I need this button to work as a web component that I can implement on external websites.
Implement like this:
<script>
window.embeddedChatbotConfig = {
chatId: "fUYYCegK9QYOtoGJ25Dv1",
domain: "www.chatai.com"
}
</script>
<script
src="https://www.chatai.com/chatbutton.min.js"
chatbotId="fUYYCegK9QYOtoGJ25Dv1"
domain="www.chatai.com"
defer>
</script>
public/chatbutton.min.js:
class ChatButton extends HTMLElement {
constructor() {
super();
// Create shadow DOM
this.attachShadow({ mode: "open" });
// Initialize the component
this.init();
}
init() {
// Create chat button
const chatButton = document.createElement("div");
chatButton.setAttribute("id", "chatbase-bubble-button");
chatButton.style.position = "fixed";
chatButton.style.bottom = "20px";
chatButton.style.right = "20px";
...
chatButton.appendChild(chatButtonIcon);
// Add click event listener
chatButton.addEventListener("click", () => this.toggleChatWindow());
// Append chat button to shadow DOM
this.shadowRoot.appendChild(chatButton);
// Create chat window
const chat = document.createElement("div");
chat.setAttribute("id", "chatbase-bubble-window");
chat.style.position = "fixed";
chat.style.flexDirection = "column";
chat.style.justifyContent = "space-between";
chat.style.bottom = "80px";
chat.style.zIndex = 999999999;
// Append iframe to chat window
chat.innerHTML = `<iframe src="https://www.chatai.com/chatbot-iframe/${this.getAttribute(
"data-chatbotId"
)}" width="100%" height="100%" frameborder="0"></iframe>`;
this.shadowRoot.appendChild(chat);
const mediaQuery = window.matchMedia("(min-width: 550px)");
function handleChatWindowSizeChange(t) {
t.matches && ((chat.style.height = "600px"), (chat.style.width = "400px"));
}
mediaQuery.addEventListener("change", handleChatWindowSizeChange);
handleChatWindowSizeChange(mediaQuery);
}
toggleChatWindow() {
const chat = this.shadowRoot.getElementById("chatbase-bubble-window");
chat.style.display = chat.style.display === "none" ? "flex" : "none";
}
get CHAT_BUTTON_ICON() {
return `
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M20 2H4C2.9 2 2 2.9 2 4V22L6 18H20C21.1 18 22 17.1 22 16V4C22 2.9 21.1 2 20 2ZM20 16H6L4 18V4H20V16Z" fill="#FFFFFF"/>
</svg>
`;
}
}
customElements.define("chatbase-bubble", ChatBaseBubble);
I need this implementation to be able to add a button that shows an iframe with my chat. But when I add the url to my script, the botton and iframe are not displayed. How can I achieve this?
Thanks!

