UNPKG

@telegram-auth/react

Version:
74 lines 3.06 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoginButton = LoginButton; const react_1 = __importStar(require("react")); const createScript_1 = require("./createScript"); /** * It takes an object with a bunch of properties and assigns it to the global variable * `TelegramAuthLogin` * * @param {TTelegramAuthLogin} options - The options to set on the global variable. */ function initTelegramAuthLogin(options) { window.TelegramAuthLogin = options; } /** * A React component that renders a Telegram login button. * * @see https://core.telegram.org/widgets/login * * @param {LoginButtonProps} props The props to pass to the component. * @returns A React component that renders the Telegram login button. */ function LoginButton(props) { const hiddenDivRef = (0, react_1.useRef)(null); const scriptRef = (0, react_1.useRef)(); (0, react_1.useEffect)(() => { // destroy the existing script element scriptRef.current?.remove(); // init the global variable initTelegramAuthLogin({ onAuthCallback: props.onAuthCallback }); // create a new script element and save it scriptRef.current = (0, createScript_1.createScript)(props); // add the script element to the DOM hiddenDivRef.current?.after(scriptRef.current); // Save siblings before unmount const siblings = hiddenDivRef.current?.parentElement?.children || []; return () => { // destroy the script element on unmount scriptRef.current?.remove(); // We also need to remove the rendered iframe for (const element of siblings) { if (element instanceof HTMLIFrameElement && element.src.includes('oauth.telegram.org')) { element.remove(); break; } } }; }, [props]); return react_1.default.createElement("div", { ref: hiddenDivRef, hidden: true }); } //# sourceMappingURL=LoginButton.js.map