hotel-ai-widget
Version:
A customizable hotel chat widget for React and vanilla HTML
41 lines (40 loc) • 1.98 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { createRoot } from "react-dom/client";
import { LoadScript } from "@react-google-maps/api";
import { Toaster } from "react-hot-toast";
import "./embed.css";
import HotelChatWidget from "./components/hotel-ai-widget";
const initWidget = (config = {}) => {
// Find all elements with data-hotel-ai-widget attribute
const containers = document.querySelectorAll("[data-hotel-ai-widget]");
containers.forEach((container) => {
if (container.children.length > 0) {
return; // Already initialized
}
// Get configuration from data attributes
const elementConfig = {
...config,
apiKey: container.getAttribute("data-api-key") || config.apiKey,
baseUrl: container.getAttribute("data-base-url") || config.baseUrl,
theme: container.getAttribute("data-theme") || config.theme,
position: container.getAttribute("data-position") || config.position,
googleMapsApiKey: container.getAttribute("data-google-maps-api-key") || config.googleMapsApiKey,
};
// Create React root and render widget
const root = createRoot(container);
const WidgetApp = () => (_jsxs("div", { className: `hotel-ai-widget-container ${elementConfig.theme || "light"}`, children: [_jsx(Toaster, { position: "top-right" }), elementConfig.googleMapsApiKey ? (_jsx(LoadScript, { googleMapsApiKey: elementConfig.googleMapsApiKey, libraries: ["places"], children: _jsx(HotelChatWidget, { config: elementConfig }) })) : (_jsx(HotelChatWidget, { config: elementConfig }))] }));
root.render(_jsx(WidgetApp, {}));
});
};
// Auto-initialize when DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () => initWidget());
}
else {
initWidget();
}
// Expose global API
window.HotelChatWidget = {
init: initWidget,
};
export default initWidget;