UNPKG

4nm

Version:

TypeScript reimplementation of Telegram's official library for communicating with Telegram Web Apps.

64 lines (63 loc) 1.61 kB
import { BACK_BUTTON_PRESSED_EVENT } from '../WebView'; /** * This object controls the back button, which can be displayed in the header * of the Web App in the Telegram interface. * @see https://core.telegram.org/bots/webapps#backbutton */ export class BackButton { webView; webApp; _isVisible; constructor(webView, webApp, _isVisible = false) { this.webView = webView; this.webApp = webApp; this._isVisible = _isVisible; } /** * * @since WebApp version 6.1+ * @see requireVersion * @private */ setVisible(visible) { this.webApp.requireVersion('6.1'); this._isVisible = visible; this.webView.postEvent('web_app_setup_back_button', { is_visible: visible, }); } /** * A method to hide the button. * @see setVisible */ hide() { this.setVisible(false); } /** * Shows whether the button is visible. */ get isVisible() { return this._isVisible; } /** * Adds new listener to button click event. * @param listener */ onClick(listener) { this.webView.on(BACK_BUTTON_PRESSED_EVENT, listener); } /** * Removes listener from button click event. * @param listener */ offClick(listener) { this.webView.off(BACK_BUTTON_PRESSED_EVENT, listener); } /** * A method to make the button active and visible. * @see setVisible */ show() { this.setVisible(true); } }