Back Button
The 💠component responsible for the Telegram Mini Apps back button.
Checking Support
To check if the back button is supported by the current Telegram Mini Apps version, use the isSupported
method:
ts
import { backButton } from '@telegram-apps/sdk';
backButton.isSupported(); // boolean
ts
import { isBackButtonSupported } from '@telegram-apps/sdk';
isBackButtonSupported(); // boolean
Mounting
Before using this component, it is necessary to mount it to work with properly configured properties. To do so, use the mount
method. It will update the isMounted
signal property.
ts
import { backButton } from '@telegram-apps/sdk';
backButton.mount();
backButton.isMounted(); // true
ts
import { mountBackButton, isBackButtonMounted } from '@telegram-apps/sdk';
mountBackButton();
isBackButtonMounted(); // true
To unmount, use the unmount
method:
ts
backButton.unmount();
backButton.isMounted(); // false
ts
import { unmountBackButton, isBackButtonMounted } from '@telegram-apps/sdk';
unmountBackButton();
isBackButtonMounted(); // false
Showing and Hiding
To change the button's visibility, use the hide()
and show()
methods. These methods update the isVisible
signal property value.
ts
backButton.show();
backButton.isVisible(); // true
backButton.hide();
backButton.isVisible(); // false
ts
import {
showBackButton,
hideBackButton,
isBackButtonVisible,
} from '@telegram-apps/sdk';
showBackButton();
isBackButtonVisible(); // true
hideBackButton();
isBackButtonVisible(); // false
Tracking Click
To add a button click listener, use the onClick
method. It returns a function to remove the bound listener. Alternatively, you can use the offClick
method.
ts
function listener() {
console.log('Clicked!');
}
const offClick = backButton.onClick(listener);
offClick();
// or
backButton.onClick(listener);
backButton.offClick(listener);
ts
import { onBackButtonClick, offBackButtonClick } from '@telegram-apps/sdk';
function listener() {
console.log('Clicked!');
}
const offClick = onBackButtonClick(listener);
offClick();
// or
onBackButtonClick(listener);
offBackButtonClick(listener);